tooot/src/components/Relationship/Incoming.tsx

93 lines
2.5 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'
2022-11-29 23:44:11 +01:00
import { QueryKeyRelationship, useRelationshipMutation } from '@utils/queryHooks/relationship'
2021-01-11 21:36:57 +01:00
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 }]
2022-11-29 23:44:11 +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')
2022-11-29 23:44:11 +01:00
queryClient.setQueryData<Mastodon.Relationship[]>(queryKeyRelationship, [res])
2021-01-11 21:36:57 +01:00
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}
2022-11-29 23:44:11 +01:00
onPress={() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'incoming',
payload: { action: 'reject' }
})
2022-11-29 23:44:11 +01:00
}
/>
<Button
round
type='icon'
content='Check'
loading={mutation.isLoading}
2022-11-29 23:44:11 +01:00
onPress={() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
id,
type: 'incoming',
payload: { action: 'authorize' }
})
2022-11-29 23:44:11 +01:00
}
style={styles.approve}
/>
</View>
)
}
const styles = StyleSheet.create({
base: {
flexShrink: 1,
flexDirection: 'row'
},
approve: {
marginLeft: StyleConstants.Spacing.M
}
})
export default RelationshipIncoming