268 lines
7.2 KiB
JavaScript
268 lines
7.2 KiB
JavaScript
import React, { useContext, useEffect, useState } from "react";
|
|
import { OPACITY_ON_LOAD } from "../../../Layout/animations";
|
|
import { Box, Button, useToast } from "@chakra-ui/react";
|
|
import { useForm, Controller } from "react-hook-form";
|
|
import { yupResolver } from "@hookform/resolvers/yup";
|
|
import * as yup from "yup";
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import FormInputMain from "../../../Components/FormInputMain";
|
|
import {useCreateSponserMutation,useGetSponserByIdQuery,useUpdateSponserMutation,} from "../../../Services/sponser.service";
|
|
import ToastBox from "../../../Components/ToastBox";
|
|
import FullscreenLoaders from "../../../Components/Loaders/FullscreenLoaders";
|
|
import CustomAlertDialog from "../../../Components/CustomAlertDialog";
|
|
|
|
// ======================= [validation] =========================
|
|
|
|
export const addSponser = yup.object().shape({
|
|
sponsorName: yup.string().required("Sponser name is required"),
|
|
sponsorNameArabic: yup.string().required("Sponser name is required"),
|
|
email: yup.string().email("Must be a valid email").required("Email is required"),
|
|
});
|
|
|
|
// ==================== [debounce] ========================
|
|
|
|
export function debounce(func, delay) {
|
|
let debounceTimer;
|
|
return function (...args) {
|
|
clearTimeout(debounceTimer);
|
|
debounceTimer = setTimeout(() => func.apply(this, args), delay);
|
|
};
|
|
}
|
|
|
|
const AddSponser = () => {
|
|
|
|
const toast = useToast();
|
|
const params = useParams();
|
|
const navigate = useNavigate();
|
|
const id = params?.id;
|
|
|
|
// =====================[useState]=======================
|
|
|
|
const [isLoadingBtn, setIsLoadingBtn] = useState(false);
|
|
const [alert, setAlert] = useState(false);
|
|
const [form, setForm] = useState();
|
|
|
|
const [createSponser] = useCreateSponserMutation();
|
|
const [updateSponser] = useUpdateSponserMutation();
|
|
|
|
// Fetch sponsor data only if id exists
|
|
const {data: sponserByIdData,error,isLoading,} = useGetSponserByIdQuery(id, {skip: !id,});
|
|
|
|
// ======================== [validators] ===========================
|
|
|
|
const {
|
|
control,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
reset,
|
|
} = useForm({
|
|
resolver: yupResolver(addSponser),
|
|
});
|
|
|
|
// ========================== [useEffect] ================================
|
|
|
|
useEffect(() => {
|
|
if (sponserByIdData?.data) {
|
|
reset({
|
|
sponsorName: sponserByIdData?.data?.sponsorName,
|
|
sponsorNameArabic: sponserByIdData?.data?.sponsorNameArabic,
|
|
email: sponserByIdData?.data?.email,
|
|
});
|
|
}
|
|
}, [sponserByIdData, reset]);
|
|
|
|
if (false) {
|
|
return <FullscreenLoaders />;
|
|
}
|
|
|
|
// ============================ [API]===============================
|
|
|
|
const handleConfirm = async () => {
|
|
setIsLoadingBtn(true);
|
|
const id = params?.id;
|
|
if (id) {
|
|
try {
|
|
const formData = {
|
|
...form,
|
|
// isActive: isSwitchOn,
|
|
};
|
|
await updateSponser({ data: formData, id }).then((response) => {
|
|
if (response?.data?.statusCode) {
|
|
toast({
|
|
render: () => <ToastBox message={response?.data?.message} />,
|
|
});
|
|
|
|
setIsLoadingBtn(false);
|
|
setAlert(false);
|
|
navigate("/sponser");
|
|
} else {
|
|
toast({
|
|
render: () => (
|
|
<ToastBox message={"Something Went Wrong"} status={"error"} />
|
|
),
|
|
});
|
|
|
|
setIsLoadingBtn(false);
|
|
navigate("/sponser");
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
setIsLoadingBtn(false);
|
|
navigate("/sponser");
|
|
}
|
|
} else {
|
|
try {
|
|
const formData = {
|
|
...form,
|
|
// isActive: isSwitchOn,
|
|
};
|
|
await createSponser(formData).then((response) => {
|
|
if (response?.data?.statusCode) {
|
|
toast({
|
|
render: () => <ToastBox message={response?.data?.message} />,
|
|
});
|
|
|
|
setIsLoadingBtn(false);
|
|
navigate("/sponser");
|
|
} else {
|
|
toast({
|
|
render: () => (
|
|
<ToastBox message={"Something Went Wrong"} status={"error"} />
|
|
),
|
|
});
|
|
|
|
setIsLoadingBtn(false);
|
|
navigate("/sponser");
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
|
|
setIsLoadingBtn(false);
|
|
navigate("/sponser");
|
|
}
|
|
}
|
|
};
|
|
|
|
// ====================== [Update Form Object] =========================
|
|
|
|
const formFields = [
|
|
{
|
|
label: "Sponser name (English)",
|
|
placeHolder: " ",
|
|
name: "sponsorName",
|
|
type: "text",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
},
|
|
{
|
|
label: "Sponser name (Arabic)",
|
|
name: "sponsorNameArabic",
|
|
placeHolder: " ",
|
|
type: "text",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
isArabic: true,
|
|
right: true,
|
|
},
|
|
{
|
|
label: "Email address",
|
|
name: "email",
|
|
placeHolder: " ",
|
|
type: "email",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
},
|
|
];
|
|
|
|
// ==================== [Create Form Object] =======================
|
|
|
|
const formEditFields = [
|
|
{
|
|
label: "Sponser name",
|
|
placeHolder: " ",
|
|
name: "sponsorName",
|
|
type: "text",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
},
|
|
{
|
|
label: "Sponser name (Arabic)",
|
|
name: "sponsorNameArabic",
|
|
placeHolder: " ",
|
|
type: "text",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
isArabic: true,
|
|
},
|
|
{
|
|
label: "Email adress",
|
|
name: "email",
|
|
placeHolder: " ",
|
|
type: "email",
|
|
isRequired: true,
|
|
section: "Add Details",
|
|
},
|
|
];
|
|
|
|
// ====================== [Group Create Fields] =========================
|
|
|
|
const groupedEditFields = formEditFields.reduce((groups, field) => {
|
|
const { section } = field;
|
|
if (!groups[section]) {
|
|
groups[section] = [];
|
|
}
|
|
groups[section].push(field);
|
|
return groups;
|
|
}, {});
|
|
|
|
// ====================== [Group Update Fields] =======================
|
|
|
|
const groupedFields = formFields.reduce((groups, field) => {
|
|
const { section } = field;
|
|
if (!groups[section]) {
|
|
groups[section] = [];
|
|
}
|
|
groups[section].push(field);
|
|
return groups;
|
|
}, {});
|
|
|
|
// ==================== [On Submit] ========================
|
|
|
|
const onSubmit = async (data) => {
|
|
if (Object.keys(errors).length === 0) {
|
|
setForm(data);
|
|
setAlert(true);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Box {...OPACITY_ON_LOAD} overflowY={"scroll"} height={"100vh"} pb={14}>
|
|
|
|
{/* ====================== [Form Input] ====================== */}
|
|
|
|
<FormInputMain
|
|
groupedFields={params?.id ? groupedEditFields : groupedFields}
|
|
control={control}
|
|
errors={errors}
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
submitTitle={params?.id ? "Update" : "Submit"}
|
|
></FormInputMain>
|
|
|
|
{/* ======================= [Modal] =========================== */}
|
|
|
|
<CustomAlertDialog
|
|
isOpen={alert}
|
|
onClose={() => setAlert(false)}
|
|
alertHandler={handleConfirm}
|
|
message={"Are you sure you want to add this?"}
|
|
isLoading={isLoadingBtn}
|
|
/>
|
|
</Box>
|
|
);
|
|
};
|
|
|
|
export default AddSponser;
|