Skip to content

Commit

Permalink
Reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Takeda committed Aug 11, 2023
1 parent afc0ca5 commit f5b458c
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions react/components/QuotesTableContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,42 +142,43 @@ const QuotesTableContainer: FunctionComponent = () => {
const costCenters = [] as string[]
const statuses = [] as string[]

statements.forEach((statement) => {
if (!statement?.object) return
const { subject, object } = statement

switch (subject) {
case 'status': {
if (!object || typeof object !== 'object') return
const keys = Object.keys(object)
const isAllTrue = !keys.some((key) => !object[key])
const isAllFalse = !keys.some((key) => object[key])
const trueKeys = keys.filter((key) => object[key])

if (isAllTrue) break
if (isAllFalse) statuses.push('none')
statuses.push(...trueKeys)
break
}
statements
.filter(
(statement) => statement?.object && typeof statement.object === 'object'
)
.forEach((statement) => {
const { subject } = statement
const object = statement.object as Record<string, unknown> // guarantee by filter

switch (subject) {
case 'status': {
const keys = Object.keys(object)
const isAllTrue = !keys.some((key) => !object[key])
const isAllFalse = !keys.some((key) => object[key])
const trueKeys = keys.filter((key) => object[key])

if (isAllTrue) break
if (isAllFalse) statuses.push('none')
statuses.push(...trueKeys)
break
}

case 'organizationAndCostCenter': {
if (!object || typeof object !== 'object') return
case 'organizationAndCostCenter': {
if (object.organizationId) {
organizations.push(object.organizationId as string)
}

if (object.organizationId) {
organizations.push(object.organizationId as string)
}
if (object.costCenterId) {
costCenters.push(object.costCenterId as string)
}

if (object.costCenterId) {
costCenters.push(object.costCenterId as string)
break
}

break
default:
break
}

default:
break
}
})
})

setFilterState({
status: statuses,
Expand Down

0 comments on commit f5b458c

Please sign in to comment.