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

Connect server error with catches #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/UserContext/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cookie from 'js-cookie';
import axios from 'axios';
import { userData } from '../interfaces/userData';
import userContext from './context';
import { ServerError } from '../pages/Errors';

const AuthContext = ({ children }: { children: any }) => {
const [user, setUserData] = useState<userData | null>(null);
Expand All @@ -13,7 +14,8 @@ const AuthContext = ({ children }: { children: any }) => {
.then((res) => {
setUserData(res.data.userData);
})
.catch(() => {
.catch((err) => {
if (err.response.state >= 500) ServerError();
Cookie.remove('token');
});
}, []);
Expand Down
2 changes: 2 additions & 0 deletions src/components/AddProject/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ErrorAlert, SuccessAlert } from '..';
import THEME from '../../theme';
import { addProjectSchema } from '../../helper/validation/schema';
import { Props2 } from '../../interfaces';
import { ServerError } from '../../pages/Errors';

const AddProjectModal = ({ open, handleClose }:Props2) => {
const [openError, setOpenError] = useState(false);
Expand All @@ -25,6 +26,7 @@ const AddProjectModal = ({ open, handleClose }:Props2) => {
setMessageSuccess(res.data.message);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(err.response.data.message);
}); handleClose();
Expand Down
2 changes: 2 additions & 0 deletions src/components/Login/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { validationSchema } from '../../../helper/validation/schema';
import { ErrorAlert, SuccessAlert } from '../..';
import userContext from '../../../UserContext/context';
import { ServerError } from '../../../pages/Errors';

interface LoginFormValues {
email: string;
Expand All @@ -41,6 +42,7 @@ const LoginForm = () => {
if (!openSuccess) navigator('/');
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
setOpenError(true);
if (err) setMessageError(err.response.data.message);
});
Expand Down
3 changes: 3 additions & 0 deletions src/components/ProjectsCard/ProjectCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { iProjects, iProjectTasks } from '../../interfaces';
import { ErrorAlert } from '..';
import Loader from './Loader';
import { ServerError } from '../../pages/Errors';

const ProjectsCard = () => {
const [userProjects, setUserProjects] = useState<iProjects[]>([]);
Expand All @@ -36,6 +37,7 @@ const ProjectsCard = () => {
setUserProjects(updatedProjects);
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand All @@ -56,6 +58,7 @@ const ProjectsCard = () => {
setIsLoading(false);
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
setIsLoading(false);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { iProjects } from '../../interfaces';
import { NAV_LIST } from '../../constants';
import { Logo, NavItem } from '../Common';
import UserCard from '../UserCard';
import { ServerError } from '../../pages/Errors';

const Sidebar = () => {
const navigate = useNavigate();
Expand All @@ -34,6 +35,7 @@ const Sidebar = () => {
setProjects(response.data.data);
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand All @@ -47,6 +49,7 @@ const Sidebar = () => {
navigate('/login', { state: { success: 'Logout Successfully' } });
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand Down
2 changes: 2 additions & 0 deletions src/components/Signup/SignupForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { signupSchema } from '../../../helper/validation/schema';
import { ErrorAlert, SuccessAlert } from '../..';
import userContext from '../../../UserContext/context';
import { ServerError } from '../../../pages/Errors';

interface SignupFormValues {
email: string;
Expand All @@ -38,6 +39,7 @@ const SignupForm = () => {
if (!openSuccess) navigator('/');
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(err.response.data.message);
});
Expand Down
2 changes: 2 additions & 0 deletions src/components/StatisticsCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Typography from '@mui/material/Typography';
import { StatisticsBox, StatisticsContent } from './statistics.styed';
import { task } from '../../interfaces';
import { ErrorAlert } from '..';
import { ServerError } from '../../pages/Errors';

const StatisticsCard = () => {
const [completedTasks, setCompletedTasks] = useState<task[]>([]);
Expand All @@ -19,6 +20,7 @@ const StatisticsCard = () => {
setCompletedTasks(filteredTasks);
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand Down
2 changes: 2 additions & 0 deletions src/components/TaskTable/TaskTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { task } from '../../interfaces';
import Theme from '../../theme';
import TaskRowSkeleton from './TaskRowSkeleton';
import empty from '../../lotties/empty.json';
import { ServerError } from '../../pages/Errors';

const TaskTable = () => {
const [tasks, setTasks] = useState<task[]>([]);
Expand Down Expand Up @@ -45,6 +46,7 @@ const TaskTable = () => {
setTasks(res.data.data);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
setLoading(false);
navigator('/', { state: { error: err.response.data.message } });
});
Expand Down
5 changes: 4 additions & 1 deletion src/components/UpdateTask/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { task, ISection } from '../../interfaces';
import {
TaskBox, Label, Date, Section, Textarea,
} from './updateTask.styled';
import { ServerError } from '../../pages/Errors';

interface IProps {
data: task, open: boolean, handleClose: () => void,
Expand Down Expand Up @@ -77,6 +78,7 @@ const EditTaskForm = ({ data, open, handleClose }:IProps) => {
setMessageSuccess(response.data.message);
})
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand All @@ -88,7 +90,8 @@ const EditTaskForm = ({ data, open, handleClose }:IProps) => {
.then((values) => {
setLoading(false);
setSections(values.data.data);
}).catch(() => {
}).catch((err) => {
if (err.response.state >= 500) ServerError();
setLoading(false);
});
}, []);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/AccountPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useNavigate } from 'react-router-dom';
import { AccountInput, ErrorAlert } from '../../components';
import { WrappBtn } from '../../components/AccountInput/acount.styled';
import userContext from '../../UserContext/context';
import { ServerError } from '../Errors';

const AccountPage = () => {
const navigator = useNavigate();
Expand All @@ -17,6 +18,7 @@ const AccountPage = () => {
.delete('/api/account')
.then(() => navigator('/login'))
.catch((error) => {
if (error.response.state >= 500) ServerError();
setOpenError(true);
setMessageError(error.response.data.message);
});
Expand Down
2 changes: 2 additions & 0 deletions src/pages/Calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import timeGridPlugin from '@fullcalendar/timegrid';
import { Box } from '@mui/material';
import axios from 'axios';
import { task } from '../../interfaces';
import { ServerError } from '../Errors';

const Calendar = () => {
const [tasks, setTasks] = useState<task[]>([]);
Expand All @@ -23,6 +24,7 @@ const Calendar = () => {
setTasks(res.data.data);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
navigator('/', { state: { error: err.response.data.message } });
});
}, [pathname]);
Expand Down
4 changes: 4 additions & 0 deletions src/pages/Errors/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ClientError from './clientError';
import ServerError from './serverError';

export { ClientError, ServerError };
4 changes: 4 additions & 0 deletions src/pages/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../interfaces';
import presenation from '../../lotties/presentation.json';
import AddMemberModal from '../../components/AddMember';
import { ServerError } from '../Errors';

const Overview = () => {
const [open, setOpen] = useState(false);
Expand All @@ -32,6 +33,7 @@ const Overview = () => {
setManager(res.data.manager);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
navigator('/', { state: { error: err.response.data.message } });
});

Expand All @@ -45,6 +47,7 @@ const Overview = () => {
setTasks(res.data.data);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
navigator('/', { state: { error: err.response.data.message } });
});

Expand All @@ -54,6 +57,7 @@ const Overview = () => {
setMembers(res.data.data);
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
navigator('/', { state: { error: err.response.data.message } });
});
}, [id, open]);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/TaskBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { task } from '../../interfaces/task';
import { ISection } from '../../interfaces';
import empty from '../../lotties/empty.json';
import TaskSkeleton from './TaskSkeleton';
import { ServerError } from '../Errors';

const TaskBoard = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -38,6 +39,7 @@ const TaskBoard = () => {
}
})
.catch((err) => {
if (err.response.state >= 500) ServerError();
setLoading(false);
navigate('/', { state: { error: err.response.data.message } });
});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ProjectsCards, TaskTable } from '../components';

import HomeLayout from '../layout';
import ClientError from '../pages/Errors/clientError';
import { ClientError } from '../pages/Errors';

const router = createBrowserRouter([
{
Expand Down