"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 BottomSheetCreateSection = (props) => {
const CTX = useContext(AppContext);
const [title, setTitle] = useState("");
const [description, setDescription] = useState("");
const t = useTranslations("BottomSheet");
const locale = useLocale();
const isRTL = locale === "fa";
const [, forceUpdate] = useState();
const validator = useRef(
new SimpleReactValidator({
messages: {
required: "پر کردن این فیلد الزامی میباشد",
},
element: (message) => (
<>
{message}
>
),
})
);
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 (
handleBottomSheetCreateSectionOpen(e)}
open={CTX.state.BottomSheetCreateSectionOpen}
onDismiss={() => CTX.setBottomSheetCreateSectionOpen(false)}
blocking={true}
>
{t("BottomSheetCreateSectionTitle")}{" "}
);
};
export default BottomSheetCreateSection;