Skip to content

Commit

Permalink
Image upload error (#8700)
Browse files Browse the repository at this point in the history
* Add helper function for displaying API error message

* Provide feedback on image upload

* Update notification
  • Loading branch information
SchrodingersGat authored Dec 18, 2024
1 parent 4569fd2 commit 1eaf3a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
26 changes: 20 additions & 6 deletions src/frontend/src/components/details/DetailsImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)) {
setImage(response.data.image);
modals.closeAll();
}
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();
Expand Down
28 changes: 28 additions & 0 deletions src/frontend/src/functions/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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`
});
});
}, []);
Expand Down

0 comments on commit 1eaf3a4

Please sign in to comment.