mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Added push server error messaging
Also clean up <Message> component
This commit is contained in:
@ -2,15 +2,13 @@ import apiInstance from '@api/instance'
|
||||
import haptics from '@components/haptics'
|
||||
import { displayMessage } from '@components/Message'
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import { Theme } from '@utils/styles/themes'
|
||||
import { AxiosError } from 'axios'
|
||||
import i18next from 'i18next'
|
||||
import { RefObject } from 'react'
|
||||
import FlashMessage from 'react-native-flash-message'
|
||||
import { useMutation, useQuery, UseQueryOptions } from 'react-query'
|
||||
|
||||
type AccountWithSource = Mastodon.Account &
|
||||
Required<Pick<Mastodon.Account, 'source'>>
|
||||
type AccountWithSource = Mastodon.Account & Required<Pick<Mastodon.Account, 'source'>>
|
||||
|
||||
type QueryKeyProfile = ['Profile']
|
||||
const queryKey: QueryKeyProfile = ['Profile']
|
||||
@ -52,7 +50,6 @@ type MutationVarsProfileBase =
|
||||
}
|
||||
|
||||
type MutationVarsProfile = MutationVarsProfileBase & {
|
||||
theme: Theme
|
||||
messageRef: RefObject<FlashMessage>
|
||||
message: {
|
||||
text: string
|
||||
@ -92,73 +89,70 @@ const mutationFunction = async ({ type, data }: MutationVarsProfile) => {
|
||||
}
|
||||
|
||||
const useProfileMutation = () => {
|
||||
return useMutation<
|
||||
{ body: AccountWithSource },
|
||||
AxiosError,
|
||||
MutationVarsProfile
|
||||
>(mutationFunction, {
|
||||
onMutate: async variables => {
|
||||
await queryClient.cancelQueries(queryKey)
|
||||
return useMutation<{ body: AccountWithSource }, AxiosError, MutationVarsProfile>(
|
||||
mutationFunction,
|
||||
{
|
||||
onMutate: async variables => {
|
||||
await queryClient.cancelQueries(queryKey)
|
||||
|
||||
const oldData = queryClient.getQueryData<AccountWithSource>(queryKey)
|
||||
const oldData = queryClient.getQueryData<AccountWithSource>(queryKey)
|
||||
|
||||
queryClient.setQueryData<AccountWithSource | undefined>(queryKey, old => {
|
||||
if (old) {
|
||||
switch (variables.type) {
|
||||
case 'source[privacy]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, privacy: variables.data }
|
||||
}
|
||||
case 'source[sensitive]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, sensitive: variables.data }
|
||||
}
|
||||
case 'locked':
|
||||
return { ...old, locked: variables.data }
|
||||
case 'bot':
|
||||
return { ...old, bot: variables.data }
|
||||
default:
|
||||
return old
|
||||
queryClient.setQueryData<AccountWithSource | undefined>(queryKey, old => {
|
||||
if (old) {
|
||||
switch (variables.type) {
|
||||
case 'source[privacy]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, privacy: variables.data }
|
||||
}
|
||||
case 'source[sensitive]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, sensitive: variables.data }
|
||||
}
|
||||
case 'locked':
|
||||
return { ...old, locked: variables.data }
|
||||
case 'bot':
|
||||
return { ...old, bot: variables.data }
|
||||
default:
|
||||
return old
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return oldData
|
||||
},
|
||||
onError: (err, variables, context) => {
|
||||
queryClient.setQueryData(queryKey, context)
|
||||
haptics('Error')
|
||||
if (variables.message.failed) {
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.failed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
...(err && { description: err.message }),
|
||||
theme: variables.theme,
|
||||
type: 'error'
|
||||
})
|
||||
return oldData
|
||||
},
|
||||
onError: (err, variables, context) => {
|
||||
queryClient.setQueryData(queryKey, context)
|
||||
haptics('Error')
|
||||
if (variables.message.failed) {
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.failed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
...(err && { description: err.message }),
|
||||
type: 'danger'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
if (variables.message.succeed) {
|
||||
haptics('Success')
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.succeed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
type: 'success'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
}
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
if (variables.message.succeed) {
|
||||
haptics('Success')
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.succeed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
theme: variables.theme,
|
||||
type: 'success'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export { useProfileQuery, useProfileMutation }
|
||||
|
Reference in New Issue
Block a user