@@ -6,6 +6,9 @@ import styled from "styled-components";
66// dynamic from Next.js
77import dynamic from "next/dynamic" ;
88
9+ // harperDb fetch call
10+ import { harperFetch } from "../../utils/HarperFetch" ;
11+
912// Toast
1013import toast from "react-hot-toast" ;
1114
@@ -144,6 +147,69 @@ const ShapeList = ({ setOpen, user, data }) => {
144147 }
145148 }
146149
150+ const performLike = async ( event , shapeId ) => {
151+ if ( user . length === 0 ) {
152+ showOpen ( true ) ;
153+ } else {
154+ // Initialize likes
155+ let likes = 0 ;
156+
157+ // Check if already an entry for this user's like
158+ // for the shape present.
159+ const isPresent = await harperFetch ( {
160+ operation : "sql" ,
161+ sql : `SELECT *
162+ FROM tryshape.likes
163+ WHERE shape_id='${ shapeId } ' AND email='${ user . email } '` ,
164+ } ) ;
165+ // Get the latest likes count from db
166+ const returnValue = await harperFetch ( {
167+ operation : "sql" ,
168+ sql : `SELECT s.likes
169+ FROM tryshape.shapes s
170+ WHERE s.shape_id='${ shapeId } '` ,
171+ } ) ;
172+
173+ if ( isPresent . length === 0 ) {
174+ // If not present, add for like
175+ const insertLike = await harperFetch ( {
176+ operation : "sql" ,
177+ sql : `INSERT into tryshape.likes(shape_id, email)
178+ values('${ shapeId } ', '${ user . email } ')` ,
179+ } ) ;
180+
181+ if ( insertLike ) {
182+ // Update the count by 1
183+ likes = returnValue [ 0 ] . likes + 1 ;
184+
185+ // Update the like state
186+ }
187+ } else {
188+ // If present, delete to remove like
189+ const deleteLike = await harperFetch ( {
190+ operation : "sql" ,
191+ sql : `DELETE from tryshape.likes
192+ WHERE shape_id='${ shapeId } ' AND email='${ user . email } '` ,
193+ } ) ;
194+ if ( deleteLike ) {
195+ // update the like count decrease by 1
196+ likes = returnValue [ 0 ] . likes - 1 ;
197+
198+ // update the likes state
199+ }
200+ }
201+
202+ // Update the shape data with the updated count
203+ const updated = await harperFetch ( {
204+ operation : "sql" ,
205+ sql : `UPDATE tryshape.shapes SET likes = ${ likes } WHERE shape_id='${ shapeId } '`
206+ } ) ;
207+
208+ // Update the shape data in the shapes array
209+
210+ }
211+ } ;
212+
147213 return (
148214 < ShapePallete >
149215 < ShapeCards >
@@ -161,7 +227,10 @@ const ShapeList = ({ setOpen, user, data }) => {
161227 { shape . private && < FiLock /> }
162228 < ShapeActions >
163229 < span title = "Like" >
164- < LikeIcon size = { 24 } />
230+ < LikeIcon
231+ size = { 24 }
232+ onClick = { ( event , shapeId ) => performLike ( event , shape [ 'shape_id' ] ) } />
233+ { shape . likes }
165234 </ span > { " " }
166235 < span title = "Export" >
167236 < ExportIcon
0 commit comments