tooot/src/screens/Tabs/Shared/Users.tsx

89 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-03-15 00:18:44 +01:00
import ComponentAccount from '@components/Account'
2022-12-03 16:50:54 +01:00
import { HeaderLeft } from '@components/Header'
2022-12-15 01:41:34 +01:00
import Icon from '@components/Icon'
2021-03-15 00:18:44 +01:00
import ComponentSeparator from '@components/Separator'
2022-12-15 01:41:34 +01:00
import CustomText from '@components/Text'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2021-03-15 00:18:44 +01:00
import { QueryKeyUsers, useUsersQuery } from '@utils/queryHooks/users'
2022-12-15 01:41:34 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-03 16:50:54 +01:00
import React, { useCallback, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
2022-12-15 01:41:34 +01:00
import { View } from 'react-native'
2021-03-15 00:18:44 +01:00
import { FlatList } from 'react-native-gesture-handler'
2022-12-03 16:50:54 +01:00
const TabSharedUsers: React.FC<TabSharedStackScreenProps<'Tab-Shared-Users'>> = ({
navigation,
route: { params }
}) => {
2022-12-15 01:41:34 +01:00
const { colors } = useTheme()
2022-12-03 16:50:54 +01:00
const { t } = useTranslation('screenTabs')
useEffect(() => {
navigation.setOptions({
title: t(`shared.users.${params.reference}.${params.type}`, { count: params.count }),
headerLeft: () => <HeaderLeft onPress={() => navigation.goBack()} />
})
}, [])
2021-03-15 00:18:44 +01:00
2022-12-03 16:50:54 +01:00
const queryKey: QueryKeyUsers = ['Users', params]
const { data, hasNextPage, fetchNextPage, isFetchingNextPage } = useUsersQuery({
...queryKey[1],
options: {
2022-12-15 01:41:34 +01:00
getPreviousPageParam: firstPage =>
firstPage.links?.prev?.id && { min_id: firstPage.links.prev.id },
getNextPageParam: lastPage => lastPage.links?.next?.id && { max_id: lastPage.links.next.id }
2022-12-03 16:50:54 +01:00
}
})
const flattenData = data?.pages ? data.pages.flatMap(page => [...page.body]) : []
2021-03-15 00:18:44 +01:00
2022-12-03 16:50:54 +01:00
const onEndReached = useCallback(
() => hasNextPage && !isFetchingNextPage && fetchNextPage(),
[hasNextPage, isFetchingNextPage]
)
2021-03-15 00:18:44 +01:00
2022-12-03 16:50:54 +01:00
return (
<FlatList
windowSize={7}
data={flattenData}
style={{
minHeight: '100%'
}}
renderItem={({ item }) => <ComponentAccount account={item} />}
onEndReached={onEndReached}
onEndReachedThreshold={0.75}
ItemSeparatorComponent={ComponentSeparator}
maintainVisibleContentPosition={{
minIndexForVisible: 0,
autoscrollToTopThreshold: 2
}}
2022-12-15 01:41:34 +01:00
ListHeaderComponent={
data?.pages[0]?.warnIncomplete === true ? (
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginHorizontal: StyleConstants.Spacing.Global.PagePadding,
padding: StyleConstants.Spacing.S,
borderColor: colors.border,
borderWidth: 1,
borderRadius: StyleConstants.Spacing.S
}}
>
<Icon
name='AlertCircle'
color={colors.secondary}
size={StyleConstants.Font.Size.M}
style={{ marginRight: StyleConstants.Spacing.S }}
/>
<CustomText fontStyle='S' style={{ flexShrink: 1, color: colors.secondary }}>
{t('shared.users.resultIncomplete')}
</CustomText>
</View>
) : null
}
2022-12-03 16:50:54 +01:00
/>
)
}
2021-03-15 00:18:44 +01:00
export default TabSharedUsers