11import React , { useState , useEffect } from "react" ;
22
3+ // axios
4+ import axios from "axios" ;
5+
36// Bootstrap
47import Container from 'react-bootstrap/Container'
58import Button from "react-bootstrap/Button" ;
@@ -10,9 +13,6 @@ import styled from "styled-components";
1013// dynamic from Next.js
1114import dynamic from "next/dynamic" ;
1215
13- // harperDb fetch call
14- import { harperFetch } from "../../utils/HarperFetch" ;
15-
1616// Toast
1717import toast from "react-hot-toast" ;
1818
@@ -350,64 +350,75 @@ const ShapeList = (
350350
351351 // Check if already an entry for this user's like
352352 // for the shape present.
353- const isPresent = await harperFetch ( {
354- operation : "sql" ,
355- sql : `SELECT *
356- FROM tryshape.likes
357- WHERE shape_id=' ${ shapeId } ' AND email=' ${ user . email } '` ,
353+ const likedResponse = await axios . get ( "/api/GET/likes" , {
354+ params : {
355+ shapeId : shapeId ,
356+ email : user . email
357+ }
358358 } ) ;
359+ const isPresent = likedResponse . data ;
360+ console . log ( { isPresent} ) ;
359361 // Get the latest likes count from db
360- const returnValue = await harperFetch ( {
361- operation : "sql" ,
362- sql : `SELECT s.likes
363- FROM tryshape.shapes s
364- WHERE s.shape_id='${ shapeId } '` ,
362+ const shapeResponse = await axios . get ( "/api/GET/shape" , {
363+ params : {
364+ shapeId : shapeId
365+ }
365366 } ) ;
367+ const returnValue = shapeResponse . data ;
366368
367369 if ( isPresent . length === 0 ) {
368370 // If not present, add for like
369- const insertLike = await harperFetch ( {
370- operation : "sql" ,
371- sql : `INSERT into tryshape.likes(shape_id, email)
372- values('${ shapeId } ', '${ user . email } ')` ,
371+ const insertLikeResponse = await axios . post ( '/api/POST/like' , {
372+ shapeId : shapeId ,
373+ email : user . email
373374 } ) ;
375+ const insertLike = insertLikeResponse . data ;
376+ console . log ( { insertLike} ) ;
374377
375- if ( insertLike ) {
378+ if ( insertLike . data . inserted_hashes . length > 0 ) {
376379 // Update the count by 1
377380 likes = returnValue [ 0 ] . likes + 1 ;
378381 }
379382 } else {
380- // If present, delete to remove like
381- const deleteLike = await harperFetch ( {
382- operation : "sql" ,
383- sql : `DELETE from tryshape.likes
384- WHERE shape_id=' ${ shapeId } ' AND email=' ${ user . email } '` ,
383+ // If present, get the like id
384+ const likeId = isPresent [ 0 ] . like_id ;
385+ // delete to remove like
386+ const deleteLikeResponse = await axios . post ( '/api/DELETE/like' , {
387+ likeId : likeId
385388 } ) ;
386- if ( deleteLike ) {
389+ const deleteLike = deleteLikeResponse . data ;
390+ console . log ( { deleteLike} ) ;
391+
392+ if ( deleteLike . data . deleted_hashes . length > 0 ) {
387393 // update the like count decrease by 1
388394 likes = returnValue [ 0 ] . likes - 1 ;
389395 }
390396 }
391397
392398 // Update the shape data with the updated count
393- const updated = await harperFetch ( {
394- operation : "sql" ,
395- sql : `UPDATE tryshape.shapes SET likes = ${ likes } WHERE shape_id=' ${ shapeId } '`
399+ const updateShapeResponse = await axios . post ( '/api/PUT/shape' , {
400+ shapeId : shapeId ,
401+ likes : likes
396402 } ) ;
397-
398- // Update the shape data in the shapes array
399- let modifiedShapes = shapes . map ( ( shape , index ) => {
400- if ( shape [ 'shape_id' ] === shapeId ) {
401- return {
402- ...shape ,
403- liked : ! shape . liked ,
404- likes : likes
405- } ;
406- }
407- return shape ;
408- } ) ;
409- setShapes ( ...[ modifiedShapes ] ) ;
410- //setData(...[modifiedShapes]);
403+ const updated = updateShapeResponse . data ;
404+ console . log ( { updated} ) ;
405+
406+ if ( updated . data . update_hashes . length > 0 ) {
407+ // Update the shape data in the shapes array
408+ let modifiedShapes = shapes . map ( ( shape , index ) => {
409+ if ( shape [ 'shape_id' ] === shapeId ) {
410+ return {
411+ ...shape ,
412+ liked : ! shape . liked ,
413+ likes : likes
414+ } ;
415+ }
416+ return shape ;
417+ } ) ;
418+ setShapes ( ...[ modifiedShapes ] ) ;
419+ } else {
420+ toast . error ( 'Not able to update the likes at this moment.' ) ;
421+ }
411422 }
412423 } ;
413424
0 commit comments