mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
* Add helper function for displaying API error message * Provide feedback on image upload * Update notification (cherry picked from commit 1eaf3a4594b404e08e52bebeef3fd3952d40b0bc) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
parent
04aec83e95
commit
6bd32c9236
@ -19,10 +19,12 @@ import { useHover } from '@mantine/hooks';
|
||||
import { modals } from '@mantine/modals';
|
||||
import { useMemo, useState } from 'react';
|
||||
|
||||
import { showNotification } from '@mantine/notifications';
|
||||
import { api } from '../../App';
|
||||
import type { UserRoles } from '../../enums/Roles';
|
||||
import { cancelEvent } from '../../functions/events';
|
||||
import { InvenTreeIcon } from '../../functions/icons';
|
||||
import { showApiErrorMessage } from '../../functions/notifications';
|
||||
import { useEditApiFormModal } from '../../hooks/UseForm';
|
||||
import { useGlobalSettingsState } from '../../states/SettingsState';
|
||||
import { useUserState } from '../../states/UserState';
|
||||
@ -159,12 +161,24 @@ function UploadModal({
|
||||
const formData = new FormData();
|
||||
formData.append('image', file, file.name);
|
||||
|
||||
const response = await api.patch(apiPath, formData);
|
||||
|
||||
if (response.data.image.includes(file.name)) {
|
||||
api
|
||||
.patch(apiPath, formData)
|
||||
.then((response) => {
|
||||
setImage(response.data.image);
|
||||
modals.closeAll();
|
||||
}
|
||||
showNotification({
|
||||
title: t`Image uploaded`,
|
||||
message: t`Image has been uploaded successfully`,
|
||||
color: 'green'
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
showApiErrorMessage({
|
||||
error: error,
|
||||
title: t`Upload Error`,
|
||||
field: 'image'
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const { colorScheme } = useMantineColorScheme();
|
||||
|
@ -74,3 +74,31 @@ export function showLoginNotification({
|
||||
autoClose: 2500
|
||||
});
|
||||
}
|
||||
|
||||
export function showApiErrorMessage({
|
||||
error,
|
||||
title,
|
||||
message,
|
||||
field
|
||||
}: {
|
||||
error: any;
|
||||
title: string;
|
||||
message?: string;
|
||||
field?: string;
|
||||
}) {
|
||||
// Extract error description from response
|
||||
const error_data: any = error.response?.data ?? {};
|
||||
|
||||
let error_msg: any =
|
||||
message ?? error_data[field ?? 'error'] ?? error_data['non_field_errors'];
|
||||
|
||||
if (!error_msg) {
|
||||
error_msg = t`An error occurred`;
|
||||
}
|
||||
|
||||
notifications.show({
|
||||
title: title,
|
||||
message: error_msg,
|
||||
color: 'red'
|
||||
});
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import { ActionButton } from '../../../../components/buttons/ActionButton';
|
||||
import { FactCollection } from '../../../../components/settings/FactCollection';
|
||||
import { GlobalSettingList } from '../../../../components/settings/SettingList';
|
||||
import { ApiEndpoints } from '../../../../enums/ApiEndpoints';
|
||||
import { showApiErrorMessage } from '../../../../functions/notifications';
|
||||
import { useTable } from '../../../../hooks/UseTable';
|
||||
import { apiUrl } from '../../../../states/ApiState';
|
||||
import { InvenTreeTable } from '../../../../tables/InvenTreeTable';
|
||||
@ -46,10 +47,9 @@ export function CurrencyTable({
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
showNotification({
|
||||
title: t`Exchange rate update error`,
|
||||
message: error,
|
||||
color: 'red'
|
||||
showApiErrorMessage({
|
||||
error: error,
|
||||
title: t`Exchange rate update error`
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
Loading…
x
Reference in New Issue
Block a user