Skip to content

Commit

Permalink
Merge pull request #71 from episerver/feature/AFORM-3867-add-prev-upl…
Browse files Browse the repository at this point in the history
…oaded-file

Add previous posted file to FileUploadElement
  • Loading branch information
epi-qang2 authored Jan 4, 2024
2 parents e0cc4f3 + d9afc21 commit 4ab5fdf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FileUpload } from "@episerver/forms-sdk"
import { FileUpload, FormContainer, FormStorage, isNullOrEmpty } from "@episerver/forms-sdk"
import React, { useMemo } from "react";
import ElementWrapper from "./shared/ElementWrapper";
import { useElement } from "../../hooks/useElement";
import { ElementCaption, ValidationMessage } from "./shared";
import { useForms } from "../../context/store";

export interface FileUploadElementBlockProps {
element: FileUpload
Expand All @@ -17,6 +18,12 @@ export const FileUploadElementBlock = (props: FileUploadElementBlockProps) => {
}).join(",") : "";
const { isVisible, validationResults, extraAttr, validatorClasses, elementRef } = elementContext;

const formContext = useForms();
const form = formContext?.formContainer as FormContainer;
const formStorage = new FormStorage(form);
const data = formStorage.loadFormDataFromStorage();
const prevFilenames = data.find(fe => fe.elementKey === element.key)?.prevValue

return useMemo(()=>(
<ElementWrapper className={`FormFileUpload ${validatorClasses}`} validationResults={validationResults} isVisible={isVisible}>
<ElementCaption element={element} />
Expand All @@ -33,9 +40,12 @@ export const FileUploadElementBlock = (props: FileUploadElementBlockProps) => {
onChange={handleChange}
ref={elementRef}
/>
<div className="FormFileUpload__PostedFile"></div>

<div className="FormFileUpload__PostedFile">
(Previous posted file(s): {prevFilenames} )
</div>

<ValidationMessage element={element} validationResults={validationResults} />
</ElementWrapper>
),[isVisible, validationResults]);
}
}
15 changes: 10 additions & 5 deletions src/@episerver/forms-sdk/src/form-submit/formSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ export class FormSubmitter {
formData.append(key + "_file_" + idx, ofile);
}

//always send file name to server if existed to handle case upload file then click back
fileNames += files[idx].name + "|"; // charactor | cannot be used in filename and then is safe for splitting later
// always send file name to server if existed to handle case upload file then click back
// charactor | cannot be used in filename and then is safe for splitting later
if(idx > 0){
fileNames += " | "
}

fileNames += files[idx].name;
}
formData.append(key, fileNames);
data.value = fileNames
data.prevValue = fileNames
}
else {
formData.append(key, data.value);
Expand All @@ -153,8 +158,8 @@ export class FormSubmitter {

// Save data to session storage
let formStorage = new FormStorage(this._form);
let currentData = formStorage.loadFormDataFromStorage()
let dataCombined = this.combineData(currentData, model.submissionData)
let currentData = formStorage.loadFormDataFromStorage();
let dataCombined = this.combineData(currentData, model.submissionData);
formStorage.saveFormDataToStorage(dataCombined);

//init a request and call ajax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
export interface FormSubmission {
elementKey: string
value: any
prevValue?: any
}

0 comments on commit 4ab5fdf

Please sign in to comment.