Add Company
Add Company
Add Company
// ** MUI Imports
import Autocomplete from "@mui/material/Autocomplete";
import Box, { BoxProps } from "@mui/material/Box";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import FormControl from "@mui/material/FormControl";
import FormHelperText from "@mui/material/FormHelperText";
import Grid from "@mui/material/Grid";
import Input from "@mui/material/Input";
import InputLabel from "@mui/material/InputLabel";
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import TextField from "@mui/material/TextField";
import Typography from "@mui/material/Typography";
import { styled } from "@mui/material/styles";
// ** Icon Imports
import Icon from "src/@core/components/icon";
// ** Store Imports
import { useDispatch, useSelector } from "react-redux";
// ** Actions Imports
import { RootState, AppDispatch } from "src/store";
// ** Types Imports
import { DialogActions } from "@mui/material";
import InputAdornment from "@mui/material/InputAdornment";
import format from "date-fns/format";
import { toast } from "react-hot-toast";
import HeaderIcon from "src/@core/layouts/components/header-icon";
import { addCompany } from "src/store/apps/company";
import { DateType } from "src/types/forms/reactDatepickerTypes";
import { fetchData, getData } from "src/store/apps/appMaster";
// ** Import Component
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
interface SidebarAddCompanyType {
open: boolean;
toggle: () => void;
}
interface PickerProps {
start: Date | number;
end: Date | number;
}
interface CompanyData {
address: string;
app_id: number;
color: string;
contract_end_date: string;
contract_start_date: string;
contract_value: number;
cpm: number;
email: string;
impression: number;
include_dapil?: boolean;
include_disclaimer?: boolean;
mh_secret_key: string;
logo: string;
name: string;
phone: string;
pic_email: string;
parent_id: number;
referral_code: string;
pic_name: string;
pic_phone: string;
trial_duration: number;
token: number;
user_quota: number;
website: string;
}
interface CompanyDataStep1 {
address: string;
color: string;
email: string;
name: string;
phone: string;
pic_email: string;
parent_id: number;
pic_name: string;
pic_phone: string;
website: string;
}
interface CompanyDataStep2 {
contract_end_date: string;
contract_start_date: string;
contract_value: number;
user_quota: number;
token: number;
referral_code: string;
cpm: number;
impression: number;
contract_end_date: yup.string().required(),
contract_start_date: yup.string().required(),
contract_value: yup.string().required(),
cpm: yup.number(),
impression: yup.number(),
parent_id: yup.number(),
app_id: yup.number(),
trial_duration: yup.number(),
include_dapil: yup.boolean(),
mh_secret_key: yup.string(),
include_disclaimer: yup.boolean(),
logo: yup.string().required(),
pic_email: yup.string().email().required(),
pic_name: yup.string().required(),
pic_phone: yup
.string()
.typeError("Phone Number field is required")
.min(10, (obj) => showErrors("Phone Number", obj.value.length, obj.min))
.required(),
referral_code: yup.string(),
token: yup.number(),
user_quota: yup.number(),
});
const defaultValues = {
address: "",
app_id: Number(""),
color: "",
contract_end_date: "2023-03-23",
contract_start_date: "2023-02-12",
contract_value: Number(""),
cpm: Number(""),
email: "",
impression: Number(""),
include_dapil: false,
include_disclaimer: false,
logo: "",
name: "",
phone: "",
pic_email: "",
pic_name: "",
pic_phone: "",
token: Number(""),
parent_id: Number(""),
mh_secret_key: "",
trial_duration: Number(""),
referral_code: "",
user_quota: Number(""),
website: "https://",
};
// ** Hooks
const dispatch = useDispatch<AppDispatch>();
useEffect(() => {
dispatch(getData());
}, [dispatch]);
const {
reset,
control,
setValue,
handleSubmit,
watch,
formState: { errors },
} = useForm({
defaultValues:{
},
mode: "onChange",
resolver: yupResolver(schema),
});
},
resolver: yupResolver(
schemaStep2.transform((data) => {
data.contract_start_date = dayjs(data.to).format("YYYY-MM-DD");
data.contract_end_date = dayjs(data.from).format("YYYY-MM-DD");
return data;
})
),
});
// toggle();
// reset();
} catch (error) {
toast.error("An error occurred while adding the company");
}
};
reader.readAsDataURL(file[0]);
}
};
return (
<TextField
{...props}
size="small"
value={value}
inputRef={ref}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Icon icon="tabler:calendar-event" />
</InputAdornment>
),
endAdornment: (
<InputAdornment position="end">
<Icon icon="tabler:chevron-down" />
</InputAdornment>
),
}}
/>
);
});
CustomInput.displayName = "CustomInput";
return (
<Dialog
open={open}
onClose={handleClose}
fullWidth
maxWidth="md"
sx={{ maxHeight: "80rem" }}
>
<DialogTitle>
<Box sx={{ justifyContent: "space-between", display: "flex" }}>
<Typography variant="h5">Add Company</Typography>
</Box>
</DialogTitle>
<Box sx={{ display: "flex", justifyContent: "center" }}>
<Tabs value={tabs} onChange={handleChangeTabs}>
<Tab label="Company Profile" />
<Tab label="Company Contract" />
<Tab label="Other" />
</Tabs>
</Box>
<DialogContent
sx={{
overflow: "auto",
scrollbarWidth: "thin",
scrollbarColor: "#bbb transparent",
minHeight: { xs: "60rem", md: "40rem" },
}}
>
<form onSubmit={handleSubmit(onSubmit)}>
<Grid
container
spacing={1}
sx={{ p: (theme) => theme.spacing(0, 6, 6), marginTop: 2 }}
>
<Grid item container md={12} spacing={5} marginTop={1}>
{tabs === 0 && (
<>
<Grid item xs={12} md={6}>
<FormControl fullWidth sx={{ mb: 4 }}>
<Controller
name="name"
control={control}
rules={{ required: true }}
render={({ field: { value, onChange } }) => (
<TextField
value={value}
label="Company Name"
onChange={onChange}
placeholder="PT. MAJU MUNDUR"
error={Boolean(errors.name)}
/>
)}
/>
{errors.name && (
<FormHelperText sx={{ color: "error.main" }}>
{errors.name.message}
</FormHelperText>
)}
</FormControl>
</Grid>