mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-02 16:09:08 +02:00
If need be (#168)
This commit is contained in:
parent
6375e80641
commit
17dc9519d2
48 changed files with 1033 additions and 642 deletions
61
components/poll/manage-poll/use-csv-exporter.ts
Normal file
61
components/poll/manage-poll/use-csv-exporter.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { format } from "date-fns";
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import { usePoll } from "@/components/poll-context";
|
||||
|
||||
export const useCsvExporter = () => {
|
||||
const { poll, options } = usePoll();
|
||||
const { t } = useTranslation("app");
|
||||
|
||||
return {
|
||||
exportToCsv: () => {
|
||||
const header = [
|
||||
t("participantCount", {
|
||||
count: poll.participants.length,
|
||||
}),
|
||||
...options.map((decodedOption) => {
|
||||
const day = `${decodedOption.dow} ${decodedOption.day} ${decodedOption.month}`;
|
||||
return decodedOption.type === "date"
|
||||
? day
|
||||
: `${day} ${decodedOption.startTime} - ${decodedOption.endTime}`;
|
||||
}),
|
||||
].join(",");
|
||||
const rows = poll.participants.map((participant) => {
|
||||
return [
|
||||
participant.name,
|
||||
...poll.options.map((option) => {
|
||||
const vote = participant.votes.find((vote) => {
|
||||
return vote.optionId === option.id;
|
||||
});
|
||||
|
||||
switch (vote?.type) {
|
||||
case "yes":
|
||||
return t("yes");
|
||||
case "ifNeedBe":
|
||||
return t("ifNeedBe");
|
||||
default:
|
||||
return t("no");
|
||||
}
|
||||
}),
|
||||
].join(",");
|
||||
});
|
||||
const csv = `data:text/csv;charset=utf-8,${[header, ...rows].join(
|
||||
"\r\n",
|
||||
)}`;
|
||||
|
||||
const encodedCsv = encodeURI(csv);
|
||||
const link = document.createElement("a");
|
||||
link.setAttribute("href", encodedCsv);
|
||||
link.setAttribute(
|
||||
"download",
|
||||
`${poll.title.replace(/\s/g, "_")}-${format(
|
||||
Date.now(),
|
||||
"yyyyMMddhhmm",
|
||||
)}`,
|
||||
);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue