Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Front end #50

Merged
merged 2 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Route, Routes } from 'react-router';
import Admin from './components/Admin';
import TweetCollectionAdminPanel from './components/Admin/TweetCollectionAdminPanel';
import AuthProvider from './components/AuthProvider';
import EndUser from './components/EndUser';
import Login from './components/Login';
Expand All @@ -18,7 +19,16 @@ function App() {
<Admin />
</RequireAuth>
}
/>
>
<Route
path="modify"
element={<TweetCollectionAdminPanel action="modify" />}
/>
<Route
index
element={<TweetCollectionAdminPanel action="verify" />}
/>
</Route>
</Routes>
</AuthProvider>
);
Expand Down
75 changes: 51 additions & 24 deletions client/src/components/Admin/Tweet.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { Button, Checkbox, TableCell, TableRow } from "@mui/material";
import axios from "axios";
import { useEffect, useState } from "react";
import { columns } from "../../constants";

const Tweet = ({ row }) => {
const Tweet = ({ row, verified, action }) => {
const [changedColumn, setChangedColumn] = useState({ ...row });
const [isVerified, setIsVerified] = useState(
"verified_at" in Object.keys(row)
);
const [isVerified, setIsVerified] = useState(verified);
useEffect(() => {
setChangedColumn({ ...row });
setIsVerified("verified_at" in Object.keys(row));
}, [row]);
setIsVerified(verified);
}, [row, verified]);
const modifySubmit = () => {
let toSubmit = {};
for (const prop in row) {
toSubmit[prop] = changedColumn[prop];
}
let accessToken = sessionStorage.getItem("accessToken");
axios
.patch(`/tweets/${row.id}`, toSubmit, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.then((data) => data.data)
.then((data) => {
setIsVerified(true);
});
};
const verifySubmit = () => {
let toSubmit = {};
for (const prop in row) {
Expand All @@ -19,7 +35,6 @@ const Tweet = ({ row }) => {
}
}
let accessToken = sessionStorage.getItem("accessToken");
console.log(accessToken);
axios
.patch(`/tweets/pseudo/${row.id}`, toSubmit, {
headers: {
Expand All @@ -43,31 +58,43 @@ const Tweet = ({ row }) => {
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell align="right">
{isVerified ? (
<Button color="success" onClick={verifySubmit}>
{"Verified"}
</Button>
) : (
<Button variant="contained" onClick={verifySubmit}>
{"Verify"}
</Button>
)}
{
(action = "modify" ? (
<Button variant="contained" onClick={modifySubmit}>
{"Modify"}
</Button>
) : (
<>
{isVerified ? (
<Button
color="warning"
// variant="contained"
onClick={verifySubmit}
>
{"Verified"}
</Button>
) : (
<Button variant="contained" onClick={verifySubmit}>
{"Verify"}
</Button>
)}
</>
))
}
</TableCell>
{Object.keys(row)
.filter(
(datum) =>
datum !== "created_at" && datum !== "username" && datum !== "id"
)
.map((datum) => {
{columns
.map((column) => column.field)
.filter((datum) => datum !== "verify")
.map((datum, index) => {
if (datum === "text")
return (
<TableCell sx={{ fontSize: "1rem" }} align="left">{`${
<TableCell key={index} sx={{ fontSize: "1rem" }} align="left">{`${
row[`${datum}`]
}`}</TableCell>
);
else
return (
<TableCell align="right">
<TableCell key={index} align="right">
<Checkbox
checked={changedColumn[`${datum}`]}
onChange={(event) => {
Expand Down
60 changes: 16 additions & 44 deletions client/src/components/Admin/TweetCollectionAdminPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,33 @@ import {
} from "@mui/material";
import axios from "axios";
import { useEffect, useState } from "react";
import { columns } from "../../constants";
import Tweet from "./Tweet";
// const buttonRef = React.createRef();

const TweetCollectionAdminPanel = () => {
const TweetCollectionAdminPanel = ({ action }) => {
const [dataList, setDataList] = useState([]);
const [offset, setOffset] = useState(0);
const [minority, setMinority] = useState(true);
const [offsetTemp, setOffsetTemp] = useState(offset);
const [reload, setReload] = useState(true);
const columns = [
{ field: "verify", headerName: "Verify" },
{
field: "text",
headerName: "text",
},
{
field: "covid_stats",
headerName: "covid \n stats",
},
{
field: "vaccination",
headerName: "vaccination",
},
{
field: "covid_politics",
headerName: "covid \n politics",
},
{
field: "humour",
headerName: "humour",
},
{
field: "lockdown",
headerName: "lockdown",
},
{
field: "civic_views",
headerName: "civic \n views",
},
{
field: "life_during_pandemic",
headerName: "life during \npandemic",
},
{
field: "covid_waves_and_variants",
headerName: "covid waves \n and \n variants",
},
];

useEffect(() => {
axios
.get(`/tweets/pseudo/?offset=${offset}&limit=10&minority=${minority}`)
.get(
`/tweets/${
action === "verify" ? `pseudo/` : ""
}?offset=${offset}&limit=10&minority=${minority}`
)
.then((data) => data.data)
.then((data) => {
console.log(data);
setDataList(data);
});
}, [offset, minority, reload]);
}, [offset, minority, reload, action]);

return (
<div className=" mt-10 w-11/12 mx-auto h-96">
<div className=" mt-10 w-11/12 mx-auto ">
<div className="w-3/12 flex justify-between items-end mb-3">
<div>
<InputLabel id="minority">Minority</InputLabel>
Expand Down Expand Up @@ -139,7 +106,12 @@ const TweetCollectionAdminPanel = () => {
</TableHead>
<TableBody>
{dataList.map((row, index) => (
<Tweet key={index} row={{ ...row }} />
<Tweet
key={index}
row={{ ...row }}
action={action}
verified={!!row["verified_at"]}
/>
))}
</TableBody>
</Table>
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/Admin/index.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Outlet } from "react-router";
import Nav from "../Nav";
import TweetCollectionAdminPanel from "./TweetCollectionAdminPanel";

const Admin = () => {
return (
<div>
<Nav />
<TweetCollectionAdminPanel />
{/* <TweetCollectionAdminPanel action="verify" /> */}
<Outlet />
</div>
);
};
Expand Down
42 changes: 40 additions & 2 deletions client/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
const baseAddress = "http://localhost:8000";

export { baseAddress };
const columns = [
{ field: "verify", headerName: "Verify" },
{
field: "text",
headerName: "text",
},
{
field: "covid_stats",
headerName: "covid \n stats",
},
{
field: "vaccination",
headerName: "vaccination",
},
{
field: "covid_politics",
headerName: "covid \n politics",
},
{
field: "humour",
headerName: "humour",
},
{
field: "lockdown",
headerName: "lockdown",
},
{
field: "civic_views",
headerName: "civic \n views",
},
{
field: "life_during_pandemic",
headerName: "life during \npandemic",
},
{
field: "covid_waves_and_variants",
headerName: "covid waves \n and \n variants",
},
];
export { baseAddress,columns };