1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(bug) refresh trainings list on delete success

This commit is contained in:
Sylvain 2023-02-16 11:52:44 +01:00
parent 2fb18b8a1b
commit b55eb68813
3 changed files with 22 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import { FabButton } from './fab-button';
import { FabModal } from './fab-modal'; import { FabModal } from './fab-modal';
interface EditDestroyButtonsProps { interface EditDestroyButtonsProps {
onSuccess: (message: string) => void, onDeleteSuccess: (message: string) => void,
onError: (message: string) => void, onError: (message: string) => void,
onEdit: () => void, onEdit: () => void,
itemId: number, itemId: number,
@ -22,7 +22,7 @@ interface EditDestroyButtonsProps {
* Destroy : shows a modal dialog to ask the user for confirmation about the deletion of the provided item. * Destroy : shows a modal dialog to ask the user for confirmation about the deletion of the provided item.
* Edit : triggers the provided function. * Edit : triggers the provided function.
*/ */
export const EditDestroyButtons: React.FC<EditDestroyButtonsProps> = ({ onSuccess, onError, onEdit, itemId, itemType, apiDestroy, confirmationMessage, className, iconSize = 20 }) => { export const EditDestroyButtons: React.FC<EditDestroyButtonsProps> = ({ onDeleteSuccess, onError, onEdit, itemId, itemType, apiDestroy, confirmationMessage, className, iconSize = 20 }) => {
const { t } = useTranslation('admin'); const { t } = useTranslation('admin');
const [deletionModal, setDeletionModal] = useState<boolean>(false); const [deletionModal, setDeletionModal] = useState<boolean>(false);
@ -40,7 +40,7 @@ export const EditDestroyButtons: React.FC<EditDestroyButtonsProps> = ({ onSucces
*/ */
const onDeleteConfirmed = (): void => { const onDeleteConfirmed = (): void => {
apiDestroy(itemId).then(() => { apiDestroy(itemId).then(() => {
onSuccess(t('app.admin.edit_destroy_buttons.deleted', { TYPE: itemType })); onDeleteSuccess(t('app.admin.edit_destroy_buttons.deleted', { TYPE: itemType }));
}).catch((error) => { }).catch((error) => {
onError(t('app.admin.edit_destroy_buttons.unable_to_delete', { TYPE: itemType }) + error); onError(t('app.admin.edit_destroy_buttons.unable_to_delete', { TYPE: itemType }) + error);
}); });

View File

@ -110,7 +110,7 @@ export const ConfigurePacksButton: React.FC<ConfigurePacksButtonProps> = ({ pack
{formatDuration(p.minutes)} - {FormatLib.price(p.amount)} {formatDuration(p.minutes)} - {FormatLib.price(p.amount)}
<EditDestroyButtons className='pack-actions' <EditDestroyButtons className='pack-actions'
onError={onError} onError={onError}
onSuccess={handleSuccess} onDeleteSuccess={handleSuccess}
onEdit={() => handleRequestEdit(p)} onEdit={() => handleRequestEdit(p)}
itemId={p.id} itemId={p.id}
itemType={t('app.admin.configure_packs_button.pack')} itemType={t('app.admin.configure_packs_button.pack')}

View File

@ -53,14 +53,21 @@ export const Trainings: React.FC<TrainingsProps> = ({ onError, onSuccess }) => {
}, []); }, []);
useEffect(() => { useEffect(() => {
fetchTrainings(filter);
}, [filter]);
/**
* Fetch the trainings from the API
*/
const fetchTrainings = (filterDisabled?: boolean) => {
const trainingsFilters = Object.assign( const trainingsFilters = Object.assign(
{ requested_attributes: ['override_settings'] }, { requested_attributes: ['override_settings'] },
(typeof filter === 'boolean') ? { disabled: filter } : {} (typeof filterDisabled === 'boolean') ? { disabled: filterDisabled } : {}
) as TrainingIndexFilter; ) as TrainingIndexFilter;
TrainingAPI.index(trainingsFilters) TrainingAPI.index(trainingsFilters)
.then(setTrainings) .then(setTrainings)
.catch(onError); .catch(onError);
}, [filter]); };
/** Creates filtering options to the react-select format */ /** Creates filtering options to the react-select format */
const buildFilterOptions = (): Array<SelectOption<boolean>> => { const buildFilterOptions = (): Array<SelectOption<boolean>> => {
@ -92,6 +99,14 @@ export const Trainings: React.FC<TrainingsProps> = ({ onError, onSuccess }) => {
return training.machine_ids.filter(id => activesMachines.includes(id)).length > 0; return training.machine_ids.filter(id => activesMachines.includes(id)).length > 0;
}; };
/**
* Callback triggered when a training was successfully deleted
*/
const handleTrainingDeleted = (message: string): void => {
onSuccess(message);
fetchTrainings(filter);
};
/** /**
* Redirect the user to the given training edition page * Redirect the user to the given training edition page
*/ */
@ -177,7 +192,7 @@ export const Trainings: React.FC<TrainingsProps> = ({ onError, onSuccess }) => {
<div className='actions'> <div className='actions'>
<EditDestroyButtons className='grpBtn' <EditDestroyButtons className='grpBtn'
onError={onError} onError={onError}
onSuccess={onSuccess} onDeleteSuccess={handleTrainingDeleted}
onEdit={() => toTrainingEdit(training)} onEdit={() => toTrainingEdit(training)}
itemId={training.id} itemId={training.id}
itemType={t('app.admin.trainings.training')} itemType={t('app.admin.trainings.training')}