minor error handling for excel upload

This commit is contained in:
Khalim Conn-Kowlessar 2025-11-28 08:26:05 +00:00
parent 03d5e06ec0
commit 75da3e81ec
2 changed files with 13 additions and 9 deletions

View file

@ -6,10 +6,12 @@ export function InputFile({
onFileSelect,
isValidating,
isValid,
errorMessage,
}: {
onFileSelect: (file: File) => void;
isValidating: boolean;
isValid: boolean | null;
errorMessage: string | null;
}) {
function handleOnChange(e: React.ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0];
@ -18,14 +20,11 @@ export function InputFile({
const validExtensions = ["csv", "xls", "xlsx"];
const fileExtension = file.name.split(".").pop()?.toLowerCase();
console.log("Extension: ", fileExtension);
if (!fileExtension || !validExtensions.includes(fileExtension)) {
console.error("Unsupported file type");
e.target.value = "";
return;
}
console.log("TRIGGER");
onFileSelect(file);
}
@ -46,9 +45,7 @@ export function InputFile({
)}
{isValid === false && !isValidating && (
<span className="text-sm text-red-500 mt-1">
File validation failed
</span>
<span className="text-sm text-red-500 mt-1">{errorMessage}</span>
)}
</div>
);

View file

@ -168,12 +168,13 @@ export async function validateClientFile(file: File): Promise<{
sheetRowCounts[sheetName] = rowCount;
}
console.log("Sheet Counts", sheetRowCounts);
if (!isStandardised) {
// Explain why the file is not standardised
return {
isValid: false,
error: "Excel file is not a valid domna asset list.",
error:
"Excel file is not a valid domna asset list - either no 'Standardised Asset List' tab or no domna property ID reference.",
file_type: "xlsx",
sheetNames,
sheetRowCounts,
@ -369,6 +370,7 @@ export default function UploadCsvModal({
const [isValidating, setIsValidating] = useState(false);
const [fileIsValid, setFileIsValid] = useState<boolean | null>(null);
const [sheetCounts, setSheetCounts] = useState<Record<string, number>>({});
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const scenarioOptions = useMemo(
() =>
@ -482,6 +484,10 @@ export default function UploadCsvModal({
if (!result.isValid) {
setFileIsValid(false);
// Set validation message
setErrorMessage(
result.error || "File validation failed"
);
setCsvFile(null);
setSheetNames([]);
setSelectedSheet("");
@ -504,6 +510,7 @@ export default function UploadCsvModal({
}}
isValidating={isValidating}
isValid={fileIsValid}
errorMessage={errorMessage}
/>
<a