Skip to content

Commit

Permalink
refactor: implemented ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Curtis committed Aug 16, 2024
1 parent 5904bc5 commit 040b2ea
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const Header = (): JSX.Element => {
setIsSigningOut(true);

try {
setTimeout(async () => {
await signOut();
setTimeout(() => {
signOut();
}, 1000);
} catch (error: any) {
console.log(error.message);
Expand Down
27 changes: 10 additions & 17 deletions src/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import { useAuth } from "../hooks/useAuth";
import { useEffect } from "react";
import PropTypes from "prop-types";
import { useAuth } from "../hooks";
import React, { useEffect } from "react";
import { Navigate } from "react-router-dom";

export const ProtectedRoute = ({ children }) => {
export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
const { user, authenticate } = useAuth();

useEffect(() => {
const waitThenCheckUser = async () => {
await authenticate();

if (!user) {
return <Navigate to="/login" />;
}
};

waitThenCheckUser();
if (!user) {
authenticate();
}
}, [user, authenticate]);

return children;
};
if (!user) {
return <Navigate to="/login" />;;
}

ProtectedRoute.propTypes = {
children: PropTypes.element,
return children;
};
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./useAuthHook";
export * from "./useFeedback";
export * from "./useFeedbackHook";
export * from "./useLocalStorage";
export * from "./useUpdateLessonPlan";
9 changes: 5 additions & 4 deletions src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useAuthHook } from "./useAuthHook";
import { createContext, useContext, useMemo } from "react";
import { UserData } from "../interfaces";

const AuthContext = createContext({
user: null,
signIn: () => {},
signOut: () => {},
register: () => {},
authenticate: () => {},
register: async (userData: UserData): Promise<void> => {},
authenticate: async (userData: UserData): Promise<void> => {},
signIn: async (userData: UserData): Promise<void> => {},
signOut: async (): Promise<void> => {},
});

export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
Expand Down
26 changes: 16 additions & 10 deletions src/hooks/useAuthHook.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useLocalStorage } from "./useLocalStorage";
import UserService from "../service/UserService";
import { UserService } from "../services";
import { UserData, Response } from "../interfaces";
import { useNavigate } from "react-router-dom";
import { AxiosResponse } from "axios";

export const useAuthHook = () => {
const [user, setUser] = useLocalStorage("user", "");
Expand All @@ -9,12 +11,12 @@ export const useAuthHook = () => {

const api = new UserService(VITE_API_ENDPOINT);

const authenticate = async () => {
const authenticate = async (): Promise<AxiosResponse | Response | void> => {
try {
const response = await api.authenticate();

if (response.status >= 300) {
setUser("");
setUser(null);
}

return response;
Expand All @@ -23,36 +25,40 @@ export const useAuthHook = () => {
}
};

const signIn = async (userData) => {
const signIn = async (
userData: UserData
): Promise<AxiosResponse | Response | void> => {
try {
const response = await api.login(userData);

if (response.status === 200 && response.data.user._id) {
if (response.status < 300) {
setTimeout(() => {
setUser(response.data.user);
}, 2000);
}

return response;
} catch (error) {
} catch (error: any) {
console.log(error.message);
}
};

const signOut = async () => {
const signOut = async (): Promise<AxiosResponse | Response | void> => {
try {
const response = await api.logout();

setUser(null);
navigate("/");

return response;
} catch (error) {
} catch (error: any) {
console.log(error.message);
}
};

const register = async (userData) => {
const register = async (
userData: UserData
): Promise<AxiosResponse | Response | void> => {
try {
const response = await api.register(userData);

Expand All @@ -63,7 +69,7 @@ export const useAuthHook = () => {
}

return response;
} catch (error) {
} catch (error: any) {
console.log(error.message);
}
};
Expand Down
36 changes: 15 additions & 21 deletions src/pages/LessonPlanCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import { useAuth } from "../hooks/useAuth";
import { useFeedback } from "../hooks/useFeedback";
import { useUpdateLessonPlan } from "../hooks/useUpdateLessonPlan";
import LessonPlanService from "../service/LessonPlanService";
import { useUpdateLessonPlan, useFeedback, useAuth } from "../hooks";
import { LessonPlanService } from "../services";
import lessonPlanTemplate from "../../data/lessonPlanTemplate.json";
// import { Error } from "./Error";

import { useState, useEffect, useRef } from "react";

import PropTypes from "prop-types";

import { Link, useNavigate } from "react-router-dom";
import { useParams } from "react-router-dom";

import Container from "react-bootstrap/Container";
import Col from "react-bootstrap/Col";
import Row from "react-bootstrap/Row";
import Image from "react-bootstrap/Image";
import Button from "react-bootstrap/Button";
import Nav from "react-bootstrap/Nav";
import Tab from "react-bootstrap/Tab";
import Card from "react-bootstrap/Card";
import Form from "react-bootstrap/Form";
import InputGroup from "react-bootstrap/InputGroup";
import Spinner from "react-bootstrap/Spinner";

import {
Container,
Col,
Row,
Image,
Button,
Nav,
Tab,
Card,
Form,
InputGroup,
Spinner,
} from "react-bootstrap";
import { motion } from "framer-motion";

export const LessonPlanCreate = () => {
Expand Down
11 changes: 5 additions & 6 deletions src/pages/LessonPlanEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useAuth } from "../hooks/useAuth";
import { useUpdateLessonPlan } from "../hooks/useUpdateLessonPlan";
import LessonPlanService from "../service/LessonPlanService";
import { useAuth, useUpdateLessonPlan } from "../hooks";
import { LessonPlanService } from "../services";
import lessonPlanTemplate from "../../data/lessonPlanTemplate.json";
import { fetchOneLessonPlan } from "../helper/fetchHelper";
import { TransitionDecorator } from "../components/decorators/TransitionDecorator";
import { fetchOneLessonPlan } from "../helpers";
import { TransitionDecorator } from "../components/decorators";
import { useState, useEffect, useRef } from "react";
import { Link, useNavigate } from "react-router-dom";
import { useParams } from "react-router-dom";
Expand Down Expand Up @@ -31,7 +30,7 @@ export const LessonPlanEdit = () => {
useUpdateLessonPlan(lessonPlanTemplate);

useEffect(() => {
if (!lessonApiRef.current) {
if (!lessonApiRef.current && userId) {
lessonApiRef.current = new LessonPlanService(
import.meta.env.VITE_API_ENDPOINT,
userId
Expand Down
10 changes: 5 additions & 5 deletions src/pages/LessonPlanView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import lessonPlanTemplate from "../../data/lessonPlanTemplate";
import LessonPlanService from "../service/LessonPlanService";
import { fetchOneLessonPlan } from "../helper/fetchHelper";
import { useAuth } from "../hooks/useAuth";
import { ConfirmDeleteModal } from "../components/ConfirmDeleteModal";
import { LessonPlanService } from "../services";
import { fetchOneLessonPlan } from "../helpers";
import { useAuth } from "../hooks";
import { ConfirmDeleteModal } from "../components";
import { TransitionDecorator } from "../components/decorators";
import { useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Link, useParams, useNavigate } from "react-router-dom";
Expand All @@ -17,7 +18,6 @@ import {
Card,
Spinner,
} from "react-bootstrap";
import { TransitionDecorator } from "../components/decorators/TransitionDecorator";

export const LessonPlanView = () => {
const [lessonPlan, setLessonPlan] = useState(lessonPlanTemplate);
Expand Down
8 changes: 4 additions & 4 deletions src/pages/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import LessonPlanService from "../service/LessonPlanService";
import { useAuth } from "../hooks/useAuth";
import { fetchLessonPlans } from "../helper/fetchHelper";
import { TransitionDecorator } from "../components/decorators/TransitionDecorator";
import { LessonPlanService } from "../services";
import { useAuth } from "../hooks";
import { fetchLessonPlans } from "../helpers";
import { TransitionDecorator } from "../components/decorators"
import { useEffect, useState, useRef } from "react";
import { Link, useParams } from "react-router-dom";
import { Container, Row, Col, Table, Button, Image } from "react-bootstrap";
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
InputGroup,
Spinner,
} from "react-bootstrap";
import { FeedbackItem, RegisterResponse } from "../interfaces";
import { FeedbackItem, Response } from "../interfaces";

export const Register = () => {
const { triggerFeedback } = useFeedback();
Expand Down Expand Up @@ -75,7 +75,7 @@ export const Register = () => {
}
} else {
try {
const response: RegisterResponse = await register(formState);
const response = await register(formState);

reportedFeedback.push({
title:
Expand Down
2 changes: 1 addition & 1 deletion src/pages/SignIn.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleChangeHelper } from "../helper/handleHelper";
import { handleChangeHelper } from "../helpers";
import { useAuth, useFeedback } from "../hooks";
import { LoadingWithSpinner, PrimaryButton } from "../components";
import {
Expand Down

0 comments on commit 040b2ea

Please sign in to comment.