skolplattformen-backup/packages/app/components/childListItem.component.js

211 lines
5.3 KiB
JavaScript
Raw Normal View History

import {
useCalendar,
useClassmates,
useNews,
useNotifications,
useSchedule,
} from '@skolplattformen/api-hooks'
2021-02-20 08:38:08 +00:00
import { Avatar, Button, Card, Text } from '@ui-kitten/components'
import { DateTime } from 'luxon'
import moment from 'moment'
2021-02-20 08:38:08 +00:00
import React from 'react'
import { StyleSheet, View } from 'react-native'
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
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
const { data: notifications, status: notificationsStatus } = useNotifications(
child
)
const { data: news, status: newsStatus } = useNews(child)
const { data: classmates, status: classmatesStatus } = useClassmates(child)
const { data: calendar, status: calendarStatus } = useCalendar(child)
const { data: schedule } = useSchedule(
child,
DateTime.local(),
DateTime.local().plus({ days: 7 })
)
2021-01-04 21:40:54 +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
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',
F: 'Förskoleklass',
2021-01-04 21:40:54 +00:00
}
return child.status
.split(';')
.map((status) => abbrevations[status] || status)
.join(', ')
2021-01-04 21:40:54 +00:00
}
const Header = (props) => (
<View {...props} style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ margin: 20, marginRight: 0 }}>
<Avatar source={require('../assets/avatar.png')} shape="square" />
2021-01-04 21:40:54 +00:00
</View>
<View style={{ margin: 20, flex: 1 }}>
<Text category="h6">{studentName(child.name)}</Text>
<Text category="s1">{`${getClassName()}`}</Text>
2021-01-04 21:40:54 +00:00
</View>
</View>
)
const Footer = () => (
<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',
})
}
accessoryLeft={CalendarOutlineIcon}
>
{`${(notifications || []).length}`}
</Button>
<Button
style={[styles.item, styles[classmatesStatus]]}
status="control"
size="small"
onPress={() =>
navigation.navigate('Child', {
child,
color,
initialRouteName: 'Klassen',
})
}
accessoryLeft={ClassIcon}
>
{`${(classmates || []).length}`}
</Button>
</View>
2021-01-04 21:40:54 +00:00
)
return (
<Card
style={styles.card}
appearance="filled"
2021-01-04 21:40:54 +00:00
status={color}
header={Header}
footer={Footer}
onPress={() => navigation.navigate('Child', { child, color })}
>
{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>
)}
<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
</Card>
)
}
const styles = StyleSheet.create({
card: {
marginBottom: 20,
2021-01-04 21:40:54 +00:00
},
itemFooter: {
flexDirection: 'row',
justifyContent: 'space-evenly',
paddingVertical: 8,
2021-01-05 11:27:38 +00:00
borderRadius: 5,
margin: 0,
2021-01-05 11:27:38 +00:00
},
itemFooterAbsence: {
alignItems: 'flex-start',
marginTop: 16,
},
item: {
2021-01-05 11:27:38 +00:00
paddingHorizontal: 0,
},
loaded: {
color: '#000',
2021-01-04 21:40:54 +00:00
},
2021-01-05 11:27:38 +00:00
loading: {
color: '#555',
},
error: {
color: '#500',
},
2021-01-04 21:40:54 +00:00
})