This repository has been archived by the owner on Sep 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
632 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
//tslint:disable:no-console | ||
import { action } from 'mobx'; | ||
import { MainStore } from './mainStore'; | ||
|
||
/** | ||
* This class wraps all common store functions with success/error popups. The desired methods that start with "do" should be overriden in the specific stores. | ||
*/ | ||
export class DomainStore<T, OverviewType = T> { | ||
constructor(protected mainStore: MainStore) {} | ||
|
||
protected get entityName() { | ||
return { singular: 'Die Entität', plural: 'Die Entitäten' }; | ||
} | ||
|
||
public get entity(): T | undefined { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public set entity(e: T | undefined) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public get entities(): Array<OverviewType> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
@action | ||
public async fetchAll() { | ||
try { | ||
await this.doFetchAll(); | ||
} catch (e) { | ||
this.mainStore.displayError(`${this.entityName.plural} konnten nicht geladen werden.`); | ||
console.error(e); | ||
throw e; | ||
} | ||
} | ||
|
||
protected async doFetchAll() { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
@action | ||
public async fetchOne(id: number) { | ||
try { | ||
this.entity = undefined; | ||
return await this.doFetchOne(id); | ||
} catch (e) { | ||
this.mainStore.displayError(`${this.entityName.plural} konnten nicht geladen werden.`); | ||
console.error(e); | ||
throw e; | ||
} | ||
} | ||
|
||
protected async doFetchOne(id: number): Promise<T | void> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
@action | ||
public async post(entity: T) { | ||
this.displayLoading(async () => { | ||
try { | ||
await this.doPost(entity); | ||
this.mainStore.displaySuccess(`${this.entityName.singular} wurde gespeichert.`); | ||
} catch (e) { | ||
this.mainStore.displayError(`${this.entityName.singular} konnte nicht gespeichert werden.`); | ||
console.error(e); | ||
throw e; | ||
} | ||
}); | ||
} | ||
|
||
protected async doPost(entity: T) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
@action | ||
public async put(entity: T) { | ||
this.displayLoading(async () => { | ||
try { | ||
await this.doPut(entity); | ||
this.mainStore.displaySuccess(`${this.entityName.singular} wurde gespeichert.`); | ||
} catch (e) { | ||
this.mainStore.displayError(`${this.entityName.singular} konnte nicht gespeichert werden.`); | ||
console.error(e); | ||
throw e; | ||
} | ||
}); | ||
} | ||
|
||
@action | ||
protected async doPut(entity: T) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
@action | ||
public async delete(id: number) { | ||
this.displayLoading(async () => { | ||
try { | ||
await this.doDelete(id); | ||
this.mainStore.displaySuccess(`${this.entityName.singular} wurde gelöscht.`); | ||
} catch (e) { | ||
this.mainStore.displayError(`${this.entityName.singular} konnte nicht gelöscht werden.`); | ||
console.error(e); | ||
throw e; | ||
} | ||
}); | ||
} | ||
|
||
@action | ||
protected async doDelete(id: number) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public async displayLoading<T>(f: () => Promise<T>) { | ||
//TODO: trigger loading indicator in MainStore | ||
await f(); | ||
} | ||
|
||
public async notifyProgress<P>(f: () => Promise<P>, { errorMessage = 'Fehler!', successMessage = 'Erfolg!' } = {}) { | ||
this.displayLoading(async () => { | ||
try { | ||
await f(); | ||
if (successMessage) { | ||
this.mainStore.displaySuccess(successMessage); | ||
} | ||
} catch (e) { | ||
if (successMessage) { | ||
this.mainStore.displayError(errorMessage); | ||
} | ||
console.error(e); | ||
throw e; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { action, computed, observable } from 'mobx'; | ||
import { MainStore } from './mainStore'; | ||
import { DomainStore } from './domainStore'; | ||
import { Holiday } from '../types'; | ||
|
||
export class HolidayStore extends DomainStore<Holiday> { | ||
protected get entityName() { | ||
return { | ||
singular: 'Der Feiertag', | ||
plural: 'Die Feiertage', | ||
}; | ||
} | ||
|
||
@computed | ||
get entities(): Array<Holiday> { | ||
return this.holidays; | ||
} | ||
|
||
@computed | ||
get entity(): Holiday | undefined { | ||
return this.holiday; | ||
} | ||
|
||
set entity(holiday: Holiday | undefined) { | ||
this.holiday = holiday; | ||
} | ||
|
||
@observable | ||
public holidays: Holiday[] = []; | ||
|
||
@observable | ||
public holiday?: Holiday; | ||
|
||
constructor(mainStore: MainStore) { | ||
super(mainStore); | ||
} | ||
|
||
public filter = (h: Holiday) => { | ||
return [h.description, this.mainStore!.formatDate(h.date_from)].some(s => s.toLowerCase().includes(this.searchQuery)); | ||
}; | ||
|
||
@action | ||
protected async doDelete(id: number) { | ||
await this.mainStore.api.delete('/holidays/' + id); | ||
await this.doFetchAll(); | ||
} | ||
|
||
@action | ||
protected async doFetchAll() { | ||
const res = await this.mainStore.api.get<Holiday[]>('/holidays'); | ||
this.holidays = res.data; | ||
} | ||
|
||
@action | ||
protected async doPost(holiday: Holiday) { | ||
const response = await this.mainStore.api.post<Holiday[]>('/holidays', holiday); | ||
this.holidays = response.data; | ||
} | ||
|
||
@action | ||
protected async doPut(holiday: Holiday) { | ||
const response = await this.mainStore.api.put<Holiday[]>('/holidays/' + holiday.id, holiday); | ||
this.holidays = response.data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.