2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Image upload error (#8700)

* Add helper function for displaying API error message

* Provide feedback on image upload

* Update notification
This commit is contained in:
Oliver 2024-12-18 23:24:18 +11:00 committed by GitHub
parent 4569fd273d
commit 1eaf3a4594
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 10 deletions

View File

@ -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();

View File

@ -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'
});
}

View File

@ -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`
});
});
}, []);