mirror of
https://github.com/sartography/uva-covid19-testing-frontend.git
synced 2025-01-30 17:36:09 +00:00
Stores settings in local storage
This commit is contained in:
parent
69d490d76f
commit
508dceceb2
@ -18,43 +18,56 @@ 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.
|
||||
initialsLength: 5,
|
||||
initialsRegExp: /^[a-zA-Z]{2,5}$/,
|
||||
labelLayout: labelLayouts.round_32mm_1up, // Which label layout to use for printing. Can be overridden by user setting.
|
||||
lineCountRegExp: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records.
|
||||
lineCountRegExp: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records.
|
||||
locationId: '0000', // Default location ID. Can be overridden by user setting.
|
||||
locationIdRegExp: /^[\d]{4}$/, // ID format for Line Count records.
|
||||
locationIdRegExp: /^[\d]{4}$/, // ID format for Line Count records.
|
||||
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.
|
||||
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.
|
||||
});
|
||||
};
|
||||
|
||||
// Default form field and data values
|
||||
export const defaults: AppDefaults = new AppDefaults(defaultOptions);
|
||||
|
||||
// Deserializes settings from local storage and returns AppDefaults instance
|
||||
export const getStoredSettings = (): AppDefaults => {
|
||||
// tslint:disable-next-line:no-eval
|
||||
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 => {
|
||||
const storedSettings = localStorage.getItem('settings');
|
||||
|
||||
if (storedSettings) {
|
||||
// tslint:disable-next-line:no-eval
|
||||
return new AppDefaults(eval(`(${storedSettings})`));
|
||||
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;
|
||||
};
|
||||
|
@ -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];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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 {
|
||||
}
|
||||
|
@ -39,7 +39,9 @@ export class SettingsComponent implements OnInit {
|
||||
|
||||
save() {
|
||||
saveSettings({
|
||||
|
||||
labelLayout: labelLayouts[this.labelLayoutFormControl.value],
|
||||
numCopies: this.numCopiesFormControl.value,
|
||||
locationId: this.locationIdFormControl.value,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user