Skip to content

Commit eb9180c

Browse files
committed
feat: Implemented the Like shape functionality
1 parent e04e063 commit eb9180c

2 files changed

Lines changed: 84 additions & 20 deletions

File tree

components/core/App.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,63 @@ const App = (props) => {
2525
let shapes = [];
2626

2727
if(user.length === 0) {
28-
// User is not logged In
28+
// User is not logged In. Fetch all the public shapes
2929
shapes = await harperFetch({
3030
operation: "sql",
3131
sql: `SELECT *
3232
FROM tryshape.shapes s
3333
INNER JOIN tryshape.users u
3434
ON s.createdBy=u.email
35-
WHERE s.private=false`,
35+
WHERE s.private=false
36+
ORDER BY s.likes DESC`,
3637
});
3738
} else {
38-
// User is logged in. Let's fetch the private shape and pther public shapes.
39+
// User is logged in. Let's fetch the private shape and other public shapes.
3940
shapes = await harperFetch({
4041
operation: "sql",
4142
sql: `SELECT *
4243
FROM tryshape.shapes s
4344
INNER JOIN tryshape.users u
4445
ON s.createdBy=u.email
4546
WHERE s.private=false
46-
OR createdBy = '${user.email}'`,
47+
OR createdBy = '${user.email}'
48+
ORDER BY s.likes DESC`,
4749
});
50+
51+
// Fetch the shapes liked by the logged-in user
52+
const likedShapes = await harperFetch({
53+
operation: "sql",
54+
sql: `SELECT *
55+
FROM tryshape.likes
56+
WHERE email = '${user.email}'`,
57+
});
58+
59+
// If there are liked shapes, take out the shape_id
60+
if (likedShapes.length > 0) {
61+
let likedShapeIds = likedShapes.map((liked, index) => {
62+
return liked['shape_id'];
63+
})
64+
shapes.map((shape, index) => {
65+
66+
if (likedShapeIds.includes(shape['shape_id'])) {
67+
shape['liked'] = true;
68+
} else {
69+
shape['liked'] = false;
70+
}
71+
return shape;
72+
});
73+
}
4874
}
4975

50-
console.log(shapes);
51-
let modifiedShapes = shapes.map((shape, index) => {
76+
// Add the showAdvanced property
77+
shapes.map((shape, index) => {
5278
shape.showAdvanced = false;
5379
return shape;
5480
});
5581

56-
console.log(modifiedShapes);
82+
console.log(shapes);
5783

58-
await setData(modifiedShapes);
84+
await setData(shapes);
5985
setLoading(false);
6086
}, [user]);
6187

components/utils/ShapeList.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const Shape = dynamic(import("react-clip-path"), { ssr: false });
1919
import Switch from "react-switch";
2020

2121
// icons
22-
import { FiCopy, FiDownload, FiHeart, FiLock } from 'react-icons/fi';
22+
import { FiCopy, FiDownload, FiLock } from 'react-icons/fi';
2323
import { BiExport } from "react-icons/bi";
24+
import { BsFillHeartFill, BsHeart} from "react-icons/bs";
2425

2526
// Export Shape
2627
import { ExportShape } from '..';
@@ -100,8 +101,17 @@ const ExportIcon = styled(BiExport)`
100101
}
101102
`;
102103

103-
const LikeIcon = styled(FiHeart)`
104+
const LikeIcon = styled(BsHeart)`
104105
cursor: pointer;
106+
color: red;
107+
&:hover {
108+
color: #f71b6f;
109+
}
110+
`;
111+
112+
const LikeFilledIcon = styled(BsFillHeartFill)`
113+
cursor: pointer;
114+
color: red;
105115
&:hover {
106116
color: #f71b6f;
107117
}
@@ -127,6 +137,9 @@ const ShapeList = ({ setOpen, user, data }) => {
127137
setShapes(...[modifiedShapes]);
128138
};
129139

140+
/**
141+
* Copy the clip-path value to clipboard
142+
*/
130143
async function performCopy(event, formula) {
131144
event.preventDefault();
132145
try {
@@ -138,20 +151,32 @@ const ShapeList = ({ setOpen, user, data }) => {
138151
}
139152
}
140153

154+
/**
155+
* Method to execute when user clicks on the export shape
156+
*/
141157
const performExport = shape => {
158+
// Check if user logged-in
142159
if (user.length === 0) {
160+
// Show the login modal if user is not authenticated
143161
setOpen(true);
144162
} else {
163+
// Set the shape details to export
145164
setShapeToExport(shape);
165+
// Show the export modal
146166
setShowExportModal(true);
147167
}
148168
}
149169

170+
/**
171+
* Method to execute when user clicks on the likes
172+
*/
150173
const performLike = async (event, shapeId) => {
174+
// Check if user logged-in
151175
if (user.length === 0) {
152-
showOpen(true);
176+
// Show the login modal if user is not authenticated
177+
setOpen(true);
153178
} else {
154-
// Initialize likes
179+
// Good to go. Initialize likes
155180
let likes = 0;
156181

157182
// Check if already an entry for this user's like
@@ -181,8 +206,6 @@ const ShapeList = ({ setOpen, user, data }) => {
181206
if (insertLike) {
182207
// Update the count by 1
183208
likes = returnValue[0].likes + 1;
184-
185-
// Update the like state
186209
}
187210
} else {
188211
// If present, delete to remove like
@@ -194,8 +217,6 @@ const ShapeList = ({ setOpen, user, data }) => {
194217
if (deleteLike) {
195218
// update the like count decrease by 1
196219
likes = returnValue[0].likes - 1;
197-
198-
// update the likes state
199220
}
200221
}
201222

@@ -206,7 +227,17 @@ const ShapeList = ({ setOpen, user, data }) => {
206227
});
207228

208229
// Update the shape data in the shapes array
209-
230+
let modifiedShapes = shapes.map((shape, index) => {
231+
if (shape['shape_id'] === shapeId) {
232+
return {
233+
...shape,
234+
liked: !shape.liked,
235+
likes: likes
236+
};
237+
}
238+
return shape;
239+
});
240+
setShapes(...[modifiedShapes]);
210241
}
211242
};
212243

@@ -227,9 +258,16 @@ const ShapeList = ({ setOpen, user, data }) => {
227258
{shape.private && <FiLock />}
228259
<ShapeActions>
229260
<span title="Like">
230-
<LikeIcon
231-
size={24}
232-
onClick={(event, shapeId) => performLike(event, shape['shape_id'])}/>
261+
{
262+
shape.liked ?
263+
(<LikeFilledIcon
264+
size={24}
265+
onClick={(event, shapeId) => performLike(event, shape['shape_id'])}/>)
266+
:
267+
(<LikeIcon
268+
size={24}
269+
onClick={(event, shapeId) => performLike(event, shape['shape_id'])}/>)
270+
}
233271
{shape.likes}
234272
</span>{" "}
235273
<span title="Export">

0 commit comments

Comments
 (0)