webstudio/ui/src/features/trigger/presentation/logic/trigger_store.ts

81 lines
2 KiB
TypeScript
Raw Normal View History

2023-11-01 18:24:43 +03:00
import { makeAutoObservable } from "mobx";
import { v4 as uuidv4 } from "uuid";
import { TriggerType } from "../../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);
}
triggerType: TriggerType;
codeTriggerValue = "";
triggers: TriggerViewModel[] = [];
repository: TriggerRepository;
deleteItem(id: string): void {
this.triggers = this.triggers.filter((el) => el.id !== id);
console.log(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 => {
console.log(value);
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.repository.save({
type: this.getTriggerDescription(),
value: this.triggers.map((el) => {
return el.value;
}),
});
}
}
export const triggerStore = new TriggerStore(new TriggerRepository());