Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removes equals & not_equals operators from fields with hasMany #5885

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ const contains = {
value: 'contains',
}

const filterOperators = (operators, hasMany = false) => {
if (hasMany) {
return operators.filter(
(operator) => operator.value !== 'equals' && operator.value !== 'not_equals',
)
}
return operators
}

const fieldTypeConditions = {
checkbox: {
component: 'Text',
Expand All @@ -100,7 +109,7 @@ const fieldTypeConditions = {
},
number: {
component: 'Number',
operators: [...base, ...numeric],
operators: (hasMany) => filterOperators([...base, ...numeric], hasMany),
},
point: {
component: 'Point',
Expand All @@ -120,11 +129,11 @@ const fieldTypeConditions = {
},
select: {
component: 'Select',
operators: [...base],
operators: (hasMany) => filterOperators([...base], hasMany),
},
text: {
component: 'Text',
operators: [...base, like, contains],
operators: (hasMany) => filterOperators([...base, like, contains], hasMany),
},
textarea: {
component: 'Text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,44 @@ const baseClass = 'where-builder'

const reduceFields = (fields, i18n) =>
flattenTopLevelFields(fields).reduce((reduced, field) => {
let operators = []

if (typeof fieldTypes[field.type] === 'object') {
const operatorKeys = new Set()
const operators = fieldTypes[field.type].operators.reduce((acc, operator) => {
if (!operatorKeys.has(operator.value)) {
operatorKeys.add(operator.value)
return [
...acc,
{
...operator,
label: i18n.t(`operators:${operator.label}`),
},
]
}
return acc
}, [])

const formattedField = {
label: getTranslation(field.label || field.name, i18n),
value: field.name,
...fieldTypes[field.type],
operators,
props: {
...field,
},
if (typeof fieldTypes[field.type].operators === 'function') {
operators = fieldTypes[field.type].operators(
'hasMany' in field && field.hasMany ? true : false,
)
} else {
operators = fieldTypes[field.type].operators
}
}

return [...reduced, formattedField]
const operatorKeys = new Set()
const filteredOperators = operators.reduce((acc, operator) => {
if (!operatorKeys.has(operator.value)) {
operatorKeys.add(operator.value)
return [
...acc,
{
...operator,
label: i18n.t(`operators:${operator.label}`),
},
]
}
return acc
}, [])

const formattedField = {
label: getTranslation(field.label || field.name, i18n),
value: field.name,
...fieldTypes[field.type],
operators: filteredOperators,
props: {
...field,
},
}

return reduced
return [...reduced, formattedField]
}, [])

/**
Expand Down Expand Up @@ -185,7 +193,7 @@ const WhereBuilder: React.FC<Props> = (props) => {
iconStyle="with-border"
onClick={() => {
if (reducedFields.length > 0)
dispatchConditions({ field: reducedFields[0].value, type: 'add' })
dispatchConditions({ type: 'add', field: reducedFields[0].value })
}}
>
{t('or')}
Expand All @@ -203,7 +211,7 @@ const WhereBuilder: React.FC<Props> = (props) => {
iconStyle="with-border"
onClick={() => {
if (reducedFields.length > 0)
dispatchConditions({ field: reducedFields[0].value, type: 'add' })
dispatchConditions({ type: 'add', field: reducedFields[0].value })
}}
>
{t('addFilter')}
Expand Down
23 changes: 23 additions & 0 deletions test/fields/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,29 @@ describe('fields', () => {
await saveDocAndAssert(page)
await expect(field.locator('.rs__value-container')).toContainText('One')
})

test('should not allow filtering by hasMany field / equals / not equals', async () => {
await page.goto(url.list)

await page.locator('.list-controls__toggle-columns').click()
await page.locator('.list-controls__toggle-where').click()
await page.waitForSelector('.list-controls__where.rah-static--height-auto')
await page.locator('.where-builder__add-first-filter').click()

const conditionField = page.locator('.condition__field')
await conditionField.click()

const dropdownFieldOptions = conditionField.locator('.rs__option')
await dropdownFieldOptions.locator('text=Select Has Many').nth(0).click()

const operatorField = page.locator('.condition__operator')
await operatorField.click()

const dropdownOperatorOptions = operatorField.locator('.rs__option')

await expect(dropdownOperatorOptions.locator('text=equals')).toBeHidden()
await expect(dropdownOperatorOptions.locator('text=not equals')).toBeHidden()
})
})

describe('point', () => {
Expand Down