Skip to content

Commit 11278d4

Browse files
committed
'App.tsx' is now entirely generated
1 parent cd16a24 commit 11278d4

File tree

11 files changed

+80
-30
lines changed

11 files changed

+80
-30
lines changed

solves/base-template/src/components/App.tsx.ejs

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ function parseInput(s: string): string[] {
1717
interface State {
1818
initialized: boolean;
1919

20-
inputColours: string;
21-
inputVertices: string;
22-
inputEdges: string;
20+
<%-
21+
Object.entries(input).map(([k, v]) => {
22+
const x = {};
23+
x.inputID = k;
24+
return ht.stateTypeInputValue(x);
25+
}).join("")
26+
-%>
2327

2428
outputResult: null | ClingoResult;
2529
invalidInputLines: {
26-
colours: number[];
27-
vertices: number[];
28-
edges: number[];
30+
<%-
31+
Object.entries(input).map(([k, v]) => {
32+
const x = {};
33+
x.inputID = k;
34+
return ht.stateTypeInputValidation(x);
35+
}).join("")
36+
-%>
2937
};
3038
}
3139

@@ -44,24 +52,38 @@ export class App extends React.Component<{}, State> {
4452
// Either implement a proper uncontrolled
4553
// component, or improve LineHighlighterTextbox into a
4654
// controllable component!
47-
inputColours: "red\ngreen\nblue",
48-
inputVertices: "v1\nv2\nv3\nv4",
49-
inputEdges: "v1,v2\nv2,v3\nv3,v4\nv4,v1\nv1,v3",
55+
<%-
56+
Object.entries(input).map(([k, v]) => {
57+
const x = {};
58+
x.inputID = k;
59+
x.initialValues = v.initialValues;
60+
return ht.stateInputValue(x);
61+
}).join("")
62+
-%>
5063

5164
outputResult: null,
5265
invalidInputLines: {
53-
colours: [],
54-
vertices: [],
55-
edges: [],
66+
<%-
67+
Object.entries(input).map(([k, v]) => {
68+
const x = {};
69+
x.inputID = k;
70+
x.initialValues = v.initialValues;
71+
return ht.stateInputValidation(x);
72+
}).join("")
73+
-%>
5674
},
5775
};
5876
}
5977

6078
async _recalculateOutputs() {
6179
const result = await runSolver({
62-
colours: parseInput(this.state.inputColours),
63-
vertices: parseInput(this.state.inputVertices),
64-
edges: parseInput(this.state.inputEdges),
80+
<%-
81+
Object.entries(input).map(([k, v]) => {
82+
const x = {};
83+
x.inputID = k;
84+
return ht.resultParameter(x);
85+
}).join("")
86+
-%>
6587
});
6688
this.setState({
6789
initialized: true,
@@ -77,15 +99,14 @@ export class App extends React.Component<{}, State> {
7799
override render() {
78100
const recalcCallback = async () => this._recalculateOutputs();
79101

80-
const setColours = async (event: {target: {value: string;};}) => {
81-
this.setState({inputColours: event.target.value}, recalcCallback);
82-
};
83-
const setVertices = async (event: {target: {value: string;};}) => {
84-
this.setState({inputVertices: event.target.value}, recalcCallback);
85-
};
86-
const setEdges = async (event: {target: {value: string;};}) => {
87-
this.setState({inputEdges: event.target.value}, recalcCallback);
88-
};
102+
<%-
103+
Object.entries(input).map(([k, v]) => {
104+
const x = {};
105+
x.stateValue = "input" + upperCaseFirst(k);
106+
x.onChangeFnName = "set" + upperCaseFirst(k);
107+
return ht.onChangeHelpers(x);
108+
}).join("")
109+
-%>
89110

90111
return <div id="app">
91112
<%-
@@ -94,10 +115,10 @@ export class App extends React.Component<{}, State> {
94115
x.title = v.title;
95116
x.stateValue = "input" + upperCaseFirst(k);
96117
x.stateValidation = k;
97-
x.onChange = "set" + upperCaseFirst(k);
118+
x.onChangeFnName = "set" + upperCaseFirst(k);
98119
return ht.jsxParts(x);
99120
}).join("")
100-
%>
121+
-%>
101122
<div className="output-table-wrapper">
102123
<ResultDisplay
103124
clingoResult={this.state.outputResult}

solves/base-template/src/components/App.tsx.ejshelpers/jsxParts.tsx.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
label="<%- title %>"
44
initialValue={this.state.<%- stateValue %>}
55
errorLineNumbers={this.state.invalidInputLines.<%- stateValidation %>}
6-
onChange={<%- onChange %>}
6+
onChange={<%- onChangeFnName %>}
77
/>
88
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const <%- onChangeFnName %> = async (event: {target: {value: string;};}) => {
2+
this.setState({<%- stateValue %>: event.target.value}, recalcCallback);
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- inputID %>: parseInput(this.state.input<%- upperCaseFirst(inputID) %>),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- inputID %>: [],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
input<%- upperCaseFirst(inputID) %>: "<%- initialValues.join("\\n") %>",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- inputID %>: number[];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
input<%- upperCaseFirst(inputID) %>: string;

solves/base-template/src/components/ResultDisplay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function renderSatisfiable(solution: string[]) {
3030
return <table className="result-satisfiable">
3131
<thead>
3232
<tr>
33-
<td>Vertex</td>
34-
<td>Colour</td>
33+
<th>Vertex</th>
34+
<th>Colour</th>
3535
</tr>
3636
</thead>
3737
<tbody>

solves/src/spec-parse.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ interface InputPartialSpec {
5959
title: string;
6060
template: PartialTemplate[];
6161
substitutionsCount: number;
62+
initialValues: string[];
6263
}
6364

6465
interface OutputPartialSpec {
@@ -107,13 +108,18 @@ export function getSpecValues(specPath: string): SpecValues {
107108
const title: string = ensureStr(obj.title, "input[].title");
108109
const template: string = ensureStr(obj.template, "input[].template");
109110
const substitutionsCount: number = (template.match(/%/g) || []).length;
111+
const initialValues: string[] = obj.initialValues
112+
.map((s: any) => ensureStr(s, "input[].initialValues[]"));
113+
110114
if (substitutionsCount === 0) {
111115
throw new Error(`Input ${obj.title} must provide a template that includes substitution markers.`);
112116
}
117+
113118
return {
114119
title,
115120
template: parseSimpleTemplate(template),
116-
substitutionsCount
121+
substitutionsCount,
122+
initialValues,
117123
};
118124
}),
119125

0 commit comments

Comments
 (0)