2021-05-26 23:30:15 +02:00
|
|
|
import haptics from '@components/haptics'
|
|
|
|
import { displayMessage } from '@components/Message'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { useMutation, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import apiInstance from '@utils/api/instance'
|
2023-01-03 23:57:23 +01:00
|
|
|
import { queryClient } from '@utils/queryHooks'
|
2021-05-09 21:59:03 +02:00
|
|
|
import { AxiosError } from 'axios'
|
2021-05-26 23:30:15 +02:00
|
|
|
import i18next from 'i18next'
|
|
|
|
import { RefObject } from 'react'
|
|
|
|
import FlashMessage from 'react-native-flash-message'
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
type AccountWithSource = Mastodon.Account & Required<Pick<Mastodon.Account, 'source'>>
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-12-10 00:31:11 +01:00
|
|
|
export type QueryKeyProfile = ['Profile']
|
2021-05-09 21:59:03 +02:00
|
|
|
const queryKey: QueryKeyProfile = ['Profile']
|
|
|
|
|
2022-12-10 00:31:11 +01:00
|
|
|
const queryFunctionProfile = async () => {
|
2021-12-18 19:59:38 +01:00
|
|
|
const res = await apiInstance<AccountWithSource>({
|
2021-05-09 21:59:03 +02:00
|
|
|
method: 'get',
|
|
|
|
url: `accounts/verify_credentials`
|
2021-12-18 19:59:38 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 21:09:00 +01:00
|
|
|
const useProfileQuery = (
|
|
|
|
params: {
|
|
|
|
options: UseQueryOptions<AccountWithSource, AxiosError>
|
|
|
|
} | void
|
|
|
|
) => {
|
2022-12-29 23:13:22 +01:00
|
|
|
return useQuery(queryKey, queryFunctionProfile, {
|
|
|
|
...params?.options,
|
|
|
|
staleTime: Infinity,
|
|
|
|
cacheTime: Infinity
|
|
|
|
})
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 23:30:15 +02:00
|
|
|
type MutationVarsProfileBase =
|
2021-05-09 21:59:03 +02:00
|
|
|
| { type: 'display_name'; data: string }
|
|
|
|
| { type: 'note'; data: string }
|
|
|
|
| { type: 'avatar'; data: string }
|
|
|
|
| { type: 'header'; data: string }
|
|
|
|
| { type: 'locked'; data: boolean }
|
|
|
|
| { type: 'bot'; data: boolean }
|
|
|
|
| {
|
|
|
|
type: 'source[privacy]'
|
|
|
|
data: Mastodon.Preferences['posting:default:visibility']
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'source[sensitive]'
|
|
|
|
data: Mastodon.Preferences['posting:default:sensitive']
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'fields_attributes'
|
|
|
|
data: { name: string; value: string }[]
|
|
|
|
}
|
|
|
|
|
2021-05-26 23:30:15 +02:00
|
|
|
type MutationVarsProfile = MutationVarsProfileBase & {
|
|
|
|
messageRef: RefObject<FlashMessage>
|
|
|
|
message: {
|
|
|
|
text: string
|
|
|
|
succeed: boolean
|
|
|
|
failed: boolean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
const mutationFunction = async ({ type, data }: MutationVarsProfile) => {
|
|
|
|
const formData = new FormData()
|
2023-01-29 00:37:56 +01:00
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
if (type === 'fields_attributes') {
|
2023-01-29 00:37:56 +01:00
|
|
|
const body: { fields_attributes: { name: string; value: string }[] } = {
|
|
|
|
fields_attributes: []
|
|
|
|
}
|
|
|
|
if (data.length) {
|
|
|
|
body.fields_attributes = data as { name: string; value: string }[]
|
2022-06-21 23:45:11 +02:00
|
|
|
}
|
2023-01-29 00:37:56 +01:00
|
|
|
return apiInstance<AccountWithSource>({
|
|
|
|
method: 'patch',
|
|
|
|
url: 'accounts/update_credentials',
|
|
|
|
body
|
|
|
|
})
|
2021-05-09 21:59:03 +02:00
|
|
|
} else if (type === 'avatar' || type === 'header') {
|
|
|
|
formData.append(type, {
|
2022-06-21 23:45:11 +02:00
|
|
|
uri: data,
|
2021-05-09 21:59:03 +02:00
|
|
|
name: 'image/jpeg',
|
|
|
|
type: 'image/jpeg'
|
2022-06-05 17:58:18 +02:00
|
|
|
} as any)
|
2021-05-09 21:59:03 +02:00
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
formData.append(type, data)
|
|
|
|
}
|
|
|
|
return apiInstance<AccountWithSource>({
|
|
|
|
method: 'patch',
|
|
|
|
url: 'accounts/update_credentials',
|
|
|
|
body: formData
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const useProfileMutation = () => {
|
2022-12-05 23:15:07 +01:00
|
|
|
return useMutation<{ body: AccountWithSource }, AxiosError, MutationVarsProfile>(
|
|
|
|
mutationFunction,
|
|
|
|
{
|
|
|
|
onMutate: async variables => {
|
|
|
|
await queryClient.cancelQueries(queryKey)
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
const oldData = queryClient.getQueryData<AccountWithSource>(queryKey)
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-12-05 23:15:07 +01:00
|
|
|
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
|
|
|
|
}
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
2021-05-26 23:30:15 +02:00
|
|
|
})
|
2022-12-05 23:15:07 +01:00
|
|
|
|
|
|
|
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', {
|
2022-12-23 15:53:40 +01:00
|
|
|
type: i18next.t(`screenTabs:${variables.message.text}` as any)
|
2022-12-05 23:15:07 +01:00
|
|
|
}),
|
|
|
|
...(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', {
|
2022-12-23 15:53:40 +01:00
|
|
|
type: i18next.t(`screenTabs:${variables.message.text}` as any)
|
2022-12-05 23:15:07 +01:00
|
|
|
}),
|
|
|
|
type: 'success'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKey)
|
2021-05-26 23:30:15 +02:00
|
|
|
}
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
2022-12-05 23:15:07 +01:00
|
|
|
)
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
|
|
|
|
2022-12-10 00:31:11 +01:00
|
|
|
export { queryFunctionProfile, useProfileQuery, useProfileMutation }
|