Skip to content

Commit

Permalink
axios to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
aatmanvaidya committed Sep 11, 2023
1 parent 4d5010a commit 40059a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
12 changes: 7 additions & 5 deletions browser-extension/api-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const port = 3000;
const cors = require("cors");
const { upload } = require("./s3");

const { preference, post, feedback, slur, category } = require("./db/models");
const { Op } = require("sequelize");
const { preference, post, feedback, slur, category, sequelize } = require("./db/models");


const { registerAnonymousUser, resetUser } = require("./controller-auth");
const { sendEmail } = require("./email");
const {
Expand Down Expand Up @@ -229,6 +230,7 @@ app.get("/slur", async (req, res) => {
app.post("/slur/create", async (req, res) => {
const { userId, label, labelMeaning, appropriated, appropriationContext, categories } = req.body;
// const t = await Op.transaction();
const t = await sequelize.transaction()

try {
const newSlur = await slur.create(
Expand All @@ -239,7 +241,7 @@ app.post("/slur/create", async (req, res) => {
appropriated,
appropriationContext,
},
// { transaction: t }
{ transaction: t }
);

const categoryPromises = categories.map(async (categoryData) => {
Expand All @@ -248,7 +250,7 @@ app.post("/slur/create", async (req, res) => {
slurId: newSlur.id,
category: categoryData.category,
},
// { transaction: t }
{ transaction: t }
);
return newCategory;
});
Expand All @@ -257,7 +259,7 @@ app.post("/slur/create", async (req, res) => {
// https://stackoverflow.com/questions/28897708/sequelize-save-in-multiple-tables
const createdCategories = await Promise.all(categoryPromises);

// await t.commit();
await t.commit();

res.send({
slur: newSlur,
Expand Down
5 changes: 0 additions & 5 deletions browser-extension/plugin/src/ui-components/pages/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ async function resetAccount(accessToken) {
);
}

//https://www.freecodecamp.org/news/axios-react-how-to-make-get-post-and-delete-api-requests/
// const axiosInstance = axios.create({
// baseURL: API_URL,
// });

// GET request for slur and category
async function getSlurAndCategory(accessToken) {
const result = await axios.get(`${API_URL}/slur`, {
Expand Down
22 changes: 22 additions & 0 deletions browser-extension/plugin/src/ui-components/pages/Slur.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { useState, useEffect, useContext } from 'react';
import { Box, Text, Button } from 'grommet';
import { Add } from 'grommet-icons';
import { useNavigate } from 'react-router-dom';
import Api from './Api';
import { UserContext } from '../atoms/AppContext';

const { getSlurAndCategory } = Api;

export function Slur() {
let navigate = useNavigate();
const [, setGetSlurs] = useState([]);
const { user } = useContext(UserContext);
// const { notification, showNotification } = useContext(NotificationContext);

const navigateToAddSlur = () => {
navigate('/slur/create');
};
async function fetchSlurs() {
try {
const slur = await getSlurAndCategory(user.accessToken);
setGetSlurs(slur);
console.log(slur);
} catch (error) {
console.error('error fetching slurs', error);
}
}

useEffect(() => {
fetchSlurs();
}, []);

return (
<Box fill gap={'medium'}>
Expand Down

0 comments on commit 40059a4

Please sign in to comment.