|
| 1 | +import { useContext, useState } from 'react'; |
| 2 | +import NextLink from 'next/link'; |
| 3 | +import { useRouter } from 'next/router'; |
| 4 | +import { |
| 5 | + Button, |
| 6 | + Card, |
| 7 | + CardHeader, |
| 8 | + List, |
| 9 | + ListItem, |
| 10 | + ListItemSecondaryAction, |
| 11 | + ListItemText, |
| 12 | +} from '@material-ui/core'; |
| 13 | +import { Chapter, SnackbarAction } from '@zoonk/models'; |
| 14 | +import { firebaseError, GlobalContext, theme, timestamp } from '@zoonk/utils'; |
| 15 | +import { addChapterToTopic } from '@zoonk/services'; |
| 16 | +import Snackbar from './Snackbar'; |
| 17 | + |
| 18 | +interface ChapterSelectorProps { |
| 19 | + chapters: ReadonlyArray<Chapter.Index>; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Chapter list with an option to add it to the current topic. |
| 24 | + */ |
| 25 | +const ChapterSelector = ({ chapters }: ChapterSelectorProps) => { |
| 26 | + const { profile, translate, user } = useContext(GlobalContext); |
| 27 | + const [snackbar, setSnackbar] = useState<SnackbarAction | null>(null); |
| 28 | + const { query, push } = useRouter(); |
| 29 | + const topicId = String(query.id); |
| 30 | + |
| 31 | + if (chapters.length === 0 || !profile || !user) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + const add = (id: string) => { |
| 36 | + setSnackbar({ type: 'progress', msg: translate('chapter_adding') }); |
| 37 | + const metadata = { |
| 38 | + updatedAt: timestamp, |
| 39 | + updatedBy: profile, |
| 40 | + updatedById: user.uid, |
| 41 | + }; |
| 42 | + |
| 43 | + addChapterToTopic(id, topicId, metadata) |
| 44 | + .then(() => |
| 45 | + push( |
| 46 | + '/topics/[topicId]/chapters/[chapterId]', |
| 47 | + `/topics/${topicId}/chapters/${id}`, |
| 48 | + ), |
| 49 | + ) |
| 50 | + .catch((e) => setSnackbar(firebaseError(e, 'add_existing_chapter'))); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Card |
| 55 | + variant="outlined" |
| 56 | + style={{ width: '100%', margin: theme.spacing(1) }} |
| 57 | + > |
| 58 | + <CardHeader |
| 59 | + title={translate('chapter_select_title')} |
| 60 | + subheader={translate('chapter_select_desc')} |
| 61 | + /> |
| 62 | + <List> |
| 63 | + {chapters.map((chapter, index) => ( |
| 64 | + <ListItem |
| 65 | + key={chapter.objectID} |
| 66 | + divider={chapters.length > index + 1} |
| 67 | + > |
| 68 | + <ListItemText primary={chapter.title} /> |
| 69 | + <ListItemSecondaryAction> |
| 70 | + <NextLink |
| 71 | + href="/topics/[id]/chapters/[chapterId]" |
| 72 | + as={`/topics/${topicId}/chapters/${chapter.objectID}`} |
| 73 | + passHref |
| 74 | + > |
| 75 | + <Button |
| 76 | + component="a" |
| 77 | + target="_blank" |
| 78 | + rel="noopener noreferrer" |
| 79 | + color="primary" |
| 80 | + > |
| 81 | + {translate('view')} |
| 82 | + </Button> |
| 83 | + </NextLink> |
| 84 | + <Button color="secondary" onClick={() => add(chapter.objectID)}> |
| 85 | + {translate('add')} |
| 86 | + </Button> |
| 87 | + </ListItemSecondaryAction> |
| 88 | + </ListItem> |
| 89 | + ))} |
| 90 | + </List> |
| 91 | + |
| 92 | + <Snackbar action={snackbar} /> |
| 93 | + </Card> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default ChapterSelector; |
0 commit comments