-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathSettings.js
More file actions
330 lines (308 loc) · 9.95 KB
/
Settings.js
File metadata and controls
330 lines (308 loc) · 9.95 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import React, { useState, useContext } from "react";
import shortid from "shortid";
import { useRecoilState, useRecoilValue } from "recoil";
import { DARK } from "../constants/constants";
import createAppHelper from "../helpers/createAppHelper";
import { ReactComponent as DeleteIcon } from "../assets/trash.svg";
import gear from "../assets/gear.png";
//State
import {
routeAtom,
componentsAtom,
projectNameAtom,
buildToolAtom,
dependenciesToAddAtom,
environmentAtom,
highlightAtom,
} from "../context/GlobalState";
import { ThemeContext } from "../App";
import { isMobile } from "../helpers/utility";
import { GithubMetrics } from "./GithubMetrics";
const Settings = () => {
//State Management
const [route, setRoute] = useRecoilState(routeAtom);
const environment = useRecoilValue(environmentAtom);
const [projectName, setProjetctName] = useRecoilState(projectNameAtom);
const [components, setComponents] = useRecoilState(componentsAtom);
const [buildTool, setBuildTool] = useRecoilState(buildToolAtom);
const [dependencies, setDependencies] = useRecoilState(dependenciesToAddAtom);
const highlight = useRecoilValue(highlightAtom);
const [script, setScript] = useState(null);
const [isCreated, setIsCreated] = useState(false);
const mode = useContext(ThemeContext);
const themeClass = mode.theme === DARK ? mode.theme : null;
const updateUserCount = () => {
fetch(
"https://api.countapi.xyz/update/react-builder/usedby/?amount=1"
).then(() => setIsCreated(true));
};
const handleCreateApp = () => {
updateUserCount();
const fileName = shortid.generate();
createAppHelper({ environment, route, components, projectName, fileName });
if (buildTool === "yarn") {
setScript(
`yarn create react-app ${projectName} && cd ${projectName} && yarn add ${dependencies} && cd .. && node ${fileName}.js && rm ${fileName}.js`
);
} else {
setScript(
`npx create-react-app ${projectName} && cd ${projectName} && npm install ${dependencies} && cd .. && node ${fileName}.js && rm ${fileName}.js`
);
}
};
const handleRouteChange = (e) => {
e.preventDefault();
setRoute({
enabled: route.enabled,
navigation: e.target.value,
});
};
const handleCheckBox = () => {
setRoute((prevState) => {
return {
enabled: !prevState.enabled,
navigation: prevState.navigation,
};
});
};
const addEmptyComponent = () => {
setComponents([
...components,
{
id: components.length,
name: "Name",
type: "FunctionalArrow",
page: false,
},
]);
};
const handleNameChange = (e, id) => {
setComponents((prevState) => {
let tempArray = [];
prevState.forEach((comp) => {
if (comp.id === id) {
tempArray.push({
id: comp.id,
type: comp.type,
page: comp.page,
name: e.target.value,
});
} else {
tempArray.push(comp);
}
});
return tempArray;
});
};
const handleTypeChange = (e, id) => {
setComponents((prevState) => {
let tempArray = [];
prevState.forEach((comp) => {
if (comp.id === id) {
tempArray.push({
id: comp.id,
type: e.target.value,
page: comp.page,
name: comp.name,
});
} else {
tempArray.push(comp);
}
});
return tempArray;
});
};
const handlePageChange = (id) => {
setComponents((prevState) => {
let tempArray = [];
prevState.forEach((comp) => {
if (comp.id === id) {
tempArray.push({
id: comp.id,
type: comp.type,
page: !comp.page,
name: comp.name,
});
} else {
tempArray.push(comp);
}
});
return tempArray;
});
};
const handleDelete = (id) => {
setComponents((prevState) => {
let tempArray = [];
prevState.forEach((comp) => {
if (comp.id !== id) {
tempArray.push(comp);
}
});
return tempArray;
});
};
return (
!isMobile && (
<div className={`SettingsPane ${themeClass}`}>
<div className="SettingsHead">
<div>
<h1 className="setup-app">
<img
src={gear}
className="gearIcon"
alt="gear icon"
/>{" "}
Setup Your App
</h1>
</div>
<GithubMetrics isCreated={isCreated} setIsCreated={setIsCreated} />
</div>
<div className={`container-settings ${highlight === 1 && "highlight"}`}>
<div className="env">
<div className="form-container">
<h4 className="head">Environment</h4>
<select name="environment">
<option value="create-react-app">Create React App</option>
<option disabled value="comming-soon">
More comming soon
</option>
</select>
</div>
</div>
<div className="buildtool">
<div className="form-container">
<h4 className="head">Build Tool</h4>
<select
name="buildtool"
value={buildTool}
onChange={(e) => setBuildTool(e.target.value)}
>
<option value="yarn">yarn</option>
<option value="npx">npx</option>
</select>
</div>
</div>
<div className="projectname">
<div className="form-container">
<div className={`main ${themeClass}`}>
<h4 className="head">Project Name</h4>
<p>All small without spaces</p>
</div>
<input
type="text"
value={projectName}
onChange={(e) => setProjetctName(e.target.value)}
/>
</div>
</div>
<div className="dependencies">
<div className="form-container">
<div className={`main ${themeClass}`}>
<h4 className="head">Dependencies to be added</h4>
<p>
Seperated by spaces <br /> Do not remove react-router-dom if
routing enabled
</p>
</div>
<input
type="text"
value={dependencies}
onChange={(e) => setDependencies(e.target.value)}
/>
</div>
</div>
<div className={`route ${themeClass}`}>
<h4 className="head">Routing</h4>
<p>Done using react-router-dom</p>
<div className="route-flex">
<div className="enabled">
<div className="head">Enable </div>
<input
type="checkbox"
onChange={() => handleCheckBox()}
defaultChecked={route.enabled}
/>
</div>
<div className="navigation">
<div className="head">Navigation Component</div>
<select
value={route.navigation}
onChange={(e) => handleRouteChange(e)}
name="route"
>
{components.map(({ name, id, page }) => {
if (!page) {
return (
<option key={id} value={name}>
{name}
</option>
);
} else {
return null;
}
})}
</select>
</div>
</div>
</div>
<div className="components">
<div className="head-flex">
<h4 className="head">Components</h4>
<button onClick={() => addEmptyComponent()}>+</button>
</div>
<div className="headers-flex">
<div className="name">Name</div>
<div className="type">Type</div>
<div className="page">Page</div>
<div className="delete"></div>
</div>
<div className="all-comps">
{components.map(({ id, name, type, page }) => (
<React.Fragment key={id}>
<div className="input-comp">
<input
type="text"
onChange={(e) => handleNameChange(e, id)}
value={name}
/>
<select
onChange={(e) => handleTypeChange(e, id)}
value={type}
>
<option value="FunctionalArrow">FunctionalArrow</option>
<option value="Functional">Functional</option>
<option value="ClassStateFul">ClassStateFul</option>
<option value="ClassStateLess">ClassStateLess</option>
</select>
<input
type="checkbox"
onChange={() => handlePageChange(id)}
defaultChecked={page}
/>
<DeleteIcon
className="delete-icon"
fill="currentColor"
onClick={() => handleDelete(id)}
/>
</div>
</React.Fragment>
))}
</div>
</div>
</div>
<div className="create-app-container">
<button className={`create-app-btn ${highlight === 2 && "highlight"}`} onClick={() => handleCreateApp()}>
{" "}
Create App
</button>
{script && (
<div className={`script ${themeClass}`}>
<code>{script}</code>
</div>
)}
</div>
</div>
)
);
};
export default Settings;