Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 2x 2x 10x 10x 2x 2x 2x 10x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 10x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 10x 10x 10x 10x 10x 10x 10x 10x | import React from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import AnnouncementForm from "main/components/Announcement/AnnouncementForm";
import { useNavigate, useParams } from "react-router";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
export default function AdminCreateAnnouncementsPage() {
const { commonsId } = useParams();
const navigate = useNavigate();
// Stryker disable all : hard to test query caching
const { data: commonsPlus } = useBackend(
[`/api/commons/plus?id=${commonsId}`],
{
method: "GET",
url: "/api/commons/plus",
params: {
id: commonsId,
},
},
);
// Stryker restore all
const mutation = useBackendMutation(
(params) => ({
url: "/api/announcements/post",
method: "POST",
params,
}),
{
onSuccess: () => {
toast("Announcement created");
navigate(`/admin/announcements/${commonsId}`);
},
},
);
// Stryker disable all : date formatting helper tested through manual Dokku testing
const toBackendDate = (dateTimeLocal) => {
const date = new Date(dateTimeLocal);
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const pad = (n) => String(n).padStart(2, "0");
const timezone = date
.toLocaleTimeString("en-us", { timeZoneName: "short" })
.split(" ")
.pop();
return `${days[date.getDay()]} ${
months[date.getMonth()]
} ${pad(date.getDate())} ${pad(date.getHours())}:${pad(
date.getMinutes(),
)}:00 ${timezone} ${date.getFullYear()}`;
};
// Stryker restore all
const submitAction = (data) => {
const params = {
commonsId: Number(commonsId),
announcementText: data.announcementText,
startDate: toBackendDate(data.startDate),
};
if (data.endDate) {
params.endDate = toBackendDate(data.endDate);
}
mutation.mutate(params);
};
const commonsName = commonsPlus?.commons.name;
return (
<BasicLayout>
<div className="pt-2">
<h1>Create Announcement</h1>
<h2>for Commons: {commonsName}</h2>
<AnnouncementForm submitAction={submitAction} buttonLabel="Create" />
</div>
</BasicLayout>
);
}
|