Skip to content

Commit 254c9d5

Browse files
committed
feat: database now updates when editing shape
1 parent 831cda9 commit 254c9d5

1 file changed

Lines changed: 77 additions & 44 deletions

File tree

components/core/CreateShape.js

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ const CreateShape = (props) => {
5858
useEffect(() => {
5959

6060
if (props.edit) {
61-
6261
let formula = props.shape.formula;
6362
let slicedFormula = formula.slice(formula.indexOf("(") + 1, formula.indexOf(")"));
6463
let newVerticeCoordinates = slicedFormula.split(", ");
65-
6664
newVerticeCoordinates = newVerticeCoordinates.map((value) => {
6765
let coordinates = value.split(" ");
6866
return {
@@ -80,9 +78,8 @@ const CreateShape = (props) => {
8078
"edges": props.shape.edges,
8179
"notes": props.shape.notes,
8280
"clipPathType": props.shape.type,
83-
"showShadow": true,
8481
"backgroundColor": props.shape.backgroundColor,
85-
"verticeCoordinates" : newVerticeCoordinates
82+
"verticeCoordinates" : newVerticeCoordinates,
8683
});
8784
}
8885

@@ -299,56 +296,92 @@ const CreateShape = (props) => {
299296
setValidated(true);
300297

301298
console.log(shapeInformation);
299+
300+
if (props.edit) {
302301

303-
// Create the shape in the DB
304-
const insertShape = await harperFetch({
305-
operation: "sql",
306-
sql: `INSERT into tryshape.shapes(backgroundColor, createdAt, createdBy, edges, email, formula, likes, name, notes, private, type, vertices)
307-
values('${shapeInformation.backgroundColor}', null, '${props.user.email}', ${shapeInformation.edges}, null, '${shapeInformation.formula}', 0, '${shapeInformation.name}', '${shapeInformation.notes}', ${shapeInformation.private}, '${shapeInformation.clipPathType}', ${shapeInformation.vertices})`,
308-
});
302+
const editShape = await harperFetch({
303+
operation: "sql",
304+
sql: `
305+
UPDATE tryshape.shapes
306+
SET
307+
name = '${shapeInformation.name}',
308+
formula = '${shapeInformation.formula}',
309+
vertices = '${shapeInformation.vertices}',
310+
private = '${shapeInformation.private}',
311+
edges = '${shapeInformation.edges}',
312+
notes = '${shapeInformation.notes}',
313+
type = '${shapeInformation.clipPathType}',
314+
backgroundColor = '${shapeInformation.backgroundColor}'
315+
WHERE
316+
shape_id === '${props.shape.shape_id}'
317+
`
318+
});
309319

310-
console.log(insertShape);
311-
312-
// Create the user in the db
313-
if (insertShape['inserted_hashes'].length > 0) {
314-
// First check if the user exist
315-
const result = await harperFetch({
320+
console.log(editShape);
321+
322+
props.handleClose();
323+
toast.success(`Shape ${shapeInformation.name} edited successfully.`);
324+
// props.setShapeAction({
325+
// ...props.shapeAction,
326+
// "action": "add",
327+
// "payload": {
328+
// "shape_id": insertShape['inserted_hashes']
329+
// }
330+
// });
331+
332+
} else {
333+
334+
// Create the shape in the DB
335+
const insertShape = await harperFetch({
316336
operation: "sql",
317-
sql: `SELECT count(*) from tryshape.users WHERE email='${props.user.email}'`,
337+
sql: `INSERT into tryshape.shapes(backgroundColor, createdAt, createdBy, edges, email, formula, likes, name, notes, private, type, vertices)
338+
values('${shapeInformation.backgroundColor}', null, '${props.user.email}', ${shapeInformation.edges}, null, '${shapeInformation.formula}', 0, '${shapeInformation.name}', '${shapeInformation.notes}', ${shapeInformation.private}, '${shapeInformation.clipPathType}', ${shapeInformation.vertices})`,
318339
});
319-
const count = (result[0]['COUNT(*)']);
320-
console.log({count});
321-
// If doesn't exist, create in db
322-
if (count === 0) {
323-
const insertUser = await harperFetch({
340+
341+
console.log(insertShape);
342+
343+
// Create the user in the db
344+
if (insertShape['inserted_hashes'].length > 0) {
345+
// First check if the user exist
346+
const result = await harperFetch({
324347
operation: "sql",
325-
sql: `INSERT into tryshape.users(email, name, photoURL)
326-
values('${props.user.email}', '${props.user.displayName}', '${props.user.photoURL}')`,
348+
sql: `SELECT count(*) from tryshape.users WHERE email='${props.user.email}'`,
327349
});
350+
const count = (result[0]['COUNT(*)']);
351+
console.log({count});
352+
// If doesn't exist, create in db
353+
if (count === 0) {
354+
const insertUser = await harperFetch({
355+
operation: "sql",
356+
sql: `INSERT into tryshape.users(email, name, photoURL)
357+
values('${props.user.email}', '${props.user.displayName}', '${props.user.photoURL}')`,
358+
});
359+
} else {
360+
console.log(`The user ${props.user.email} present in DB`);
361+
}
362+
363+
// Finally, close the modal and update the shape in UI
364+
props.handleClose();
365+
toast.success(`Shape ${shapeInformation.name} created successfully.`);
366+
props.setShapeAction({
367+
...props.shapeAction,
368+
"action": "add",
369+
"payload": {
370+
"shape_id": insertShape['inserted_hashes']
371+
}
372+
});
373+
328374
} else {
329-
console.log(`The user ${props.user.email} present in DB`);
375+
toast.error('OOPS!! We hit a bummer. Please try again.');
330376
}
331377

332-
// Finally, close the modal and update the shape in UI
333-
props.handleClose();
334-
toast.success(`Shape ${shapeInformation.name} created successfully.`);
335-
props.setShapeAction({
336-
...props.shapeAction,
337-
"action": "add",
338-
"payload": {
339-
"shape_id": insertShape['inserted_hashes']
340-
}
341-
});
342-
343-
setShapeInformation({
344-
...initialState,
345-
});
378+
}
346379

347-
setValidated(false);
380+
setShapeInformation({
381+
...initialState,
382+
});
348383

349-
} else {
350-
toast.error('OOPS!! We hit a bummer. Please try again.');
351-
}
384+
setValidated(false);
352385

353386
}
354387

@@ -389,7 +422,7 @@ const CreateShape = (props) => {
389422
Close
390423
</Button>
391424
<Button variant="secondary" type="submit" form="createShapeForm" disabled={!shapeInformation.name}>
392-
Create
425+
{props.edit ? "Save Changes" : "Create"}
393426
</Button>
394427
</Modal.Footer>
395428
</Modal>

0 commit comments

Comments
 (0)