2021-02-07 16:14:45 +00:00
|
|
|
import {
|
|
|
|
useCalendar,
|
2021-02-15 12:33:47 +00:00
|
|
|
useClassmates,
|
|
|
|
useNews,
|
|
|
|
useNotifications,
|
2021-02-07 16:14:45 +00:00
|
|
|
useSchedule,
|
|
|
|
} from '@skolplattformen/api-hooks'
|
2021-02-20 08:38:08 +00:00
|
|
|
import { Avatar, Button, Card, Text } from '@ui-kitten/components'
|
2021-02-15 12:33:47 +00:00
|
|
|
import { DateTime } from 'luxon'
|
|
|
|
import moment from 'moment'
|
2021-02-20 08:38:08 +00:00
|
|
|
import React from 'react'
|
2021-02-15 12:33:47 +00:00
|
|
|
import { StyleSheet, View } from 'react-native'
|
2021-02-19 14:30:48 +00:00
|
|
|
import { studentName } from '../utils/peopleHelpers'
|
2021-02-20 08:38:08 +00:00
|
|
|
import {
|
|
|
|
CalendarOutlineIcon,
|
|
|
|
ClassIcon,
|
|
|
|
NewsIcon,
|
|
|
|
NotificationsIcon,
|
|
|
|
} from './icon.component'
|
2021-01-04 21:40:54 +00:00
|
|
|
|
2021-02-11 08:20:41 +00:00
|
|
|
export const ChildListItem = ({ navigation, child, color }) => {
|
2021-02-14 20:06:23 +00:00
|
|
|
// Forces rerender when child.id changes
|
2021-02-20 08:38:08 +00:00
|
|
|
React.useEffect(() => {}, [child.id])
|
2021-02-14 20:06:23 +00:00
|
|
|
|
2021-02-11 08:20:41 +00:00
|
|
|
const { data: notifications, status: notificationsStatus } = useNotifications(
|
2021-02-15 12:33:47 +00:00
|
|
|
child
|
2021-02-07 16:14:45 +00:00
|
|
|
)
|
2021-02-11 08:20:41 +00:00
|
|
|
const { data: news, status: newsStatus } = useNews(child)
|
|
|
|
const { data: classmates, status: classmatesStatus } = useClassmates(child)
|
|
|
|
const { data: calendar, status: calendarStatus } = useCalendar(child)
|
2021-02-15 12:33:47 +00:00
|
|
|
const { data: schedule } = useSchedule(
|
2021-02-07 16:14:45 +00:00
|
|
|
child,
|
|
|
|
DateTime.local(),
|
2021-02-15 12:33:47 +00:00
|
|
|
DateTime.local().plus({ days: 7 })
|
2021-02-07 16:14:45 +00:00
|
|
|
)
|
2021-01-04 21:40:54 +00:00
|
|
|
|
2021-02-19 14:29:41 +00:00
|
|
|
const notificationsThisWeek = notifications.filter((n) =>
|
|
|
|
moment(n).isSame('week')
|
|
|
|
)
|
|
|
|
|
|
|
|
const scheduleAndCalendarThisWeek = [
|
|
|
|
...(calendar ?? []),
|
|
|
|
...(schedule ?? []),
|
|
|
|
].filter((a) => moment(a.startDate).isSame('week'))
|
|
|
|
|
2021-01-04 21:40:54 +00:00
|
|
|
const getClassName = () => {
|
|
|
|
// hack: we can find the class name (ex. 8C) from the classmates. let's pick the first one and select theirs class
|
2021-02-07 16:14:45 +00:00
|
|
|
if (classmates.length > 0) {
|
|
|
|
return classmates[0].className
|
|
|
|
}
|
2021-01-04 21:40:54 +00:00
|
|
|
|
|
|
|
// otherwise we show the status: Grundskola, Gymnasium etc.
|
|
|
|
const abbrevations = {
|
|
|
|
G: 'Gymnasiet', // ? i'm guessing here
|
|
|
|
GR: 'Grundskolan',
|
2021-02-07 16:14:45 +00:00
|
|
|
F: 'Förskoleklass',
|
2021-01-04 21:40:54 +00:00
|
|
|
}
|
2021-02-07 16:14:45 +00:00
|
|
|
return child.status
|
|
|
|
.split(';')
|
|
|
|
.map((status) => abbrevations[status] || status)
|
|
|
|
.join(', ')
|
2021-01-04 21:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Header = (props) => (
|
2021-02-15 12:33:47 +00:00
|
|
|
<View {...props} style={{ flexDirection: 'row', alignItems: 'center' }}>
|
|
|
|
<View style={{ margin: 20, marginRight: 0 }}>
|
2021-02-07 16:14:45 +00:00
|
|
|
<Avatar source={require('../assets/avatar.png')} shape="square" />
|
2021-01-04 21:40:54 +00:00
|
|
|
</View>
|
2021-02-15 12:33:47 +00:00
|
|
|
<View style={{ margin: 20, flex: 1 }}>
|
2021-02-19 14:30:48 +00:00
|
|
|
<Text category="h6">{studentName(child.name)}</Text>
|
2021-02-07 16:14:45 +00:00
|
|
|
<Text category="s1">{`${getClassName()}`}</Text>
|
2021-01-04 21:40:54 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2021-02-15 12:33:47 +00:00
|
|
|
const Footer = () => (
|
2021-02-19 08:18:22 +00:00
|
|
|
<>
|
|
|
|
<View style={styles.itemFooter}>
|
|
|
|
<Button
|
|
|
|
style={[styles.item, styles[newsStatus]]}
|
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
|
|
|
navigation.navigate('Child', {
|
|
|
|
child,
|
|
|
|
color,
|
|
|
|
initialRouteName: 'Nyheter',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
accessoryLeft={NewsIcon}
|
|
|
|
>
|
|
|
|
{`${(news || []).length}`}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
style={[styles.item, styles[notificationsStatus]]}
|
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
|
|
|
navigation.navigate('Child', {
|
|
|
|
child,
|
|
|
|
color,
|
|
|
|
initialRouteName: 'Notifieringar',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
accessoryLeft={NotificationsIcon}
|
|
|
|
>
|
|
|
|
{`${(notifications || []).length}`}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
style={[styles.item, styles[calendarStatus]]}
|
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
|
|
|
navigation.navigate('Child', {
|
|
|
|
child,
|
|
|
|
color,
|
|
|
|
initialRouteName: 'Kalender',
|
|
|
|
})
|
|
|
|
}
|
2021-02-20 08:38:08 +00:00
|
|
|
accessoryLeft={CalendarOutlineIcon}
|
2021-02-19 08:18:22 +00:00
|
|
|
>
|
|
|
|
{`${(notifications || []).length}`}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
style={[styles.item, styles[classmatesStatus]]}
|
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
|
|
|
navigation.navigate('Child', {
|
|
|
|
child,
|
|
|
|
color,
|
|
|
|
initialRouteName: 'Klassen',
|
|
|
|
})
|
|
|
|
}
|
2021-02-20 08:38:08 +00:00
|
|
|
accessoryLeft={ClassIcon}
|
2021-02-19 08:18:22 +00:00
|
|
|
>
|
|
|
|
{`${(classmates || []).length}`}
|
|
|
|
</Button>
|
|
|
|
</View>
|
|
|
|
<View style={styles.itemFooterAbsence}>
|
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
onPress={() => navigation.navigate('Absence', { child })}
|
|
|
|
>
|
|
|
|
Anmäl frånvaro
|
|
|
|
</Button>
|
|
|
|
</View>
|
|
|
|
</>
|
2021-01-04 21:40:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
2021-02-16 15:48:40 +00:00
|
|
|
style={styles.card}
|
2021-02-07 16:14:45 +00:00
|
|
|
appearance="filled"
|
2021-01-04 21:40:54 +00:00
|
|
|
status={color}
|
|
|
|
header={Header}
|
|
|
|
footer={Footer}
|
2021-02-15 12:33:47 +00:00
|
|
|
onPress={() => navigation.navigate('Child', { child, color })}
|
|
|
|
>
|
2021-02-19 14:29:41 +00:00
|
|
|
{scheduleAndCalendarThisWeek.slice(0, 3).map((calendarItem, i) => (
|
|
|
|
<Text
|
|
|
|
appearance="hint"
|
|
|
|
category="c1"
|
|
|
|
key={i}
|
|
|
|
style={{ textColor: styles.loaded(notificationsStatus) }}
|
|
|
|
>
|
|
|
|
{`${calendarItem.title}`}
|
|
|
|
</Text>
|
|
|
|
))}
|
|
|
|
{notificationsThisWeek.map((notification, i) => (
|
|
|
|
<Text appearance="hint" category="c1" key={i}>
|
|
|
|
{`${notification.message}`}
|
|
|
|
</Text>
|
|
|
|
))}
|
|
|
|
{scheduleAndCalendarThisWeek.length ||
|
|
|
|
notificationsThisWeek.length ? null : (
|
|
|
|
<Text appearance="hint" category="c1">
|
|
|
|
Inga nya inlägg denna vecka.
|
|
|
|
</Text>
|
|
|
|
)}
|
2021-01-04 21:40:54 +00:00
|
|
|
</Card>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
card: {
|
2021-02-16 15:48:40 +00:00
|
|
|
marginBottom: 20,
|
2021-01-04 21:40:54 +00:00
|
|
|
},
|
|
|
|
itemFooter: {
|
|
|
|
flexDirection: 'row',
|
2021-01-26 12:39:22 +00:00
|
|
|
justifyContent: 'space-evenly',
|
2021-02-19 08:18:22 +00:00
|
|
|
paddingVertical: 8,
|
2021-01-05 11:27:38 +00:00
|
|
|
borderRadius: 5,
|
2021-02-07 16:14:45 +00:00
|
|
|
margin: 0,
|
2021-01-05 11:27:38 +00:00
|
|
|
},
|
2021-02-19 08:18:22 +00:00
|
|
|
itemFooterAbsence: {
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
padding: 8,
|
|
|
|
},
|
2021-01-26 12:39:22 +00:00
|
|
|
item: {
|
2021-01-05 11:27:38 +00:00
|
|
|
paddingHorizontal: 0,
|
2021-01-26 12:39:22 +00:00
|
|
|
},
|
|
|
|
loaded: {
|
2021-02-07 16:14:45 +00:00
|
|
|
color: '#000',
|
2021-01-04 21:40:54 +00:00
|
|
|
},
|
2021-01-05 11:27:38 +00:00
|
|
|
loading: {
|
2021-02-07 16:14:45 +00:00
|
|
|
color: '#555',
|
2021-01-26 12:39:22 +00:00
|
|
|
},
|
|
|
|
error: {
|
2021-02-07 16:14:45 +00:00
|
|
|
color: '#500',
|
|
|
|
},
|
2021-01-04 21:40:54 +00:00
|
|
|
})
|