2021-02-14 19:35:30 +00:00
|
|
|
import React, { useEffect } from 'react'
|
2021-02-11 08:20:41 +00:00
|
|
|
import { StyleSheet, View } from 'react-native'
|
|
|
|
import { DateTime } from 'luxon'
|
2021-01-30 17:46:31 +00:00
|
|
|
import moment from 'moment'
|
2021-02-07 16:14:45 +00:00
|
|
|
import {
|
|
|
|
useNotifications,
|
|
|
|
useNews,
|
|
|
|
useClassmates,
|
|
|
|
useCalendar,
|
|
|
|
useMenu,
|
|
|
|
useSchedule,
|
|
|
|
} from '@skolplattformen/api-hooks'
|
2021-02-11 08:20:41 +00:00
|
|
|
import { Button, Icon, Text, Card, Avatar } from '@ui-kitten/components'
|
2021-01-04 21:40:54 +00:00
|
|
|
|
2021-01-26 12:39:22 +00:00
|
|
|
const NotificationsIcon = (props) => (
|
2021-02-07 16:14:45 +00:00
|
|
|
<Icon {...props} name="alert-circle-outline" />
|
2021-01-04 21:40:54 +00:00
|
|
|
)
|
|
|
|
|
2021-02-07 16:14:45 +00:00
|
|
|
const NewsIcon = (props) => <Icon {...props} name="activity-outline" />
|
2021-01-04 21:40:54 +00:00
|
|
|
|
2021-02-07 16:14:45 +00:00
|
|
|
const CalendarIcon = (props) => <Icon {...props} name="calendar-outline" />
|
2021-01-26 12:39:22 +00:00
|
|
|
|
2021-02-07 16:14:45 +00:00
|
|
|
const PeopleIcon = (props) => <Icon {...props} name="people-outline" />
|
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 19:35:30 +00:00
|
|
|
useEffect(() => { }, [child.id])
|
2021-02-11 08:20:41 +00:00
|
|
|
const { data: notifications, status: notificationsStatus } = useNotifications(
|
2021-02-07 16:14:45 +00:00
|
|
|
child,
|
|
|
|
)
|
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)
|
|
|
|
const { data: menu, status: menuStatus } = useMenu(child)
|
|
|
|
const { data: schedule, status: scheduleStatus } = useSchedule(
|
2021-02-07 16:14:45 +00:00
|
|
|
child,
|
|
|
|
DateTime.local(),
|
2021-02-11 08:20:41 +00:00
|
|
|
DateTime.local().plus({ days: 7 }),
|
2021-02-07 16:14:45 +00:00
|
|
|
)
|
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-11 08:20:41 +00:00
|
|
|
<View {...props} style={{ flexDirection: 'row' }}>
|
|
|
|
<View style={{ margin: 20 }}>
|
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-11 08:20:41 +00:00
|
|
|
<View style={{ margin: 20 }}>
|
2021-02-07 16:14:45 +00:00
|
|
|
<Text category="h6">{child.name?.split('(')[0]}</Text>
|
|
|
|
<Text category="s1">{`${getClassName()}`}</Text>
|
2021-01-04 21:40:54 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
const Footer = (props, info) => (
|
|
|
|
<View style={styles.itemFooter}>
|
|
|
|
<Button
|
2021-01-26 12:39:22 +00:00
|
|
|
style={[styles.item, styles[newsStatus]]}
|
2021-02-07 16:14:45 +00:00
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
2021-02-11 08:20:41 +00:00
|
|
|
navigation.navigate('Child', { child, color, selectedTab: 0 })
|
2021-02-07 16:14:45 +00:00
|
|
|
}
|
|
|
|
accessoryLeft={NewsIcon}>
|
2021-01-26 12:39:22 +00:00
|
|
|
{`${(news || []).length}`}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
style={[styles.item, styles[notificationsStatus]]}
|
2021-02-07 16:14:45 +00:00
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
2021-02-11 08:20:41 +00:00
|
|
|
navigation.navigate('Child', { child, color, selectedTab: 1 })
|
2021-02-07 16:14:45 +00:00
|
|
|
}
|
|
|
|
accessoryLeft={NotificationsIcon}>
|
2021-01-26 12:39:22 +00:00
|
|
|
{`${(notifications || []).length}`}
|
2021-01-04 21:40:54 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
2021-01-26 12:39:22 +00:00
|
|
|
style={[styles.item, styles[calendarStatus]]}
|
2021-02-07 16:14:45 +00:00
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
2021-02-11 08:20:41 +00:00
|
|
|
navigation.navigate('Child', { child, color, selectedTab: 2 })
|
2021-02-07 16:14:45 +00:00
|
|
|
}
|
|
|
|
accessoryLeft={CalendarIcon}>
|
2021-01-05 11:27:38 +00:00
|
|
|
{`${(notifications || []).length}`}
|
2021-01-04 21:40:54 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
2021-01-26 12:39:22 +00:00
|
|
|
style={[styles.item, styles[classmatesStatus]]}
|
2021-02-07 16:14:45 +00:00
|
|
|
status="control"
|
|
|
|
size="small"
|
|
|
|
onPress={() =>
|
2021-02-11 08:20:41 +00:00
|
|
|
navigation.navigate('Child', { child, color, selectedTab: 3 })
|
2021-02-07 16:14:45 +00:00
|
|
|
}
|
|
|
|
accessoryLeft={PeopleIcon}>
|
2021-01-26 12:39:22 +00:00
|
|
|
{`${(classmates || []).length}`}
|
2021-01-04 21:40:54 +00:00
|
|
|
</Button>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
2021-02-11 08:20:41 +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-11 08:20:41 +00:00
|
|
|
onPress={() => navigation.navigate('Child', { child, color })}>
|
2021-02-07 16:14:45 +00:00
|
|
|
{[...(calendar ?? []), ...(schedule ?? [])]
|
|
|
|
.filter((a) => moment(a.startDate).isSame('week'))
|
|
|
|
.slice(0, 3)
|
|
|
|
.map((calendarItem, i) => (
|
|
|
|
<Text
|
|
|
|
appearance="hint"
|
|
|
|
category="c1"
|
|
|
|
key={i}
|
2021-02-11 08:20:41 +00:00
|
|
|
style={{ textColor: styles.loaded(notificationsStatus) }}>
|
2021-02-07 16:14:45 +00:00
|
|
|
{`${calendarItem.title}`}
|
|
|
|
</Text>
|
|
|
|
))}
|
2021-01-30 13:07:44 +00:00
|
|
|
|
2021-02-07 16:14:45 +00:00
|
|
|
{notifications
|
|
|
|
.filter((n) => moment(n).isSame('week'))
|
|
|
|
.map((notification, i) => (
|
|
|
|
<Text appearance="hint" category="c1" key={i}>
|
|
|
|
{`${notification.message}`}
|
|
|
|
</Text>
|
|
|
|
))}
|
2021-01-04 21:40:54 +00:00
|
|
|
</Card>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
card: {
|
|
|
|
flex: 1,
|
2021-01-05 11:27:38 +00:00
|
|
|
margin: 10,
|
2021-01-04 21:40:54 +00:00
|
|
|
},
|
|
|
|
itemFooter: {
|
|
|
|
flexDirection: 'row',
|
2021-01-26 12:39:22 +00:00
|
|
|
justifyContent: 'space-evenly',
|
|
|
|
paddingHorizontal: 5,
|
2021-01-04 21:40:54 +00:00
|
|
|
paddingVertical: 5,
|
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-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
|
|
|
})
|