336 lines
12 KiB
TypeScript
336 lines
12 KiB
TypeScript
import { apiConfig } from '@/config/api.config';
|
|
import { useManageProviderContext } from '../hooks/useManageProviderContext';
|
|
import { Alert, useDataGrid } from '@/components';
|
|
import { useCallApi } from '@/hooks';
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { getAuth } from '@/auth';
|
|
import { toast } from 'sonner';
|
|
import {
|
|
Dialog,
|
|
DialogBody,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle
|
|
} from '@/components/ui/dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue
|
|
} from '@/components/ui/select';
|
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
|
import {
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList
|
|
} from '@/components/ui/command';
|
|
|
|
export interface CustomerProps {
|
|
msisdn: string;
|
|
email: string;
|
|
fullname: string;
|
|
username: string;
|
|
}
|
|
|
|
export interface TransactionProps {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
const API_URL_CUSTOMER = apiConfig.service_customer;
|
|
const API_URL_MASTERDATA = apiConfig.service_master_data;
|
|
const API_URL_TRANSACTION = apiConfig.service_transaction;
|
|
|
|
const AddDialog = () => {
|
|
const { showAddDialog, handleAddDialog } = useManageProviderContext();
|
|
const { reload } = useDataGrid();
|
|
const { PostData, GetData } = useCallApi();
|
|
const parentRef = useRef<any | null>(null);
|
|
const parsedUser = getAuth()?.user;
|
|
const [open, setOpen] = useState(false);
|
|
const [alert, setAlert] = useState({
|
|
show: false,
|
|
message: ''
|
|
});
|
|
const initialState = {
|
|
name: '',
|
|
description: '',
|
|
type: '',
|
|
status: '',
|
|
transactionTypeId: '',
|
|
agentId: '',
|
|
created_by: '',
|
|
created_at: ''
|
|
};
|
|
const [formField, setFormField] = useState(initialState);
|
|
const [customers, setCustomers] = useState<CustomerProps[]>([]);
|
|
const [transactions, setTransactions] = useState<TransactionProps[]>([]);
|
|
const created_time = new Date();
|
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
|
|
|
const resetForm = () => {
|
|
setFormField(initialState);
|
|
setAlert({ show: false, message: '' });
|
|
};
|
|
|
|
const doCreateProvider = useCallback(async (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
|
|
const response = await PostData(`${API_URL_MASTERDATA}/provider/create`, formField);
|
|
|
|
if (response?.status) {
|
|
resetForm();
|
|
handleAddDialog(false);
|
|
toast.success('Success Create Provider');
|
|
reload();
|
|
} else {
|
|
toast.error('Failed Create Provider');
|
|
setAlert({ show: true, message: 'Failed Create Provider' });
|
|
}
|
|
}, []);
|
|
|
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
|
|
if (
|
|
formField.name === '' ||
|
|
formField.description === '' ||
|
|
formField.type === '' ||
|
|
formField.status === '' ||
|
|
formField.transactionTypeId === '' ||
|
|
formField.agentId === ''
|
|
) {
|
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
|
return;
|
|
}
|
|
|
|
// doCreateProvider(e);
|
|
console.log(formField);
|
|
setAlert({ show: false, message: '' });
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (showAddDialog) {
|
|
setFormField({
|
|
...formField,
|
|
created_by: parsedUser.username,
|
|
created_at: formattedTime
|
|
});
|
|
}
|
|
}, [formattedTime]);
|
|
|
|
useEffect(() => {
|
|
const getCustomerList = async (sorting: any) => {
|
|
try {
|
|
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
|
const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
|
|
limit: 100,
|
|
page: 1,
|
|
with_deleted: false,
|
|
order_field: sorting[0].id,
|
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
|
});
|
|
|
|
console.log('CUSTOMER: ', response?.data);
|
|
setCustomers(response?.data.list);
|
|
} catch (error) {
|
|
console.error('Error fetching customer', error);
|
|
}
|
|
};
|
|
|
|
const getTransactionTypeList = async (sorting: any) => {
|
|
try {
|
|
const response = await GetData(`${API_URL_TRANSACTION}/transactiontype/list`, {
|
|
limit: 100,
|
|
page: 1,
|
|
with_deleted: false,
|
|
order_field: sorting[0].id,
|
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
|
});
|
|
|
|
console.log('TRANSACTION TYPE: ', response?.data);
|
|
setTransactions(response?.data.list);
|
|
} catch (error) {
|
|
console.error('Error fetching transaction type', error);
|
|
}
|
|
};
|
|
|
|
getCustomerList([{ id: 'msisdn', desc: false }]);
|
|
getTransactionTypeList([{ id: 'name', desc: false }]);
|
|
}, []);
|
|
|
|
return (
|
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
|
<DialogHeader>
|
|
<DialogTitle>Provider - Create</DialogTitle>
|
|
<DialogDescription></DialogDescription>
|
|
</DialogHeader>
|
|
<DialogBody ref={parentRef}>
|
|
<div className="flex flex-col">
|
|
{alert.show && (
|
|
<Alert variant="danger">
|
|
<h3>{alert.message}</h3>
|
|
</Alert>
|
|
)}
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="card-body grid gap-5">
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Name<span className="text-red-500">*</span>
|
|
</label>
|
|
<Input
|
|
className="input"
|
|
type="text"
|
|
value={formField.name}
|
|
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Description<span className="text-red-500">*</span>
|
|
</label>
|
|
<Input
|
|
className="input"
|
|
type="text"
|
|
value={formField.description}
|
|
onChange={(e) => setFormField({ ...formField, description: e.target.value })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Type<span className="text-red-500">*</span>
|
|
</label>
|
|
<Select
|
|
value={formField.type}
|
|
onValueChange={(value) => setFormField({ ...formField, type: value })}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select Type" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="h2h">H2H</SelectItem>
|
|
<SelectItem value="agent">Agent</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Status<span className="text-red-500">*</span>
|
|
</label>
|
|
<Select
|
|
value={formField.status}
|
|
onValueChange={(value) => setFormField({ ...formField, status: value })}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select Status" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="Y">Active</SelectItem>
|
|
<SelectItem value="N">Inactive</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Transaction Type Id<span className="text-red-500">*</span>
|
|
</label>
|
|
<Select
|
|
value={formField.transactionTypeId}
|
|
onValueChange={(value) =>
|
|
setFormField({ ...formField, transactionTypeId: value })
|
|
}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select Transaction Type" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{transactions.map((transaction) => (
|
|
<SelectItem key={transaction.id} value={transaction.id}>
|
|
{transaction.name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
<label className="form-label flex items-center gap-1 max-w-56">
|
|
Agent Name<span className="text-red-500">*</span>
|
|
</label>
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild>
|
|
<button type="button" className="input col-span-5 text-left">
|
|
{customers.find((customer) => customer.msisdn === formField.agentId)
|
|
?.fullname || 'Select Agent'}
|
|
</button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-[400px] p-0">
|
|
<Command>
|
|
<CommandInput placeholder="Search Agent..." />
|
|
<CommandList>
|
|
<CommandEmpty>No Agent found.</CommandEmpty>
|
|
<CommandGroup>
|
|
{customers.map((customer) => (
|
|
<CommandItem
|
|
key={customer.msisdn}
|
|
value={customer.fullname}
|
|
onSelect={() => {
|
|
setFormField({
|
|
...formField,
|
|
agentId: customer.msisdn
|
|
});
|
|
setOpen(false);
|
|
}}
|
|
>
|
|
{customer.fullname}
|
|
</CommandItem>
|
|
))}
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-5">
|
|
<Button type="button" variant="outline" onClick={resetForm}>
|
|
Reset
|
|
</Button>
|
|
<Button variant="default">Create</Button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</DialogBody>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
};
|
|
|
|
export default AddDialog;
|