add crud service, crud atoms, crud table, graphs page

This commit is contained in:
2021-06-26 00:28:39 +03:00
parent 40ac760857
commit 1d228b0669
18 changed files with 267 additions and 59 deletions

View File

@ -0,0 +1,70 @@
import {CrudAPI, EntityWithId, EntityWithoutId} from '../api/CrudAPI';
import {EntityMode} from '../types/EntityModes';
import {routerService} from './RouterService';
type Actions<T> = {
loadEntityList: (entities: T[]) => void,
loadEntityForm: (entity: T) => void;
};
export class CrudService<T> {
api: CrudAPI<T>;
actions: Actions<T>;
route: string;
constructor(route: string, endpoint: string, actions: Actions<T>) {
this.api = new CrudAPI(endpoint);
this.actions = actions;
this.route = route;
}
loadEntityList() {
return this.api
.request()
.then(({data}) => {
this.actions.loadEntityList(data);
});
}
loadEntity(id?: string) {
if (id) {
this.api
.find(id)
.then(entity => {
this.actions.loadEntityForm(entity);
});
}
}
updateEntity({id, ...entity}: EntityWithId<T>) {
this.api
.update(id, entity)
.then(this.goRootAndReload);
}
removeEntity(id?: string) {
if (id) {
this.api
.remove(id)
.then(this.goRootAndReload);
}
}
createEntity(entity: EntityWithoutId<T>) {
this.api
.create(entity)
.then(this.goRootAndReload);
}
goRootAndReload = () => {
this.loadEntityList().then(() => {
this.navigate();
});
}
navigate(mode?: EntityMode, id?: string) {
routerService.pushWithQuery(this.route, {mode, id}, {reset: true});
}
}