add auth and base functions and fixed bug

This commit is contained in:
Amirhossein Mahmoodi
2024-07-09 11:28:59 +03:30
parent 6288ac926c
commit 863bdaca95
23 changed files with 592 additions and 69 deletions

View File

@@ -0,0 +1,191 @@
import { toast } from "react-toastify";
import { Box, Typography } from "@mui/material";
import { Dangerous } from "@mui/icons-material";
export const errorServerToast = (toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{"The request failed due to an internal error."}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: 5000,
hideProgressBar: true,
pauseOnHover: true,
closeOnClick: false,
draggable: true,
}
);
export const errorUnauthorizedToast = (toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{"The user is not authorized to make the request."}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: 3000,
hideProgressBar: true,
pauseOnHover: true,
closeOnClick: false,
draggable: true,
}
);
export const errorLogicToast = (message, toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{message ||
"The request was well-formed but was unable to be followed due to semantic errors."}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: false,
closeOnClick: false,
draggable: false,
}
);
export const errorValidationToast = (message, toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{message ||
"The request was well-formed but was unable to be followed due to semantic errors."}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: false,
closeOnClick: false,
draggable: false,
}
);
export const errorTooManyToast = (toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{"Too many requests have been sent within a given time span."}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: false,
closeOnClick: false,
draggable: false,
}
);
export const errorClientToast = (toastContainer) =>
toast.error(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Dangerous color="error" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{
"The API request is invalid or improperly formed. Consequently, the API server could not understand the request."
}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: false,
closeOnClick: false,
draggable: false,
}
);

View File

@@ -0,0 +1,35 @@
import { toast } from "react-toastify";
import { Box, Typography } from "@mui/material";
import { Beenhere } from "@mui/icons-material";
export const successToast = (toastContainer) =>
toast.success(
() => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
justifyContent: "start",
}}
>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Beenhere color="success" sx={{ mr: 1.6 }} />
<Box sx={{ display: "flex", flexDirection: "column" }}>
<Typography variant="caption">
{"Your operation was successful"}
</Typography>
</Box>
</Box>
</Box>
),
{
icon: false,
containerId: toastContainer,
autoClose: 3000,
hideProgressBar: true,
pauseOnHover: true,
closeOnClick: false,
draggable: true,
}
);

View File

@@ -0,0 +1,22 @@
'use client'
import { useAuth } from "@/lib/contexts/auth";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
function WithAuthMiddleware({ children }) {
const router = useRouter()
const pathName = usePathname()
const { isAuth, initAuthState } = useAuth();
useEffect(() => {
if (!initAuthState) return
if (!isAuth) {
router.replace(`${process.env.NEXT_PUBLIC_API_URL}/login?_back=${encodeURIComponent(pathName)}`)
}
}, [isAuth, initAuthState])
if (!initAuthState || !isAuth) return null;
return <>{children}</>
}
export default WithAuthMiddleware

View File

@@ -0,0 +1,61 @@
'use client'
import { toast } from "react-toastify";
import {
errorClientToast,
errorLogicToast,
errorServerToast,
errorTooManyToast,
errorUnauthorizedToast,
errorValidationToast
} from "@/core/components/toasts/error";
const isServerError = status => status >= 500 && status <= 599;
const isClientError = status => status >= 400 && status <= 499;
const errorServer = (response, notification, toastContainer) => {
if (notification) errorServerToast(toastContainer)
}
const errorClient = (response, notification, toastContainer) => {
switch (response.status) {
case 401:
if (notification) errorUnauthorizedToast(toastContainer)
break;
case 422:
if ('type' in response.data) {
if (Array.isArray(response.data.message)) {
response.data.message.map((item) => {
if (notification) errorLogicToast(item, toastContainer)
})
} else {
if (notification) errorLogicToast(response.data.message, toastContainer)
}
break;
}
if (notification) {
const errorsMap = Object.keys(response.data.errors)
const errorsArray = response.data.errors
errorsMap.map((item, index) => {
errorValidationToast(errorsArray[item][0], toastContainer);
})
}
break;
case 429:
if (notification) errorTooManyToast(toastContainer)
break
default:
if (notification) errorClientToast(toastContainer)
break
}
}
export const errorResponse = (response, notification, toastContainer) => {
if (notification) toast.dismiss({ container: toastContainer })
if (isServerError(response.status)) {
errorServer(response, notification, toastContainer)
} else if (isClientError(response.status)) {
errorClient(response, notification, toastContainer)
}
}

View File

