import { makeAutoObservable } from "mobx"; import { v4 as uuidv4 } from "uuid"; import { TriggerType } from "../../../core/model/trigger_model"; import { TriggerRepository } from "../data/trigger_repository"; import { TriggerViewModel } from "../model/trigger_form_view_model"; class TriggerStore { constructor(repository: TriggerRepository) { this.triggerType = TriggerType.FILE; this.repository = repository; makeAutoObservable(this); } isLoading = false; triggerDescription: string = ""; triggerType: TriggerType; codeTriggerValue = ""; triggers: TriggerViewModel[] = []; repository: TriggerRepository; changeTriggerDescription(value: string): void { this.triggerDescription = value; } deleteItem(id: string): void { this.triggers = this.triggers.filter((el) => el.id !== id); } getTriggerType = (): boolean => { return this.triggerType === TriggerType.FILE; }; setTriggerType = (): void => { this.triggers = []; if (this.triggerType === TriggerType.FILE) { this.triggerType = TriggerType.PROCESS; return; } this.triggerType = TriggerType.FILE; }; getTriggerDescription = (): string => { return this.triggerType === TriggerType.FILE ? TriggerType.FILE : TriggerType.PROCESS; }; pushTrigger = (value: string, type: TriggerType): void => { this.triggers.push({ value: value, id: uuidv4(), type: type, }); }; writeNewTrigger(v: string | undefined): void { if (v === undefined) { throw new Error("Editor Value is undefined"); } this.codeTriggerValue = v; } clearTriggerCode(): void { this.codeTriggerValue = ""; } saveCode(): void { if (this.codeTriggerValue !== "") { this.triggers.push({ id: uuidv4(), value: this.codeTriggerValue, type: TriggerType.PROCESS, }); this.codeTriggerValue = ""; } } async saveResult(): Promise { this.isLoading = true await this.repository.save({ type: this.getTriggerDescription(), description: this.triggerDescription, value: this.triggers.map((el) => { return el.value; }), }); this.isLoading = false } } export const triggerStore = new TriggerStore(new TriggerRepository());