180 lines
4.9 KiB
JavaScript
180 lines
4.9 KiB
JavaScript
"use client";
|
||
|
||
import React, { useContext, useEffect, useRef, useState } from "react";
|
||
import { BottomSheet } from "react-spring-bottom-sheet";
|
||
import Input from "plugins/Input/page";
|
||
import AppContext from "@ctx/AppContext";
|
||
import SimpleReactValidator from "simple-react-validator";
|
||
import { toast } from "react-toastify";
|
||
import Buttonbriz from "plugins/Buttonbriz/page";
|
||
import { useLocale, useTranslations } from "next-intl";
|
||
|
||
const BottomSheetCreateRoutine = (props) => {
|
||
const CTX = useContext(AppContext);
|
||
const [title, setTitle] = useState("");
|
||
const [description, setDescription] = useState("");
|
||
|
||
const [, forceUpdate] = useState();
|
||
const t = useTranslations("BottomSheet");
|
||
const locale = useLocale();
|
||
const isRTL = locale === "fa";
|
||
|
||
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 goToEditRoutine = CTX.state.goToEditRoutine;
|
||
const idEditRoutine = CTX.state.idEditRoutine;
|
||
const routineData = CTX.state.routineData;
|
||
|
||
const body = {
|
||
description,
|
||
name: title,
|
||
};
|
||
|
||
const bodyUpdate = {
|
||
description,
|
||
name: title,
|
||
id: idEditRoutine,
|
||
};
|
||
|
||
const clear = () => {
|
||
setTitle("");
|
||
setDescription("");
|
||
};
|
||
|
||
const handleCreateRoutine = (update) => {
|
||
if (validator.current.allValid()) {
|
||
if (update == "UPDATE") {
|
||
CTX.UpdateRoutine(bodyUpdate);
|
||
} else {
|
||
CTX.CreateRoutine(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 handleBottomSheetCreateRoutineOpen = (e) => {
|
||
if (e.type == "OPEN") {
|
||
if (goToEditRoutine) {
|
||
CTX.GetRoutine(idEditRoutine);
|
||
}
|
||
} else if (e.type == "CLOSE") {
|
||
clear();
|
||
CTX.setGoToEditRoutine(false);
|
||
CTX.setIdEditRoutine(null);
|
||
CTX.setRoutineData([]);
|
||
}
|
||
};
|
||
|
||
useEffect(() => {
|
||
if (goToEditRoutine) {
|
||
setTitle(routineData.name);
|
||
setDescription(routineData.description);
|
||
}
|
||
}, [routineData]);
|
||
|
||
return (
|
||
<BottomSheet
|
||
onSpringStart={(e) => handleBottomSheetCreateRoutineOpen(e)}
|
||
open={CTX.state.BottomSheetCreateRoutineOpen}
|
||
onDismiss={() => CTX.setBottomSheetCreateRoutineOpen(false)}
|
||
blocking={true}
|
||
>
|
||
<div className="text-center py-2 bg-secondary-950 ">
|
||
<p className="mb-0 text-primary-300 relative top-[-5px]">
|
||
{t("BottomSheetCreateRoutineTitle")}{" "}
|
||
</p>
|
||
</div>
|
||
|
||
<div className="bg-body-100 p-4 ">
|
||
<div className="">
|
||
<Input
|
||
lable={t("BottomSheetCreateRoutineNameInput")}
|
||
id="title-id"
|
||
name="title"
|
||
type={"text"}
|
||
value={title}
|
||
inputEvent={(e) => {
|
||
setTitle(e.target.value);
|
||
validator.current.showMessageFor("title");
|
||
}}
|
||
style="text-right"
|
||
validator={true}
|
||
validatorData={validator.current.message(
|
||
"title",
|
||
title,
|
||
"required"
|
||
)}
|
||
/>
|
||
</div>
|
||
<div className="">
|
||
<Input
|
||
lable={t("BottomSheetCreateRoutineDescInput")}
|
||
id="description-id"
|
||
name="description"
|
||
type={"text"}
|
||
value={description}
|
||
inputEvent={(e) => {
|
||
setDescription(e.target.value);
|
||
// validator.current.showMessageFor("description");
|
||
}}
|
||
textarea={true}
|
||
style="text-right"
|
||
// validator={true}
|
||
// validatorData={validator.current.message(
|
||
// "description",
|
||
// description,
|
||
// "required"
|
||
// )}
|
||
/>
|
||
</div>
|
||
|
||
{goToEditRoutine ? (
|
||
<Buttonbriz
|
||
title={t("BottomSheetCreateRoutineEditButton")}
|
||
color="INFO"
|
||
icon="CHECK"
|
||
buttonEvent={() => handleCreateRoutine("UPDATE")}
|
||
subButton={true}
|
||
subButtonTitle={t("BottomSheetCreateRoutineDeleteButton")}
|
||
subButtonEvent={() => CTX.DeleteRoutine(idEditRoutine)}
|
||
/>
|
||
) : (
|
||
<Buttonbriz
|
||
title={t("BottomSheetCreateRoutineRegButton")}
|
||
color="PRIMARY"
|
||
icon="CHECK"
|
||
buttonEvent={() => handleCreateRoutine()}
|
||
/>
|
||
)}
|
||
</div>
|
||
</BottomSheet>
|
||
);
|
||
};
|
||
|
||
export default BottomSheetCreateRoutine;
|