Skip to content
This repository was archived by the owner on Sep 23, 2021. It is now read-only.

Commit c1b2261

Browse files
committed
refactor(search-engine): refactor the function to edit rules
Signed-off-by: Gianfranco Manganiello <gmanganiello@teclib.com>
1 parent 5cbc2e7 commit c1b2261

2 files changed

Lines changed: 21 additions & 84 deletions

File tree

src/containers/SearchEngine/components/QueryBuilder/index.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import React, {
3131
} from 'react'
3232
import PropTypes from 'prop-types'
3333
import itemtype from 'shared/itemtype'
34-
import { flatten, unflatten } from 'shared/flat'
3534
import I18n from 'shared/i18n'
3635
import getNestedObject from 'shared/getNestedObject'
3736
import createFieldList from '../../actions/createFieldList'
@@ -138,20 +137,20 @@ class QueryBuilder extends PureComponent {
138137
})
139138
}
140139

141-
constructNestedObject = (element, rule) => {
140+
constructNestedObject = (element, rule, newRule) => {
142141
let result
143142

144143
if (element !== rule) {
145144
if (element.criteria) {
146-
const criteria = this.constructNestedObject(element.criteria, rule)
145+
const criteria = this.constructNestedObject(element.criteria, rule, newRule)
147146
if (criteria) {
148147
result = {
149148
...element,
150149
criteria,
151150
}
152151
}
153152
} else if (element.metacriteria) {
154-
const metacriteria = this.constructNestedObject(element.metacriteria, rule)
153+
const metacriteria = this.constructNestedObject(element.metacriteria, rule, newRule)
155154
if (metacriteria) {
156155
result = {
157156
...element,
@@ -161,7 +160,8 @@ class QueryBuilder extends PureComponent {
161160
} else if (Array.isArray(element)) {
162161
result = []
163162
element.forEach((element2) => {
164-
const x = this.constructNestedObject(element2, rule)
163+
const x = this.constructNestedObject(element2, rule, newRule)
164+
165165
if (x) {
166166
result.push(x)
167167
}
@@ -173,7 +173,13 @@ class QueryBuilder extends PureComponent {
173173
} else {
174174
result = element
175175
}
176+
} else if (newRule) {
177+
result = {
178+
...rule,
179+
...newRule,
180+
}
176181
}
182+
177183
return result
178184
}
179185

@@ -186,39 +192,19 @@ class QueryBuilder extends PureComponent {
186192
*/
187193
changeRule = (type, id, newValue) => {
188194
const newRules = [...this.state[type]]
189-
let index = type
190-
id.forEach((element) => {
191-
index += isNaN(element) ? `.${element}` : `[${element}]`
192-
})
195+
const rule = getNestedObject(newRules, id)
196+
const test = []
193197

194-
const flat = flatten({ [type]: newRules })
195-
196-
if (newValue) {
197-
for (const key in newValue) {
198-
if (Object.prototype.hasOwnProperty.call(newValue, key)) {
199-
flat[`${index}.${key}`] = newValue[key]
200-
}
198+
newRules.forEach((element) => {
199+
const rules = this.constructNestedObject(element, rule, newValue)
200+
if (rules) {
201+
test.push(rules)
201202
}
203+
})
202204

203-
this.setState({
204-
[type]: unflatten(flat)[type],
205-
})
206-
} else {
207-
const rule = getNestedObject(newRules, id)
208-
209-
const test = []
210-
211-
newRules.forEach((element) => {
212-
const rules = this.constructNestedObject(element, rule)
213-
if (rules) {
214-
test.push(rules)
215-
}
216-
})
217-
218-
this.setState({
219-
[type]: test,
220-
})
221-
}
205+
this.setState({
206+
[type]: test,
207+
})
222208
}
223209

224210
/**

src/shared/flat/index.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)