Skip to content

Commit

Permalink
Add remove file logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sharath-wmca committed Jan 29, 2024
1 parent 45b0705 commit f5789bb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
16 changes: 8 additions & 8 deletions src/components/App/Form/Summary/Sections/SendRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const SendYourRequest = () => {
const fileData = [];
const applicationNumber = Math.floor(Math.random() * 10000000 + 1).toString();
const files = [
formDataState.ApplicantPhoto,
...formDataState.proofDocumentArms,
...formDataState.proofDocumentBlind,
...formDataState.proofDocumentDeaf,
...formDataState.proofDocumentDrive,
...formDataState.proofDocumentLanguage,
...formDataState.proofDocumentLearn,
...formDataState.proofDocumentWalk,
[formDataState.ApplicantPhoto],
formDataState.proofDocumentArms ? [...formDataState.proofDocumentArms] : [],
formDataState.proofDocumentBlind ? [...formDataState.proofDocumentBlind] : [],
formDataState.proofDocumentDeaf ? [...formDataState.proofDocumentDeaf] : [],
formDataState.proofDocumentDrive ? [...formDataState.proofDocumentDrive] : [],
formDataState.proofDocumentLanguage ? [...formDataState.proofDocumentLanguage] : [],
formDataState.proofDocumentLearn ? [...formDataState.proofDocumentLearn] : [],
formDataState.proofDocumentWalk ? [...formDataState.proofDocumentWalk] : [],
];
const checkAnswersEl = document.getElementById('application-summary');
// remove change button
Expand Down
71 changes: 35 additions & 36 deletions src/components/shared/MultiFileUpload/MultiFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ import { Icon } from 'components/shared';
import s from './MultiFileUpload.module.scss';
import { TMultiFileUploadProps } from './MultiFileUpload.types';

// prettier-ignore

const FileUpload = ({
name,
label,
hint,
defaultFiles,
updateFiles,
removeFile,
error,
accept,
}: TMultiFileUploadProps): JSX.Element => {
const [isFileInputFocused, setIsFileInputFocused] = useState(false); // This is used to emulate the input focus class on the label
const handleFocus = () => setIsFileInputFocused(true);
const handleBlur = () => setIsFileInputFocused(false);

// const dFiles = defaultFiles ? [...defaultFiles] : [];
const selectedFiles = defaultFiles ? [...defaultFiles] : [];

// const previewUrl = defaultFile ? URL.createObjectURL(defaultFile) : undefined;

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const fileList = e.target?.files;
const files = fileList ? [...fileList] : [];
const files = fileList ? [...selectedFiles, ...fileList] : selectedFiles;

if (!files) return;
updateFiles(files);
Expand All @@ -49,14 +48,12 @@ const FileUpload = ({

<label
htmlFor={name}
className={`wmnds-btn wmnds-btn--primary ${isFileInputFocused ? s.fileUploadLabelFocused : ''
}`}
className={`wmnds-btn wmnds-btn--primary ${
isFileInputFocused ? s.fileUploadLabelFocused : ''
}`}
>
Choose file
<Icon
className="wmnds-btn__icon wmnds-btn__icon--right"
iconName="general-paperclip"
/>
<Icon className="wmnds-btn__icon wmnds-btn__icon--right" iconName="general-paperclip" />
<input
type="file"
name={name}
Expand All @@ -69,33 +66,35 @@ const FileUpload = ({
multiple
/>
</label>
<span className="wmnds-m-l-md">No files selected</span>

{defaultFiles ? (defaultFiles.map((file, i) => (
// <li key={i}>
// {file.name} - {file.type}
// </li>
<>
<div key={i} className={`${s.fileUploadUploaded}`}>
<button
className="wmnds-btn wmnds-btn--destructive"
type="button"
name={i.toString()}
id={i.toString()}
title="Remove uploaded file"
onClick={() => updateFiles(null)}
>
Remove file
<Icon className="wmnds-btn__icon wmnds-btn__icon--right" iconName="general-trash" />
</button>
<span className="wmnds-m-l-md">{file.name}</span>
</div>
<div className="wmnds-m-t-lg">
<img className={s.fileUploadPreview} src={URL.createObjectURL(file)} alt="preview" />
</div>
</>
))) : (<></>)}
<span className="wmnds-m-l-md">
{selectedFiles.length > 0 ? 'Add more files' : 'No files selected'}
</span>

{defaultFiles ? (
defaultFiles.map((file, i) => (
<>
<div key={i} className={`wmnds-m-t-md ${s.fileUploadUploaded}`}>
<button
className="wmnds-btn wmnds-btn--destructive"
type="button"
name={i.toString()}
id={i.toString()}
title="Remove uploaded file"
onClick={() => removeFile(file)}
>
Remove file
<Icon
className="wmnds-btn__icon wmnds-btn__icon--right"
iconName="general-trash"
/>
</button>
<span className="wmnds-m-l-md">{file.name}</span>
</div>
</>
))
) : (
<></>
)}
</div>
</fieldset>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export type TMultiFileUploadProps = {
hint?: string;
defaultFiles?: Nullable<File[]>;
updateFiles: (files: Nullable<File[]>) => void;
removeFile: (file: File) => void;
accept?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ const DisabilityProofStep = ({
handleNavigation();
}
};
const deleteFile = (file: File) => {
if (identityDocument.currentValue !== null) {
const array = [...identityDocument.currentValue];
const index = identityDocument.currentValue.indexOf(file);
if (index > -1) {
array.splice(index, 1);
identityDocument.set(array);
}
}
};

// const one = !question.includes('learn') ? 'one' : 'at least two';
return (
<Question
Expand Down Expand Up @@ -159,6 +170,7 @@ const DisabilityProofStep = ({
name={`${dataCategoryPrefix}proof`}
defaultFiles={identityDocument.currentValue}
updateFiles={identityDocument.set}
removeFile={deleteFile}
error={identityDocument.error}
aria-label="Files must be jpeg, png or pdf file format"
/>
Expand Down

0 comments on commit f5789bb

Please sign in to comment.