@@ -5,19 +5,25 @@ import Col from "react-bootstrap/Col";
55import Modal from "react-bootstrap/Modal" ;
66import Button from "react-bootstrap/Button" ;
77
8+ // Toast
9+ import toast from "react-hot-toast" ;
10+
11+ // harperDb fetch call
12+ import { harperFetch } from "../../utils/HarperFetch" ;
13+
814import { ShapeForm , ShapePreview } from ".." ;
915
1016const CreateShape = ( props ) => {
1117 const [ shapeInformation , setShapeInformation ] = useState ( {
1218 "name" : "Tilted Square" ,
13- "type" : "tiltedSquare" ,
1419 "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
1520 "vertices" : 4 ,
21+ "private" : false ,
1622 "edges" : 4 ,
1723 "notes" : "" ,
1824 "clipPathType" : "polygon" ,
19- "showShadow" : true ,
20- "backgroundColor" : "#12a8d6 " ,
25+ "showShadow" : false ,
26+ "backgroundColor" : "#d61284 " ,
2127 "verticeCoordinates" : [
2228 {
2329 "x" : "10%" ,
@@ -42,13 +48,18 @@ const CreateShape = (props) => {
4248 const name = event . target . name || event . type ;
4349 const value = event . target . type === "checkbox" ? event . target . checked : event . target . value ;
4450
45- console . log ( event , data ) ;
51+ // console.log(event, data);
4652
4753 if ( name === "name" ) {
4854 setShapeInformation ( {
4955 ...shapeInformation ,
5056 "name" : value ,
51- "type" : value . toLowerCase ( ) ,
57+ } ) ;
58+ } else if ( name === 'private' ) {
59+ setShapeInformation ( {
60+ ...shapeInformation ,
61+ "private" : ! shapeInformation . private ,
62+
5263 } ) ;
5364 } else if ( name === "formula" ) {
5465 const edgeVerticeNumber = shapeInformation . clipPathType === "polygon" ? value . split ( "," ) . length : 0 ;
@@ -171,17 +182,15 @@ const CreateShape = (props) => {
171182 if ( clipPathType === "polygon" ) {
172183 setShapeInformation ( {
173184 ...shapeInformation ,
174- "name" : "Tilted Square" ,
175- "type" : "tiltedSquare" ,
185+ "name" : "Tilted Square" ,
176186 "formula" : "polygon(10% 10%, 90% 10%, 90% 90%, 10% 80%)" ,
177187 } ) ;
178188 }
179189
180190 if ( clipPathType === "circle" ) {
181191 setShapeInformation ( {
182192 ...shapeInformation ,
183- "name" : "Circle" ,
184- "type" : "circle" ,
193+ "name" : "Circle" ,
185194 "formula" : "circle(50% at 50% 50%)" ,
186195 } ) ;
187196 }
@@ -190,7 +199,6 @@ const CreateShape = (props) => {
190199 setShapeInformation ( {
191200 ...shapeInformation ,
192201 "name" : "Ellipse" ,
193- "type" : "ellipse" ,
194202 "formula" : "ellipse(25% 40% at 50% 50%)" ,
195203 } ) ;
196204 }
@@ -208,13 +216,48 @@ const CreateShape = (props) => {
208216
209217 const [ validated , setValidated ] = useState ( false ) ;
210218
211- const handleSubmit = ( event ) => {
219+ const handleSubmit = async ( event ) => {
220+ event . preventDefault ( ) ;
212221 const form = event . currentTarget ;
213222 if ( form . checkValidity ( ) === false ) {
214223 event . preventDefault ( ) ;
215224 event . stopPropagation ( ) ;
216225 }
217226 setValidated ( true ) ;
227+
228+ console . log ( shapeInformation ) ;
229+
230+ // Create the shape in the DB
231+ const insertShape = await harperFetch ( {
232+ operation : "sql" ,
233+ sql : `INSERT into tryshape.shapes(backgroundColor, createdAt, createdBy, edges, email, formula, likes, name, notes, private, type, vertices)
234+ values('${ shapeInformation . backgroundColor } ', null, '${ props . user . email } ', ${ shapeInformation . edges } , null, '${ shapeInformation . formula } ', 0, '${ shapeInformation . name } ', '${ shapeInformation . notes } ', ${ shapeInformation . private } , '${ shapeInformation . clipPathType } ', ${ shapeInformation . vertices } )` ,
235+ } ) ;
236+
237+ console . log ( insertShape ) ;
238+
239+ // Create the user in the db
240+ if ( insertShape [ 'inserted_hashes' ] . length > 0 ) {
241+ // First check if the user exist
242+ const result = await harperFetch ( {
243+ operation : "sql" ,
244+ sql : `SELECT count(*) from tryshape.users WHERE email='${ props . user . email } '` ,
245+ } ) ;
246+ const count = ( result [ 0 ] [ 'COUNT(*)' ] ) ;
247+ console . log ( { count} ) ;
248+ // If doesn't exist, create in db
249+ if ( count === 0 ) {
250+ const insertUser = await harperFetch ( {
251+ operation : "sql" ,
252+ sql : `INSERT into tryshape.users(email, name, photoURL)
253+ values('${ props . user . email } ', '${ props . user . displayName } ', '${ props . user . photoURL } ')` ,
254+ } ) ;
255+ } else {
256+ console . log ( `The user ${ props . user . email } present in DB` ) ;
257+ }
258+ }
259+ props . handleClose ( ) ;
260+ toast . success ( `Shape ${ shapeInformation . name } created successfully.` ) ;
218261 }
219262
220263 return (
@@ -227,7 +270,7 @@ const CreateShape = (props) => {
227270 backdrop = "static"
228271 >
229272 < Modal . Header closeButton >
230- < Modal . Title > Create Shape</ Modal . Title >
273+ < Modal . Title > Create a Shape</ Modal . Title >
231274 </ Modal . Header >
232275 < Modal . Body >
233276 < Container fluid >
0 commit comments