web/plugins/BottomSheet/BottomSheetCreateShifts.jsx

239 lines
6.7 KiB
React
Raw Normal View History

2023-10-19 11:44:56 +03:30
"use client";
2023-10-26 00:26:44 +03:30
import React, { useContext, useEffect, useRef, useState } from "react";
2023-10-19 11:44:56 +03:30
import { BottomSheet } from "react-spring-bottom-sheet";
import Input from "plugins/Input/page";
import AppContext from "@ctx/AppContext";
2023-10-26 00:26:44 +03:30
import SimpleReactValidator from "simple-react-validator";
import { toast } from "react-toastify";
import Buttonbriz from "plugins/Buttonbriz/page";
2023-10-19 11:44:56 +03:30
const BottomSheetCreateShifts = (props) => {
const CTX = useContext(AppContext);
2023-10-26 00:26:44 +03:30
const [title, setTitle] = useState("");
const [description, setDescription] = useState("");
const [dayOfWeeksCurrent, setDayOfWeeksCurrent] = useState("");
const [, forceUpdate] = useState();
const startAtTimeShift = CTX.state.startAtTimeShift;
const endAtTimeShift = CTX.state.endAtTimeShift;
const goToEditShift = CTX.state.goToEditShift;
const idEditShift = CTX.state.idEditShift;
const shiftData = CTX.state.shiftData;
const validator = useRef(
new SimpleReactValidator({
messages: {
required: "پر کردن این فیلد الزامی میباشد",
},
element: (message) => (
<>
<div className="text-right px-1 ">
<small className="text-red-600 t-ig-small ">{message}</small>
</div>
</>
),
})
);
const clear = () => {
setDayOfWeeksCurrent("");
setDescription("");
setTitle("");
// CTX.setEndAtTimeShift(["00", "00"]);
// CTX.setStartAtTimeShift(["00", "00"]);
};
const body = {
title,
description,
endAt: endAtTimeShift[0] + ":" + endAtTimeShift[1] + ":00",
startAt: startAtTimeShift[0] + ":" + startAtTimeShift[1] + ":00",
};
const bodyUpdate = {
title,
description,
endAt: endAtTimeShift[0] + ":" + endAtTimeShift[1] + ":00",
startAt: startAtTimeShift[0] + ":" + startAtTimeShift[1] + ":00",
id: shiftData.id,
};
const handleCreateShift = (update) => {
if (validator.current.allValid()) {
if (update == "UPDATE") {
CTX.UpdateShift(bodyUpdate);
} else {
CTX.CreateShift(body);
}
} else {
toast.error("پرکردن همه ی فیلد ها واجب است", {
position: "bottom-right",
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
validator.current.showMessages();
forceUpdate(1);
}
};
console.log(body);
const handleBottomSheetCreateShift = (e) => {
if (e.type == "OPEN") {
if (goToEditShift) {
CTX.GetShift(idEditShift);
}
} else if (e.type == "CLOSE") {
CTX.setGoToEditShift(false);
CTX.setIdEditShift(null);
CTX.setShiftData([]);
// clear();
}
};
useEffect(() => {
if (goToEditShift) {
setTitle(shiftData.title);
setDescription(shiftData.description);
CTX.setStartAtTimeShift([
shiftData.startAt.split(":")[0],
shiftData.startAt.split(":")[1],
]);
CTX.setEndAtTimeShift([
shiftData.endAt.split(":")[0],
shiftData.endAt.split(":")[1],
]);
}
}, [shiftData]);
2023-10-19 11:44:56 +03:30
return (
<BottomSheet
2023-10-26 00:26:44 +03:30
onSpringStart={(e) => handleBottomSheetCreateShift(e)}
2023-10-19 11:44:56 +03:30
open={CTX.state.BottomSheetCreateShiftsOpen}
onDismiss={() => CTX.setBottomSheetCreateShiftsOpen(false)}
2023-10-26 00:26:44 +03:30
blocking={false}
2023-10-19 11:44:56 +03:30
>
<div className="text-center py-2 bg-primary-300 ">
<p className="mb-0 text-white relative top-[-5px]">
افزودن شیفت کاری جدید
</p>
</div>
<div className="bg-body-100 p-4 ">
<div className="">
<Input
2023-10-26 00:26:44 +03:30
lable="نام شیفت مورد نظر"
id="title-id"
name="title"
2023-10-19 11:44:56 +03:30
type={"text"}
2023-10-26 00:26:44 +03:30
value={title}
inputEvent={(e) => {
setTitle(e.target.value);
validator.current.showMessageFor("title");
}}
style="text-right"
validator={true}
validatorData={validator.current.message(
"title",
title,
"required"
)}
2023-10-19 11:44:56 +03:30
/>
</div>
<div className="">
<Input
2023-10-26 00:26:44 +03:30
lable=" ساعت شروع شیفت"
id="startAtTimeShift-id"
name="startAtTimeShift"
2023-10-19 11:44:56 +03:30
type={"text"}
2023-10-26 00:26:44 +03:30
value={startAtTimeShift}
inputEvent={(e) => {
validator.current.showMessageFor("startAt");
}}
inputFocus={() => {
CTX.setOpenTimePicker(true);
CTX.setBottomSheetCreateShiftsOpen(false);
CTX.setTimePickerOrder("CREATE-SHIFT-START");
}}
style="text-right"
validator={true}
validatorData={validator.current.message(
"startAtTimeShift",
startAtTimeShift,
"required"
)}
2023-10-19 11:44:56 +03:30
/>
</div>
<div className="">
<Input
2023-10-26 00:26:44 +03:30
lable=" ساعت پایان شیفت"
id="endAtTimeShift-id"
name="endAtTimeShift"
2023-10-19 11:44:56 +03:30
type={"text"}
2023-10-26 00:26:44 +03:30
value={endAtTimeShift}
inputEvent={(e) => {
validator.current.showMessageFor("startAt");
}}
inputFocus={() => {
CTX.setOpenTimePicker(true);
CTX.setBottomSheetCreateShiftsOpen(false);
CTX.setTimePickerOrder("CREATE-SHIFT-END");
}}
style="text-right"
validator={true}
validatorData={validator.current.message(
"endAtTimeShift",
endAtTimeShift,
"required"
)}
2023-10-19 11:44:56 +03:30
/>
</div>
<div className="">
<Input
2023-10-26 00:26:44 +03:30
lable=" توضیحات"
id="description-id"
name="description"
2023-10-19 11:44:56 +03:30
type={"text"}
2023-10-26 00:26:44 +03:30
value={description}
inputEvent={(e) => {
setDescription(e.target.value);
validator.current.showMessageFor("description");
}}
2023-10-19 11:44:56 +03:30
textarea={true}
2023-10-26 00:26:44 +03:30
style="text-right"
2023-10-19 11:44:56 +03:30
/>
</div>
2023-10-26 00:26:44 +03:30
{goToEditShift ? (
<Buttonbriz
title="ویرایش شیفت"
color="INFO"
icon="CHECK"
buttonEvent={() => handleCreateShift("UPDATE")}
subButton={true}
subButtonTitle="حذف شیفت"
subButtonEvent={() => CTX.DeleteShift(idEditShift)}
/>
) : (
<Buttonbriz
title="ثبت شیفت"
color="PRIMARY"
icon="CHECK"
buttonEvent={() => handleCreateShift()}
/>
)}
2023-10-19 11:44:56 +03:30
</div>
</BottomSheet>
);
};
export default BottomSheetCreateShifts;