2021-01-04 10:50:24 +01:00
|
|
|
import client from '@api/client'
|
|
|
|
import Button from '@components/Button'
|
|
|
|
import haptics from '@components/haptics'
|
|
|
|
import { toast } from '@components/toast'
|
2021-01-07 19:13:09 +01:00
|
|
|
import hookRelationship from '@utils/queryHooks/relationship'
|
2021-01-04 10:50:24 +01:00
|
|
|
import React, { useCallback } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-01-07 19:13:09 +01:00
|
|
|
import { useMutation, useQueryClient } from 'react-query'
|
2021-01-04 10:50:24 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
}
|
|
|
|
|
|
|
|
const RelationshipOutgoing: React.FC<Props> = ({ id }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-01-07 19:13:09 +01:00
|
|
|
const relationshipQueryKey = ['Relationship', { id }]
|
|
|
|
const query = hookRelationship({ id })
|
2021-01-04 10:50:24 +01:00
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
const fireMutation = useCallback(
|
|
|
|
({ type, state }: { type: 'follow' | 'block'; state: boolean }) => {
|
2021-01-07 19:13:09 +01:00
|
|
|
return client<Mastodon.Relationship>({
|
2021-01-04 10:50:24 +01:00
|
|
|
method: 'post',
|
|
|
|
instance: 'local',
|
|
|
|
url: `accounts/${id}/${state ? 'un' : ''}${type}`
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
const mutation = useMutation(fireMutation, {
|
2021-01-07 19:13:09 +01:00
|
|
|
onSuccess: res => {
|
2021-01-04 10:50:24 +01:00
|
|
|
haptics('Success')
|
2021-01-07 19:13:09 +01:00
|
|
|
queryClient.setQueryData(relationshipQueryKey, res)
|
2021-01-04 10:50:24 +01:00
|
|
|
},
|
|
|
|
onError: (err: any, { type }) => {
|
|
|
|
haptics('Error')
|
|
|
|
toast({
|
|
|
|
type: 'error',
|
|
|
|
message: t('common:toastMessage.error.message', {
|
|
|
|
function: t(`relationship:${type}.function`)
|
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
|
|
|
description: err.data.error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let content: string
|
|
|
|
let onPress: () => void
|
|
|
|
|
|
|
|
if (query.isError) {
|
|
|
|
content = t('relationship:button.error')
|
|
|
|
onPress = () => {}
|
|
|
|
} else {
|
|
|
|
if (query.data?.blocked_by) {
|
|
|
|
content = t('relationship:button.blocked_by')
|
|
|
|
onPress = () => null
|
|
|
|
} else {
|
|
|
|
if (query.data?.blocking) {
|
|
|
|
content = t('relationship:button.blocking')
|
|
|
|
onPress = () =>
|
|
|
|
mutation.mutate({
|
|
|
|
type: 'block',
|
|
|
|
state: query.data?.blocking
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (query.data?.following) {
|
|
|
|
content = t('relationship:button.following')
|
|
|
|
onPress = () =>
|
|
|
|
mutation.mutate({
|
|
|
|
type: 'follow',
|
|
|
|
state: query.data?.following
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (query.data?.requested) {
|
|
|
|
content = t('relationship:button.requested')
|
|
|
|
onPress = () =>
|
|
|
|
mutation.mutate({
|
|
|
|
type: 'follow',
|
|
|
|
state: query.data?.requested
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
content = t('relationship:button.default')
|
|
|
|
onPress = () =>
|
|
|
|
mutation.mutate({
|
|
|
|
type: 'follow',
|
|
|
|
state: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
type='text'
|
|
|
|
content={content}
|
|
|
|
onPress={onPress}
|
|
|
|
loading={query.isLoading || mutation.isLoading}
|
|
|
|
disabled={query.isError || query.data?.blocked_by}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RelationshipOutgoing
|