@@ -12,7 +12,7 @@ export const pageMenu = [
id: 'userManagement',
label: 'مدیریت کاربران',
type: "page",
route: "./v2/user_management",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/user_management",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
@@ -26,35 +26,35 @@ export const pageMenu = [
id: 'projectsManagmentFinance',
label: 'ثبت قرارداد و پروژه',
type: "page",
route: "./v2/rahdari_projects/finance",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/rahdari_projects/finance",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'projectsManagmentList',
label: 'لیست پروژه ها',
type: "page",
route: "./v2/rahdari_projects/projects_list",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/rahdari_projects/projects_list",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'projectsManagmentProposal',
label: 'پروژه های پیشنهادی',
type: "page",
route: "./v2/rahdari_projects/proposal",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/rahdari_projects/proposal",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'projectsManagmentLawmaker',
label: 'درخواست نمایندگان',
type: "page",
route: "./v2/rahdari_projects/lawmakers",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/rahdari_projects/lawmakers",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'projectsManagmentMap',
label: 'پراکندگی بر روی نقشه',
type: "page",
route: "./v2/map?type=rahdari_projects",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=rahdari_projects",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -77,7 +77,7 @@ export const pageMenu = [
id: 'roadItemManagmentSupervisorCartable',
label: 'کارتابل',
type: "page",
route: "./v2/road_items/supervisor/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_items/supervisor/cartable",
},
]
},
@@ -92,13 +92,13 @@ export const pageMenu = [
id: 'roadItemManagmentOparationCreate',
label: 'ثبت فعالیت',
type: "page",
route: "./v2/road_items/operator/create",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_items/operator/create",
},
{
id: 'roadItemManagmentOparationCartable',
label: 'کارتابل',
type: "page",
route: "./v2/road_items/operator/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_items/operator/cartable",
},
]
},
@@ -106,14 +106,14 @@ export const pageMenu = [
id: 'roadItemManagmentMap',
label: 'پراکندگی بر روی نقشه',
type: "page",
route: "./v2/map?type=road_items",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=road_items",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'roadItemManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/road_items/report",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_items/report",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -136,7 +136,7 @@ export const pageMenu = [
id: 'roadPatrolManagmentSupervisorCartable',
label: 'کارتابل',
type: "page",
route: "./road_patrols/supervisor/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/road_patrols/supervisor/cartable",
},
]
},
@@ -151,13 +151,13 @@ export const pageMenu = [
id: 'roadPatrolManagmentOparationCreate',
label: 'ثبت اقدام',
type: "page",
route: "./v2/road_patrols/operator/create",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_patrols/operator/create",
},
{
id: 'roadPatrolManagmentOparationCartable',
label: 'کارتابل',
type: "page",
route: "./v2/road_patrols/operator/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_patrols/operator/cartable",
},
]
},
@@ -165,14 +165,14 @@ export const pageMenu = [
id: 'roadPatrolManagmentMap',
label: 'پراکندگی بر روی نقشه',
type: "page",
route: "./v2/map?type=road_patrols",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=road_patrols",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'roadPatrolManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/road_patrols/report",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_patrols/report",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -195,13 +195,13 @@ export const pageMenu = [
id: 'safetyAndPrivacyManagmentOparationCreate',
label: 'ثبت فعالیت',
type: "page",
route: "./safety_and_privacy/operator/first_step",
route: process.env.NEXT_PUBLIC_API_URL + "/safety_and_privacy/operator/first_step",
},
{
id: 'safetyAndPrivacyManagmentOparationCartable',
label: 'کارتابل',
type: "page",
route: "./v2/safety_and_privacy/operator/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/safety_and_privacy/operator/cartable",
},
]
},
@@ -209,14 +209,14 @@ export const pageMenu = [
id: 'safetyAndPrivacyManagmentMap',
label: 'پراکندگی بر روی نقشه',
type: "page",
route: "./v2/map?type=safety_and_privacy",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=safety_and_privacy",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'safetyAndPrivacyManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/safety_and_privacy/report",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/safety_and_privacy/report",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -239,7 +239,7 @@ export const pageMenu = [
id: 'roadObservationsManagmentSupervisorCartable',
label: 'کارتابل',
type: "page",
route: "./v2/road_observations/supervisor/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_observations/supervisor/cartable",
},
]
},
@@ -247,7 +247,7 @@ export const pageMenu = [
id: 'roadObservationsManagmentList',
label: 'رسیدگی به شکایات',
type: "page",
route: "./v2/road_observations",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_observations",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
@@ -261,7 +261,7 @@ export const pageMenu = [
id: 'roadObservationsManagmentOparationCartable',
label: 'کارتابل',
type: "page",
route: "./v2/road_observations/operator/cartable",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_observations/operator/cartable",
},
]
},
@@ -269,14 +269,14 @@ export const pageMenu = [
id: 'roadObservationsManagmentMap',
label: 'پراکندگی بر روی نقشه',
type: "page",
route: "./v2/map?type=road_observations",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=road_observations",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'roadObservationsManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/road_observations/report/index",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/road_observations/report/index",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -292,21 +292,21 @@ export const pageMenu = [
id: 'winterCampManagmentBlocking',
label: 'محور های انسدادی',
type: "page",
route: "./v2/winter_camp",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/winter_camp",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'winterCampManagmentFirstRing',
label: 'محور های حلقه اول',
type: "page",
route: "./v2/map?type=camp",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=camp",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
{
id: 'winterCampManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/map?type=campNew",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/map?type=campNew",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]
@@ -329,7 +329,7 @@ export const pageMenu = [
id: 'receiptManagmentOparationCartable',
label: 'کارتابل',
type: "page",
route: "./v2/receipt",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/receipt",
},
]
},
@@ -337,7 +337,7 @@ export const pageMenu = [
id: 'receiptManagmentReport',
label: 'گزارش ها',
type: "page",
route: "./v2/receipt/report",
route: process.env.NEXT_PUBLIC_API_URL + "/v2/receipt/report",
icon: <Security sx={{ width: 'inherit', height: 'inherit' }} />,
},
]

4
src/core/utils/routes.js Normal file
View File

@@ -0,0 +1,4 @@
const api = process.env.NEXT_PUBLIC_API_URL
export const GET_USER_ROUTE = api + '/webapi/user/get-permission'
export const GET_LOGIN_ROUTE = api + '/test_login'

View File

@@ -0,0 +1,10 @@
'use client'
import { toast } from "react-toastify";
import { successToast } from "@/core/components/toasts/success";
export const successRequest = (notification, toastContainer) => {
if (notification) {
toast.dismiss({ container: toastContainer })
successToast(toastContainer)
}
}