Skip to content

Commit d1d03db

Browse files
committed
feat: Now using Axios and translated the shape query as the API
1 parent 06e1fbc commit d1d03db

4 files changed

Lines changed: 68 additions & 18 deletions

File tree

components/core/App.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from "react";
2+
import axios from "axios";
23
import dynamic from "next/dynamic";
34

45
// harperDb fetch call
@@ -33,28 +34,22 @@ const App = (props) => {
3334
let shapes = [];
3435

3536
if(user.length === 0) {
36-
// User is not logged In. Fetch all the public shapes
37-
shapes = await harperFetch({
38-
operation: "sql",
39-
sql: `SELECT *
40-
FROM tryshape.shapes s
41-
INNER JOIN tryshape.users u
42-
ON s.createdBy=u.email
43-
WHERE s.private=false
44-
ORDER BY s.likes DESC`,
37+
// User is not logged In. Fetch all the public shapes
38+
const response = await axios.get("/api/GET/shapes", {
39+
params: {
40+
type: 'private'
41+
}
4542
});
43+
shapes = response.data;
4644
} else {
4745
// User is logged in. Let's fetch the private shape and other public shapes.
48-
shapes = await harperFetch({
49-
operation: "sql",
50-
sql: `SELECT *
51-
FROM tryshape.shapes s
52-
INNER JOIN tryshape.users u
53-
ON s.createdBy=u.email
54-
WHERE s.private=false
55-
OR createdBy = '${user.email}'
56-
ORDER BY s.likes DESC`,
46+
const response = await axios.get("/api/GET/shapes", {
47+
params: {
48+
type: 'public-logged-in',
49+
email: user.email
50+
}
5751
});
52+
shapes = response.data;
5853

5954
// Fetch the shapes liked by the logged-in user
6055
const likedShapes = await harperFetch({

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"start": "next start"
99
},
1010
"dependencies": {
11+
"axios": "^0.21.1",
1112
"bootstrap": "4.6.0",
1213
"date-fns": "^2.22.1",
1314
"downloadjs": "^1.4.7",

pages/api/GET/shapes.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export default async function handler(req, res) {
2+
const { type, email } = req.query;
3+
console.log(req.query);
4+
console.log(`Shape type to query is ${type}`);
5+
6+
let sql;
7+
if (type === 'private') {
8+
sql = `SELECT *
9+
FROM tryshape.shapes s
10+
INNER JOIN tryshape.users u ON s.createdBy=u.email
11+
WHERE s.private=false
12+
ORDER BY s.likes DESC`;
13+
} else if(type === 'public') {
14+
sql = `SELECT * FROM tryshape.shapes`;
15+
} else if(type === 'public-logged-in') {
16+
console.log(`email is ${email}`);
17+
sql = `SELECT *
18+
FROM tryshape.shapes s
19+
INNER JOIN tryshape.users u
20+
ON s.createdBy=u.email
21+
WHERE s.private=false
22+
OR createdBy = '${email}'
23+
ORDER BY s.likes DESC`;
24+
}
25+
26+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
27+
method: "POST",
28+
headers: {
29+
"Content-Type": "application/json",
30+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
31+
},
32+
body: JSON.stringify({
33+
operation: "sql",
34+
sql: sql,
35+
}),
36+
});
37+
38+
const data = await request.json();
39+
// console.log(data);
40+
41+
res.status(200).json(data);
42+
}

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ available-typed-arrays@^1.0.2:
677677
resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.4.tgz#9e0ae84ecff20caae6a94a1c3bc39b955649b7a9"
678678
integrity sha512-SA5mXJWrId1TaQjfxUYghbqQ/hYioKmLJvPJyDuYRtXXenFNMjj4hSSt1Cf1xsuXSXrtxrVC5Ot4eU6cOtBDdA==
679679

680+
axios@^0.21.1:
681+
version "0.21.1"
682+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
683+
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
684+
dependencies:
685+
follow-redirects "^1.10.0"
686+
680687
"babel-plugin-styled-components@>= 1.12.0", babel-plugin-styled-components@^1.12.0:
681688
version "1.12.0"
682689
resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-1.12.0.tgz#1dec1676512177de6b827211e9eda5a30db4f9b9"
@@ -1290,6 +1297,11 @@ firebase@^8.6.7:
12901297
"@firebase/storage" "0.5.4"
12911298
"@firebase/util" "1.1.0"
12921299

1300+
follow-redirects@^1.10.0:
1301+
version "1.14.1"
1302+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.1.tgz#d9114ded0a1cfdd334e164e6662ad02bfd91ff43"
1303+
integrity sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==
1304+
12931305
foreach@^2.0.5:
12941306
version "2.0.5"
12951307
resolved "https://registry.yarnpkg.com/foreach/-/foreach-2.0.5.tgz#0bee005018aeb260d0a3af3ae658dd0136ec1b99"

0 commit comments

Comments
 (0)