84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import makeAutoObservable from "mobx-store-inheritance";
|
|
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";
|
|
import { SimpleErrorState } from "../../../core/store/base_store";
|
|
|
|
export class TriggerStore extends SimpleErrorState {
|
|
constructor(repository: TriggerRepository) {
|
|
super();
|
|
this.triggerType = TriggerType.FILE;
|
|
this.repository = repository;
|
|
makeAutoObservable(this);
|
|
}
|
|
|
|
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<void> {
|
|
await this.httpHelper(
|
|
this.repository.save({
|
|
type: this.getTriggerDescription(),
|
|
description: this.triggerDescription,
|
|
value: this.triggers.map((el) => {
|
|
return el.value;
|
|
}),
|
|
})
|
|
);
|
|
}
|
|
}
|
|
export const triggerStore = new TriggerStore(new TriggerRepository());
|