Skip to content

Commit

Permalink
finishing the first implementation. I will creste a new issue for err…
Browse files Browse the repository at this point in the history
…or handling

Signed-off-by: Ash Evans <[email protected]>
  • Loading branch information
aevo98765 committed Nov 28, 2024
1 parent 81fbf13 commit ebcdba5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 49 deletions.
2 changes: 0 additions & 2 deletions src/components/Contribute/Knowledge/UploadFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ export const UploadFile: React.FunctionComponent<{ onFilesChange: (files: File[]
};

const successfullyReadFileCount = readFileData.filter((fileData) => fileData.loadResult === 'success').length;
console.log('Successfully read file count:', successfullyReadFileCount);
console.log('Current files count:', currentFiles.length);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Knowledge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno
</Title>
</FlexItem>
<FlexItem>
<Button variant="secondary" aria-label="User upload of pre-existing yaml file">
<Button variant="secondary" aria-label="User upload of pre-existing yaml file" onClick={() => setIsModalOpen(true)}>
Upload a YAML file
</Button>
</FlexItem>
Expand Down
1 change: 0 additions & 1 deletion src/components/Contribute/Skill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import SkillsInformation from './SkillsInformation/SkillsInformation';
import SkillsDescriptionContent from './SkillsDescription/SkillsDescriptionContent';
import { autoFillSkillsFields } from './AutoFill';
import { Spinner } from '@patternfly/react-core/dist/dynamic/components/Spinner';
import YamlFileUpload from '../YamlFileUpload';
import { YamlFileUploadModal } from '../YamlFileUploadModal';

export interface SeedExample {
Expand Down
64 changes: 21 additions & 43 deletions src/components/Contribute/YamlFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import yaml from 'js-yaml';
import { KnowledgeYamlData, SkillYamlData } from '@/types';
import { MultipleFileUpload } from '@patternfly/react-core/dist/esm/components/MultipleFileUpload/MultipleFileUpload';
import { MultipleFileUploadMain } from '@patternfly/react-core/dist/esm/components/MultipleFileUpload/MultipleFileUploadMain';
import { MultipleFileUploadStatus } from '@patternfly/react-core/dist/esm/components/MultipleFileUpload/MultipleFileUploadStatus';
import { MultipleFileUploadStatusItem } from '@patternfly/react-core/dist/esm/components/MultipleFileUpload/MultipleFileUploadStatusItem';
import { DropEvent } from '@patternfly/react-core/dist/esm/helpers/typeUtils';
import { UploadIcon } from '@patternfly/react-icons/dist/esm/icons/upload-icon';

Expand All @@ -31,10 +29,10 @@ const YamlFileUpload: React.FC<YamlFileUploadProps> = ({
onYamlUploadSkillsFillForm
}) => {
const [currentFiles, setCurrentFiles] = React.useState<File[]>([]);
const [showStatus, setShowStatus] = React.useState(false);
const [statusIcon, setStatusIcon] = React.useState('inProgress');
const [readFileData, setReadFileData] = React.useState<readFile[]>([]);
const [fileUploadShouldFail, setFileUploadShouldFail] = React.useState(false);
// Implement a failiure condition in a future PR
// const [fileUploadShouldFail, setFileUploadShouldFail] = React.useState(false);
const fileUploadShouldFail = false;

const handleFileInputChange = (file: File) => {
if (file) {
Expand Down Expand Up @@ -100,7 +98,7 @@ const YamlFileUpload: React.FC<YamlFileUploadProps> = ({
if (fileUploadShouldFail) {
const corruptedFiles = files.map((file) => ({ ...file, lastModified: 'foo' as unknown as number }));

setCurrentFiles((prevFiles) => [...prevFiles, ...(corruptedFiles as any)]);
setCurrentFiles((prevFiles) => [...prevFiles, ...corruptedFiles]);
} else {
setCurrentFiles((prevFiles) => [...prevFiles, ...files]);
const latestFile = files.at(-1);
Expand All @@ -125,29 +123,27 @@ const YamlFileUpload: React.FC<YamlFileUploadProps> = ({
.then(() => updateCurrentFiles(droppedFiles));
};

const successfullyReadFileCount = readFileData.filter((fileData) => fileData.loadResult === 'success').length;

// callback called by the status item when a file is successfully read with the built-in file reader
const handleReadSuccess = (data: string, file: File) => {
setReadFileData((prevReadFiles) => [...prevReadFiles, { data, fileName: file.name, loadResult: 'success' }]);
};
// const handleReadSuccess = (data: string, file: File) => {
// setReadFileData((prevReadFiles) => [...prevReadFiles, { data, fileName: file.name, loadResult: 'success' }]);
// };

// callback called by the status item when a file encounters an error while being read with the built-in file reader
const handleReadFail = (error: DOMException, file: File) => {
setReadFileData((prevReadFiles) => [...prevReadFiles, { loadError: error, fileName: file.name, loadResult: 'danger' }]);
};
// // callback called by the status item when a file encounters an error while being read with the built-in file reader
// const handleReadFail = (error: DOMException, file: File) => {
// setReadFileData((prevReadFiles) => [...prevReadFiles, { loadError: error, fileName: file.name, loadResult: 'danger' }]);
// };

// add helper text to a status item showing any error encountered during the file reading process
const createHelperText = (file: File) => {
const fileResult = readFileData.find((readFile) => readFile.fileName === file.name);
if (fileResult?.loadError) {
return (
<HelperText isLiveRegion>
<HelperTextItem variant="error">{fileResult.loadError.toString()}</HelperTextItem>
</HelperText>
);
}
};
// const createHelperText = (file: File) => {
// const fileResult = readFileData.find((readFile) => readFile.fileName === file.name);
// if (fileResult?.loadError) {
// return (
// <HelperText isLiveRegion>
// <HelperTextItem variant="error">{fileResult.loadError.toString()}</HelperTextItem>
// </HelperText>
// );
// }
// };

return (
<>
Expand All @@ -166,24 +162,6 @@ const YamlFileUpload: React.FC<YamlFileUploadProps> = ({
titleTextSeparator="or"
infoText="Accepted file types: YAML"
/>
{showStatus && (
<MultipleFileUploadStatus
statusToggleText={`${successfullyReadFileCount} of ${currentFiles.length} files uploaded`}
statusToggleIcon={statusIcon}
aria-label="Current uploads"
>
{currentFiles.map((file) => (
<MultipleFileUploadStatusItem
file={file}
key={file.name}
onClearClick={() => removeFiles([file.name])}
onReadSuccess={handleReadSuccess}
onReadFail={handleReadFail}
progressHelperText={createHelperText(file)}
/>
))}
</MultipleFileUploadStatus>
)}
</MultipleFileUpload>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Contribute/YamlFileUploadModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Button, Modal, ModalBody, ModalFooter, ModalHeader, ModalVariant, Radio } from '@patternfly/react-core';
import { Modal, ModalBody, ModalFooter, ModalHeader, ModalVariant } from '@patternfly/react-core/dist/dynamic/components/Modal';
import YamlFileUpload from './YamlFileUpload';
import { KnowledgeYamlData, SkillYamlData } from '@/types';

Expand All @@ -18,7 +18,7 @@ export const YamlFileUploadModal: React.FunctionComponent<Props> = ({
onYamlUploadKnowledgeFillForm,
onYamlUploadSkillsFillForm
}) => {
const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => {
const handleModalToggle = () => {
setIsModalOpen(!isModalOpen);
};

Expand Down

0 comments on commit ebcdba5

Please sign in to comment.