From 6cbc02ff67b2b04ae53718b26a4e33dc57b6004a Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Tue, 11 Jul 2023 12:40:51 +0800 Subject: [PATCH] Refactor error handling for oauth apps mutations --- .../oauth/authorized-app-revoke-mutation.ts | 15 +++++++++++++-- .../data/oauth/oauth-app-create-mutation.ts | 19 +++++++++++++------ .../data/oauth/oauth-app-delete-mutation.ts | 15 +++++++++++++-- .../data/oauth/oauth-app-update-mutation.ts | 15 +++++++++++++-- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/studio/data/oauth/authorized-app-revoke-mutation.ts b/studio/data/oauth/authorized-app-revoke-mutation.ts index d47499a4a77..b44fc2d2f79 100644 --- a/studio/data/oauth/authorized-app-revoke-mutation.ts +++ b/studio/data/oauth/authorized-app-revoke-mutation.ts @@ -1,6 +1,9 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' +import { toast } from 'react-hot-toast' + import { delete_ } from 'lib/common/fetch' import { API_ADMIN_URL } from 'lib/constants' +import { ResponseError } from 'types' import { oauthAppKeys } from './keys' export type AuthorizedAppRevokeVariables = { @@ -23,14 +26,15 @@ type AuthorizedAppRevokeData = Awaited> export const useAuthorizedAppRevokeMutation = ({ onSuccess, + onError, ...options }: Omit< - UseMutationOptions, + UseMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() - return useMutation( + return useMutation( (vars) => revokeAuthorizedApp(vars), { async onSuccess(data, variables, context) { @@ -38,6 +42,13 @@ export const useAuthorizedAppRevokeMutation = ({ await queryClient.invalidateQueries(oauthAppKeys.authorizedApps(slug)) await onSuccess?.(data, variables, context) }, + async onError(data, variables, context) { + if (onError === undefined) { + toast.error(`Failed to revoke application: ${data.message}`) + } else { + onError(data, variables, context) + } + }, ...options, } ) diff --git a/studio/data/oauth/oauth-app-create-mutation.ts b/studio/data/oauth/oauth-app-create-mutation.ts index 14152e5472f..696c29783ea 100644 --- a/studio/data/oauth/oauth-app-create-mutation.ts +++ b/studio/data/oauth/oauth-app-create-mutation.ts @@ -1,7 +1,10 @@ +import { toast } from 'react-hot-toast' import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' + import { post } from 'lib/common/fetch' import { API_ADMIN_URL } from 'lib/constants' import { oauthAppKeys } from './keys' +import { ResponseError } from 'types' export type OAuthAppCreateVariables = { slug: string @@ -24,10 +27,6 @@ export async function createOAuthApp({ icon, redirect_uris, }: OAuthAppCreateVariables) { - if (!slug) throw new Error('Organization slug is required') - if (!name) throw new Error('OAuth app name is required') - if (!website) throw new Error('OAuth app URL is required') - const response = await post(`${API_ADMIN_URL}/organizations/${slug}/oauth/apps`, { name, website, @@ -42,14 +41,15 @@ type OAuthAppCreateData = Awaited> export const useOAuthAppCreateMutation = ({ onSuccess, + onError, ...options }: Omit< - UseMutationOptions, + UseMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() - return useMutation( + return useMutation( (vars) => createOAuthApp(vars), { async onSuccess(data, variables, context) { @@ -57,6 +57,13 @@ export const useOAuthAppCreateMutation = ({ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug)) await onSuccess?.(data, variables, context) }, + async onError(data, variables, context) { + if (onError === undefined) { + toast.error(`Failed to create application: ${data.message}`) + } else { + onError(data, variables, context) + } + }, ...options, } ) diff --git a/studio/data/oauth/oauth-app-delete-mutation.ts b/studio/data/oauth/oauth-app-delete-mutation.ts index 28592be674b..2f48b906670 100644 --- a/studio/data/oauth/oauth-app-delete-mutation.ts +++ b/studio/data/oauth/oauth-app-delete-mutation.ts @@ -1,6 +1,9 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' +import { toast } from 'react-hot-toast' + import { delete_ } from 'lib/common/fetch' import { API_ADMIN_URL } from 'lib/constants' +import { ResponseError } from 'types' import { oauthAppKeys } from './keys' export type OAuthAppDeleteVariables = { @@ -23,14 +26,15 @@ type OAuthAppDeleteData = Awaited> export const useOAuthAppDeleteMutation = ({ onSuccess, + onError, ...options }: Omit< - UseMutationOptions, + UseMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() - return useMutation( + return useMutation( (vars) => deleteOAuthApp(vars), { async onSuccess(data, variables, context) { @@ -38,6 +42,13 @@ export const useOAuthAppDeleteMutation = ({ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug)) await onSuccess?.(data, variables, context) }, + async onError(data, variables, context) { + if (onError === undefined) { + toast.error(`Failed to delete application: ${data.message}`) + } else { + onError(data, variables, context) + } + }, ...options, } ) diff --git a/studio/data/oauth/oauth-app-update-mutation.ts b/studio/data/oauth/oauth-app-update-mutation.ts index acc4ab79f3b..453b0647e08 100644 --- a/studio/data/oauth/oauth-app-update-mutation.ts +++ b/studio/data/oauth/oauth-app-update-mutation.ts @@ -1,6 +1,9 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' +import { toast } from 'react-hot-toast' + import { put } from 'lib/common/fetch' import { API_ADMIN_URL } from 'lib/constants' +import { ResponseError } from 'types' import { oauthAppKeys } from './keys' export type OAuthAppUpdateVariables = { @@ -40,14 +43,15 @@ type OAuthAppUpdateData = Awaited> export const useOAuthAppUpdateMutation = ({ onSuccess, + onError, ...options }: Omit< - UseMutationOptions, + UseMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() - return useMutation( + return useMutation( (vars) => updateOAuthApp(vars), { async onSuccess(data, variables, context) { @@ -55,6 +59,13 @@ export const useOAuthAppUpdateMutation = ({ await queryClient.invalidateQueries(oauthAppKeys.oauthApps(slug)) await onSuccess?.(data, variables, context) }, + async onError(data, variables, context) { + if (onError === undefined) { + toast.error(`Failed to update application: ${data.message}`) + } else { + onError(data, variables, context) + } + }, ...options, } )