-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact_view.js
More file actions
165 lines (158 loc) · 4.51 KB
/
react_view.js
File metadata and controls
165 lines (158 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const Workflow = require("@saltcorn/data/models/workflow");
const Form = require("@saltcorn/data/models/form");
const Table = require("@saltcorn/data/models/table");
const { div } = require("@saltcorn/markup/tags");
const {
stateFieldsToWhere,
readState,
} = require("@saltcorn/data/plugin-helper");
const {
buildSafeViewName,
buildAndUpdateView,
handleUserCode,
reactViewSystemPrompt,
} = require("./common");
const get_state_fields = () => [];
const defaultUserCode = (tableId) => {
return `import React from "react";
export default function App({ viewName, query${
tableId ? ", state, tableName, rows" : " "
} }) {
return <h3>Please write your React code here</h3>;
};
`;
};
// TODO default state, joinFields, aggregations, include_fml, exclusion_relation
const run = async (table_id, viewname, { timestamp }, state, extra) => {
const req = extra.req;
const query = req.query || {};
const props = {
"view-name": buildSafeViewName(viewname),
query: encodeURIComponent(JSON.stringify(query)),
user: encodeURIComponent(JSON.stringify(req.user || {})),
};
if (table_id) {
// with table
const table = Table.findOne(table_id);
const fields = table.getFields();
const where = stateFieldsToWhere({
fields,
state,
table,
prefix: "a.",
});
const rows = await table.getRows(where, {
forUser: req.user,
forPublic: !req.user,
});
readState(state, fields, req);
props["table-name"] = table.name;
props.state = encodeURIComponent(JSON.stringify(state));
props.rows = encodeURIComponent(JSON.stringify(rows));
}
return div({
class: "_sc_react-view",
...props,
...(timestamp ? { timestamp } : {}),
});
};
const configuration_workflow = () =>
new Workflow({
onDone: async (context) => {
const newTimestamp = new Date().valueOf();
await handleUserCode(
context.user_code || defaultUserCode(context.table_id),
context.build_mode,
context.viewname,
context.timestamp,
newTimestamp
);
context.timestamp = newTimestamp;
return context;
},
steps: [
{
name: "User code",
disablePreview: true,
form: async (context) => {
const userCodeUndefined = context?.user_code === undefined;
return new Form({
fields: [
{
name: "user_code",
label: "User code",
input_type: "code",
required: true,
attributes: { mode: "application/javascript" },
},
{
name: "build_mode",
label: "Build mode",
sublabel: "Bundle your code for production or development",
type: "String",
required: true,
default: "production",
attributes: {
options: ["production", "development"],
},
},
],
additionalButtons: [
{
id: "build_view_btn_id",
label: "Build",
onclick:
`view_post('${context.viewname}', 'build_user_code', {}, () => { ` +
`setTimeout(() => { restore_old_button('build_view_btn_id'); }, 50); }); ` +
"press_store_button(this, true);",
class: "btn btn-primary",
},
],
...(userCodeUndefined
? {
values: {
user_code: defaultUserCode(context?.table_id),
},
}
: {}),
});
},
},
],
});
const build_user_code = async (
table_id,
viewname,
{ user_code, build_mode, timestamp },
{},
{ req }
) => {
try {
await buildAndUpdateView(
user_code || defaultUserCode(table_id),
build_mode,
viewname,
timestamp
);
return { json: { notify_success: "Build successful" } };
} catch (e) {
return { json: { error: e.message || "An error occured" } };
}
};
module.exports = {
name: "React",
description: "React view",
get_state_fields,
configuration_workflow,
run,
routes: { build_user_code },
copilot_generate_view_prompt: async () => reactViewSystemPrompt,
copilot_post_create: async ({ name, configuration }) => {
await buildAndUpdateView(
configuration.user_code || defaultUserCode(configuration.table_id),
configuration.build_mode || "production",
name
);
},
table_optional: true,
};