tooot/src/components/Relationship/Incoming.tsx

104 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
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
} from '@utils/queryHooks/relationship'
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
import { StyleConstants } from '@utils/styles/constants'
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'
import { StyleSheet, View } from 'react-native'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
export interface Props {
id: Mastodon.Account['id']
}
const RelationshipIncoming: React.FC<Props> = ({ id }) => {
2022-02-12 14:51:01 +01:00
const { theme } = useTheme()
const { t } = useTranslation()
2021-01-10 02:12:14 +01:00
const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id }]
2021-01-11 21:36:57 +01:00
const queryKeyNotification: QueryKeyTimeline = [
'Timeline',
{ page: 'Notifications' }
]
const queryClient = useQueryClient()
2021-01-11 21:36:57 +01:00
const mutation = useRelationshipMutation({
2021-01-07 19:13:09 +01:00
onSuccess: res => {
haptics('Success')
2021-01-11 21:36:57 +01:00
queryClient.setQueryData<Mastodon.Relationship[]>(queryKeyRelationship, [
res
])
queryClient.refetchQueries(queryKeyNotification)
},
onError: (err: any, { type }) => {
haptics('Error')
2021-02-28 22:49:55 +01:00
displayMessage({
type: 'error',
2022-02-12 14:51:01 +01:00
theme,
2021-03-28 23:31:10 +02:00
message: t('common:message.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
})
})
}
})
return (
<View style={styles.base}>
<Button
round
type='icon'
content='X'
loading={mutation.isLoading}
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('relationship_incoming_press_reject')
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'incoming',
payload: { action: 'reject' }
})
2021-01-24 02:25:43 +01:00
}}
/>
<Button
round
type='icon'
content='Check'
loading={mutation.isLoading}
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('relationship_incoming_press_authorize')
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'incoming',
payload: { action: 'authorize' }
})
2021-01-24 02:25:43 +01:00
}}
style={styles.approve}
/>
</View>
)
}
const styles = StyleSheet.create({
base: {
flexShrink: 1,
flexDirection: 'row'
},
approve: {
marginLeft: StyleConstants.Spacing.M
}
})
export default RelationshipIncoming