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

195 lines
4.6 KiB
TypeScript
Raw Normal View History

import {
List,
ListItem,
ViewPager,
Text,
TabBar,
Tab,
} from '@ui-kitten/components'
import React, { useEffect, useState } from 'react'
2021-04-25 19:40:45 +00:00
import moment from 'moment'
import { StyleSheet, View } from 'react-native'
2021-04-25 19:40:45 +00:00
import { useMenu, useTimetable } from '@skolplattformen/api-hooks'
import { TimetableEntry, Child, MenuItem } from '@skolplattformen/embedded-api'
2021-04-27 13:26:50 +00:00
import { LanguageService } from '../services/languageService'
2021-04-28 12:43:38 +00:00
import { translate } from '../utils/translation'
2021-04-25 19:16:24 +00:00
interface WeekProps {
child: Child
}
interface LessonListProps {
lessons: TimetableEntry[]
header: string
}
interface DayProps {
weekDay: string
lunch?: MenuItem
2021-04-25 19:16:24 +00:00
lessons: TimetableEntry[]
}
2021-04-25 19:16:24 +00:00
const LessonList = ({ lessons, header }: LessonListProps) => (
<List
2021-04-25 19:16:24 +00:00
style={styles.part}
data={lessons}
2021-04-11 19:50:00 +00:00
ListHeaderComponent={() => (
<Text category="c1" style={styles.header}>
{header}
</Text>
)}
2021-04-27 07:45:34 +00:00
renderItem={({
item: { id, name, timeStart, timeEnd, teacher, location },
}) => (
<ListItem
key={id}
style={styles.item}
2021-04-27 07:45:34 +00:00
title={name}
2021-04-25 11:54:18 +00:00
description={`${timeStart.slice(0, 5)}-${timeEnd.slice(0, 5)} ${
2021-04-27 07:45:34 +00:00
location ? `(${location})` : ''
2021-04-25 11:54:18 +00:00
} ${teacher}`}
/>
)}
/>
)
2021-04-25 19:40:45 +00:00
export const Day = ({ weekDay, lunch, lessons }: DayProps) =>
2021-04-25 19:16:24 +00:00
lessons.length ? (
<View style={styles.tab} key={weekDay}>
<View style={styles.summary}>
2021-04-25 19:40:45 +00:00
<Text category="c1" style={styles.startTime}>
2021-04-28 13:35:32 +00:00
{translate('schedule.start', { defaultValue: 'Börjar' })}
2021-04-25 19:16:24 +00:00
</Text>
<Text category="h4">{lessons[0].timeStart.slice(0, 5)}</Text>
<Text category="c1" style={styles.lunchLabel}>
2021-04-28 13:35:32 +00:00
{translate('schedule.lunch', { defaultValue: 'Lunch' })}
</Text>
2021-04-25 20:20:34 +00:00
<Text category="c2" style={styles.lunch}>
{lunch?.description}
2021-04-25 20:20:34 +00:00
</Text>
<Text category="c1" style={styles.endTime}>
2021-04-28 13:35:32 +00:00
{translate('schedule.end', { defaultValue: 'Slutar' })}
</Text>
2021-04-25 19:16:24 +00:00
<Text category="h4">
{lessons[lessons.length - 1].timeEnd.slice(0, 5)}
</Text>
<Text category="c2">
{lessons.some((lesson) => lesson.code === 'IDH')
2021-04-28 13:35:32 +00:00
? `🤼‍♀️ ${translate('schedule.gymBag', {
defaultValue: 'Gympapåse',
})}`
2021-04-25 19:16:24 +00:00
: ''}
</Text>
</View>
<LessonList
header="FM"
lessons={lessons.filter(({ timeStart }) => timeStart < '12:00')}
/>
<LessonList
header="EM"
lessons={lessons.filter(({ timeStart }) => timeStart >= '12:00')}
/>
</View>
) : null
export const Week = ({ child }: WeekProps) => {
2021-04-27 13:26:50 +00:00
moment.locale(LanguageService.getLanguageCode())
const days = moment.weekdaysShort().slice(1, 6)
const [selectedIndex, setSelectedIndex] = useState(0)
2021-04-25 19:16:24 +00:00
const [year, week] = [moment().isoWeekYear(), moment().isoWeek()]
const { data: lessons } = useTimetable(
child,
week,
year,
LanguageService.getLanguageCode()
)
2021-04-25 19:40:45 +00:00
const { data: menu } = useMenu(child)
2021-04-21 21:57:07 +00:00
useEffect(() => {
const updatedSelectedIndex = Math.min(moment().isoWeekday() - 1, 5)
setSelectedIndex(updatedSelectedIndex)
}, [lessons])
return (
2021-04-11 19:50:00 +00:00
<View style={styles.view}>
<TabBar
selectedIndex={selectedIndex}
onSelect={(index) => setSelectedIndex(index)}
>
2021-04-25 19:16:24 +00:00
{days.map((weekDay) => (
<Tab key={weekDay} title={weekDay} />
))}
</TabBar>
2021-04-25 19:16:24 +00:00
<ViewPager
selectedIndex={selectedIndex}
style={styles.pager}
onSelect={(index) => setSelectedIndex(index)}
>
{days.map((weekDay, index) => (
2021-04-25 19:16:24 +00:00
<Day
2021-04-25 19:40:45 +00:00
key={weekDay}
2021-04-25 19:16:24 +00:00
weekDay={weekDay}
lunch={menu[index] || {}}
2021-04-25 19:40:45 +00:00
lessons={lessons
.filter((lesson) => days[lesson.dayOfWeek - 1] === weekDay)
.sort((a, b) => a.dateStart.localeCompare(b.dateStart))}
2021-04-25 19:16:24 +00:00
/>
))}
</ViewPager>
</View>
)
}
const styles = StyleSheet.create({
2021-04-11 19:50:00 +00:00
view: {
backgroundColor: '#fafafa',
},
2021-04-11 20:02:30 +00:00
part: {
backgroundColor: 'transparent',
width: '33%',
},
tab: {
flexDirection: 'row',
padding: 0,
},
item: {
2021-04-11 20:02:30 +00:00
height: 45,
backgroundColor: 'white',
2021-04-11 19:50:00 +00:00
paddingHorizontal: 0,
2021-04-11 20:02:30 +00:00
borderRadius: 2,
margin: 2,
2021-04-11 20:04:59 +00:00
width: '90%',
},
time: {
color: '#333',
fontSize: 9,
},
dayTab: {
textAlign: 'left',
},
2021-04-11 19:50:00 +00:00
summary: {
2021-04-11 20:02:30 +00:00
paddingRight: 20,
2021-04-11 19:50:00 +00:00
paddingLeft: 2,
},
2021-04-25 19:40:45 +00:00
startTime: {
2021-04-25 19:16:24 +00:00
paddingBottom: 2,
},
lunchLabel: {
2021-04-25 19:40:45 +00:00
paddingTop: 10,
paddingBottom: 2,
},
lunch: {
width: 100,
},
2021-04-25 19:40:45 +00:00
endTime: {
paddingTop: 10,
},
2021-04-11 19:50:00 +00:00
pager: {
margin: 10,
},
header: {
paddingLeft: 8,
},
})