Skip to content

Commit

Permalink
Merge pull request #37 from episerver/feature/AFORM-3696-Save-submiss…
Browse files Browse the repository at this point in the history
…sion-data-to-sessionStorage

Add class to save form submission to storage
  • Loading branch information
hungoptimizely authored Nov 16, 2023
2 parents 672d793 + e57229a commit 4143dd8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/@optimizely/forms-sdk/src/form-storage/formStorage.ts
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);
}
}
1 change: 1 addition & 0 deletions src/@optimizely/forms-sdk/src/form-storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./formStorage";

0 comments on commit 4143dd8

Please sign in to comment.