Skip to content

Commit

Permalink
run the linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Bechthold committed Apr 22, 2024
1 parent 3fd2bdc commit 7d14afc
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 81 deletions.
53 changes: 37 additions & 16 deletions frontend/src/components/forms/ExportLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import {
FormLabel,
Flex,
} from "@chakra-ui/react";
import { ArrowDownIcon, ArrowUpIcon } from '@chakra-ui/icons';
import { ArrowDownIcon, ArrowUpIcon } from "@chakra-ui/icons";
import { TiExport } from "react-icons/ti";
import LogRecordAPIClient from "../../APIClients/LogRecordAPIClient";
import { singleDatePickerStyle } from "../../theme/forms/datePickerStyles";
import { convertLogsToDOCX, convertLogsToCSV } from "../../helper/exportHelpers";
import {
convertLogsToDOCX,
convertLogsToCSV,
} from "../../helper/exportHelpers";
import CreateToast from "../common/Toasts";
import { getFormattedDateAndTime } from "../../helper/dateHelpers";
import { SingleDatepicker } from "../common/Datepicker";
Expand All @@ -51,7 +54,7 @@ const ExportLogs = (): React.ReactElement => {
const handleClear = () => {
setStartDate(undefined);
setEndDate(undefined);
setSortDirection("desc")
setSortDirection("desc");
setDateError(false);
setStartDateError(false);
setEndDateError(false);
Expand Down Expand Up @@ -112,7 +115,7 @@ const ExportLogs = (): React.ReactElement => {
return false;
}
return true;
}
};

const constructDateRange = (): (string | null)[] | undefined => {
let dateRange;
Expand All @@ -126,8 +129,8 @@ const ExportLogs = (): React.ReactElement => {
];
}

return dateRange
}
return dateRange;
};

