Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useToastHook): don't re-render text field on focus #1341

Merged
merged 1 commit into from
Aug 3, 2023
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
6 changes: 5 additions & 1 deletion src/hooks/settingsHooks/useUpdateSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const MEDIA_COLOUR_TITLES = [
]

export const useUpdateSettings = (
siteName: string
siteName: string,
onSuccess: () => void,
onError: () => void
): UseMutationResult<void, AxiosError, SiteSettings> => {
const queryClient = useQueryClient()
return useMutation<void, AxiosError, SiteSettings>(
Expand All @@ -32,6 +34,8 @@ export const useUpdateSettings = (
onSettled: () => {
queryClient.invalidateQueries([SETTINGS_CONTENT_KEY, siteName])
},
onSuccess,
onError,
}
)
}
Expand Down
51 changes: 23 additions & 28 deletions src/layouts/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,35 @@ const SettingsForm = ({ settings, isError }: SettingsFormProps) => {
const { formState, reset, getValues } = methods
const { isDirty, dirtyFields } = formState
const successToast = useSuccessToast()
const errorToast = useErrorToast()
const { setIsDirty } = useDirtyFieldContext()
const {
mutateAsync: updateSettings,
error: updateSettingsError,
isLoading,
isSuccess,
} = useUpdateSettings(siteName)
const errorToast = useErrorToast()
} = useUpdateSettings(
siteName,
() => {
successToast({
id: "update-settings-success",
title: "Success",
description: "Site settings have been updated",
})
},
() => {
const errorMessage =
updateSettingsError?.response?.status === 409
? "Your site settings have recently been changed by another user. You can choose to either override their changes, or go back to editing. We recommend you to make a copy of your changes elsewhere, and come back later to reconcile your changes."
: `Site settings could not be updated. ${DEFAULT_RETRY_MSG}`
return errorToast({
id: "update-settings-error",
title: "Error",
description: errorMessage,
})
}
)

Comment on lines +108 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good if we shifted all these to the hook and mark as optional but is a good to have bah...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof merge liao :/ + I abit dont get this what do you mean by shift to hook ah?

const onSubmit = methods.handleSubmit((data: SiteSettings) => {
updateSettings(data)
})
Expand Down Expand Up @@ -145,32 +166,6 @@ const SettingsForm = ({ settings, isError }: SettingsFormProps) => {
}
}, [isSuccess, getValues, reset, settings])

// Trigger an error toast informing the user if settings data could not be updated
useEffect(() => {
if (updateSettingsError) {
const errorMessage =
updateSettingsError?.response?.status === 409
? "Your site settings have recently been changed by another user. You can choose to either override their changes, or go back to editing. We recommend you to make a copy of your changes elsewhere, and come back later to reconcile your changes."
: `Site settings could not be updated. ${DEFAULT_RETRY_MSG}`
errorToast({
id: "update-settings-error",
title: "Error",
description: errorMessage,
})
}
}, [errorToast, updateSettingsError])

// Trigger a success toast informing the user if settings data was updated successfully
useEffect(() => {
if (isSuccess) {
successToast({
id: "update-settings-success",
title: "Success!",
description: "Successfully updated settings!",
})
}
}, [isSuccess, successToast])

return (
<Box w="100%">
<FormProvider {...methods}>
Expand Down