@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
137 lines (109 loc) • 3.04 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import Dexie from "dexie";
import { set } from "lodash";
const defaultCount = {
orgUnits: {
total: 0,
page: 0
},
levels: {
total: 0,
page: 0
},
groups: {
total: 0,
page: 0
}
};
export class OfflineOrganisationUnitDB extends Dexie {
constructor(name) {
super(name !== null && name !== void 0 ? name : "ou");
_defineProperty(this, "organisationUnits", void 0);
_defineProperty(this, "organisationUnitGroups", void 0);
_defineProperty(this, "organisationUnitLevels", void 0);
_defineProperty(this, "count", void 0);
_defineProperty(this, "localStorageKey", "ou");
this.version(2).stores({
organisationUnits: "&id, displayName, parent.id, *organisationUnitGroups",
organisationUnitGroups: "&id, displayName",
organisationUnitLevels: "&id, displayName"
});
this.getCount();
}
getCount() {
const count = localStorage.getItem(this.localStorageKey);
if (count) {
this.count = JSON.parse(count);
} else {
this.count = defaultCount;
}
}
clearCount() {
localStorage.setItem(this.localStorageKey, JSON.stringify(defaultCount));
}
updateCount(_ref) {
let {
resource,
key,
value
} = _ref;
const updatedCount = this.count;
set(updatedCount, [resource, key], value);
localStorage.setItem(this.localStorageKey, JSON.stringify(updatedCount));
}
async addOrganisationUnits(organisationUnits) {
return this.organisationUnits.bulkAdd(organisationUnits);
}
async clearOrgUnits() {
return this.organisationUnits.clear();
}
async addOrganisationUnitGroups(organisationUnitGroups) {
return this.organisationUnitGroups.bulkAdd(organisationUnitGroups);
}
async clearOrgUnitGroups() {
return this.organisationUnitGroups.clear();
}
async addOrganisationUnitLevels(organisationUnitLevels) {
return this.organisationUnitLevels.bulkAdd(organisationUnitLevels);
}
async clearOrgUnitLevels() {
return this.organisationUnitLevels.clear();
}
async hasOrganisationUnitLevels() {
const {
total,
page
} = this.count.orgUnits;
if (total === 0) {
return false;
}
return total === page;
}
async hasOrganisationUnitGroups() {
const {
total,
page
} = this.count.groups;
if (total === 0) {
return false;
}
return total === page;
}
async hasOrganisationUnits() {
const {
total,
page
} = this.count.orgUnits;
if (total === 0) {
return false;
}
return total === page;
}
async clear() {
await this.organisationUnits.clear();
await this.organisationUnitLevels.clear();
await this.organisationUnitGroups.clear();
this.clearCount();
}
}
export const db = new OfflineOrganisationUnitDB();