1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Optimisations and haptics

This commit is contained in:
Zhiyuan Zheng
2021-05-26 23:30:15 +02:00
parent 926bce8f58
commit 8a56e3a4f0
8 changed files with 162 additions and 264 deletions

View File

@ -1,6 +1,11 @@
import apiInstance from '@api/instance'
import haptics from '@components/haptics'
import { displayMessage } from '@components/Message'
import queryClient from '@helpers/queryClient'
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 &
@ -24,7 +29,7 @@ const useProfileQuery = <TData = AccountWithSource>({
return useQuery(queryKey, queryFunction, options)
}
type MutationVarsProfile =
type MutationVarsProfileBase =
| { type: 'display_name'; data: string }
| { type: 'note'; data: string }
| { type: 'avatar'; data: string }
@ -44,6 +49,16 @@ type MutationVarsProfile =
data: { name: string; value: string }[]
}
type MutationVarsProfile = MutationVarsProfileBase & {
mode: 'light' | 'dark'
messageRef: RefObject<FlashMessage>
message: {
text: string
succeed: boolean
failed: boolean
}
}
const mutationFunction = async ({ type, data }: MutationVarsProfile) => {
const formData = new FormData()
if (type === 'fields_attributes') {
@ -107,8 +122,35 @@ const useProfileMutation = () => {
return oldData
},
onError: (_, variables, context) => {
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 }),
mode: variables.mode,
type: 'error'
})
}
},
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}`)
}),
mode: variables.mode,
type: 'success'
})
} else {
haptics('Light')
}
},
onSettled: () => {
queryClient.invalidateQueries(queryKey)

View File

@ -1,4 +1,5 @@
import apiGeneral from '@api/general'
import haptics from '@components/haptics'
import { AxiosError } from 'axios'
import { Buffer } from 'buffer'
import Constants from 'expo-constants'
@ -47,6 +48,7 @@ const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
url: `v1/translate/${uriEncoded}/${target}`,
headers: { key, original }
})
haptics('Light')
return res.body
}