Skip to content

Commit

Permalink
Merge pull request #8 from TharindaPrabhath/frontend-day-09-stepper
Browse files Browse the repository at this point in the history
Frontend day 09 stepper
  • Loading branch information
TharindaPrabhath authored Oct 1, 2021
2 parents 7738700 + 4bf8ccd commit 2ea4f25
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 134 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"jspdf": "^2.4.0",
"moment": "^2.29.1",
"notistack": "^1.0.10",
"pdf-lib": "^1.16.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.5",
Expand Down
26 changes: 20 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import { SnackbarProvider } from "notistack";
import ProtectedRoute from "./components/ProtectedRoute";
import AuthRoute from "./components/AuthRoute";
import useLocalStorage from "./utils/useLocalStorage";
import { useDispatch, useSelector } from "react-redux";
import { useSelector } from "react-redux";
import { ReducerType } from "./redux/store";
import { fetchAdminByUsername } from "./utils/requestHelper";
import Activity from "./screens/Activity";
import { bindActionCreators } from "redux";
import { actionCreators } from "../src/redux";
import Settings from "./screens/Settings";
import useTokenService from "./utils/useTokenService";

const theme = createTheme({
palette: {
Expand All @@ -42,12 +41,27 @@ const theme = createTheme({
});

function App() {
const { saveAdmin, getAdmin } = useLocalStorage();
const { saveAdmin } = useLocalStorage();
const currentAdmin = useSelector(
(state: ReducerType) => state.adminReducer.currentAdmin
);
const dispatch = useDispatch();
const { setAdmin } = bindActionCreators(actionCreators, dispatch);
const { getRefreshTokenExpiresAt } = useTokenService();

useEffect(() => {
const refreshTokenExpiresAt = getRefreshTokenExpiresAt();
if (refreshTokenExpiresAt && refreshTokenExpiresAt !== undefined) {
// checking refersh token is expires or not
if (Date.now() > Date.parse(getRefreshTokenExpiresAt()!)) {
// refresh token has expired
// logging out the admin
localStorage.clear();
}
} else {
// no refresh token in the local storage. cannot trust the situation
// logging out the admin
localStorage.clear();
}
}, [getRefreshTokenExpiresAt]);

useEffect(() => {
fetchAdminByUsername(currentAdmin?.username!)
Expand Down
Binary file added src/assets/certificate.pdf
Binary file not shown.
Binary file added src/assets/fonts/Sanchez-Regular.ttf
Binary file not shown.
74 changes: 37 additions & 37 deletions src/components/CertificateTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
DialogContentText,
DialogTitle,
} from "@material-ui/core";
import React, { useEffect, useState } from "react";
import React, { useState } from "react";
import { useHistory } from "react-router-dom";
import { useButtonStyles } from "../data/styles";
import CertificateDto from "../types/models/CertificateDto";
import { CertificateTableProp } from "../types/TableProp";
import { useSnackbar } from "notistack";
import { deleteCertificate, fetchCertificates } from "../utils/requestHelper";
import { deleteCertificate } from "../utils/requestHelper";
import { useDispatch, useSelector } from "react-redux";
import { bindActionCreators } from "redux";
import { actionCreators } from "../redux";
Expand All @@ -25,18 +24,17 @@ import useLocalStorage from "../utils/useLocalStorage";
import useBadge from "../utils/useBadge";
import useSWR from "swr";
import requests from "../data/requests";
import axios from "../utils/axios";
import useAxios from "../utils/axios";

const getRow = (
certificateId: string,
recievedBy: string,
issuedBy: string,
type: string,
issuedDate: string
): CertificateTableProp => {
return { certificateId, recievedBy, issuedBy, type, issuedDate };
};
// const getRow = (
// certificateId: string,
// recievedBy: string,
// issuedBy: string,
// type: string,
// issuedDate: string
// ): CertificateTableProp => {
// return { certificateId, recievedBy, issuedBy, type, issuedDate };
// };

// const rows: CertificateTableProp[] = [
// getRow("001", "Tharinda P", "Lasana", "Participation", "2001.03.12"),
Expand All @@ -49,10 +47,15 @@ const getRow = (
// ];

const CertificateTable = () => {
// pagination & sorting
const [pageNo, setPageNo] = useState(0);
const [pageSize, setPageSize] = useState(5);
const [sortBy, setSortBy] = useState("id");

const buttonStyles = useButtonStyles();
const [openConfirmBox, setOpenConfirmBox] = useState<boolean>(false);
const [confirmBoxAgree, setConfirmBoxAgree] = useState<boolean>(false);
const [certificates, setCertficates] = useState<CertificateTableRow[]>([]);
//const [certificates, setCertficates] = useState<CertificateTableRow[]>([]);
const { enqueueSnackbar } = useSnackbar();
const dispatch = useDispatch();
const { removeCertificate } = bindActionCreators(actionCreators, dispatch);
Expand All @@ -64,23 +67,15 @@ const CertificateTable = () => {
const { CertificateCategoryBadge } = useBadge();
const axios = useAxios();

const { data } = useSWR(requests.fetchCertificates, (url: string) =>
axios
.get(url)
.then((r) => toCertificateTableData(r.data))
.catch((err) => console.error(err))
const { data: certificates } = useSWR(
`${requests.fetchCertificates}?page_no=${pageNo}&page_size=${pageSize}&sort_by=${sortBy}`,
(url: string) =>
axios
.get(url)
.then((r) => toCertificateTableData(r.data))
.catch((err) => console.error(err))
);

// useEffect(() => {
// fetchCertificates()
// .then((res) => {
// setCertficates(toCertificateTableData(res.data));
// })
// .catch((err) => {
// console.error(err);
// });
// }, []);

type CertificateTableRow = {
id: string;
recievedBy: string;
Expand Down Expand Up @@ -134,7 +129,7 @@ const CertificateTable = () => {
);

// removing the item from the table
setCertficates(certificates.filter((c) => c.id !== certificate.id));
//setCertficates(certificates.filter((c) => c.id !== certificate.id));

// if the deleted certificate is the current certificate in redux store. delete it
if (currentCertificate?.id === certificate.id) removeCertificate();
Expand Down Expand Up @@ -207,7 +202,7 @@ const CertificateTable = () => {
{
field: "action",
headerName: "Action",
width: 230,
width: 170,
editable: false,
renderCell: (params) => {
return (
Expand All @@ -219,12 +214,12 @@ const CertificateTable = () => {
>
View
</Button>
<Button
{/* <Button
className={buttonStyles.editBtn}
//onClick={(e) => handleEditClick(e, params.row.id)}
onClick={(e) => handleEditClick(e, params.row.id)}
>
Edit
</Button>
</Button> */}
<Button
className={buttonStyles.deleteBtn}
onClick={(e) => handleDeleteClick(e, params.row.id)}
Expand All @@ -247,10 +242,15 @@ const CertificateTable = () => {
}}
>
<DataGrid
rows={data || []}
rows={certificates || []}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
rowCount={100}
rowsPerPageOptions={[5, 10]}
paginationMode="server"
pagination
pageSize={pageSize}
onPageChange={(n) => setPageNo(n)}
onPageSizeChange={(s) => setPageSize(s)}
checkboxSelection
disableSelectionOnClick
style={{
Expand Down
18 changes: 0 additions & 18 deletions src/components/Topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,19 @@ import {
} from "@material-ui/core";
import { useState } from "react";
import Sidebar from "./Sidebar";
import { useDispatch, useSelector } from "react-redux";
import { bindActionCreators } from "redux";
import { actionCreators } from "../redux";
import { ReducerType } from "../redux/store";
import colors from "../data/colors";
import { Redirect } from "react-router";
import NotificationsIcon from "@material-ui/icons/Notifications";
import SearchIcon from "@material-ui/icons/Search";
import Notification from "./Notification";
import LoadingLinearProgress from "./LoadingLinearProgress";

const Topbar = () => {
const [sidebarOpen, setSidebarOpen] = useState<boolean>(false);
const [avatarMenuOpen, setAvatarMenuOpen] = useState<boolean>(false);
const [notificationOpen, setNotificationOpen] = useState<boolean>(false);
const [logout, setLogout] = useState<boolean>(false);
const notificationBtnRef = React.useRef<HTMLButtonElement>(null);
const avatarBtnRef = React.useRef<HTMLButtonElement>(null);

const ICON_SIZE = 24;
const ICON_INACTIVE_COLOR = "#f5f5f5";

const dispatch = useDispatch();
const { setAdmin, removeAdmin } = bindActionCreators(
actionCreators,
dispatch
);
const currentAdmin = useSelector(
(state: ReducerType) => state.adminReducer.currentAdmin
);

const toggleSidebar = (open: boolean) => {
setSidebarOpen(open);
};
Expand Down
94 changes: 47 additions & 47 deletions src/components/UserTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useState } from "react";

import {
Button,
Expand All @@ -13,13 +13,12 @@ import { DataGrid, GridColDef } from "@mui/x-data-grid";

import { useButtonStyles } from "../data/styles";
import { Redirect } from "react-router-dom";
import UserDto from "../types/models/UserDto";

import { useDispatch, useSelector } from "react-redux";
import { bindActionCreators } from "redux";
import { actionCreators } from "../redux";
import { toUserDtos } from "../utils/mapper";
import { deleteUser, fetchUsers } from "../utils/requestHelper";
import { deleteUser } from "../utils/requestHelper";
import { useSnackbar } from "notistack";
import { ReducerType } from "../redux/store";
import colors from "../data/colors";
Expand All @@ -31,36 +30,40 @@ import useSWR from "swr";
import requests from "../data/requests";
import useAxios from "../utils/axios";

const getRow = (
id: string,
fname: string,
lname: string,
email: string,
role: string,
birthday: string
) => {
return { id, fname, lname, email, role, birthday };
};
// const getRow = (
// id: string,
// fname: string,
// lname: string,
// email: string,
// role: string,
// birthday: string
// ) => {
// return { id, fname, lname, email, role, birthday };
// };

const rows = [
getRow("001", "Tharinda", "P", "[email protected]", "Uni", "2001.03.12"),
getRow("002", "Prabhath", "", "[email protected]", "Uni", "2001.03.12"),
getRow("003", "Lasana", "sanketh", "[email protected]", "Uni", "2001.03.12"),
getRow(
"004",
"Lishitha",
"Alahakoon",
"[email protected]",
"Uni",
"2001.03.12"
),
getRow("005", "Nadun", "", "[email protected]", "Uni", "2001.03.12"),
getRow("006", "Chamath", "Roo", "[email protected]", "Uni", "2001.03.12"),
getRow("007", "Hasitha", "G", "[email protected]", "Uni", "2001.03.12"),
];
// const rows = [
// getRow("001", "Tharinda", "P", "[email protected]", "Uni", "2001.03.12"),
// getRow("002", "Prabhath", "", "[email protected]", "Uni", "2001.03.12"),
// getRow("003", "Lasana", "sanketh", "[email protected]", "Uni", "2001.03.12"),
// getRow(
// "004",
// "Lishitha",
// "Alahakoon",
// "[email protected]",
// "Uni",
// "2001.03.12"
// ),
// getRow("005", "Nadun", "", "[email protected]", "Uni", "2001.03.12"),
// getRow("006", "Chamath", "Roo", "[email protected]", "Uni", "2001.03.12"),
// getRow("007", "Hasitha", "G", "[email protected]", "Uni", "2001.03.12"),
// ];

const UserTable = () => {
//const [users, setUsers] = useState<UserDto[]>([]);
// pagination & sorting
const [pageNo, setPageNo] = useState(0);
const [pageSize, setPageSize] = useState(5);
const [sortBy, setSortBy] = useState("uid");

const [selectedUserId, setSelectedUserId] = useState<number>();
const [redirect, setRedirect] = useState<boolean>(false);
const [openConfirmBox, setOpenConfirmBox] = useState<boolean>(false);
Expand All @@ -77,23 +80,15 @@ const UserTable = () => {
const { StatusBadge } = useBadge();
const axios = useAxios();

const { data: users } = useSWR(requests.fetchUsers, (url: string) =>
axios
.get(url)
.then((r) => toUserDtos(r.data))
.catch((err) => console.error(err))
const { data: users } = useSWR(
`${requests.fetchUsers}?page_no=${pageNo}&page_size=${pageSize}&sort_by=${sortBy}`,
(url: string) =>
axios
.get(url)
.then((r) => toUserDtos(r.data))
.catch((err) => console.error(err))
);

// useEffect(() => {
// fetchUsers()
// .then((res) => {
// setUsers(toUserDtos(res.data));
// })
// .catch((err) => {
// console.error(err);
// });
// }, []);

const handleViewClick = (event: React.MouseEvent<unknown>, id: number) => {
// set current user in redux store
setUser(users?.find((u) => u.id === id)!);
Expand Down Expand Up @@ -238,8 +233,13 @@ const UserTable = () => {
<DataGrid
rows={users || []}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
rowCount={100}
rowsPerPageOptions={[5, 10]}
paginationMode="server"
pagination
pageSize={pageSize}
onPageChange={(n) => setPageNo(n)}
onPageSizeChange={(s) => setPageSize(s)}
checkboxSelection
disableSelectionOnClick
style={{
Expand Down
3 changes: 2 additions & 1 deletion src/data/requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const login = "/auth/login"
const validateToken = "/auth/validateToken"
const refreshToken = "/auth/refresh-token"
const validateCertificate = "/certificate/validate"
const changePassword = "/admin/change-password"

Expand All @@ -26,7 +27,7 @@ const updateCertificate = "/certificate/update"
const deleteUser = "/user/delete"
const deleteCertificate = "/certificate/delete"

const requests = {login, validateToken, validateCertificate, changePassword, fetchAdminByUsername , fetchAdmins, fetchUser, fetchUsers, fetchSuggestionUsers, fetchCertificate,
const requests = {login, validateToken, refreshToken, validateCertificate, changePassword, fetchAdminByUsername , fetchAdmins, fetchUser, fetchUsers, fetchSuggestionUsers, fetchCertificate,
fetchCertificatesByUser, fetchCertificates, fetchAdminsAnalytics, fetchUsersAnalytics, fetchCertificatesAnalytics, fetchActivities, postNewUser, postNewCertificate,
postNewThirdPartyCertificate, updateUser, updateCertificate, deleteUser, deleteCertificate }

Expand Down
2 changes: 1 addition & 1 deletion src/screens/CertficateVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const CertificateVerification = () => {
}
})
.catch((err) => {
setInvalidCertificate(true);
setVerified(false);
setInvalidCertificate(true);
console.error(err);
})
.finally(() => setLoading(false));
Expand Down
Loading

0 comments on commit 2ea4f25

Please sign in to comment.