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

195 lines
5.7 KiB
TypeScript
Raw Normal View History

/* eslint-disable react-native-a11y/has-accessibility-hint */
import { useNavigation } from '@react-navigation/native'
import { StackNavigationProp } from '@react-navigation/stack'
import {
useCalendar,
useNews,
useNotifications,
useSchedule,
} from '@skolplattformen/api-hooks'
2021-03-26 08:38:15 +00:00
import { Child } from '@skolplattformen/embedded-api'
import {
Button,
StyleService,
Text,
useStyleSheet,
} from '@ui-kitten/components'
import moment from 'moment'
2021-02-20 08:38:08 +00:00
import React from 'react'
import { TouchableOpacity, View } from 'react-native'
import { Layout, Sizing } from '../styles'
import { studentName } from '../utils/peopleHelpers'
2021-04-13 15:03:14 +00:00
import { translate } from '../utils/translation'
2021-03-26 08:38:15 +00:00
import { RootStackParamList } from './navigation.component'
import { StudentAvatar } from './studentAvatar.component'
2021-01-04 21:40:54 +00:00
2021-03-26 08:38:15 +00:00
interface ChildListItemProps {
child: Child
color: string
}
type ChildListItemNavigationProp = StackNavigationProp<
RootStackParamList,
'Children'
>
export const ChildListItem = ({ child, color }: ChildListItemProps) => {
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 navigation = useNavigation<ChildListItemNavigationProp>()
2021-06-20 23:12:25 +00:00
const { data: notifications } = useNotifications(child)
const { data: news } = useNews(child)
const { data: calendar } = useCalendar(child)
const { data: schedule } = useSchedule(
child,
moment().toISOString(),
moment().add(7, 'days').toISOString()
)
2021-01-04 21:40:54 +00:00
const notificationsThisWeek = notifications.filter(({ dateCreated }) =>
2021-04-01 11:41:36 +00:00
dateCreated ? moment(dateCreated).isSame(moment(), 'week') : false
)
const newsThisWeek = news.filter(({ published }) =>
published ? moment(published).isSame(moment(), 'week') : false
)
const scheduleAndCalendarThisWeek = [
...(calendar ?? []),
...(schedule ?? []),
2021-04-01 11:41:36 +00:00
].filter(({ startDate }) =>
startDate
? moment(startDate).isBetween(
moment().startOf('day'),
moment().add(7, 'days')
)
: false
2021-04-01 11:41:36 +00:00
)
const displayDate = (date: moment.MomentInput) => {
return moment(date).fromNow()
}
const getClassName = () => {
// Taken from Skolverket
// https://www.skolverket.se/skolutveckling/anordna-och-administrera-utbildning/administrera-utbildning/skoltermer-pa-engelska
const abbrevations = {
G: translate('abbrevations.upperSecondarySchool'),
GR: translate('abbrevations.compulsorySchool'),
F: translate('abbrevations.leisureTimeCentre'),
FS: translate('abbrevations.preSchool'),
}
return child.status
? child.status
.split(';')
.map((status) => {
const statusAsAbbreviation = status as keyof typeof abbrevations
return abbrevations[statusAsAbbreviation] || status
})
.join(', ')
: null
}
const className = getClassName()
const styles = useStyleSheet(themeStyles)
2021-01-04 21:40:54 +00:00
return (
<TouchableOpacity
onPress={() => navigation.navigate('Child', { child, color })}
>
<View style={styles.card}>
<View style={styles.cardHeader}>
<View style={styles.cardHeaderLeft}>
<StudentAvatar name={studentName(child.name)} color={color} />
<View style={styles.cardHeaderText}>
<Text category="h6">{studentName(child.name)}</Text>
{className ? <Text category="s1">{className}</Text> : null}
</View>
</View>
</View>
{scheduleAndCalendarThisWeek.slice(0, 3).map((calendarItem, i) => (
<Text category="p1" key={i}>
{`${calendarItem.title} (${displayDate(calendarItem.startDate)})`}
</Text>
))}
{notificationsThisWeek.slice(0, 3).map((notification, i) => (
<Text category="p1" key={i}>
{translate('notifications.notificationTitle', {
message: notification.message,
dateCreated: displayDate(notification.dateCreated),
})}
</Text>
))}
{newsThisWeek.slice(0, 3).map((newsItem, i) => (
<Text category="p1" key={i}>
{translate('news.notificationTitle', {
header: newsItem.header,
published: displayDate(newsItem.published),
})}
</Text>
))}
{scheduleAndCalendarThisWeek.length ||
notificationsThisWeek.length ||
newsThisWeek.length ? null : (
<Text category="p1" style={styles.noNewNewsItemsText}>
{translate('news.noNewNewsItemsThisWeek')}
</Text>
)}
<View style={styles.itemFooterAbsence}>
<Button
accessible
accessibilityRole="button"
accessibilityLabel={`${child.name}, ${translate('abscense.title')}`}
size="small"
2021-06-20 23:12:25 +00:00
status="basic"
onPress={() => navigation.navigate('Absence', { child })}
>
{translate('abscense.title')}
</Button>
</View>
</View>
</TouchableOpacity>
2021-01-04 21:40:54 +00:00
)
}
const themeStyles = StyleService.create({
2021-01-04 21:40:54 +00:00
card: {
borderRadius: 25,
padding: Sizing.t5,
marginBottom: Sizing.t4,
backgroundColor: 'background-basic-color-1',
2021-01-04 21:40:54 +00:00
},
cardHeader: {
...Layout.flex.row,
...Layout.mainAxis.center,
...Layout.crossAxis.spaceBetween,
marginBottom: Sizing.t4,
},
cardHeaderLeft: {
...Layout.flex.row,
...Layout.mainAxis.center,
flex: 1,
},
cardHeaderText: {
marginHorizontal: Sizing.t4,
flex: 1,
},
2021-01-04 21:40:54 +00:00
itemFooter: {
...Layout.flex.row,
marginTop: Sizing.t4,
2021-01-05 11:27:38 +00:00
},
itemFooterAbsence: {
...Layout.mainAxis.flexStart,
marginTop: Sizing.t4,
},
item: {
marginRight: 12,
paddingHorizontal: 2,
paddingVertical: 0,
marginBottom: 0,
},
noNewNewsItemsText: {},
2021-01-04 21:40:54 +00:00
})