import {BaseStore} from '../common/BaseStore'; import axios from 'axios'; import * as config from '../config'; import {action} from 'mobx'; import {SnackReporter} from '../snack/SnackManager'; import {IApplication} from '../types'; export class AppStore extends BaseStore { public onDelete: () => void = () => {}; public constructor(private readonly snack: SnackReporter) { super(); } protected requestItems = (): Promise => axios .get(`${config.get('url')}application`) .then((response) => response.data); protected requestDelete = (id: number): Promise => axios.delete(`${config.get('url')}application/${id}`).then(() => { this.onDelete(); return this.snack('Application deleted'); }); @action public uploadImage = async (id: number, file: Blob): Promise => { const formData = new FormData(); formData.append('file', file); await axios.post(`${config.get('url')}application/${id}/image`, formData, { headers: {'content-type': 'multipart/form-data'}, }); await this.refresh(); this.snack('Application image updated'); }; @action public update = async ( id: number, name: string, description: string, defaultPriority: number ): Promise => { await axios.put(`${config.get('url')}application/${id}`, { name, description, defaultPriority, }); await this.refresh(); this.snack('Application updated'); }; @action public create = async ( name: string, description: string, defaultPriority: number ): Promise => { await axios.post(`${config.get('url')}application`, { name, description, defaultPriority, }); await this.refresh(); this.snack('Application created'); }; public getName = (id: number): string => { const app = this.getByIDOrUndefined(id); return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown'; }; }