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

@ -35,6 +35,7 @@ import {
import { useManageCurrencyContext } from '../hooks/useManageCurrencyContext';
import { prefix } from 'stylis';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
interface CurrencyProps {
ID: string;
name: string;
@ -50,6 +51,7 @@ const AddDialog = () => {
const parsedUser = getAuth()?.user;
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
@ -74,24 +76,31 @@ const AddDialog = () => {
const doCreateCurrency = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
const response = await PostData(`${API_URL}/dashboard/currency/`, formField);
try {
const response = await PostData(`${API_URL}/dashboard/currency/`, formField);
if (response?.status) {
resetForm();
handleAddDialog(false);
toast.success('Success Create Currency');
const createActivity = {
module: 'Manage Currency',
description: `Create Currency=> ${formField.name}`,
action: 'C'
};
if (response?.status) {
resetForm();
handleAddDialog(false);
toast.success('Success Create Currency');
const createActivity = {
module: 'Manage Currency',
description: `Create Currency=> ${formField.name}`,
action: 'C'
};
doSaveLogActivity(createActivity);
reload();
} else {
toast.error('Error Create Currency');
setAlert({ show: true, message: 'Failed to create Currency. Please try again.' });
doSaveLogActivity(createActivity);
reload();
} else {
toast.error('Error Create Currency');
setAlert({ show: true, message: response?.message });
}
} catch (error) {
toast.error('Something went wrong');
} finally {
setIsSubmitting(false);
}
},
[formField]
@ -220,7 +229,13 @@ const AddDialog = () => {
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button variant="default">Save Changes</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>

View File

@ -34,6 +34,7 @@ import {
} from '@/components/ui/select';
import { useManageCurrencyContext } from '../hooks/useManageCurrencyContext';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
interface CurrencyProps {
ID: string;
name: string;
@ -49,6 +50,7 @@ const EditDialog = () => {
const parsedUser = getAuth()?.user;
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [alert, setAlert] = useState({
show: false,
@ -74,26 +76,34 @@ const EditDialog = () => {
const doUpdateCurrency = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!showEditDialog) return;
const response = await PutData(`${API_URL}/dashboard/currency/${selectedCurrency}`, {
...formField
});
setIsSubmitting(true);
if (response?.status) {
resetForm();
handleEditDialog(false, null);
toast.success('Success Update Currency');
const createActivity = {
module: 'Manage Currency',
description: `Update Currency=> ${selectedCurrency}`,
action: 'U'
};
try {
const response = await PutData(
`${API_URL}/dashboard/currency/${selectedCurrency}`,
formField
);
doSaveLogActivity(createActivity);
reload();
} else {
toast.error('Error Create Currency');
setAlert({ show: true, message: 'Failed to Update Currency. Please try again.' });
if (response?.status) {
resetForm();
handleEditDialog(false, null);
toast.success('Success Update Currency');
const createActivity = {
module: 'Manage Currency',
description: `Update Currency=> ${selectedCurrency}`,
action: 'U'
};
doSaveLogActivity(createActivity);
reload();
} else {
toast.error('Error Create Currency');
setAlert({ show: true, message: response?.message });
}
} catch (error) {
toast.error('Something went wrong');
} finally {
setIsSubmitting(false);
}
},
[formField]
@ -235,7 +245,13 @@ const EditDialog = () => {
</div>
<div className="flex justify-end gap-5">
<Button variant="default">Save Changes</Button>
<Button variant="default" type="submit" disabled={isSubmitting}>
{isSubmitting ? (
<RefreshCw className="animate-spin h-8 w-8 text-white mx-7" />
) : (
'Save Changes'
)}
</Button>
</div>
</div>
</form>