@gpa-gemstone/common-pages
Version:
Common UI pages for GPA products
179 lines (178 loc) • 11.4 kB
JavaScript
;
// ******************************************************************************************************
// BulkUpload.tsx - Gbtc
//
// Copyright © 2024, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/24/2024 - Preston Crawford
// Generated original version of source code.
//
// ******************************************************************************************************
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = BulkUpload;
var React = __importStar(require("react"));
var react_interactive_1 = require("@gpa-gemstone/react-interactive");
var react_forms_1 = require("@gpa-gemstone/react-forms");
var steps = [{ short: 'Upload', long: 'Upload', id: 'Upload' }, { short: 'Process', long: 'Process', id: 'Process' }, { short: "Review", id: 'Review', long: 'Review' }, { short: 'Complete', long: 'Complete', id: 'Complete' }];
var fileExtRegex = /(\.[^.]+)$/;
function BulkUpload(props) {
var _a, _b;
var _c = React.useState([]), data = _c[0], setData = _c[1];
var _d = React.useState([]), pipelineErrors = _d[0], setPipelineErrors = _d[1];
var _e = React.useState(null), fileName = _e[0], setFileName = _e[1];
var _f = React.useState(null), rawFileContent = _f[0], setRawFileContent = _f[1];
var _g = React.useState(null), currentPipelineIndex = _g[0], setCurrentPipelineIndex = _g[1];
var _h = React.useState(true), isFileTypeValid = _h[0], setIsFileTypeValid = _h[1];
var _j = React.useState('Upload'), activeProgressStep = _j[0], setActiveProgressStep = _j[1];
var pipelineUI = React.useMemo(function () {
var _a, _b, _c, _d;
if (props.Step === 'Upload' || props.Step === 'Complete' || currentPipelineIndex == null || currentPipelineIndex > props.Pipelines.length - 1 || props.CurrentPipelineStep > ((_c = (_b = (_a = props.Pipelines) === null || _a === void 0 ? void 0 : _a[currentPipelineIndex]) === null || _b === void 0 ? void 0 : _b.Steps) === null || _c === void 0 ? void 0 : _c.length) - 1)
return React.createElement(React.Fragment, null);
var pipeline = props.Pipelines[currentPipelineIndex].Steps[props.CurrentPipelineStep];
return React.createElement(pipeline.UI, { AdditionalProps: (_d = props.Pipelines[currentPipelineIndex].Steps[props.CurrentPipelineStep]) === null || _d === void 0 ? void 0 : _d.AdditionalProps, RawFileData: rawFileContent, SetData: setData, Data: data, SetPipelineStep: props.SetCurrentPipelineStep, CurrentPipelineStep: props.CurrentPipelineStep, Errors: pipelineErrors, SetErrors: setPipelineErrors });
}, [props.Step, currentPipelineIndex, rawFileContent, props.CurrentPipelineStep, props.Pipelines]);
var progressSteps = React.useMemo(function () {
var _a, _b, _c;
if (props.ProgressBar != null)
return [];
if (currentPipelineIndex == null || currentPipelineIndex > props.Pipelines.length - 1 || props.CurrentPipelineStep > ((_c = (_b = (_a = props.Pipelines) === null || _a === void 0 ? void 0 : _a[currentPipelineIndex]) === null || _b === void 0 ? void 0 : _b.Steps) === null || _c === void 0 ? void 0 : _c.length) - 1)
return steps;
var pipelineSteps = props.Pipelines[currentPipelineIndex].Steps.map(function (step, i) { return ({ short: step.Label, long: step.Label, id: i }); });
var uploadIndex = steps.findIndex(function (step) { return step.short === 'Upload'; });
var progressIndex = steps.findIndex(function (step) { return step.short === 'Process'; });
var remainingSteps = steps.slice(progressIndex + 1);
return [steps[uploadIndex]].concat(pipelineSteps, remainingSteps);
}, [currentPipelineIndex, props.Pipelines]);
React.useEffect(function () {
var _a, _b, _c;
if (props.Step !== 'Process') {
setActiveProgressStep(props.Step);
return;
}
if (currentPipelineIndex == null || currentPipelineIndex > props.Pipelines.length - 1 || props.CurrentPipelineStep > ((_c = (_b = (_a = props.Pipelines) === null || _a === void 0 ? void 0 : _a[currentPipelineIndex]) === null || _b === void 0 ? void 0 : _b.Steps) === null || _c === void 0 ? void 0 : _c.length) - 1)
return;
setActiveProgressStep(props.CurrentPipelineStep); //ids for conditional progressSteps are index of the pipeline steps
}, [props.CurrentPipelineStep, currentPipelineIndex, props.Step]);
React.useEffect(function () {
var pipelineErrs = props.Step == 'Process' ? pipelineErrors : [];
var errors = __spreadArray([], pipelineErrs, true);
if (props.Step === 'Upload') {
if (fileName == null)
errors.push('A file must be uploaded to continue');
if (rawFileContent == null || rawFileContent == '')
errors.push('File content is empty');
if (!isFileTypeValid)
errors.push("File must be of type ".concat(props.FileTypeAttribute));
}
props.SetErrors(errors);
}, [rawFileContent, fileName, isFileTypeValid, pipelineErrors, props.Step]);
React.useEffect(function () {
var _a;
if ((props.Step === 'Review' && ((_a = props.CompleteOnReview) !== null && _a !== void 0 ? _a : false)) || props.Step === 'Complete')
props.OnComplete(data);
}, [props.Step, props.CompleteOnReview, data]);
var handleFileUpload = function (file) {
return new Promise(function (resolve, reject) {
try {
if (file == null) {
reject();
return;
}
var matchArray = file.name.match(fileExtRegex);
var fileExtension_1 = matchArray != null ? matchArray[0].substring(1) : '';
var pipelineIndex = props.Pipelines.findIndex(function (pipe) { return pipe.Select(file.type, fileExtension_1); });
if (pipelineIndex == -1) {
setIsFileTypeValid(false);
reject();
return;
}
setCurrentPipelineIndex(pipelineIndex);
setFileName(file.name);
var fileReader = new FileReader();
fileReader.readAsText(file);
fileReader.onload = function (e) {
if (e.target == null) {
reject();
return;
}
setRawFileContent(e.target.result);
resolve();
};
fileReader.onerror = function () { return reject(); };
}
catch (_a) {
reject();
}
});
};
var handleFileOnClear = function () {
setIsFileTypeValid(true);
setCurrentPipelineIndex(null);
setFileName(null);
setRawFileContent(null);
};
return (React.createElement("div", { className: "container-fluid d-flex flex-column p-0 h-100" },
React.createElement("div", { className: 'row h-100' },
React.createElement("div", { className: 'col-12 d-flex flex-column h-100' },
React.createElement("div", { className: 'row' },
React.createElement("div", { className: 'col-12' }, props.ProgressBar != null ? props.ProgressBar :
React.createElement(react_interactive_1.ProgressBar, { steps: progressSteps, activeStep: activeProgressStep }))),
props.Step === 'Upload' ?
React.createElement(React.Fragment, null,
React.createElement("div", { className: 'row justify-content-center' },
React.createElement("div", { className: 'col-6' },
React.createElement(react_forms_1.FileUpload, { OnLoadHandler: handleFileUpload, OnClearHandler: handleFileOnClear, FileTypeAttribute: props.FileTypeAttribute }))),
React.createElement("div", { className: 'row' },
React.createElement("div", { className: 'col-12 h-100' }, currentPipelineIndex != null && ((_a = props.Pipelines[currentPipelineIndex]) === null || _a === void 0 ? void 0 : _a.AdditionalUploadUI) != null ? (_b = props.Pipelines[currentPipelineIndex]) === null || _b === void 0 ? void 0 : _b.AdditionalUploadUI : null)))
: null,
React.createElement("div", { className: "".concat(props.Step !== 'Process' ? 'd-none' : 'row flex-grow-1'), style: { overflowY: 'hidden' } },
React.createElement("div", { className: 'col-12 h-100' }, pipelineUI)),
props.Step === 'Review' || (props.Step === 'Complete' && props.CompleteUI != null) ?
React.createElement("div", { className: 'row flex-grow-1', style: { overflowY: 'hidden' } },
React.createElement("div", { className: 'col-12 h-100' }, props.Step == 'Review' ? React.createElement(props.ReviewUI, { Data: data }) : props.Step === 'Complete' ? props.CompleteUI : null)) : null))));
}