From fc8682b132e7e9bf81c0bc52bdc70a5b9a2bc26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=AD=D8=B3=DB=8C=D9=86=20=D9=85=D8=B9=D8=B5=D9=88=D9=85?= =?UTF-8?q?=DB=8C=20=D9=BE=D9=88=D8=B1?= Date: Wed, 13 Dec 2023 12:02:50 +0330 Subject: [PATCH] first use --- components/AppHeader/page.jsx | 35 ++++- .../BottomSheetAddUserToPositionShiftPlan.jsx | 81 +---------- plugins/Chapar/index.js | 10 +- src/app/acount/page.jsx | 5 +- src/app/home/page.jsx | 2 + src/app/layout.jsx | 64 ++++++++- src/app/shifts/manage-shift/page.jsx | 131 +++++++++++++----- 7 files changed, 207 insertions(+), 121 deletions(-) diff --git a/components/AppHeader/page.jsx b/components/AppHeader/page.jsx index 09c5e26..e2f3ff9 100644 --- a/components/AppHeader/page.jsx +++ b/components/AppHeader/page.jsx @@ -2,6 +2,8 @@ import Avatar from "boring-avatars"; import Link from "next/link"; import { useRouter } from "next/navigation"; import React from "react"; +import { useLongPress } from "@uidotdev/usehooks"; +import { toast } from "react-toastify"; const AppHeader = ({ title, @@ -20,7 +22,24 @@ const AppHeader = ({ userIconData, userIconHref, userRole, + logOut, }) => { + const router = useRouter(); + + const handlelogOut = () => { + localStorage.removeItem("token"); + router.push("/login"); + }; + + const attrs = useLongPress(() => handlelogOut(), { + // onStart: (event) => setSubButtonAction(true), + onFinish: (event) => { + handlelogOut(); + }, + // onCancel: (event) => setSubButtonAction(false), + threshold: 1000, + }); + const icons = [ { iconName: "ACOUNT", @@ -124,6 +143,20 @@ const AppHeader = ({ {!userRole ? (
+ {logOut && ( +
{ + toast.error(`برای خروج نگه دارید`, { + position: "bottom-right", + closeOnClick: true, + }); + }} + > +

خروج

+
+ )} {icon1 ? (
) : (

{iconName3}

diff --git a/plugins/BottomSheet/BottomSheetAddUserToPositionShiftPlan.jsx b/plugins/BottomSheet/BottomSheetAddUserToPositionShiftPlan.jsx index 20c3559..7f21789 100644 --- a/plugins/BottomSheet/BottomSheetAddUserToPositionShiftPlan.jsx +++ b/plugins/BottomSheet/BottomSheetAddUserToPositionShiftPlan.jsx @@ -14,13 +14,12 @@ const BottomSheetAddUserToPositionShiftPlan = ({ userAndPositionIds, positionId, positionName, + usersData, }) => { const CTX = useContext(AppContext); const [, forceUpdate] = useState(); - const usersData = CTX.state.usersData; - const validator = useRef( new SimpleReactValidator({ messages: { @@ -36,76 +35,8 @@ const BottomSheetAddUserToPositionShiftPlan = ({ }) ); - // const body = { - // persianName, - // englishName, - // description, - // permissions: permissionsChoose, - // }; - // const bodyUpdate = { - // persianName, - // englishName, - // description, - // permissions: permissionsChoose, - // roleId: roleData.roleId, - // }; - - // const clear = () => { - // setPersianName(""); - // setEnglishName(""); - // setDescription(""); - // CTX.setPermissionsChoose([]); - // }; - - // const handleCreateRole = (update) => { - // if (validator.current.allValid()) { - // if (update == "UPDATE") { - // CTX.UpdateRole(bodyUpdate); - // } else { - // CTX.CreateRole(body); - // } - // } else { - // toast.error("پرکردن همه ی فیلد ها واجب است", { - // position: "bottom-right", - // autoClose: 2000, - // hideProgressBar: false, - // closeOnClick: true, - // pauseOnHover: true, - // draggable: true, - // progress: undefined, - // }); - - // validator.current.showMessages(); - // forceUpdate(1); - // } - // }; - - const handleBottomSheetAddUserToPositionShiftPlan = (e) => { - if (e.type == "OPEN") { - if (!!usersData.length <= 0) { - // User data is not in state, send request to get user - CTX.GetUsers(); - } - } else if (e.type == "CLOSE") { - // clear(); - // CTX.setGoToEditRole(false); - // CTX.setIdEditRole(null); - // CTX.setRoleData([]); - } - }; - - // useEffect(() => { - // if (goToEditRole) { - // setPersianName(roleData.persianName); - // setEnglishName(roleData.englishName); - // setDescription(roleData.description); - // CTX.setPermissionsChoose(roleData.permissions); - // } - // }, [roleData]); - return ( handleBottomSheetAddUserToPositionShiftPlan(e)} open={CTX.state.BottomSheetAddUserToPositionShiftPlanOpen} onDismiss={() => CTX.setBottomSheetAddUserToPositionShiftPlanOpen(false)} blocking={false} @@ -119,28 +50,28 @@ const BottomSheetAddUserToPositionShiftPlan = ({ {usersData?.map((e) => (
item.value === e.userId && item.key == positionId ) ? "bg-primary-100" : "bg-gray-100" }`} onClick={() => { - const userExistsIndex = userAndPositionIds.findIndex( + const userExistsIndex = userAndPositionIds?.findIndex( (item) => item.value === e.userId && item.key === positionId ); if (userExistsIndex !== -1) { setUserAndPositionIds((current) => [ - ...current.slice(0, userExistsIndex), - ...current.slice(userExistsIndex + 1), + ...(current ? current?.slice(0, userExistsIndex) : []), + ...(current ? current?.slice(userExistsIndex + 1) : []), ]); } else { setUserAndPositionIds((current) => [ ...current, { key: positionId, - value: e.userId, + value: e?.userId, }, ]); } diff --git a/plugins/Chapar/index.js b/plugins/Chapar/index.js index ace50e0..cc97fa0 100644 --- a/plugins/Chapar/index.js +++ b/plugins/Chapar/index.js @@ -29,12 +29,12 @@ Chapar.interceptors.response.use( function (error, status) { // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error - // ; - // if (error.response.status === 401) { - // localStorage.removeItem("token"); - // window.location.href = "/"; - // } + + if (error.response.status === 401) { + localStorage.removeItem("token"); + window.location.href = "/"; + } return Promise.reject({ error, status: error?.response?.status }); } diff --git a/src/app/acount/page.jsx b/src/app/acount/page.jsx index a44e65a..b2ee00f 100644 --- a/src/app/acount/page.jsx +++ b/src/app/acount/page.jsx @@ -3,14 +3,12 @@ import AppHeader from "@comp/AppHeader/page"; import Buttonbriz from "plugins/Buttonbriz/page"; import Input from "plugins/Input/page"; import React, { useContext, useEffect, useRef, useState } from "react"; -import person from "@img/person.png"; -import Image from "next/image"; import AppContext from "@ctx/AppContext"; import Avatar from "boring-avatars"; import { toast } from "react-toastify"; import SimpleReactValidator from "simple-react-validator"; import DatePickerIran from "plugins/DatePickerIran/page"; -import moment from "jalali-moment"; +import { useLongPress } from "@uidotdev/usehooks"; const page = () => { const CTX = useContext(AppContext); @@ -65,6 +63,7 @@ const page = () => { icon2={true} iconName2="ARROW" iconHref2="/home" + logOut={true} />
diff --git a/src/app/home/page.jsx b/src/app/home/page.jsx index 3fa7f05..7e58911 100644 --- a/src/app/home/page.jsx +++ b/src/app/home/page.jsx @@ -74,6 +74,8 @@ const Home = (props) => { // } // }, [profile]); + console.log(profile); + return (
{ + setLoading(true); + try { + const data = await Chapar.get( + `${process.env.NEXT_PUBLIC_API_URL}/shift/plan/${id}`, + { + headers: { + Authorization: getToken(), + }, + } + ); + setShiftPlanData(data); + setLoading(false); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + setLoading(false); + } + }; + const UpdateShiftPlan = async (body, id) => { + setLoading(true); + try { + const data = await Chapar.put( + `${process.env.NEXT_PUBLIC_API_URL}/shift/plan`, + body, + + { + headers: { + Authorization: getToken(), + }, + } + ); + toast.success(`شیفت ویرایش شد`, { + position: "bottom-right", + closeOnClick: true, + }); + + setLoading(false); + setShiftPlanData(id); + } catch ({ error, status }) { + toast.error(`${error?.response?.data?.message}`, { + position: "bottom-right", + closeOnClick: true, + }); + setLoading(false); + } + }; + const GetShift = async (id) => { setLoading(true); try { @@ -873,11 +924,11 @@ export default function RootLayout({ children }) { } }; - const GetRoutineShiftPlan = async (id) => { + const GetRoutineShiftPlan = async (id, time) => { setLoading(true); try { const data = await Chapar.get( - `${process.env.NEXT_PUBLIC_API_URL}/routine/${id}/shift`, + `${process.env.NEXT_PUBLIC_API_URL}/routine/${id}/shift?selectedDate=${time}`, { headers: { @@ -1373,8 +1424,9 @@ export default function RootLayout({ children }) { b:::::bbbbbb::::::br:::::r i::::::i z::::::zzzzzzzc:::::::cccccc:::::o:::::ooooo:::::o b::::::::::::::::b r:::::r i::::::iz::::::::::::::zc:::::::::::::::::o:::::::::::::::o b:::::::::::::::b r:::::r i::::::z:::::::::::::::z cc:::::::::::::::coo:::::::::::oo - bbbbbbbbbbbbbbbb rrrrrrr iiiiiiizzzzzzzzzzzzzzzzz cccccccccccccccc ooooooooooo - soli chizi bood bgo hossein__masoomi + bbbbbbbbbbbbbbbb rrrrrrr iiiiiiizzzzzzzzzzzzzzzzz cccccccccccccccc ooooooooooo + + soli chizi bood bgo hossein__masoomi `); @@ -1448,6 +1500,7 @@ export default function RootLayout({ children }) { BottomSheetChangeRoleOpen, activitiesData, completeActivities, + shiftPlanData, }, setBottomSheetCreateRoleOpen, setBottomSheetCreateEmployeesOpen, @@ -1556,6 +1609,9 @@ export default function RootLayout({ children }) { changeRole, setCompleteActivities, CloseShift, + setShiftPlanData, + GetShiftPlan, + UpdateShiftPlan, }} > diff --git a/src/app/shifts/manage-shift/page.jsx b/src/app/shifts/manage-shift/page.jsx index 0860d9a..83937c0 100644 --- a/src/app/shifts/manage-shift/page.jsx +++ b/src/app/shifts/manage-shift/page.jsx @@ -23,6 +23,7 @@ const Calendar = () => { const routinesData = CTX.state.routinesData; const routineShiftPlan = CTX.state.routineShiftPlan; const positionsData = CTX.state.positionsData; + const shiftPlanData = CTX.state.shiftPlanData; const [daysUntilWeek, setDaysUntilWeek] = useState([]); const [selectDay, setSelectDay] = useState(null); @@ -30,6 +31,7 @@ const Calendar = () => { const [selectRoutine, setSelectRoutine] = useState(-1); const [shiftsPlan, setShiftsPlan] = useState([]); const [manageShiftEmployeesData, setManageShiftEmployeesData] = useState([]); + const [editManageShift, setEditManageShift] = useState(false); const [shiftPlanSteps, setShiftPlanSteps] = useState(0); @@ -38,6 +40,8 @@ const Calendar = () => { const [positionSelectIdBottomSheet, setPositionSelectIdBottomSheet] = useState([]); const [userAndPositionIds, setUserAndPositionIds] = useState([]); + const [idRoutineShiftPlan, setIdRoutineShiftPlan] = useState(null); + const [currentShiftPlanId, setCurrentShiftPlanId] = useState(null); const week = [ { key: "شنبه", value: 6 }, @@ -55,26 +59,31 @@ const Calendar = () => { .jDate(); const daysEndOfMonth = () => { - const daysOfWeek = []; - const startOfNextWeek = moment(today, "jYYYY/jM/jD") - .startOf("jWeek") - .add(7, "days"); - // Iterate from شنبه (Saturday) to جمعه (Friday) and add each day to the array - for (let i = 0; i < 7; i++) { - const currentDay = startOfNextWeek.clone().add(i, "days"); - const isToday = currentDay.isSame(today, "day"); - const dayOfWeekName = week[i].key; // Get the day name from the week array + const today = moment(); + const lastDayOfNextWeek = today.clone().add(1, "week").endOf("week"); - daysOfWeek.push({ - checkDay: currentDay.format("jYYYY/jM/jD"), - date: currentDay.format("jD / jM"), - dayOfWeek: dayOfWeekName, - today: isToday, - value: week[i].value, + const daysArray = []; + let currentDate = today.clone(); + + while (currentDate.isBefore(lastDayOfNextWeek, "day")) { + const shamsiDate = currentDate.format("jYYYY/jM/jD"); + const dayOfWeek = week.find((day) => day.value === currentDate.day()).key; + const isToday = currentDate.isSame(moment(), "day"); + const value = currentDate.day(); + + daysArray.push({ + checkDay: shamsiDate, + date: currentDate.format("jD / jM"), + dayOfWeek, + isToday, + value, }); - } - return setDaysUntilWeek(daysOfWeek); + currentDate.add(1, "day"); + } + console.log(daysArray); + + return setDaysUntilWeek(daysArray); }; const handleManageShiftEmployeesOpen = (e) => { @@ -103,17 +112,40 @@ const Calendar = () => { userAndPositionIds, }; + const bodyUpdate = { + planDate: + daysUntilWeek[selectDay]?.checkDay && + moment.utc(daysUntilWeek[selectDay].checkDay, "jYYYY-jMM-jDDTHH").unix() * + 1000, + shiftId: shiftsPlan && shiftsPlan[selectShift]?.id, + routineId: routinesData && routinesData[selectRoutine]?.id, + userAndPositionIds, + id: currentShiftPlanId, + }; + const handleRoutineShiftPlan = (index, id) => { setSelectRoutine(index); - CTX.GetRoutineShiftPlan(id); setShiftPlanSteps(1); + setIdRoutineShiftPlan(id); + }; + + const handleRoutineShiftPlanWithDay = (index) => { + setSelectDay(index); + setShiftPlanSteps(2); + + CTX.GetRoutineShiftPlan( + idRoutineShiftPlan, + moment(daysUntilWeek[index]?.checkDay, "jYYYY-jM-jD").unix() * 1000 + ); }; const handleCreateShiftPlan = (update) => { if (update == "UPDATE") { - // CTX.UpdateShiftPlan(bodyUpdate); + CTX.UpdateShiftPlan(bodyUpdate); + setShiftPlanSteps(1); } else { CTX.CreateShifPlan(body); + setShiftPlanSteps(1); } }; @@ -136,7 +168,33 @@ const Calendar = () => { ); setselectShift(-1); - }, [selectDay]); + }, [routineShiftPlan]); + + useEffect(() => { + console.log("11111", shiftsPlan && shiftsPlan[selectShift]); + setEditManageShift(false); + CTX.setShiftPlanData(null); + if (shiftsPlan && shiftsPlan[selectShift]?.hasCurrentShiftPlan) { + setEditManageShift(true); + setCurrentShiftPlanId(shiftsPlan[selectShift]?.currentShiftPlanId); + CTX.GetShiftPlan(shiftsPlan[selectShift]?.currentShiftPlanId); + } + + if (!!usersData.length <= 0) { + // User data is not in state, send request to get user + CTX.GetUsers(); + } + }, [selectShift]); + + useEffect(() => { + console.log( + "shiftPlanDataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + shiftPlanData + ); + setUserAndPositionIds( + shiftPlanData?.users?.map((e) => ({ key: e.positionId, value: e.userId })) + ); + }, [shiftPlanData]); return (
@@ -209,10 +267,7 @@ const Calendar = () => { : " opacity-70" }`} key={index} - onClick={() => { - setSelectDay(index); - setShiftPlanSteps(2); - }} + onClick={() => handleRoutineShiftPlanWithDay(index)} >

@@ -271,9 +326,9 @@ const Calendar = () => {

{ setselectShift(index); setShiftPlanSteps(3); @@ -416,12 +471,21 @@ const Calendar = () => {
))}
- handleCreateShiftPlan()} - /> + {editManageShift ? ( + handleCreateShiftPlan("UPDATE")} + /> + ) : ( + handleCreateShiftPlan()} + /> + )}
)} @@ -431,6 +495,7 @@ const Calendar = () => { positionId={positionSelectIdBottomSheet} userAndPositionIds={userAndPositionIds} setUserAndPositionIds={setUserAndPositionIds} + usersData={usersData} />
);