mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
deleted redundant pages
This commit is contained in:
parent
acd018daaf
commit
1989f07c4b
3 changed files with 1 additions and 549 deletions
|
|
@ -1,264 +0,0 @@
|
|||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { SelectFolder } from "../components/due-considerations/SelectFolder";
|
||||
import { Button } from "../shadcn_components/ui/button";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { Input } from "../shadcn_components/ui/input";
|
||||
|
||||
const Spinner = () => {
|
||||
return (
|
||||
<div className="w-16 h-16 border-t-4 border-brandgold border-solid rounded-full animate-spin"></div>
|
||||
);
|
||||
};
|
||||
|
||||
function generateDueConsiderationsS3Folder(userId: string) {
|
||||
const timestamp = new Date().toISOString().replace(/[:.-]/g, "");
|
||||
const key = `${userId}/${timestamp}/`;
|
||||
return key;
|
||||
}
|
||||
|
||||
async function postDueConsiderations(
|
||||
userId: string,
|
||||
folderKey: string,
|
||||
schemeName: string
|
||||
) {
|
||||
// Triggers the due considerations process
|
||||
const body = JSON.stringify({
|
||||
userId: userId,
|
||||
folderKey: folderKey,
|
||||
scheme: schemeName,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/due-considerations`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: body,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
// Handle the response as needed
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Handle the error appropriately
|
||||
}
|
||||
}
|
||||
|
||||
const useUploadFiles = ({
|
||||
dueConsiderationsFiles,
|
||||
userId,
|
||||
schemeName,
|
||||
setDownloadUrl,
|
||||
}: {
|
||||
dueConsiderationsFiles: File[];
|
||||
userId: string;
|
||||
schemeName: string;
|
||||
setDownloadUrl: React.Dispatch<React.SetStateAction<string>>;
|
||||
}) => {
|
||||
const { mutate: mutateUploadFiles, isLoading: isUploadLoading } = useMutation(
|
||||
uploadFilesToS3,
|
||||
{
|
||||
onSuccess: async () => {
|
||||
console.log("Trigger the due considerations process");
|
||||
console.log("Folder key: ", folderKey);
|
||||
const data = await postDueConsiderations(userId, folderKey, schemeName);
|
||||
setDownloadUrl(data.download_url);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const { mutate, isLoading: isGeneratingUrlLoading } = useMutation(
|
||||
generatePresignedUrls,
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
try {
|
||||
const response = mutateUploadFiles({
|
||||
presignedUrls: data.urls,
|
||||
files: dueConsiderationsFiles,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const [folderKey, setFolderKey] = useState("");
|
||||
|
||||
const handleUpload = (newFolderKey: string) => {
|
||||
setFolderKey(newFolderKey);
|
||||
mutate({ folderKey: newFolderKey, files: dueConsiderationsFiles });
|
||||
};
|
||||
|
||||
return {
|
||||
handleUpload,
|
||||
isGeneratingUrlLoading,
|
||||
isUploadLoading,
|
||||
};
|
||||
};
|
||||
|
||||
async function generatePresignedUrls({
|
||||
folderKey,
|
||||
files,
|
||||
}: {
|
||||
folderKey: string;
|
||||
files: File[];
|
||||
}) {
|
||||
const body = JSON.stringify({
|
||||
files: files.map((file) => ({
|
||||
fileKey: folderKey + file.name,
|
||||
contentType: file.type,
|
||||
})),
|
||||
});
|
||||
|
||||
const presignedResponse = await fetch("/api/upload/due-considerations", {
|
||||
method: "POST",
|
||||
body: body,
|
||||
});
|
||||
|
||||
if (!presignedResponse.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
const presignedUrls = await presignedResponse.json();
|
||||
return presignedUrls;
|
||||
}
|
||||
|
||||
async function uploadFilesToS3({
|
||||
presignedUrls,
|
||||
files,
|
||||
}: {
|
||||
presignedUrls: string[];
|
||||
files: File[];
|
||||
}) {
|
||||
await Promise.all(
|
||||
files.map((file, index) => {
|
||||
return fetch(presignedUrls[index], {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": file.type,
|
||||
},
|
||||
body: file,
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export default function DueConsiderationsHome() {
|
||||
const [dueConsiderationsFiles, setDueConsiderationFile] = useState<File[]>(
|
||||
[]
|
||||
);
|
||||
const [buttonDisabled, setButtonDisabled] = useState(true);
|
||||
const [uploadMessage, setUploadMessage] = useState("");
|
||||
const [schemeName, setSchemeName] = useState("");
|
||||
const [downloadUrl, setDownloadUrl] = useState("");
|
||||
|
||||
const session = useSession();
|
||||
const userId = String(session.data?.user.dbId);
|
||||
|
||||
const { handleUpload, isGeneratingUrlLoading, isUploadLoading } =
|
||||
useUploadFiles({
|
||||
dueConsiderationsFiles,
|
||||
userId,
|
||||
schemeName,
|
||||
setDownloadUrl,
|
||||
});
|
||||
|
||||
const initiateUpload = () => {
|
||||
setDownloadUrl("");
|
||||
const newFolderKey = generateDueConsiderationsS3Folder(userId);
|
||||
handleUpload(newFolderKey);
|
||||
};
|
||||
|
||||
function handleOnChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
if (e.target.files && e.target.files.length === 3) {
|
||||
const filesArray = Array.from(e.target.files);
|
||||
const extensions = filesArray.map((file) =>
|
||||
file.name.split(".").pop()?.toLowerCase()
|
||||
);
|
||||
|
||||
if (
|
||||
extensions.includes("xml") &&
|
||||
extensions.includes("pdf") &&
|
||||
extensions.includes("docx")
|
||||
) {
|
||||
setDueConsiderationFile(filesArray);
|
||||
setButtonDisabled(false);
|
||||
setUploadMessage("");
|
||||
} else {
|
||||
setUploadMessage("Please select a .xml, .pdf, and .docx file.");
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
} else {
|
||||
setUploadMessage("Please select exactly 3 files.");
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center mt-20 tracking-wider leading-loose">
|
||||
<div className="text-center">
|
||||
<div className="mb-4">Select a folder containing:</div>
|
||||
<ul className="list-disc list-inside text-left ml-8 mb-8">
|
||||
<li>A full SAP xml</li>
|
||||
<li>EPR pdf</li>
|
||||
<li>Condition report word document</li>
|
||||
</ul>
|
||||
<div className="mb-4">
|
||||
Make sure these documents all relate to the same property
|
||||
</div>
|
||||
|
||||
<div className="mb-5">
|
||||
<SelectFolder handleOnChange={handleOnChange} />
|
||||
</div>
|
||||
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="Choose an optional scheme name"
|
||||
onChange={(e) => setSchemeName(e.target.value)}
|
||||
/>
|
||||
|
||||
<div className="mb-4"></div>
|
||||
|
||||
<div className="flex justify-between w-full">
|
||||
<div>{uploadMessage}</div>
|
||||
<Button
|
||||
disabled={
|
||||
buttonDisabled || isGeneratingUrlLoading || isUploadLoading
|
||||
}
|
||||
className="bg-brandblue hover:bg-hoverblue"
|
||||
onClick={initiateUpload}
|
||||
>
|
||||
{isGeneratingUrlLoading || isUploadLoading
|
||||
? "Loading..."
|
||||
: "Upload"}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex items-center justify-center">
|
||||
{isGeneratingUrlLoading || isUploadLoading ? (
|
||||
<Spinner />
|
||||
) : downloadUrl ? (
|
||||
<a href={downloadUrl} className="text-blue-500 hover:underline">
|
||||
Download Due Considerations
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,284 +0,0 @@
|
|||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { SelectFolder } from "../components/due-considerations/SelectFolder";
|
||||
import { Button } from "../shadcn_components/ui/button";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
|
||||
const Spinner = () => {
|
||||
return (
|
||||
<div className="w-16 h-16 border-t-4 border-brandgold border-solid rounded-full animate-spin"></div>
|
||||
);
|
||||
};
|
||||
|
||||
function generateEcoSpreadsheetS3Folder(userId: string) {
|
||||
const timestamp = new Date().toISOString().replace(/[:.-]/g, "");
|
||||
const key = `${userId}/${timestamp}/`;
|
||||
return key;
|
||||
}
|
||||
|
||||
async function postEcoSpreadsheet(userId: string, folderKey: string) {
|
||||
// Triggers the eco spreadsheet process
|
||||
const body = JSON.stringify({
|
||||
userId: userId,
|
||||
folderKey: folderKey,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/eco-spreadsheet`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: body,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
// Handle the response as needed
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Handle the error appropriately
|
||||
}
|
||||
}
|
||||
|
||||
const useUploadFiles = ({
|
||||
files,
|
||||
userId,
|
||||
setDownloadUrl,
|
||||
}: {
|
||||
files: File[];
|
||||
userId: string;
|
||||
setDownloadUrl: React.Dispatch<React.SetStateAction<string>>;
|
||||
}) => {
|
||||
const { mutate: mutateUploadFiles, isLoading: isUploadLoading } = useMutation(
|
||||
uploadFilesToS3,
|
||||
{
|
||||
onSuccess: async () => {
|
||||
console.log("Trigger the eco spreadsheet process");
|
||||
console.log("Folder key: ", folderKey);
|
||||
const data = await postEcoSpreadsheet(userId, folderKey);
|
||||
setDownloadUrl(data.download_url);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const { mutate, isLoading: isGeneratingUrlLoading } = useMutation(
|
||||
generatePresignedUrls,
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
try {
|
||||
console.log("Trying to generate presigned urls");
|
||||
const response = mutateUploadFiles({
|
||||
presignedUrls: data.urls,
|
||||
files: files,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const [folderKey, setFolderKey] = useState("");
|
||||
|
||||
const handleUpload = (newFolderKey: string) => {
|
||||
setFolderKey(newFolderKey);
|
||||
mutate({ folderKey: newFolderKey, files: files });
|
||||
};
|
||||
|
||||
return {
|
||||
handleUpload,
|
||||
isGeneratingUrlLoading,
|
||||
isUploadLoading,
|
||||
};
|
||||
};
|
||||
|
||||
async function generatePresignedUrls({
|
||||
folderKey,
|
||||
files,
|
||||
}: {
|
||||
folderKey: string;
|
||||
files: File[];
|
||||
}) {
|
||||
const body = JSON.stringify({
|
||||
files: files.map((file) => ({
|
||||
fileKey: folderKey + file.name,
|
||||
contentType: file.type,
|
||||
})),
|
||||
});
|
||||
|
||||
const presignedResponse = await fetch("/api/upload/eco-spreadsheet", {
|
||||
method: "POST",
|
||||
body: body,
|
||||
});
|
||||
|
||||
if (!presignedResponse.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
const presignedUrls = await presignedResponse.json();
|
||||
return presignedUrls;
|
||||
}
|
||||
|
||||
async function uploadFilesToS3({
|
||||
presignedUrls,
|
||||
files,
|
||||
}: {
|
||||
presignedUrls: string[];
|
||||
files: File[];
|
||||
}) {
|
||||
await Promise.all(
|
||||
files.map((file, index) => {
|
||||
return fetch(presignedUrls[index], {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": file.type,
|
||||
},
|
||||
body: file,
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export default function EcoSpreadsheetHome() {
|
||||
const [files, setFiles] = useState<File[]>([]);
|
||||
const [buttonDisabled, setButtonDisabled] = useState(true);
|
||||
const [uploadMessage, setUploadMessage] = useState("");
|
||||
const [downloadUrl, setDownloadUrl] = useState("");
|
||||
|
||||
const session = useSession();
|
||||
const userId = String(session.data?.user.dbId);
|
||||
|
||||
const { handleUpload, isGeneratingUrlLoading, isUploadLoading } =
|
||||
useUploadFiles({
|
||||
files,
|
||||
userId,
|
||||
setDownloadUrl,
|
||||
});
|
||||
|
||||
const initiateUpload = () => {
|
||||
setDownloadUrl("");
|
||||
const newFolderKey = generateEcoSpreadsheetS3Folder(userId);
|
||||
handleUpload(newFolderKey);
|
||||
};
|
||||
|
||||
function handleOnChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
if (e.target.files && e.target.files.length === 3) {
|
||||
const filesArray = Array.from(e.target.files);
|
||||
const extensions = filesArray.map((file) =>
|
||||
file.name.split(".").pop()?.toLowerCase()
|
||||
);
|
||||
const names = filesArray.map((file) => file.name.toLowerCase());
|
||||
|
||||
if (
|
||||
extensions.includes("xml") &&
|
||||
extensions.includes("pdf") &&
|
||||
names.some((name) => name.includes("epr")) &&
|
||||
names.some((name) => name.includes("ventilation"))
|
||||
) {
|
||||
setFiles(filesArray);
|
||||
setButtonDisabled(false);
|
||||
setUploadMessage("");
|
||||
} else {
|
||||
setUploadMessage(
|
||||
"Please select the xml, the epr and the ventilation and condition report"
|
||||
);
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
} else {
|
||||
setUploadMessage("Please select exactly 3 files.");
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
function handleOnChangeExcel(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
if (e.target.files && e.target.files.length === 1) {
|
||||
const ExcelfilesArray = Array.from(e.target.files);
|
||||
const extensions = ExcelfilesArray.map((file) =>
|
||||
file.name.split(".").pop()?.toLowerCase()
|
||||
);
|
||||
|
||||
if (extensions.includes("xlsx")) {
|
||||
// Append the excel onto the existing files
|
||||
setFiles((prevFiles) => [...prevFiles, ...ExcelfilesArray]);
|
||||
setUploadMessage("");
|
||||
} else {
|
||||
setUploadMessage("Please select the existing ECO spreadsheet excel");
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
} else {
|
||||
setUploadMessage("Please select exactly one Excel file");
|
||||
setButtonDisabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center mt-20 tracking-wider leading-loose">
|
||||
<div className="text-center">
|
||||
<div className="mb-4">Please select the following files:</div>
|
||||
<ul className="list-disc list-inside text-left ml-8 mb-8">
|
||||
<li>An xml</li>
|
||||
<li>EPR pdf</li>
|
||||
<li>Ventilation and Condition pdf</li>
|
||||
</ul>
|
||||
|
||||
<div className="mb-4">
|
||||
Make sure these documents all relate to the same property
|
||||
</div>
|
||||
|
||||
<div className="mb-5">
|
||||
<SelectFolder handleOnChange={handleOnChange} />
|
||||
</div>
|
||||
|
||||
<div className="text-red-500 mb-2">{uploadMessage}</div>
|
||||
|
||||
<div className="mb-4 text-left">
|
||||
Additionally, you can upload an optional ECO Excel spreadsheet
|
||||
<br />
|
||||
if you wish to add new records to an already populated spreadsheet
|
||||
</div>
|
||||
|
||||
<div className="mb-5">
|
||||
<SelectFolder handleOnChange={handleOnChangeExcel} accept={".xlsx"} />
|
||||
</div>
|
||||
|
||||
<div className="mb-4"></div>
|
||||
|
||||
<div className="flex justify-between w-full">
|
||||
<Button
|
||||
disabled={
|
||||
buttonDisabled || isGeneratingUrlLoading || isUploadLoading
|
||||
}
|
||||
className="bg-brandblue hover:bg-hoverblue"
|
||||
onClick={initiateUpload}
|
||||
>
|
||||
{isGeneratingUrlLoading || isUploadLoading
|
||||
? "Loading..."
|
||||
: "Upload"}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex items-center justify-center">
|
||||
{isGeneratingUrlLoading || isUploadLoading ? (
|
||||
<Spinner />
|
||||
) : downloadUrl ? (
|
||||
<a href={downloadUrl} className="text-blue-500 hover:underline">
|
||||
Download Eco Spreadsheet
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -454,7 +454,7 @@ export default function CompletionTrendsChart({
|
|||
dataKey={isStacked ? "_total" : cat}
|
||||
position="top"
|
||||
style={{ fontSize: 10, fill: "#6b7280", fontWeight: 500 }}
|
||||
formatter={(v: number) => (v === 0 ? "" : v)}
|
||||
formatter={(v: unknown) => (v === 0 ? "" : v)}
|
||||
/>
|
||||
)}
|
||||
</Bar>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue