web/plugins/BottomSheet/BottomSheetCreateSection.jsx

181 lines
4.9 KiB
React
Raw Permalink Normal View History

2023-11-14 16:22:32 +03:30
"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";
2024-11-02 04:48:38 +03:30
import { useLocale, useTranslations } from "next-intl";
2023-11-14 16:22:32 +03:30
const BottomSheetCreateSection = (props) => {
const CTX = useContext(AppContext);
const [title, setTitle] = useState("");
const [description, setDescription] = useState("");
2024-11-02 04:48:38 +03:30
const t = useTranslations("BottomSheet");
const locale = useLocale();
const isRTL = locale === "fa";
2023-11-14 16:22:32 +03:30
const [, forceUpdate] = useState();
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 goToEditSection = CTX.state.goToEditSection;
const idEditSection = CTX.state.idEditSection;
const sectionData = CTX.state.sectionData;
const body = {
description,
title,
};
const bodyUpdate = {
description,
title,
id: idEditSection,
};
const clear = () => {
setTitle("");
setDescription("");
};
const handleCreateSection = (update) => {
if (validator.current.allValid()) {
if (update == "UPDATE") {
CTX.UpdateSection(bodyUpdate);
} else {
CTX.CreateSection(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 handleBottomSheetCreateSectionOpen = (e) => {
if (e.type == "OPEN") {
if (goToEditSection) {
CTX.GetSection(idEditSection);
}
} else if (e.type == "CLOSE") {
clear();
CTX.setGoToEditSection(false);
CTX.setIdEditSection(null);
CTX.setSectionData([]);
}
};
useEffect(() => {
if (goToEditSection) {
setTitle(sectionData.name);
setDescription(sectionData.description);
}
}, [sectionData]);
return (
<BottomSheet
onSpringStart={(e) => handleBottomSheetCreateSectionOpen(e)}
open={CTX.state.BottomSheetCreateSectionOpen}
onDismiss={() => CTX.setBottomSheetCreateSectionOpen(false)}
blocking={true}
2023-11-14 16:22:32 +03:30
>
<div className="text-center py-2 bg-secondary-950 ">
<p className="mb-0 text-primary-300 relative top-[-5px]">
2024-11-02 04:48:38 +03:30
{t("BottomSheetCreateSectionTitle")}{" "}
</p>
2023-11-14 16:22:32 +03:30
</div>
<div className="bg-body-100 p-4 ">
<div className="">
<Input
2024-11-02 04:48:38 +03:30
lable={t("BottomSheetCreateSectionNameInput")}
2023-11-14 16:22:32 +03:30
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
2024-11-02 04:48:38 +03:30
lable={t("BottomSheetCreateSectionDescInput")}
2023-11-14 16:22:32 +03:30
id="description-id"
name="description"
type={"text"}
value={description}
inputEvent={(e) => {
setDescription(e.target.value);
// validator.current.showMessageFor("description");
2023-11-14 16:22:32 +03:30
}}
textarea={true}
style="text-right"
// validator={true}
// validatorData={validator.current.message(
// "description",
// description,
// "required"
// )}
2023-11-14 16:22:32 +03:30
/>
</div>
{goToEditSection ? (
<Buttonbriz
2024-11-02 04:48:38 +03:30
title={t("BottomSheetCreateSectionEditButton")}
2023-11-14 16:22:32 +03:30
color="INFO"
icon="CHECK"
buttonEvent={() => handleCreateSection("UPDATE")}
subButton={true}
2024-11-02 04:48:38 +03:30
subButtonTitle={t("BottomSheetCreateSectionEditButton")}
2023-11-14 16:22:32 +03:30
subButtonEvent={() => CTX.DeleteSection(idEditSection)}
/>
) : (
<Buttonbriz
2024-11-02 04:48:38 +03:30
title={t("BottomSheetCreateSectionRegButton")}
2023-11-14 16:22:32 +03:30
color="PRIMARY"
icon="CHECK"
buttonEvent={() => handleCreateSection()}
/>
)}
</div>
</BottomSheet>
);
};
export default BottomSheetCreateSection;