-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from episerver/feature/AFORM-3696-Save-submiss…
…sion-data-to-sessionStorage Add class to save form submission to storage
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { FormContainer, FormSubmission } from "../models"; | ||
|
||
/** | ||
* Class to manage form submission in session storage | ||
*/ | ||
export class FormStorage { | ||
readonly _form: FormContainer | ||
|
||
constructor(form: FormContainer){ | ||
this._form = form; | ||
} | ||
|
||
/** | ||
* Storage to store EPiForms's data | ||
* @returns the session storage | ||
*/ | ||
getStorage(): Storage { | ||
return window.sessionStorage; | ||
} | ||
|
||
/** | ||
* Save form submission which specified by form key to storage | ||
* @param data | ||
* @returns return the saved form submission | ||
*/ | ||
saveFormDataToStorage(data: FormSubmission[]): FormSubmission[] { | ||
let storage = this.getStorage(); | ||
|
||
try { // safari private mode does not allow local storage | ||
storage.setItem(this._form.key, JSON.stringify(data)); | ||
} catch (e: any) { | ||
console.log("Local Storage not supported: " + e.message); | ||
} | ||
return data; | ||
} | ||
|
||
/** | ||
* Load a specified form submission from storage. | ||
* @returns return form submission | ||
*/ | ||
loadFormDataFromStorage(): FormSubmission[] { | ||
let storage = this.getStorage(), | ||
data = storage[this._form.key]; | ||
if (!data) { | ||
return []; | ||
} | ||
|
||
data = JSON.parse(data); | ||
if (!data) { | ||
return []; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* Clear saved data of specified Form in storage | ||
*/ | ||
removeFormDataInStorage(): void { | ||
this.getStorage().removeItem(this._form.key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./formStorage"; |