11import React , { useState , useEffect } from "react" ;
22
3+ // axios
4+ import axios from "axios" ;
5+
36// Bootstrap
47import Container from "react-bootstrap/Container" ;
58import Row from "react-bootstrap/Row" ;
@@ -13,9 +16,6 @@ import { ShapeForm, ShapePreview } from "..";
1316// Toast
1417import toast from "react-hot-toast" ;
1518
16- // harperDb fetch call
17- import { harperFetch } from "../../utils/HarperFetch" ;
18-
1919const CreateShape = ( props ) => {
2020
2121 // Store the default state as a variable so resetting form is easier
@@ -74,7 +74,7 @@ const CreateShape = (props) => {
7474 "name" : props . shape . name ,
7575 "formula" : props . shape . formula ,
7676 "vertices" : props . shape . vertices ,
77- "private" : true ,
77+ "private" : props . shape . private ,
7878 "edges" : props . shape . edges ,
7979 "notes" : props . shape . notes ,
8080 "clipPathType" : props . shape . type ,
@@ -88,8 +88,6 @@ const CreateShape = (props) => {
8888 // Changes shapeInformation when something in ShapeForm or ShapePreview is altered
8989 const handleChange = ( event , data , number ) => {
9090
91- // console.log(event, data);
92-
9391 const name = event . target . name || event . type ;
9492 const value = event . target . type === "checkbox" ? event . target . checked : event . target . value ;
9593
@@ -131,7 +129,25 @@ const CreateShape = (props) => {
131129 ...shapeInformation ,
132130 "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
133131 "vertices" : 4 ,
134- "edges" : 4 ,
132+ "edges" : 4 ,
133+ "verticeCoordinates" : [
134+ {
135+ "x" : "10%" ,
136+ "y" : "10%" ,
137+ } ,
138+ {
139+ "x" : "90%" ,
140+ "y" : "10%" ,
141+ } ,
142+ {
143+ "x" : "90%" ,
144+ "y" : "90%" ,
145+ } ,
146+ {
147+ "x" : "10%" ,
148+ "y" : "80%" ,
149+ } ,
150+ ]
135151 } ) ;
136152 }
137153
@@ -163,7 +179,7 @@ const CreateShape = (props) => {
163179 }
164180
165181 // If DraggableVertice is moved, adjust verticeCoordinates and formula
166- if ( name === "mousemove" ) {
182+ if ( name === "mousemove" || name === "touchmove" ) {
167183
168184 const newVerticeCoordinates = addNewVerticeCoordinates ( data . x , data . y , number ) ;
169185 const newFormula = generateNewFormula ( newVerticeCoordinates ) ;
@@ -193,7 +209,8 @@ const CreateShape = (props) => {
193209 }
194210
195211 // If delete button is pressed and passes a number that corresponds to the vertice, remove the corresponding verticeCoordinate and adjust formula
196- if ( event . target . id . includes ( "deleteButton" ) && number !== undefined ) {
212+ if ( ( event . target . id . includes ( "deleteButton" )
213+ || event . target . parentElement . id . includes ( "deleteButton" ) ) && number !== undefined ) {
197214
198215 let newVerticeCoordinates = [ ] ;
199216
@@ -302,34 +319,28 @@ const CreateShape = (props) => {
302319 // Editing Shape
303320 if ( props . edit ) {
304321
305- const editShape = await harperFetch ( {
306- operation : "sql" ,
307- sql : `
308- UPDATE tryshape.shapes
309- SET
310- name = '${ shapeInformation . name } ',
311- formula = '${ shapeInformation . formula } ',
312- vertices = '${ shapeInformation . vertices } ',
313- private = '${ shapeInformation . private } ',
314- edges = '${ shapeInformation . edges } ',
315- notes = '${ shapeInformation . notes } ',
316- type = '${ shapeInformation . clipPathType } ',
317- backgroundColor = '${ shapeInformation . backgroundColor } '
318- WHERE
319- shape_id === '${ props . shape . shape_id } '
320- `
322+ const updateShapeResponse = await axios . post ( '/api/PUT/shape' , {
323+ shapeId : props . shape . shape_id ,
324+ name : shapeInformation . name ,
325+ formula : shapeInformation . formula ,
326+ vertices : shapeInformation . vertices ,
327+ visibility : shapeInformation . private ,
328+ edges : shapeInformation . edges ,
329+ notes : shapeInformation . notes ,
330+ type : shapeInformation . clipPathType ,
331+ backgroundColor : shapeInformation . backgroundColor
321332 } ) ;
333+ const editShape = updateShapeResponse . data ;
334+ console . log ( { editShape} ) ;
322335
323- console . log ( editShape ) ;
324-
325- if ( editShape [ "update_hashes" ] . length > 0 ) {
336+ if ( editShape . data [ "update_hashes" ] . length > 0 ) {
326337 props . handleClose ( ) ;
327338 toast . success ( `Shape ${ shapeInformation . name } edited successfully.` ) ;
328339 props . setShapeAction ( {
329340 ...props . shapeAction ,
330341 "action" : "edit" ,
331342 "payload" : {
332- "shape_id" : editShape [ 'update_hashes' ]
343+ "shape_id" : editShape . data [ 'update_hashes' ]
333344 }
334345 } ) ;
335346 } else {
@@ -340,30 +351,43 @@ const CreateShape = (props) => {
340351 } else {
341352
342353 // Create the shape in the DB
343- const insertShape = await harperFetch ( {
344- operation : "sql" ,
345- sql : `INSERT into tryshape.shapes(backgroundColor, createdAt, createdBy, edges, email, formula, likes, name, notes, private, type, vertices)
346- values('${ shapeInformation . backgroundColor } ', null, '${ props . user . email } ', ${ shapeInformation . edges } , null, '${ shapeInformation . formula } ', 0, '${ shapeInformation . name } ', '${ shapeInformation . notes } ', ${ shapeInformation . private } , '${ shapeInformation . clipPathType } ', ${ shapeInformation . vertices } )` ,
354+ const insertShapeResponse = await axios . post ( '/api/POST/shape' , {
355+ name : shapeInformation . name ,
356+ formula : shapeInformation . formula ,
357+ vertices : shapeInformation . vertices ,
358+ visibility : shapeInformation . private ,
359+ edges : shapeInformation . edges ,
360+ notes : shapeInformation . notes ,
361+ type : shapeInformation . clipPathType ,
362+ backgroundColor : shapeInformation . backgroundColor ,
363+ createdBy : props . user . email ,
364+ likes : 0
347365 } ) ;
366+ const insertShape = insertShapeResponse . data
348367
349- console . log ( insertShape ) ;
368+ console . log ( { insertShape} ) ;
350369
351370 // Create the user in the db
352- if ( insertShape [ 'inserted_hashes' ] . length > 0 ) {
371+ if ( insertShape . data [ 'inserted_hashes' ] . length > 0 ) {
353372 // First check if the user exist
354- const result = await harperFetch ( {
355- operation : "sql" ,
356- sql : `SELECT count(*) from tryshape.users WHERE email='${ props . user . email } '` ,
357- } ) ;
358- const count = ( result [ 0 ] [ 'COUNT(*)' ] ) ;
373+ const userResponse = await axios . get ( "/api/GET/user" , {
374+ params : {
375+ email : props . user . email
376+ }
377+ } ) ;
378+ const result = userResponse . data ;
379+ const count = result . length ;
359380 console . log ( { count} ) ;
381+
360382 // If doesn't exist, create in db
361383 if ( count === 0 ) {
362- const insertUser = await harperFetch ( {
363- operation : "sql" ,
364- sql : `INSERT into tryshape.users( email, name, photoURL)
365- values(' ${ props . user . email } ', ' ${ props . user . displayName } ', ' ${ props . user . photoURL } ')` ,
384+ const insertUserResponse = await axios . post ( '/api/POST/user' , {
385+ displayName : props . user . displayName ,
386+ email : props . user . email ,
387+ photoURL : props . user . photoURL
366388 } ) ;
389+ const insertUser = insertUserResponse . data ;
390+ console . log ( { insertUser} ) ;
367391 } else {
368392 console . log ( `The user ${ props . user . email } present in DB` ) ;
369393 }
@@ -378,7 +402,7 @@ const CreateShape = (props) => {
378402 ...props . shapeAction ,
379403 "action" : "add" ,
380404 "payload" : {
381- "shape_id" : insertShape [ 'inserted_hashes' ]
405+ "shape_id" : insertShape . data [ 'inserted_hashes' ]
382406 }
383407 } ) ;
384408
0 commit comments