add loading when post/put data on master data module
This commit is contained in:
@ -25,6 +25,7 @@ import {
|
||||
CommandList
|
||||
} from '@/components/ui/command';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
|
||||
interface MunicipioProps {
|
||||
id: number;
|
||||
@ -41,6 +42,7 @@ const AddDialog = () => {
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [municipios, setMunicipios] = useState<MunicipioProps[]>([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
@ -65,27 +67,34 @@ const AddDialog = () => {
|
||||
const doCreatePostoAdm = useCallback(
|
||||
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
const response = await PostData(`${API_URL}/postoadms/create`, formField);
|
||||
try {
|
||||
const response = await PostData(`${API_URL}/postoadms/create`, formField);
|
||||
|
||||
if (response?.status) {
|
||||
handleAddDialog(false);
|
||||
resetForm();
|
||||
reload();
|
||||
toast.success('Postu Administrativo created successfully!');
|
||||
const createActivity = {
|
||||
module: 'Manage Posto Administrativo',
|
||||
description: `Create Postu Administrativo => ${formField.name}`,
|
||||
action: 'C'
|
||||
};
|
||||
if (response?.status) {
|
||||
handleAddDialog(false);
|
||||
resetForm();
|
||||
reload();
|
||||
toast.success('Postu Administrativo created successfully!');
|
||||
const createActivity = {
|
||||
module: 'Manage Posto Administrativo',
|
||||
description: `Create Postu Administrativo => ${formField.name}`,
|
||||
action: 'C'
|
||||
};
|
||||
|
||||
doSaveLogActivity(createActivity);
|
||||
} else {
|
||||
toast.error('Failed to create Postu Administrativo. Please try again.');
|
||||
setAlert({
|
||||
show: true,
|
||||
message: 'Failed to create Postu Administrativo. Please try again.'
|
||||
});
|
||||
doSaveLogActivity(createActivity);
|
||||
} else {
|
||||
toast.error('Failed to create Postu Administrativo. Please try again.');
|
||||
setAlert({
|
||||
show: true,
|
||||
message: response?.message
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong. Please try again.');
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
},
|
||||
[formField]
|
||||
@ -222,7 +231,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>
|
||||
|
||||
Reference in New Issue
Block a user