This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
/
actions.ts
163 lines (157 loc) · 5.88 KB
/
actions.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import allowedDatatypes from '@/allowedDataTypes';
import FormValues from '@/form/FormValues';
import Validator from '@/form/Validator';
import { MenuItem } from '@wmde/wikit-vue-components/dist/components/MenuItem';
import { ActionContext } from 'vuex';
import RootState, { ConditionRow } from './RootState';
import SearchResult from '@/data-access/SearchResult';
import Error from '@/data-model/Error';
import PropertyValueRelation from '@/data-model/PropertyValueRelation';
import MetricsCollector from '@/data-access/MetricsCollector';
import SearchEntityRepository from '@/data-access/SearchEntityRepository';
import SearchOptions from '@/data-access/SearchOptions';
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types
export default ( searchEntityRepository: SearchEntityRepository, metricsCollector: MetricsCollector ) => ( {
async searchProperties(
_context: ActionContext<RootState, RootState>,
options: SearchOptions ): Promise<SearchResult[]> {
const searchResults = await searchEntityRepository.searchProperties(
options.search,
options.limit,
options.offset,
);
return searchResults.map( ( searchResult: MenuItem & SearchResult ) => {
if ( !allowedDatatypes.includes( searchResult.datatype ) ) {
searchResult.tag = 'query-builder-property-lookup-limited-support-tag';
}
return searchResult;
} );
},
async searchItemValues(
_context: ActionContext<RootState, RootState>,
options: SearchOptions ): Promise<SearchResult[]> {
// check for empty
return await searchEntityRepository.searchItemValues(
options.search,
options.limit,
options.offset,
);
},
updateValue(
context: ActionContext<RootState, RootState>,
payload: { value: string; conditionIndex: number } ): void {
context.commit( 'setValue', payload );
},
unsetProperty( context: ActionContext<RootState, RootState>, conditionIndex: number ): void {
context.commit( 'unsetProperty', conditionIndex );
context.commit(
'clearFieldErrors',
{
conditionIndex,
errorsToClear: 'property',
},
);
},
updateProperty( context: ActionContext<RootState, RootState>,
payload: { property: { label: string; id: string; datatype: string }; conditionIndex: number } ): void {
const oldDatatype = context.getters.datatype( payload.conditionIndex );
if ( oldDatatype && oldDatatype !== payload.property.datatype ) {
context.commit( 'clearValue', payload.conditionIndex );
}
context.commit( 'setProperty', payload );
if ( !allowedDatatypes.includes( payload.property.datatype ) ) {
context.dispatch( 'setConditionAsLimitedSupport', payload.conditionIndex );
} else {
context.commit(
'clearFieldErrors',
{
conditionIndex: payload.conditionIndex,
errorsToClear: 'property',
},
);
}
},
updatePropertyValueRelation( context: ActionContext<RootState, RootState>,
payload: { propertyValueRelation: PropertyValueRelation; conditionIndex: number } ): void {
context.commit( 'setPropertyValueRelation', payload );
},
setLimit( context: ActionContext<RootState, RootState>, limit: number ): void {
context.commit( 'setLimit', limit );
},
setUseLimit( context: ActionContext<RootState, RootState>, useLimit: boolean ): void {
context.commit( 'setUseLimit', useLimit );
},
setOmitLabels( context: ActionContext<RootState, RootState>, omitLabels: boolean ): void {
context.commit( 'setOmitLabels', omitLabels );
},
setSubclasses( context: ActionContext<RootState, RootState>,
payload: { subclasses: boolean; conditionIndex: number } ): void {
context.commit( 'setSubclasses', payload );
},
setErrors( context: ActionContext<RootState, RootState>, errors: Error[] ): void {
context.commit( 'setErrors', errors );
},
incrementMetric( context: ActionContext<RootState, RootState>, metric: string ): void {
metricsCollector.increment( metric );
},
addCondition( context: ActionContext<RootState, RootState> ): void {
context.commit( 'addCondition' );
},
removeCondition( context: ActionContext<RootState, RootState>, conditionIndex: number ): void {
context.commit( 'removeCondition', conditionIndex );
},
setConditionAsLimitedSupport( context: ActionContext<RootState, RootState>, conditionIndex: number ): void {
context.dispatch(
'updatePropertyValueRelation',
{ propertyValueRelation: PropertyValueRelation.Regardless, conditionIndex },
);
context.dispatch( 'updateValue', { value: '', conditionIndex } );
context.commit(
'setFieldErrors',
{
index: conditionIndex,
errors: {
propertyError: {
type: 'warning',
message: 'query-builder-property-lookup-limited-support-note',
},
},
},
);
},
validateForm( context: ActionContext<RootState, RootState> ): void {
const validator = new Validator(
context.rootState.conditionRows.map( ( condition: ConditionRow ): FormValues => {
// TODO: refactor FormValues to match ConditionRow and remove this mapping
return {
property: condition.propertyData.isPropertySet ? condition.propertyData : null,
value: condition.valueData.value,
propertyValueRelation: condition.propertyValueRelationData.value,
};
} ),
);
const validationResult = validator.validate();
context.commit( 'setErrors', validationResult.formErrors );
// set field errors for each row
validationResult.fieldErrors.forEach( ( errors, conditionIndex ) => {
context.commit(
'setFieldErrors',
{
index: conditionIndex,
errors: {
propertyError: errors.property,
valueError: errors.value,
},
},
);
} );
// re-set limited support warning again where applicable
context.rootState.conditionRows.forEach( ( conditionRow, index ) => {
const datatype = conditionRow.propertyData?.datatype;
if ( datatype && !allowedDatatypes.includes( datatype ) ) {
context.dispatch( 'setConditionAsLimitedSupport', index );
}
} );
},
} );