add loading when post/put data on master data module

This commit is contained in:
Wikzyy
2025-04-23 13:35:32 +07:00
parent e248c463c9
commit 94ffc332e9
26 changed files with 838 additions and 454 deletions

View File

@ -23,6 +23,7 @@ import {
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
interface CurrencyProps {
ID: string;
@ -64,6 +65,7 @@ const AddDialog = () => {
id_currency: ''
};
const [formField, setFormField] = useState(initialState);
const [isSubmitting, setIsSubmitting] = useState(false);
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
const [groups, setGroups] = useState<GroupProps[]>([]);
@ -95,24 +97,31 @@ const AddDialog = () => {
const doCreateWallet = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
const response = await PostData(`${API_URL_MASTER_DATA}/wallet/create`, formField);
try {
const response = await PostData(`${API_URL_MASTER_DATA}/wallet/create`, formField);
if (response?.status) {
handleAddDialog(false);
toast.success('Success Create Wallet');
reload();
if (response?.status) {
handleAddDialog(false);
toast.success('Success Create Wallet');
reload();
const createActivity = {
module: 'Manage Wallet',
description: `Create Wallet => ${formField.name}`,
action: 'C'
};
const createActivity = {
module: 'Manage Wallet',
description: `Create Wallet => ${formField.name}`,
action: 'C'
};
doSaveLogActivity(createActivity);
} else {
toast.error('Failed Create Wallet');
setAlert({ show: true, message: 'Failed Create Wallet' });
doSaveLogActivity(createActivity);
} else {
toast.error('Failed Create Wallet');
setAlert({ show: true, message: response?.message });
}
} catch (error) {
toast.error('Something went wrong, please try again.');
} finally {
setIsSubmitting(false);
}
},
[formField]
@ -293,7 +302,13 @@ const AddDialog = () => {
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button variant="default">Create</Button>
<Button variant="default" type="submit" disabled={isSubmitting}>
{isSubmitting ? (
<RefreshCw className="animate-spin h-8 w-8 text-white mx-3" />
) : (
'Create'
)}
</Button>
</div>
</div>
</form>