Skip to content

Commit adac7eb

Browse files
committed
feat: replaced the harperdb calls with APIs for shape list component
1 parent acbcd6f commit adac7eb

8 files changed

Lines changed: 167 additions & 52 deletions

File tree

components/core/CreateShape.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,

components/utils/ShapeList.js

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React, { useState, useEffect } from "react";
22

3+
// axios
4+
import axios from "axios";
5+
36
// Bootstrap
47
import Container from 'react-bootstrap/Container'
58
import Button from "react-bootstrap/Button";
@@ -10,9 +13,6 @@ import styled from "styled-components";
1013
// dynamic from Next.js
1114
import dynamic from "next/dynamic";
1215

13-
// harperDb fetch call
14-
import { harperFetch } from "../../utils/HarperFetch";
15-
1616
// Toast
1717
import 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

pages/api/DELETE/like.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
export default async function handler(req, res) {
3+
4+
const { likeId } = req.body;
5+
6+
console.log(likeId);
7+
8+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
9+
method: "POST",
10+
headers: {
11+
"Content-Type": "application/json",
12+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
13+
},
14+
body: JSON.stringify({
15+
operation: "delete",
16+
schema: "tryshape",
17+
table: "likes",
18+
hash_values: [likeId],
19+
}),
20+
});
21+
22+
const data = await request.json();
23+
24+
res.status(200).json({ data });
25+
}
26+

pages/api/GET/likes.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
export default async function handler(req, res) {
2-
const { email } = req.query;
2+
const { email, shapeId } = req.query;
33

4-
let sql = `SELECT *
5-
FROM tryshape.likes
6-
WHERE email = '${email}'`;
4+
let sql;
5+
6+
if(shapeId) {
7+
sql = `SELECT * FROM tryshape.likes WHERE shape_id='${shapeId}' AND email='${email}'`;
8+
} else {
9+
sql = `SELECT * FROM tryshape.likes WHERE email = '${email}'`;
10+
}
711

8-
912
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
1013
method: "POST",
1114
headers: {

pages/api/GET/shape.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default async function handler(req, res) {
2+
const { shapeId } = req.query;
3+
4+
let sql = `SELECT s.likes FROM tryshape.shapes s WHERE s.shape_id='${shapeId}'`;
5+
6+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
11+
},
12+
body: JSON.stringify({
13+
operation: "sql",
14+
sql: sql,
15+
}),
16+
});
17+
18+
const data = await request.json();
19+
20+
res.status(200).json(data);
21+
}

pages/api/GET/shapes.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
export default async function handler(req, res) {
22
const { type, email } = req.query;
3-
console.log(req.query);
4-
console.log(`Shape type to query is ${type}`);
5-
3+
64
let sql;
75
if (type === 'private') {
86
sql = `SELECT *
@@ -13,7 +11,6 @@ export default async function handler(req, res) {
1311
} else if(type === 'public') {
1412
sql = `SELECT * FROM tryshape.shapes`;
1513
} else if(type === 'public-logged-in') {
16-
console.log(`email is ${email}`);
1714
sql = `SELECT *
1815
FROM tryshape.shapes s
1916
INNER JOIN tryshape.users u
@@ -36,7 +33,6 @@ export default async function handler(req, res) {
3633
});
3734

3835
const data = await request.json();
39-
// console.log(data);
4036

4137
res.status(200).json(data);
4238
}

pages/api/POST/like.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
export default async function handler(req, res) {
3+
4+
const { shapeId, email } = req.body;
5+
6+
console.log(shapeId, email);
7+
8+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
9+
method: "POST",
10+
headers: {
11+
"Content-Type": "application/json",
12+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
13+
},
14+
body: JSON.stringify({
15+
operation: "insert",
16+
schema: "tryshape",
17+
table: "likes",
18+
records: [
19+
{
20+
shape_id: shapeId,
21+
email: email
22+
},
23+
],
24+
}),
25+
});
26+
27+
const data = await request.json();
28+
29+
res.status(200).json({ data });
30+
}

pages/api/PUT/shape.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
export default async function handler(req, res) {
3+
4+
const { likes, shapeId } = req.body;
5+
6+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
11+
},
12+
body: JSON.stringify({
13+
operation: "update",
14+
schema: "tryshape",
15+
table: "shapes",
16+
records: [
17+
{
18+
shape_id: shapeId,
19+
likes: likes,
20+
},
21+
],
22+
}),
23+
});
24+
25+
const data = await request.json();
26+
27+
res.status(200).json({ data });
28+
}

0 commit comments

Comments
 (0)