* Fix sign up form bug * Add run.sh to run setup and run web app * Output node version * Update readme with run.sh * Fix file upload form to handle ota_update service * Enable file upload form Enable error boundary to catch React errors (#7) Fix warning for link noreferrer Include authorization header with file upload * Remove default localhost settings (#8) * Remove default localhost settings Replace with deployment settings * Fix for upload data format * Fix test data for last commit * Fix json link format and remove localhost default settings (#10) * Remove default localhost settings Replace with deployment settings * Fix for upload data format * Fix test data for last commit * Fix link data format * Fix link json again (#12) Use id token instead of access token * nginx things * Web Worker Sign Out and Use Go API (#13) * Calculate checksum and send with file upload * Limit file upload and display rejected file error * Add sign in timeout * Check auth token structure before setting Clean up * Use web worker timer to sign out Remove checksum Point to Go ota update * Remove checksum dependency * Use compute auth service and fix static code analyzer warnings (#15) * Clean up formatting * Use new compute_auth service Implment SSO Implement token refresh Clean up unit tests * Fix unit tests * Fix auth test Fix warnings * Update default settings for compute_auth * Change main UI layout and add VINs to add and upload forms (#16) * Add new upload update package form Add new add vehicle form Add new side menu layout Add new toolbar layout Update and add unit tests * Enable add get and add vehicles * Integration issues with ota_update service * Update get vehicle JSON format * Fix related unit test Add release notes field * Add StatusContext to display error and status messages * Handle api error json (#18) * Handle api error json * Fix get vehicles error handling Update .env.template Co-authored-by: Rafi Greenberg <rgreenberg@fiskerinc.com>
108 lines
2.7 KiB
JavaScript
108 lines
2.7 KiB
JavaScript
import React, { useContext, useState } from "react";
|
|
import { uploadFile, getCancelToken } from "../../services/uploadFile";
|
|
|
|
const FileUploadContext = React.createContext();
|
|
|
|
export const FileUploadProvider = ({ children }) => {
|
|
const [uploading, setUploading] = useState(false);
|
|
const [progress, setProgress] = useState(0);
|
|
const [status, setStatus] = useState(null);
|
|
const [cancelUpload, setCancelUpload] = useState(null);
|
|
const [linkURL, setLinkURL] = useState(null);
|
|
const [files, setFiles] = useState(null);
|
|
|
|
const done = () => {
|
|
setCancelUpload(null);
|
|
setUploading(false);
|
|
setProgress(0);
|
|
};
|
|
|
|
const cancel = async () => {
|
|
if (cancelUpload && progress < 100) {
|
|
cancelUpload.cancel();
|
|
setStatus("Upload cancelled");
|
|
}
|
|
done();
|
|
};
|
|
|
|
const validateUpload = (formData, accessToken, uploadFiles) => {
|
|
if (!formData) {
|
|
throw new Error("Missing package update data");
|
|
}
|
|
|
|
if (!formData.packagename || formData.packagename.length === 0) {
|
|
throw new Error("Package name required");
|
|
}
|
|
|
|
if (!formData.version || formData.version.length === 0) {
|
|
throw new Error("Package update version required");
|
|
}
|
|
|
|
if (!formData.vehicles || formData.vehicles.length === 0) {
|
|
throw new Error("Vehicles required");
|
|
}
|
|
|
|
if (!uploadFiles || uploadFiles.length === 0) {
|
|
throw new Error("File required");
|
|
}
|
|
|
|
if (!accessToken || accessToken.length === 0) {
|
|
throw new Error("Access token required");
|
|
}
|
|
};
|
|
|
|
const upload = async (formData, accessToken, uploadFiles) => {
|
|
validateUpload(formData, accessToken, uploadFiles);
|
|
|
|
try {
|
|
const file = uploadFiles[0];
|
|
const filename = file.name;
|
|
|
|
setUploading(true);
|
|
setLinkURL(null);
|
|
setProgress(0);
|
|
setStatus(`Uploading ${filename}`);
|
|
setCancelUpload(getCancelToken());
|
|
|
|
const { data } = await uploadFile(
|
|
file,
|
|
formData,
|
|
accessToken,
|
|
setProgress,
|
|
cancelUpload
|
|
);
|
|
if (data.message) {
|
|
throw new Error(`${data.error}. ${data.message}`);
|
|
}
|
|
const url = data && data.link ? data.link : "No URL available";
|
|
setLinkURL(url);
|
|
setStatus(`Uploaded ${filename}`);
|
|
setCancelUpload(null);
|
|
setProgress(100);
|
|
} catch (e) {
|
|
setUploading(true);
|
|
setStatus(`Error occured: ${e.message}`);
|
|
setProgress(-1);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<FileUploadContext.Provider
|
|
value={{
|
|
uploading,
|
|
progress,
|
|
status,
|
|
linkURL,
|
|
files,
|
|
upload,
|
|
cancel,
|
|
setFiles,
|
|
}}
|
|
>
|
|
{children}
|
|
</FileUploadContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useFileUploadContext = () => useContext(FileUploadContext);
|