Repository URL to install this package:
|
Version:
0.0.13 ▾
|
import * as tslib_1 from "tslib";
import { observable, action } from 'xmobx/mobx';
import { oneStorage } from '@skava/persistence';
import { locationContainer } from './google/LocationContainer';
import { fetchGoogleGeoCode } from './fetchGoogleGeoCode';
import { loadGoogleIfNeeded } from './loadGoogleIfNeeded';
import { transformGeoCodeData } from './transform';
export class LocationContainerLite {
constructor() {
this.zipCode = oneStorage.get('zip_code') || '';
this.geoCode = oneStorage.get('geo_code');
}
setZipCode(zipCode) {
this.zipCode = zipCode;
oneStorage.set('zip_code', zipCode);
}
setGeoCode(geoCode) {
this.geoCode = geoCode;
oneStorage.set('geo_code', geoCode);
}
clear() {
this.geoCode = {};
this.zipCode = '';
oneStorage.remove('geo_code');
oneStorage.remove('zip_code');
}
/**
* only if needed
*/
async loadGoogleAndFetchZipCodeAndGeoCode() {
await loadGoogleIfNeeded();
await this.fetchZipCodeIfNeeded();
await this.fetchGeoCodeIfNeeded();
}
async fetchGeoCodeIfNeeded() {
if (this.geoCode) {
console.warn('[LocationContainerLite] already had geoCode');
}
else {
const response = await fetchGoogleGeoCode(this.zipCode);
const transformed = transformGeoCodeData(response);
this.setGeoCode(transformed);
}
}
async fetchZipCodeIfNeeded() {
if (this.zipCode === '') {
const result = await locationContainer.updateGeoLocation();
this.zipCode = result;
}
else {
console.warn('[LocationContainerLite] already had zipCode');
}
}
}
tslib_1.__decorate([
observable
], LocationContainerLite.prototype, "zipCode", void 0);
tslib_1.__decorate([
observable
], LocationContainerLite.prototype, "geoCode", void 0);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "setZipCode", null);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "setGeoCode", null);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "clear", null);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "loadGoogleAndFetchZipCodeAndGeoCode", null);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "fetchGeoCodeIfNeeded", null);
tslib_1.__decorate([
action
], LocationContainerLite.prototype, "fetchZipCodeIfNeeded", null);
//# sourceMappingURL=LocationContainerLite.js.map