|
| 1 | +import React, { useContext } from 'react' |
| 2 | +import getConfig from 'next/config' |
| 3 | +import { useRouter } from 'next/router' |
| 4 | +import jwt from 'jsonwebtoken' |
| 5 | +import SEO from '../../../components/SEO' |
| 6 | +import Text from '../../../components/Text' |
| 7 | +import Input from '../../../components/Input' |
| 8 | +import Checkbox from '../../../components/Checkbox' |
| 9 | +import Button from '../../../components/Button' |
| 10 | +import { withAuthServerSideProps } from '../../../utils/withAuth' |
| 11 | +import { NotificationContext } from '../../../components/Notifications' |
| 12 | + |
| 13 | +const EditAnnouncement = ({ announcement, token, userRole }) => { |
| 14 | + if (userRole !== 'admin') { |
| 15 | + return <Text>You do not have permission to do that.</Text> |
| 16 | + } |
| 17 | + |
| 18 | + const { addNotification } = useContext(NotificationContext) |
| 19 | + |
| 20 | + const router = useRouter() |
| 21 | + |
| 22 | + const { |
| 23 | + publicRuntimeConfig: { SQ_API_URL }, |
| 24 | + } = getConfig() |
| 25 | + |
| 26 | + const handleCreate = async (e) => { |
| 27 | + e.preventDefault() |
| 28 | + const form = new FormData(e.target) |
| 29 | + |
| 30 | + try { |
| 31 | + const updateAnnouncementRes = await fetch( |
| 32 | + `${SQ_API_URL}/announcements/edit/${announcement._id}`, |
| 33 | + { |
| 34 | + method: 'POST', |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json', |
| 37 | + Authorization: `Bearer ${token}`, |
| 38 | + }, |
| 39 | + body: JSON.stringify({ |
| 40 | + title: form.get('title'), |
| 41 | + body: form.get('body'), |
| 42 | + pinned: !!form.get('pinned'), |
| 43 | + }), |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + if (updateAnnouncementRes.status !== 200) { |
| 48 | + const reason = await updateAnnouncementRes.text() |
| 49 | + throw new Error(reason) |
| 50 | + } |
| 51 | + |
| 52 | + addNotification('success', 'Announcement updated successfully') |
| 53 | + |
| 54 | + const slug = await updateAnnouncementRes.text() |
| 55 | + router.push(`/announcements/${slug}`) |
| 56 | + } catch (e) { |
| 57 | + addNotification('error', `Could not update announcement: ${e.message}`) |
| 58 | + console.error(e) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <SEO title="Edit announcement" /> |
| 65 | + <Text as="h1" mb={5}> |
| 66 | + Edit announcement |
| 67 | + </Text> |
| 68 | + <form onSubmit={handleCreate}> |
| 69 | + <Input |
| 70 | + name="title" |
| 71 | + label="Title" |
| 72 | + defaultValue={announcement.title} |
| 73 | + mb={4} |
| 74 | + required |
| 75 | + /> |
| 76 | + <Input |
| 77 | + name="body" |
| 78 | + label="Body" |
| 79 | + placeholder="Markdown supported" |
| 80 | + defaultValue={announcement.body} |
| 81 | + rows={10} |
| 82 | + mb={4} |
| 83 | + required |
| 84 | + /> |
| 85 | + <Checkbox |
| 86 | + label="Pin this announcement?" |
| 87 | + name="pinned" |
| 88 | + inputProps={{ defaultChecked: announcement.pinned }} |
| 89 | + mb={4} |
| 90 | + /> |
| 91 | + <Button display="block" ml="auto"> |
| 92 | + Update announcement |
| 93 | + </Button> |
| 94 | + </form> |
| 95 | + </> |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +export const getServerSideProps = withAuthServerSideProps( |
| 100 | + async ({ token, query: { slug } }) => { |
| 101 | + if (!token) return { props: {} } |
| 102 | + |
| 103 | + const { |
| 104 | + publicRuntimeConfig: { SQ_API_URL }, |
| 105 | + serverRuntimeConfig: { SQ_JWT_SECRET }, |
| 106 | + } = getConfig() |
| 107 | + |
| 108 | + const { role } = jwt.verify(token, SQ_JWT_SECRET) |
| 109 | + |
| 110 | + const announcementRes = await fetch(`${SQ_API_URL}/announcements/${slug}`, { |
| 111 | + headers: { |
| 112 | + 'Content-Type': 'application/json', |
| 113 | + Authorization: `Bearer ${token}`, |
| 114 | + }, |
| 115 | + }) |
| 116 | + const announcement = await announcementRes.json() |
| 117 | + return { props: { announcement, token, userRole: role } } |
| 118 | + } |
| 119 | +) |
| 120 | + |
| 121 | +export default EditAnnouncement |
0 commit comments