tooot/src/components/Relationship/Outgoing.tsx

136 lines
3.7 KiB
TypeScript
Raw Normal View History

import Button from '@components/Button'
import haptics from '@components/haptics'
2021-02-28 22:49:55 +01:00
import { displayMessage } from '@components/Message'
2021-01-11 21:36:57 +01:00
import {
QueryKeyRelationship,
useRelationshipMutation,
useRelationshipQuery
2021-01-10 02:12:14 +01:00
} from '@utils/queryHooks/relationship'
2021-01-20 00:39:39 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2021-02-28 22:49:55 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-11 21:36:57 +01:00
import React from 'react'
import { useTranslation } from 'react-i18next'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
export interface Props {
id: Mastodon.Account['id']
}
2021-01-11 21:36:57 +01:00
const RelationshipOutgoing = React.memo(
({ id }: Props) => {
2022-02-12 14:51:01 +01:00
const { theme } = useTheme()
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('componentRelationship')
2021-01-11 21:36:57 +01:00
const query = useRelationshipQuery({ id })
2021-01-11 21:36:57 +01:00
const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id }]
const queryClient = useQueryClient()
const mutation = useRelationshipMutation({
2021-01-20 00:39:39 +01:00
onSuccess: (res, { payload: { action } }) => {
2021-01-11 21:36:57 +01:00
haptics('Success')
2022-11-29 23:44:11 +01:00
queryClient.setQueryData<Mastodon.Relationship[]>(queryKeyRelationship, [res])
2022-10-31 21:54:24 +01:00
if (action === 'block') {
2021-01-20 00:39:39 +01:00
const queryKey: QueryKeyTimeline = ['Timeline', { page: 'Following' }]
queryClient.invalidateQueries(queryKey)
}
2021-01-11 21:36:57 +01:00
},
2021-01-19 01:13:45 +01:00
onError: (err: any, { payload: { action } }) => {
2021-02-28 22:49:55 +01:00
displayMessage({
2022-02-12 14:51:01 +01:00
theme,
2021-01-11 21:36:57 +01:00
type: 'error',
2021-03-28 23:31:10 +02:00
message: t('common:message.error.message', {
2021-01-24 02:25:43 +01:00
function: t(`${action}.function`)
2021-01-11 21:36:57 +01:00
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
})
}
})
2021-01-11 21:36:57 +01:00
let content: string
let onPress: () => void
2021-01-11 21:36:57 +01:00
if (query.isError) {
2021-01-19 01:13:45 +01:00
content = t('button.error')
2021-01-11 21:36:57 +01:00
onPress = () => {}
} else {
2021-01-11 21:36:57 +01:00
if (query.data?.blocked_by) {
2021-01-19 01:13:45 +01:00
content = t('button.blocked_by')
2022-11-29 23:44:11 +01:00
onPress = () => {}
} else {
2021-01-11 21:36:57 +01:00
if (query.data?.blocking) {
2021-01-19 01:13:45 +01:00
content = t('button.blocking')
2021-01-24 02:25:43 +01:00
onPress = () => {
mutation.mutate({
2021-01-11 21:36:57 +01:00
id,
type: 'outgoing',
payload: {
action: 'block',
state: query.data?.blocking
}
})
2021-01-24 02:25:43 +01:00
}
} else {
2021-01-11 21:36:57 +01:00
if (query.data?.following) {
2021-01-19 01:13:45 +01:00
content = t('button.following')
2021-01-24 02:25:43 +01:00
onPress = () => {
mutation.mutate({
2021-01-11 21:36:57 +01:00
id,
type: 'outgoing',
payload: {
action: 'follow',
state: query.data?.following
}
})
2021-01-24 02:25:43 +01:00
}
} else {
2021-01-11 21:36:57 +01:00
if (query.data?.requested) {
2021-01-19 01:13:45 +01:00
content = t('button.requested')
2021-01-24 02:25:43 +01:00
onPress = () => {
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'outgoing',
payload: {
action: 'follow',
state: query.data?.requested
}
})
2021-01-24 02:25:43 +01:00
}
2021-01-11 21:36:57 +01:00
} else {
2021-01-19 01:13:45 +01:00
content = t('button.default')
2021-01-24 02:25:43 +01:00
onPress = () => {
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'outgoing',
payload: {
action: 'follow',
state: false
}
})
2021-01-24 02:25:43 +01:00
}
2021-01-11 21:36:57 +01:00
}
}
}
}
}
2021-01-11 21:36:57 +01:00
return (
<Button
type='text'
content={content}
onPress={onPress}
loading={query.isLoading || mutation.isLoading}
disabled={query.isError || query.data?.blocked_by}
/>
)
},
() => true
)
export default RelationshipOutgoing