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

Image upload error (#8700) (#8704)

* 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:
github-actions[bot] 2024-12-19 00:16:39 +11:00 committed by GitHub
parent 04aec83e95
commit 6bd32c9236
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 { modals } from '@mantine/modals';
import { useMemo, useState } from 'react'; import { useMemo, useState } from 'react';
import { showNotification } from '@mantine/notifications';
import { api } from '../../App'; import { api } from '../../App';
import type { UserRoles } from '../../enums/Roles'; import type { UserRoles } from '../../enums/Roles';
import { cancelEvent } from '../../functions/events'; import { cancelEvent } from '../../functions/events';
import { InvenTreeIcon } from '../../functions/icons'; import { InvenTreeIcon } from '../../functions/icons';
import { showApiErrorMessage } from '../../functions/notifications';
import { useEditApiFormModal } from '../../hooks/UseForm'; import { useEditApiFormModal } from '../../hooks/UseForm';
import { useGlobalSettingsState } from '../../states/SettingsState'; import { useGlobalSettingsState } from '../../states/SettingsState';
import { useUserState } from '../../states/UserState'; import { useUserState } from '../../states/UserState';
@ -159,12 +161,24 @@ function UploadModal({
const formData = new FormData(); const formData = new FormData();
formData.append('image', file, file.name); formData.append('image', file, file.name);
const response = await api.patch(apiPath, formData); api
.patch(apiPath, formData)
if (response.data.image.includes(file.name)) { .then((response) => {
setImage(response.data.image); setImage(response.data.image);
modals.closeAll(); 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(); const { colorScheme } = useMantineColorScheme();

View File

@ -74,3 +74,31 @@ export function showLoginNotification({
autoClose: 2500 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 { FactCollection } from '../../../../components/settings/FactCollection';
import { GlobalSettingList } from '../../../../components/settings/SettingList'; import { GlobalSettingList } from '../../../../components/settings/SettingList';
import { ApiEndpoints } from '../../../../enums/ApiEndpoints'; import { ApiEndpoints } from '../../../../enums/ApiEndpoints';
import { showApiErrorMessage } from '../../../../functions/notifications';
import { useTable } from '../../../../hooks/UseTable'; import { useTable } from '../../../../hooks/UseTable';
import { apiUrl } from '../../../../states/ApiState'; import { apiUrl } from '../../../../states/ApiState';
import { InvenTreeTable } from '../../../../tables/InvenTreeTable'; import { InvenTreeTable } from '../../../../tables/InvenTreeTable';
@ -46,10 +47,9 @@ export function CurrencyTable({
}); });
}) })
.catch((error) => { .catch((error) => {
showNotification({ showApiErrorMessage({
title: t`Exchange rate update error`, error: error,
message: error, title: t`Exchange rate update error`
color: 'red'
}); });
}); });
}, []); }, []);