Files
SSA-Admin-Panel/src/Pages/SubAdmin/AddModel.tsx

234 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-06-06 20:10:40 +05:30
import { Button } from "../../components/ui/button";
import {
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTitle,
DialogTrigger,
} from "../../components/ui/dialog";
2025-06-18 15:34:32 +05:30
import { Field, Grid, Heading, Input, Stack, Text } from "@chakra-ui/react";
2025-06-06 20:10:40 +05:30
import { IoMdAdd } from "react-icons/io";
import { Checkbox } from "../../components/ui/checkbox";
import { useCreateSubAdminPostMutation } from "../../Redux/Service/manage.subadmin.service";
import { toaster } from "../../components/ui/toaster";
2025-06-18 15:34:32 +05:30
import { useState } from "react";
2025-06-06 20:10:40 +05:30
function AddModel({ refetch }: { refetch: VoidFunction }) {
2025-06-18 15:34:32 +05:30
const [createSubAdminPost] = useCreateSubAdminPostMutation();
2025-06-06 20:10:40 +05:30
2025-06-18 15:34:32 +05:30
// State fields
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [userName, setUserName] = useState("");
const [dateOfBirth, setDateOfBirth] = useState("");
const [gender, setGender] = useState("");
const [ setIsOpen] = useState(false);
2025-06-06 20:10:40 +05:30
const handleSubmit = async () => {
2025-06-18 15:34:32 +05:30
if (
!firstName.trim() ||
!lastName.trim() ||
!userName.trim() ||
!dateOfBirth.trim() ||
!gender.trim()
) {
2025-06-06 20:10:40 +05:30
toaster.create({
title: "Error",
description: "Please fill in all required fields",
type: "error",
});
return;
}
const payload = {
2025-06-18 15:34:32 +05:30
principal_type_xid: 4,
principal_source_xid: 1,
user_name: userName,
2025-06-06 20:10:40 +05:30
first_name: firstName,
last_name: lastName,
2025-06-18 15:34:32 +05:30
date_of_birth: dateOfBirth,
gender: gender,
email_address: "example@yopmail.com", // Hardcoded
phone_number: "9876543210", // Hardcoded
2025-06-06 20:10:40 +05:30
created_by: 1,
};
try {
const response = await createSubAdminPost(payload).unwrap();
2025-06-18 15:34:32 +05:30
if (response) {
2025-06-06 20:10:40 +05:30
toaster.create({
title: "Success",
2025-06-18 15:34:32 +05:30
description: "Sub-admin created successfully",
2025-06-06 20:10:40 +05:30
type: "success",
});
refetch();
setIsOpen(false);
2025-06-18 15:34:32 +05:30
setFirstName("");
setLastName("");
setUserName("");
setDateOfBirth("");
setGender("");
2025-06-06 20:10:40 +05:30
}
2025-06-18 15:34:32 +05:30
} catch (error) {
console.error("Error creating sub-admin:", error);
2025-06-06 20:10:40 +05:30
toaster.create({
title: "Error",
2025-06-18 15:34:32 +05:30
description: "Failed to create sub-admin",
2025-06-06 20:10:40 +05:30
type: "error",
});
}
};
return (
2025-06-18 15:34:32 +05:30
<DialogRoot placement="center">
2025-06-06 20:10:40 +05:30
<DialogTrigger asChild>
<Button rounded={"md"} px={4} py={2} size={"xs"} bg={"#02A0A0"}>
<IoMdAdd /> Add
</Button>
</DialogTrigger>
<DialogContent
bg={"#fff"}
w={{ base: "90%", md: "400px" }}
height={"80vh"}
overflow={"scroll"}
overflowX="hidden"
p={3}
2025-06-18 15:34:32 +05:30
bgSize={"md"}
2025-06-06 20:10:40 +05:30
>
<DialogHeader bg="white">
<DialogTitle alignSelf="center" color="black" fontSize="14px">
Add Sub Admin Account
</DialogTitle>
</DialogHeader>
<DialogBody bg="white">
<Stack py={3}>
<Field.Root>
<Field.Label color="black" pt={1} fontSize="12px">
First Name
</Field.Label>
<Input
placeholder="Enter the First Name"
bgColor="#EEEEEE"
color="black"
border="none"
pl={1}
fontSize="12px"
height="30px"
2025-06-18 15:34:32 +05:30
value={firstName}
onChange={(e) => setFirstName(e.target.value)}
2025-06-06 20:10:40 +05:30
/>
<Field.Label color="black" pt={1} fontSize="12px">
Last Name
</Field.Label>
<Input
placeholder="Enter the Last Name"
bgColor="#EEEEEE"
color="black"
border="none"
pl={1}
fontSize="12px"
height="30px"
2025-06-18 15:34:32 +05:30
value={lastName}
onChange={(e) => setLastName(e.target.value)}
2025-06-06 20:10:40 +05:30
/>
<Field.Label color="black" pt={1} fontSize="12px">
2025-06-18 15:34:32 +05:30
Username
2025-06-06 20:10:40 +05:30
</Field.Label>
<Input
2025-06-18 15:34:32 +05:30
placeholder="Enter the Username"
2025-06-06 20:10:40 +05:30
bgColor="#EEEEEE"
color="black"
border="none"
pl={1}
fontSize="12px"
height="30px"
2025-06-18 15:34:32 +05:30
value={userName}
onChange={(e) => setUserName(e.target.value)}
2025-06-06 20:10:40 +05:30
/>
<Field.Label color="black" pt={1} fontSize="12px">
2025-06-18 15:34:32 +05:30
DOB
2025-06-06 20:10:40 +05:30
</Field.Label>
<Input
2025-06-18 15:34:32 +05:30
placeholder="Enter the DOB"
2025-06-06 20:10:40 +05:30
bgColor="#EEEEEE"
color="black"
border="none"
pl={1}
fontSize="12px"
height="30px"
2025-06-18 15:34:32 +05:30
type="date"
value={dateOfBirth}
onChange={(e) => setDateOfBirth(e.target.value)}
2025-06-06 20:10:40 +05:30
/>
<Field.Label color="black" pt={1} fontSize="12px">
2025-06-18 15:34:32 +05:30
Gender
2025-06-06 20:10:40 +05:30
</Field.Label>
<Input
2025-06-18 15:34:32 +05:30
placeholder="Enter the Gender"
2025-06-06 20:10:40 +05:30
bgColor="#EEEEEE"
color="black"
border="none"
pl={1}
fontSize="12px"
height="30px"
2025-06-18 15:34:32 +05:30
value={gender}
onChange={(e) => setGender(e.target.value)}
2025-06-06 20:10:40 +05:30
/>
<Heading mt={5} color={"#000"} fontSize={"sm"}>
Permissions
</Heading>
</Field.Root>
<Grid templateColumns="repeat(2, 1fr)" gap={4}>
2025-06-18 15:34:32 +05:30
{[
"Dashboard",
"Manage contact us",
"manage User",
"Manage CMS",
"Manage Post",
"Manage Reports",
"manage Sub-Admin",
"My profile",
"Manage Jobs",
"manage feedbacks",
"Manage community",
"Notification",
].map((permission) => (
<Checkbox size="sm" color="black" key={permission}>
<Text fontSize={12}>{permission}</Text>
</Checkbox>
))}
2025-06-06 20:10:40 +05:30
</Grid>
</Stack>
</DialogBody>
<DialogFooter display="flex" justifyContent="center" pt={"2"}>
<Button
size={"xs"}
w="100%"
bg="#02A0A0"
color={"#fff"}
onClick={handleSubmit}
>
Save
</Button>
</DialogFooter>
<DialogCloseTrigger color="black" />
</DialogContent>
</DialogRoot>
);
}
2025-06-06 20:10:40 +05:30
export default AddModel;