Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/components/CategoryModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isDefined,
isNotDefined,
} from '@togglecorp/fujs';
import type { CombinedError } from 'urql';

import {
type ThematicAreasQuery,
Expand All @@ -38,8 +39,9 @@ import useAlert from '#hooks/useAlert';
import useFilterState from '#hooks/useFilterState';
import {
errorMessage,
getErrorMessage,
idSelector,
transformToFormError,
type ServerError,
} from '#utils/common';

type ThematicArea = NonNullable<NonNullable<ThematicAreasQuery['thematicAreas']>['results'][number]>;
Expand Down Expand Up @@ -150,15 +152,13 @@ function CategoryModal(props: Props) {
const handleResult = useCallback((
result: { ok?: boolean | null; errors?: unknown } | null | undefined,
successMessage: string,
mutationError?: CombinedError,
) => {
if (!result?.ok) {
const serverErrors = isDefined(result?.errors)
? transformToFormError(result?.errors as Parameters<typeof transformToFormError>[0])
: undefined;
const messages = serverErrors
? Object.values(serverErrors).filter(isDefined).join(' ')
: undefined;
alert.show(messages || errorMessage, { variant: 'danger' });
alert.show(
getErrorMessage(mutationError, result?.errors as ServerError[] | null),
{ variant: 'danger' },
);
return;
}
setCategoryName(undefined);
Expand Down Expand Up @@ -216,7 +216,7 @@ function CategoryModal(props: Props) {
}
setDeletingId(undefined);
deleteThematicArea({ id: deletingId }).then((resp) => {
handleResult(resp.data?.deleteThematicArea, 'Category deleted successfully');
handleResult(resp.data?.deleteThematicArea, 'Category deleted successfully', resp.error);
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
});
Expand Down
9 changes: 2 additions & 7 deletions app/components/CategoryModal/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ const UPDATE_THEMATIC_AREA = gql`
const DELETE_THEMATIC_AREA = gql`
mutation DeleteThematicArea($id: ID!) {
deleteThematicArea(id: $id) {
... on ThematicAreaTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
16 changes: 13 additions & 3 deletions app/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
isDefined,
isFalsyString,
isNotDefined,
} from '@togglecorp/fujs';
Expand Down Expand Up @@ -50,8 +51,17 @@ export const statusFilterOptions = [

export const errorMessage = 'Something went wrong. Please try again. ';

export function getErrorMessage(error: CombinedError | undefined) {
return error?.graphQLErrors?.[0]?.message || errorMessage;
// Mutations report failures on the payload's `errors`, not on the top-level GraphQL
// errors, so prefer those messages and fall back to the transport error.
export function getErrorMessage(
error: CombinedError | undefined,
serverErrors?: ServerError[] | null,
) {
const messages = serverErrors
?.map((serverError) => serverError.messages)
.filter(isDefined)
.join(' ');
return messages || error?.graphQLErrors?.[0]?.message || errorMessage;
}

export function getReadableFileSize(bytes: number | null | undefined): string {
Expand Down Expand Up @@ -105,7 +115,7 @@ export function validateFile(file: File, maxSize: number, accept: string | undef
return undefined;
}

interface ServerError {
export interface ServerError {
field: string;
messages: string | null;
objectErrors?: ServerError[] | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function ResourceDashboards() {
}
alert.show('Dashboard deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
2 changes: 1 addition & 1 deletion app/views/CapacityAndResources/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function CapacityAndResourcesList() {
}
alert.show('Resource deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
18 changes: 4 additions & 14 deletions app/views/CapacityAndResources/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ const CAPACITY_AND_RESOURCES = gql`
const DELETE_CAPACITY_AND_RESOURCE = gql`
mutation DeleteCapacityAndResource($id: ID!) {
deleteCapacityAndResource(id: $id) {
... on CapacityAndResourceTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down Expand Up @@ -97,13 +92,8 @@ const RESOURCE_DASHBOARDS = gql`
const DELETE_RESOURCE_DASHBOARD = gql`
mutation DeleteResourceDashboard($id: ID!) {
deleteExternalDashboard(id: $id) {
... on ExternalDashboardTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down
6 changes: 5 additions & 1 deletion app/views/DataAndReports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import useRegionMap from '#hooks/useRegionMap';
import useRouting from '#hooks/useRouting';
import {
errorMessage,
getErrorMessage,
idSelector,
} from '#utils/common';

Expand Down Expand Up @@ -121,7 +122,10 @@ function DataAndReports() {
reExecuteQuery();
alert.show('Report deleted successfully', { variant: 'success' });
} else {
alert.show(errorMessage, { variant: 'danger' });
alert.show(
getErrorMessage(resp.error, result?.errors),
{ variant: 'danger' },
);
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
9 changes: 2 additions & 7 deletions app/views/DataAndReports/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ const REPORTS = gql`
const DELETE_REPORT = gql`
mutation DeleteReport($id: ID!) {
deleteReport(id: $id) {
... on ReportTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down
9 changes: 2 additions & 7 deletions app/views/Documents/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ const DOCUMENTS = gql`
const DELETE_DOCUMENT = gql`
mutation DeleteDocument($id: ID!) {
deleteReport(id: $id) {
... on ReportTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down
16 changes: 4 additions & 12 deletions app/views/Galleries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ const CREATE_GALLERY_IMAGE = gql`
const DELETE_GALLERY_IMAGE = gql`
mutation DeleteGalleryImage($id: ID!) {
deleteGalleryImage(id: $id) {
... on GalleryImageTypeMutationResponseType {
errors
ok
}
errors
ok
}
}
`;
Expand Down Expand Up @@ -122,14 +120,8 @@ const UPDATE_GALLERY_ALBUM = gql`
const DELETE_GALLERY_ALBUM = gql`
mutation DeleteGalleryAlbum($id: ID!) {
deleteGalleryAlbum(id: $id) {
... on GalleryAlbumTypeMutationResponseType {
errors
ok
result {
id
title
}
}
errors
ok
}
}
`;
4 changes: 2 additions & 2 deletions app/views/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function Home() {
reExecuteQuickLinksQuery();
alert.show('Added to quick links', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand All @@ -156,7 +156,7 @@ function Home() {
reExecuteQuickLinksQuery();
alert.show('Removed from quick links', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
2 changes: 1 addition & 1 deletion app/views/Links/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function Links() {
reExecuteQuery();
alert.show('Link deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
10 changes: 2 additions & 8 deletions app/views/Links/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,8 @@ const LINK_DETAILS = gql`
const DELETE_LINK = gql`
mutation DeleteLink($id: ID!) {
deleteLink(id: $id) {
... on LinkTypeMutationResponseType {
errors
ok
result {
id
title
}
}
errors
ok
}
}
`;
9 changes: 2 additions & 7 deletions app/views/OnlineInteractive/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ const UPDATE_ONLINE_INTERACTIVE = gql`
const DELETE_ONLINE_INTERACTIVE = gql`
mutation DeleteOnlineInteractive($id: ID!) {
deleteReport(id: $id) {
... on ReportTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
2 changes: 1 addition & 1 deletion app/views/OurWorks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function OurWorks() {
}
alert.show('Dashboard deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
9 changes: 2 additions & 7 deletions app/views/OurWorks/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ const EXTERNAL_DASHBOARDS = gql`
const DELETE_EXTERNAL_DASHBOARD = gql`
mutation DeleteExternalDashboard($id: ID!) {
deleteExternalDashboard(id: $id) {
... on ExternalDashboardTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down
9 changes: 2 additions & 7 deletions app/views/Pmer/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,8 @@ const UPDATE_PMER_REPORT = gql`
const DELETE_PMER_REPORT = gql`
mutation DeletePmerReport($id: ID!) {
deletePmerReport(id: $id) {
... on PmerReportTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
2 changes: 1 addition & 1 deletion app/views/Preparedness/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function PreparednessList() {
}
alert.show('Dashboard deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
9 changes: 2 additions & 7 deletions app/views/Preparedness/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ const EXTERNAL_DASHBOARDS = gql`
const DELETE_EXTERNAL_DASHBOARD = gql`
mutation PreparednessDeleteExternalDashboard($id: ID!) {
deleteExternalDashboard(id: $id) {
... on ExternalDashboardTypeMutationResponseType {
errors
ok
result {
id
}
}
errors
ok
}
}
`;
Expand Down
2 changes: 1 addition & 1 deletion app/views/Teams/TeamMembers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function TeamMembers() {
reExecuteQuery();
alert.show('Team member deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
2 changes: 1 addition & 1 deletion app/views/Teams/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Teams() {
reExecuteQuery();
alert.show('Team deleted successfully', { variant: 'success' });
} else {
alert.show(getErrorMessage(resp.error), { variant: 'danger' });
alert.show(getErrorMessage(resp.error, result?.errors), { variant: 'danger' });
}
}).catch(() => {
alert.show(errorMessage, { variant: 'danger' });
Expand Down
Loading
Loading