Stores settings in local storage

This commit is contained in:
Aaron Louie 2020-09-23 15:37:32 -04:00
parent 69d490d76f
commit 508dceceb2
5 changed files with 45 additions and 23 deletions

View File

@ -18,10 +18,10 @@ export const labelLayouts = {
}),
};
// Default form field and data values
export const defaults: AppDefaults = new AppDefaults({
export const defaultOptions: AppDefaultsOptions = {
barCodeNumLength: 9, // Number of digits in Bar Code.
barCodeRegExp: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long. Manually-entered ID numbers will be exactly 9 digits long.
barCodeRegExp: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long.
// Manually-entered ID numbers will be exactly 9 digits long.
countsCollection: 'counts', // Name of collection for Line Counts in Firebase.
dateDisplayFormat: 'MM/dd/yyyy, hh:mm aa', // Format for dates when displayed to user.
dateEncodedFormat: 'yyyyMMddHHmm', // Format for dates when encoded in IDs for database records.
@ -34,27 +34,40 @@ export const defaults: AppDefaults = new AppDefaults({
numCopies: 1, // Default number of copies of labels to print. Can be overridden by user setting.
qrCodeRegExp: /^[\d]{9}-[a-zA-Z]+-[\d]{12}-[\d]{4}$/, // ID format for QR Code records.
samplesCollection: 'samples', // Name of collection for Line Counts in Firebase.
});
};
export const getSettings = (): AppDefaults => {
const storedSettings = localStorage.getItem('settings');
// Default form field and data values
export const defaults: AppDefaults = new AppDefaults(defaultOptions);
if (storedSettings) {
// Deserializes settings from local storage and returns AppDefaults instance
export const getStoredSettings = (): AppDefaults => {
// tslint:disable-next-line:no-eval
return new AppDefaults(eval(`(${storedSettings})`));
return new AppDefaults(eval(`(${localStorage.getItem('settings')})`));
};
// Returns true if settings are found in local storage
export const hasStoredSettings = (): boolean => {
return !!localStorage.getItem('settings');
};
// Returns settings from local storage, or defaults if none have been saved yet.
export const getSettings = (): AppDefaults => {
if (hasStoredSettings()) {
return getStoredSettings();
} else {
localStorage.setItem('settings', serializeJs(defaults));
return defaults;
return saveSettings(defaults);
}
};
// Serializes given settings and stores them in local storage
export const saveSettings = (newSettings: AppDefaultsOptions): AppDefaults => {
const settings: AppDefaults = createClone()(getSettings());
const settings: AppDefaults = createClone({circles: true})(defaults);
Object.keys(newSettings).forEach(k => {
console.log(`${k}:`, newSettings[k]);
settings[k] = newSettings[k];
});
localStorage.setItem('settings', serializeJs(settings));
return getSettings();
return settings;
};

View File

@ -1,3 +1,4 @@
import {defaultOptions} from '../config/defaults';
import {LabelLayout} from './labelLayout.interface';
export interface AppDefaultsOptions {
@ -34,17 +35,18 @@ export class AppDefaults {
samplesCollection: string;
constructor(options: AppDefaultsOptions) {
console.log('options', options);
const keys = Object.keys(options);
const keys = Object.keys(defaultOptions);
keys.forEach(k => {
if (k.includes('RegExp')) {
if (typeof options[k] === 'string') {
this[k] = new RegExp(options[k]);
} else {
} else if (options[k] instanceof RegExp) {
this[k] = options[k];
} else {
this[k] = defaultOptions[k];
}
} else {
this[k] = options[k];
this[k] = options[k] || defaultOptions[k];
}
});
}

View File

@ -1,7 +1,7 @@
<mat-toolbar color="primary">
<mat-toolbar-row>
<button mat-button routerLink="/" class="logo">BeSAFE</button>
<button mat-button routerLink="/settings">{{testingLocation.name}}</button>
<button mat-button routerLink="/settings">{{testingLocation.name}} ({{locationId}})</button>
<span fxFlex></span>
<button mat-icon-button routerLink="/"><mat-icon>home</mat-icon></button>
<button mat-icon-button routerLink="/settings"><mat-icon>settings</mat-icon></button>

View File

@ -1,4 +1,5 @@
import {Component, Input, OnInit} from '@angular/core';
import {getSettings} from '../config/defaults';
import {TestingLocation} from '../interfaces/testingLocation.interface';
@Component({
@ -8,8 +9,12 @@ import {TestingLocation} from '../interfaces/testingLocation.interface';
})
export class NavbarComponent implements OnInit {
@Input() testingLocation: TestingLocation;
locationId: string;
constructor() { }
constructor() {
const settings = getSettings();
this.locationId = settings.locationId;
}
ngOnInit(): void {
}

View File

@ -39,7 +39,9 @@ export class SettingsComponent implements OnInit {
save() {
saveSettings({
labelLayout: labelLayouts[this.labelLayoutFormControl.value],
numCopies: this.numCopiesFormControl.value,
locationId: this.locationIdFormControl.value,
});
}
}