Skip to content

Commit

Permalink
fix(web): add notification when error occurs during asset upload (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
mulengawilfred authored Nov 6, 2024
1 parent 9d26672 commit 31176c1
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions web/src/services/api/assetsApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { useApolloClient, useMutation, useQuery } from "@apollo/client";
import {
FetchResult,
useApolloClient,
useMutation,
useQuery
} from "@apollo/client";
import {
CreateAssetInput,
GetAssetsQueryVariables,
GetAssetsQuery
GetAssetsQuery,
CreateAssetMutation
} from "@reearth/services/gql";
import {
CREATE_ASSET,
Expand Down Expand Up @@ -64,30 +70,48 @@ export default () => {
});

const useCreateAssets = useCallback(
async ({ teamId, file }: CreateAssetInput) => {
async ({
teamId,
file
}: CreateAssetInput): Promise<
| {
data: FetchResult<CreateAssetMutation>[];
result: string;
}
| undefined
> => {
if (!file || !teamId) return;

const results = await Promise.all(
Array.from(file).map((f) =>
createAssetMutation({ variables: { teamId, file: f } })
)
);

if (!results || results.some((r) => r.errors)) {
try {
const results = await Promise.all(
Array.from(file).map((f) =>
createAssetMutation({ variables: { teamId, file: f } })
)
);

if (!results || results.some((r) => r.errors)) {
setNotification({
type: "error",
text: t("Failed to add one or more assets.")
});
} else {
setNotification({
type: "success",
text: t("Successfully added one or more assets.")
});
}

apolloCache.evict({ fieldName: "assets" });

return { data: results, result: "success" };
} catch (_error) {
setNotification({
type: "error",
text: t("Failed to add one or more assets.")
});
} else {
setNotification({
type: "success",
text: t("Successfully added one or more assets.")
});
return;
// TODO: add 'error' to error tracking
}

apolloCache.evict({ fieldName: "assets" });

return { data: results, result: "success" };
},
[apolloCache, createAssetMutation, t, setNotification]
);
Expand Down

0 comments on commit 31176c1

Please sign in to comment.