@@ -185,11 +158,14 @@ const AddDialog = () => {
+ {errors.id_currency_destination && (
+
+ {errors.id_currency_destination}
+
+ )}
+ {errors.id_currency_origin && (
+ {errors.id_currency_origin}
+ )}
{
onValueChange={(values) => {
setFormField((prev) => ({
...prev,
- buy: values.floatValue || 0
+ buy: values.floatValue ?? null
}));
+ setErrors((prev) => ({ ...prev, buy: '' }));
}}
placeholder="Enter Buy"
/>
+ {errors.buy && {errors.buy}}
{
onValueChange={(values) => {
setFormField((prev) => ({
...prev,
- sell: values.floatValue || 0
+ sell: values.floatValue ?? null
}));
+ setErrors((prev) => ({ ...prev, sell: '' }));
}}
placeholder="Enter Sell"
/>
+ {errors.sell && {errors.sell}}
diff --git a/src/pages/master/conversion/blocks/EditDialog.tsx b/src/pages/master/conversion/blocks/EditDialog.tsx
index a178229..1d83ab0 100644
--- a/src/pages/master/conversion/blocks/EditDialog.tsx
+++ b/src/pages/master/conversion/blocks/EditDialog.tsx
@@ -35,6 +35,7 @@ import {
import { useManageConversionContext } from '../hooks/useManageConversionContext';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
+import { initialStateConversion, validateFormConversion } from './Types';
interface CurrencyProps {
ID: string;
name: string;
@@ -52,26 +53,15 @@ const EditDialog = () => {
const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [isLoading, setIsLoading] = useState(false);
- const [alert, setAlert] = useState({
- show: false,
- message: ''
- });
- const initialState = {
- status: '',
- id_currency_origin: '',
- id_currency_destination: '',
- buy: 0,
- sell: 0,
- created_by: '',
- created_at: ''
- };
- const [formField, setFormField] = useState(initialState);
+ const [errors, setErrors] = useState
>({});
+
+ const [formField, setFormField] = useState(initialStateConversion);
const updated_time = new Date();
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
const resetForm = () => {
- setFormField(initialState);
- setAlert({ show: false, message: '' });
+ setFormField(initialStateConversion);
+ setErrors({});
};
const doUpdateConversion = useCallback(
@@ -79,6 +69,11 @@ const EditDialog = () => {
e.preventDefault();
setIsSubmitting(true);
+ if (!validateFormConversion(formField, setErrors)) {
+ setIsSubmitting(false);
+ return;
+ }
+
try {
const response = await PutData(`${API_URL}/dashboard/conversion/${selectedConversion}`, {
...formField
@@ -97,8 +92,7 @@ const EditDialog = () => {
doSaveLogActivity(createActivity);
reload();
} else {
- toast.error('Error Create Conversion');
- setAlert({ show: true, message: response?.message });
+ toast.error(response?.message);
}
} catch (error) {
toast.error('Something went wrong');
@@ -185,12 +179,6 @@ const EditDialog = () => {
- {alert.show && (
-
- {alert.message}
-
- )}
-
{isLoading ? (
@@ -213,11 +201,12 @@ const EditDialog = () => {
+ {errors.id_currency_origin && (
+ {errors.id_currency_origin}
+ )}
+ {errors.id_currency_destination && (
+
+ {errors.id_currency_destination}
+
+ )}
diff --git a/src/pages/master/conversion/blocks/Types.ts b/src/pages/master/conversion/blocks/Types.ts
new file mode 100644
index 0000000..ec88aa9
--- /dev/null
+++ b/src/pages/master/conversion/blocks/Types.ts
@@ -0,0 +1,50 @@
+import { toast } from 'sonner';
+
+export const validateFormConversion = (
+ formField: typeof initialStateConversion,
+ setErrors: React.Dispatch
>>
+) => {
+ const requiredFields = [
+ { key: 'id_currency_origin', label: 'Currency Origin' },
+ { key: 'id_currency_destination', label: 'Currency Destination' },
+ { key: 'buy', label: 'Buy' },
+ { key: 'sell', label: 'Sell' },
+ { key: 'status', label: 'Status' }
+ ];
+
+ const newErrors: Record = {};
+ let isValid = true;
+
+ requiredFields.forEach(({ key, label }) => {
+ if (
+ formField[key as keyof typeof formField] === '' ||
+ formField[key as keyof typeof formField] === null ||
+ formField[key as keyof typeof formField] === undefined
+ ) {
+ newErrors[key] = `${label} is required`;
+ toast.error(`${label} is required`);
+ isValid = false;
+ }
+ });
+
+ setErrors(newErrors);
+ return isValid;
+};
+
+export const initialStateConversion: {
+ status: string;
+ id_currency_origin: string;
+ id_currency_destination: string;
+ buy: number | null;
+ sell: number | null;
+ created_by: string;
+ created_at: string;
+} = {
+ status: '',
+ id_currency_origin: '',
+ id_currency_destination: '',
+ buy: null,
+ sell: null,
+ created_by: '',
+ created_at: ''
+};
diff --git a/src/pages/master/currency/blocks/AddDialog.tsx b/src/pages/master/currency/blocks/AddDialog.tsx
index 9371a44..e95baa4 100644
--- a/src/pages/master/currency/blocks/AddDialog.tsx
+++ b/src/pages/master/currency/blocks/AddDialog.tsx
@@ -15,16 +15,6 @@ import {
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { NumericFormat } from 'react-number-format';
-import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
-import {
- Command,
- CommandEmpty,
- CommandGroup,
- CommandInput,
- CommandItem,
- CommandList
-} from '@/components/ui/command';
-import { set } from 'date-fns';
import {
Select,
SelectContent,
@@ -33,9 +23,9 @@ import {
SelectValue
} from '@/components/ui/select';
import { useManageCurrencyContext } from '../hooks/useManageCurrencyContext';
-import { prefix } from 'stylis';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
+import { initialStateCurrency, validateFormCurrency } from './Types';
interface CurrencyProps {
ID: string;
name: string;
@@ -50,27 +40,15 @@ const AddDialog = () => {
const { PostData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [currencies, setCurrencies] = useState([]);
- const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
- const [alert, setAlert] = useState({
- show: false,
- message: ''
- });
- const initialState = {
- code: '',
- name: '',
- prefix: '',
- status: '',
- created_by: '',
- created_at: ''
- };
- const [formField, setFormField] = useState(initialState);
+ const [errors, setErrors] = useState>({});
+ const [formField, setFormField] = useState(initialStateCurrency);
const created_time = new Date();
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
const resetForm = () => {
- setFormField(initialState);
- setAlert({ show: false, message: '' });
+ setFormField(initialStateCurrency);
+ setErrors({});
};
const doCreateCurrency = useCallback(
@@ -94,8 +72,7 @@ const AddDialog = () => {
doSaveLogActivity(createActivity);
reload();
} else {
- toast.error('Error Create Currency');
- setAlert({ show: true, message: response?.message });
+ toast.error(response?.message);
}
} catch (error) {
toast.error('Something went wrong');
@@ -109,19 +86,11 @@ const AddDialog = () => {
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
- if (
- formField.code === '' ||
- formField.name === '' ||
- formField.prefix === '' ||
- formField.status === ''
- ) {
- setAlert({ show: true, message: 'Please fill in all required fields.' });
+ if (!validateFormCurrency(formField, setErrors)) {
return;
}
doCreateCurrency(e);
- // console.log(formField);
- setAlert({ show: false, message: '' });
};
useEffect(() => {
@@ -144,17 +113,11 @@ const AddDialog = () => {