useFlipFlopData.ts
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Ref, nextTick, toRaw, unref } from 'vue';
import { FlipFlopConditionType, FlipFlopFormRecordType, FlipFlopItemType } from './types';
import { DefineComponentsBasicExpose } from '/#/utils';
import { FormFieldEnum } from './config';
import { FlipFlopTypeEnum } from '/@/enums/linkedgeEnum';
import { createNewFlipFlopItem } from '.';
export function useFlipFlopData(
flipFlopListRef: Ref<FlipFlopItemType[]>
): DefineComponentsBasicExpose<FlipFlopConditionType[]> {
const crateFlipFlopCondition = (flipFlopItem: FlipFlopItemType): FlipFlopConditionType => {
const {
deviceProfileId,
deviceType,
entityId,
entityType,
triggerType,
flipFlopType,
triggerDurationUnit,
triggerRepeatCount,
triggerDurationValue,
} = flipFlopItem.ref?.getFieldsValue() as FlipFlopFormRecordType;
const condition = flipFlopItem.conditionRef?.getFieldsValue() || [];
return {
deviceProfileId,
deviceType,
entityId,
entityType,
triggerType,
triggerCondition: {
condition: {
condition,
spec: {
type: flipFlopType,
...(flipFlopType !== FlipFlopTypeEnum.SIMPLE
? {
unit: triggerDurationUnit,
predicate: {
defaultValue:
flipFlopType === FlipFlopTypeEnum.DURATION
? triggerDurationValue
: triggerRepeatCount,
},
}
: {}),
},
},
schedule: toRaw(unref(flipFlopItem.schedule)),
},
};
};
const getFieldsValue = () => {
return unref(flipFlopListRef).map((flipFlopItem) => crateFlipFlopCondition(flipFlopItem));
};
const setFieldsValue = (flipFlopData: FlipFlopConditionType[]) => {
flipFlopListRef.value = Array.from({ length: flipFlopData.length }, () =>
createNewFlipFlopItem()
);
nextTick(() => {
unref(flipFlopListRef).forEach((item, index) => {
const data = flipFlopData[index];
const condition = data.triggerCondition.condition;
const [firstItem] = condition.condition;
const { key, valueType } = firstItem;
const { type, unit, predicate } = condition.spec;
const { defaultValue } = predicate || {};
item.ref?.setFieldsValue({
...data,
[FormFieldEnum.FLIP_FLOP_TYPE]: type,
[FormFieldEnum.CONDITION_KEY]: key.key,
[FormFieldEnum.CONDITION_VALUE_TYPE]: valueType,
[FormFieldEnum.TRIGGER_DURATION_UNIT]: unit,
[FormFieldEnum.TRIGGER_DURATION_VALUE]:
type === FlipFlopTypeEnum.DURATION ? defaultValue : null,
[FormFieldEnum.TRIGGER_REPEAT_COUNT]:
type === FlipFlopTypeEnum.REPEATING ? defaultValue : null,
});
const {
daysOfWeek,
startsOn = 0,
endsOn = 0,
timezone,
type: scheduleType,
items,
} = data.triggerCondition.schedule;
item.schedule = {
type: scheduleType,
timezone,
daysOfWeek,
startsOn,
endsOn,
items,
};
nextTick(() => {
item.conditionRef?.setFieldsValue(condition.condition);
});
});
});
};
const validate = async () => {
for (const flipFlopItem of unref(flipFlopListRef)) {
await flipFlopItem.ref?.validate();
await flipFlopItem.conditionRef?.validate?.();
}
};
const resetFieldsValue = (defaultNull?: boolean) => {
flipFlopListRef.value = defaultNull ? [] : [createNewFlipFlopItem()];
};
return {
getFieldsValue,
setFieldsValue,
validate,
resetFieldsValue,
};
}