const handleDocxExport = async () => {
if (!validateDates()) {
Expand Down Expand Up @@ -281,16 +284,29 @@ const ExportLogs = (): React.ReactElement => {
</FormHelperText>
</FormControl>
<FormControl>
<FormLabel mt={4}>Sort Direction</FormLabel>
<FormLabel mt={4}>Sort Direction</FormLabel>
<Flex alignItems="center" gap="10px">
<IconButton aria-label="calendar" size="sm" variant="secondary" fontSize="20px" icon={
sortDirection === "desc" ? <ArrowDownIcon /> : <ArrowUpIcon />
} onClick={() =>
setSortDirection(
sortDirection === "desc" ? "asc" : "desc",
)
}/>
<Text>{sortDirection === "desc" ? "Descending" : "Ascending"}</Text>
<IconButton
aria-label="calendar"
size="sm"
variant="secondary"
fontSize="20px"
icon={
sortDirection === "desc" ? (
<ArrowDownIcon />
) : (
<ArrowUpIcon />
)
}
onClick={() =>
setSortDirection(
sortDirection === "desc" ? "asc" : "desc",
)
}
/>
<Text>
{sortDirection === "desc" ? "Descending" : "Ascending"}
</Text>
</Flex>
</FormControl>
</ModalBody>
Expand All @@ -305,7 +321,12 @@ const ExportLogs = (): React.ReactElement => {
marginRight="10px"
/>
)}
<Button onClick={handleDocxExport} variant="primary" type="submit" marginRight="10px">
<Button
onClick={handleDocxExport}
variant="primary"
type="submit"
marginRight="10px"
>
Export As DOCX
</Button>
<Button onClick={handleCsvExport} variant="primary" type="submit">
Expand Down
143 changes: 78 additions & 65 deletions frontend/src/helper/exportHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,81 @@
import { Document, Packer, Paragraph, Table, TableCell, TableRow } from 'docx';
import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "docx";
import { LogRecord } from "../types/LogRecordTypes";

export enum DocType {
DOCX = "docx",
CSV = "csv",
}

const downloadBlob = (blob: Blob, type: DocType): void => {

const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;

// Get date for file name
// "fr-CA" formats the date into YYYY-MM-DD
const dateToday = new Date();
const dateTodayString = dateToday
.toLocaleString("fr-CA", { timeZone: "America/Toronto" })
.substring(0, 10);

link.setAttribute("download", `log_records_${dateTodayString}.${type}`);
document.body.appendChild(link);
link.click();

// Cleanup created object
document.body.removeChild(link);
URL.revokeObjectURL(url);
}

export const convertLogsToDOCX = async (data: LogRecord[]): Promise<boolean> => {
const downloadBlob = (blob: Blob, type: DocType): void => {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;

// Get date for file name
// "fr-CA" formats the date into YYYY-MM-DD
const dateToday = new Date();
const dateTodayString = dateToday
.toLocaleString("fr-CA", { timeZone: "America/Toronto" })
.substring(0, 10);

link.setAttribute("download", `log_records_${dateTodayString}.${type}`);
document.body.appendChild(link);
link.click();

// Cleanup created object
document.body.removeChild(link);
URL.revokeObjectURL(url);
};

export const convertLogsToDOCX = async (
data: LogRecord[],
): Promise<boolean> => {
try {
// Create a table to display log records
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph('Datetime')] }),
new TableCell({ children: [new Paragraph('Tenants')] }),
new TableCell({ children: [new Paragraph('Note')] }),
new TableCell({ children: [new Paragraph('Employee')] }),
new TableCell({ children: [new Paragraph('Attn Tos')] }),
new TableCell({ children: [new Paragraph('Tags')] }),
new TableCell({ children: [new Paragraph('Flagged')] }),
new TableCell({ children: [new Paragraph('Building')] }),
new TableCell({ children: [new Paragraph("Datetime")] }),
new TableCell({ children: [new Paragraph("Tenants")] }),
new TableCell({ children: [new Paragraph("Note")] }),
new TableCell({ children: [new Paragraph("Employee")] }),
new TableCell({ children: [new Paragraph("Attn Tos")] }),
new TableCell({ children: [new Paragraph("Tags")] }),
new TableCell({ children: [new Paragraph("Flagged")] }),
new TableCell({ children: [new Paragraph("Building")] }),
],
}),
...data.map((record) =>
new TableRow({
children: [
new TableCell({ children: [new Paragraph(record.datetime)] }),
new TableCell({ children: [new Paragraph(record.residents.join(', '))] }),
new TableCell({ children: [new Paragraph(record.note)] }),
new TableCell({ children: [new Paragraph(`${record.employee.firstName} ${record.employee.lastName}`)] }),
new TableCell({ children: [new Paragraph(record.attnTos.join(', '))] }),
new TableCell({ children: [new Paragraph(record.tags.join(', '))] }),
new TableCell({ children: [new Paragraph(record.flagged ? 'Yes' : 'No')] }),
new TableCell({ children: [new Paragraph(record.building.name)] }),
],
})
...data.map(
(record) =>
new TableRow({
children: [
new TableCell({ children: [new Paragraph(record.datetime)] }),
new TableCell({
children: [new Paragraph(record.residents.join(", "))],
}),
new TableCell({ children: [new Paragraph(record.note)] }),
new TableCell({
children: [
new Paragraph(
`${record.employee.firstName} ${record.employee.lastName}`,
),
],
}),
new TableCell({
children: [new Paragraph(record.attnTos.join(", "))],
}),
new TableCell({
children: [new Paragraph(record.tags.join(", "))],
}),
new TableCell({
children: [new Paragraph(record.flagged ? "Yes" : "No")],
}),
new TableCell({
children: [new Paragraph(record.building.name)],
}),
],
}),
),
],
});
Expand All @@ -68,36 +85,32 @@ export const convertLogsToDOCX = async (data: LogRecord[]): Promise<boolean> =>
sections: [
{
children: [table],
}
]
},
],
});

const blob = await Packer.toBlob(doc)
downloadBlob(blob, DocType.DOCX)

return true
const blob = await Packer.toBlob(doc);
downloadBlob(blob, DocType.DOCX);

return true;
} catch (error) {
console.log("Docx creation failed: ", error)

return false
return false;
}
};

export const convertLogsToCSV = (data: LogRecord[]): boolean => {

try {
const csvRows = [];

const headers = [
'Datetime',
'Tenants',
'Note',
'Employee',
'Attn Tos',
'Tags',
'Flagged',
'Building'
"Datetime",
"Tenants",
"Note",
"Employee",
"Attn Tos",
"Tags",
"Flagged",
"Building",
];
csvRows.push(headers.join(","));
data.forEach((log: LogRecord) => {
Expand All @@ -117,10 +130,10 @@ export const convertLogsToCSV = (data: LogRecord[]): boolean => {
const csvContent = csvRows.join("\n");

const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
downloadBlob(blob, DocType.CSV)
downloadBlob(blob, DocType.CSV);

return true;
} catch {
return false;
}
};
};

0 comments on commit 7d14afc

Please sign in to comment.