webstudio/ui/src/features/create_trigger/presentation/trigger_store.ts

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-11-20 00:48:40 +03:00
import makeAutoObservable from "mobx-store-inheritance";
2023-11-01 18:24:43 +03:00
import { v4 as uuidv4 } from "uuid";
2023-11-10 12:06:40 +03:00
import { TriggerType } from "../../../core/model/trigger_model";
import { TriggerRepository } from "../data/trigger_repository";
import { TriggerViewModel } from "../model/trigger_form_view_model";
2023-12-28 17:18:12 +03:00
import { SimpleErrorState } from "../../../core/store/base_store";
2023-11-01 18:24:43 +03:00
2023-12-28 17:18:12 +03:00
export class TriggerStore extends SimpleErrorState {
2023-11-01 18:24:43 +03:00
constructor(repository: TriggerRepository) {
2023-11-20 00:48:40 +03:00
super();
2023-11-01 18:24:43 +03:00
this.triggerType = TriggerType.FILE;
this.repository = repository;
makeAutoObservable(this);
}
2023-11-10 12:06:40 +03:00
triggerDescription: string = "";
2023-11-01 18:24:43 +03:00
triggerType: TriggerType;
codeTriggerValue = "";
triggers: TriggerViewModel[] = [];
repository: TriggerRepository;
2023-11-10 12:06:40 +03:00
changeTriggerDescription(value: string): void {
this.triggerDescription = value;
}
2023-11-01 18:24:43 +03:00
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 => {
2023-12-28 17:18:12 +03:00
return this.triggerType === TriggerType.FILE ? TriggerType.FILE : TriggerType.PROCESS;
2023-11-01 18:24:43 +03:00
};
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> {
2024-01-23 17:26:59 +03:00
await this.httpHelper(
2023-12-28 17:18:12 +03:00
this.repository.save({
type: this.getTriggerDescription(),
description: this.triggerDescription,
value: this.triggers.map((el) => {
return el.value;
}),
})
);
2023-11-01 18:24:43 +03:00
}
}
export const triggerStore = new TriggerStore(new TriggerRepository());