|
|
@@ -13,40 +13,39 @@ interface GuestQuestionnaireAnswerStatusStorage {
|
|
|
}
|
|
|
|
|
|
const storageKey = 'guestQuestionnaireAnswerStatuses';
|
|
|
-const daysUntilExpiration = 30;
|
|
|
+const DAYS_UNTIL_EXPIRATION = 30;
|
|
|
|
|
|
/**
|
|
|
* Get all answer statuses stored in localStorage as GuestQuestionnaireAnswerStatusStorage,
|
|
|
* and update outdated information.
|
|
|
*/
|
|
|
const getStorage = (): GuestQuestionnaireAnswerStatusStorage | null => {
|
|
|
- if (typeof window !== 'undefined') {
|
|
|
- const currentStorage = localStorage.getItem(storageKey);
|
|
|
- if (currentStorage != null) {
|
|
|
- const storageJson: GuestQuestionnaireAnswerStatusStorage = JSON.parse(currentStorage);
|
|
|
+ if (typeof window === 'undefined') { return null }
|
|
|
|
|
|
- // delete status if outdated to prevent localStorage overflow
|
|
|
- // change skipped to not_answered if different date than when skipped
|
|
|
- Object.keys(storageJson).forEach((key) => {
|
|
|
- const answerStatus = storageJson[key];
|
|
|
- const updatedDate = new Date(answerStatus.updatedDate);
|
|
|
- const expirationDate = new Date(updatedDate.setDate(updatedDate.getDate() + daysUntilExpiration));
|
|
|
- if (expirationDate < new Date()) {
|
|
|
- delete storageJson[key];
|
|
|
- }
|
|
|
- else if (answerStatus.status === StatusType.skipped
|
|
|
- && new Date().toDateString() !== answerStatus.updatedDate) {
|
|
|
- storageJson[key] = {
|
|
|
- status: StatusType.not_answered,
|
|
|
- updatedDate: new Date().toDateString(),
|
|
|
- };
|
|
|
- }
|
|
|
- });
|
|
|
+ const currentStorage = localStorage.getItem(storageKey);
|
|
|
+
|
|
|
+ if (currentStorage == null) { return null }
|
|
|
|
|
|
- return storageJson;
|
|
|
+ const storageJson: GuestQuestionnaireAnswerStatusStorage = JSON.parse(currentStorage);
|
|
|
+ // delete status if outdated to prevent localStorage overflow
|
|
|
+ // change skipped to not_answered if different date than when skipped
|
|
|
+ Object.keys(storageJson).forEach((key) => {
|
|
|
+ const answerStatus = storageJson[key];
|
|
|
+ const updatedDate = new Date(answerStatus.updatedDate);
|
|
|
+ const expirationDate = new Date(updatedDate.setDate(updatedDate.getDate() + DAYS_UNTIL_EXPIRATION));
|
|
|
+ if (expirationDate < new Date()) {
|
|
|
+ delete storageJson[key];
|
|
|
}
|
|
|
- }
|
|
|
- return null;
|
|
|
+ else if (answerStatus.status === StatusType.skipped
|
|
|
+ && new Date().toDateString() !== answerStatus.updatedDate) {
|
|
|
+ storageJson[key] = {
|
|
|
+ status: StatusType.not_answered,
|
|
|
+ updatedDate: new Date().toDateString(),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return storageJson;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -54,22 +53,22 @@ const getStorage = (): GuestQuestionnaireAnswerStatusStorage | null => {
|
|
|
* and save it in localStorage.
|
|
|
*/
|
|
|
const setStatus = (questionnaireOrderId: string, status: StatusType): void => {
|
|
|
- if (typeof window !== 'undefined') {
|
|
|
- const guestQuestionnaireAnswerStatus: GuestQuestionnaireAnswerStatus = {
|
|
|
- status,
|
|
|
- updatedDate: new Date().toDateString(),
|
|
|
- };
|
|
|
+ if (typeof window === 'undefined') { return }
|
|
|
|
|
|
- const storage = getStorage();
|
|
|
+ const guestQuestionnaireAnswerStatus: GuestQuestionnaireAnswerStatus = {
|
|
|
+ status,
|
|
|
+ updatedDate: new Date().toDateString(),
|
|
|
+ };
|
|
|
|
|
|
- if (storage != null) {
|
|
|
- storage[questionnaireOrderId] = guestQuestionnaireAnswerStatus;
|
|
|
- localStorage.setItem(storageKey, JSON.stringify(storage));
|
|
|
- }
|
|
|
- else {
|
|
|
- const initialStorage: GuestQuestionnaireAnswerStatusStorage = { [questionnaireOrderId]: guestQuestionnaireAnswerStatus };
|
|
|
- localStorage.setItem(storageKey, JSON.stringify(initialStorage));
|
|
|
- }
|
|
|
+ const storage = getStorage();
|
|
|
+
|
|
|
+ if (storage != null) {
|
|
|
+ storage[questionnaireOrderId] = guestQuestionnaireAnswerStatus;
|
|
|
+ localStorage.setItem(storageKey, JSON.stringify(storage));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const initialStorage: GuestQuestionnaireAnswerStatusStorage = { [questionnaireOrderId]: guestQuestionnaireAnswerStatus };
|
|
|
+ localStorage.setItem(storageKey, JSON.stringify(initialStorage));
|
|
|
}
|
|
|
};
|
|
|
|