|
1 | | -import React from 'react'; |
2 | | -import { Text, View } from 'react-native'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { SafeAreaView, StyleSheet, View } from 'react-native'; |
| 3 | +import { useApi } from '../Api/ApiContextProvider'; |
| 4 | +import { AttendableType } from '../Api/Models/Attendance'; |
| 5 | +import { Permission } from '../Api/Models/Permission'; |
| 6 | +import { getUserInfo, UserInfo } from '../Api/UserApi'; |
| 7 | +import InsufficientPermissions from '../Auth/InsufficientPermissions'; |
| 8 | +import LoadingScreen from '../Components/LoadingScreen'; |
| 9 | +import MenuHeader from '../Components/MenuHeader'; |
| 10 | +import MenuLink from '../Components/MenuLink'; |
| 11 | +import { useTheme } from '../Themes/ThemeContextProvider'; |
| 12 | +import AttendableSelect from './AttendableSelect'; |
3 | 13 |
|
| 14 | +const requiredPermissions: Permission[] = [Permission.CREATE_ATTENDANCE, Permission.READ_USERS]; |
| 15 | + |
| 16 | +/* |
| 17 | +Architecture: |
| 18 | +Attendance Screen > AttendableSelect > TapABuzzCard > BuzzCardPrompt |
| 19 | +*/ |
4 | 20 | function AttendanceScreen() { |
| 21 | + const api = useApi(); |
| 22 | + const [user, setUser] = useState<UserInfo | null | undefined>(undefined); |
| 23 | + const [missingPermissions, setMissingPermissions] = useState<Permission[] | undefined>(undefined); |
| 24 | + const { currentTheme } = useTheme(); |
| 25 | + |
| 26 | + async function onRefreshUser(forceRefresh: boolean = false) { |
| 27 | + if (!user || forceRefresh) { |
| 28 | + setUser(await getUserInfo(api)); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + function getPermissions() { |
| 33 | + let missingPermissions: Permission[] = []; |
| 34 | + if (user && user.allPermissions) { |
| 35 | + const permissions = user.allPermissions; |
| 36 | + missingPermissions = requiredPermissions.filter((item) => !permissions.includes(item)); |
| 37 | + } |
| 38 | + setMissingPermissions(missingPermissions); |
| 39 | + } |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + onRefreshUser(true); |
| 43 | + getPermissions(); |
| 44 | + }, []); |
| 45 | + |
| 46 | + function AttendableSelectionScreen() { |
| 47 | + const [attendanceType, setAttendanceType] = useState<AttendableType>(AttendableType.NONE); |
| 48 | + |
| 49 | + return ( |
| 50 | + <SafeAreaView style={[styles.container, { backgroundColor: currentTheme.background }]}> |
| 51 | + <View style={styles.viewContainer}> |
| 52 | + {attendanceType === AttendableType.NONE ? ( |
| 53 | + <> |
| 54 | + <MenuHeader title={'What do you want to take attendance for?'}></MenuHeader> |
| 55 | + <MenuLink |
| 56 | + title="Team" |
| 57 | + icon="group" |
| 58 | + onClick={() => { |
| 59 | + setAttendanceType(AttendableType.TEAM); |
| 60 | + }} |
| 61 | + ></MenuLink> |
| 62 | + <MenuLink |
| 63 | + title="Event" |
| 64 | + icon="event" |
| 65 | + onClick={() => { |
| 66 | + setAttendanceType(AttendableType.EVENT); |
| 67 | + }} |
| 68 | + ></MenuLink> |
| 69 | + </> |
| 70 | + ) : ( |
| 71 | + <AttendableSelect |
| 72 | + attendanceType={attendanceType} |
| 73 | + setAttendanceType={setAttendanceType} |
| 74 | + ></AttendableSelect> |
| 75 | + )} |
| 76 | + </View> |
| 77 | + </SafeAreaView> |
| 78 | + ); |
| 79 | + } |
| 80 | + |
5 | 81 | return ( |
6 | | - // eslint-disable-next-line react-native/no-inline-styles |
7 | | - <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
8 | | - <Text>Attendance Screen</Text> |
9 | | - </View> |
| 82 | + <> |
| 83 | + {user && missingPermissions ? ( |
| 84 | + missingPermissions.length != 0 ? ( |
| 85 | + <InsufficientPermissions |
| 86 | + featureName="Attendance" |
| 87 | + requiredPermissions={requiredPermissions} |
| 88 | + missingPermissions={missingPermissions} |
| 89 | + onRetry={() => {}} |
| 90 | + ></InsufficientPermissions> |
| 91 | + ) : ( |
| 92 | + <AttendableSelectionScreen></AttendableSelectionScreen> |
| 93 | + ) |
| 94 | + ) : ( |
| 95 | + <LoadingScreen></LoadingScreen> |
| 96 | + )} |
| 97 | + </> |
10 | 98 | ); |
11 | 99 | } |
12 | 100 |
|
| 101 | +const styles = StyleSheet.create({ |
| 102 | + container: { flex: 1 }, |
| 103 | + viewContainer: { flex: 1, padding: 10 }, |
| 104 | +}); |
| 105 | + |
13 | 106 | export default AttendanceScreen; |
0 commit comments