Skip to content

Commit

Permalink
Fix Conditions components lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Apr 13, 2020
1 parent 7cb97f6 commit d13367c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
49 changes: 32 additions & 17 deletions react/components/Conditions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Statement from '../Statement/index'
import { Labels, Operator } from './typings'
import { DEFAULT_LABELS, MEDIUM_ICON_SIZE } from './constants'
import { SubjectOptions } from '../Statement/Atoms/SubjectAtom'
import { StatementProp } from '../Statement/typings'

type Props = {
canDelete?: boolean
Expand All @@ -21,16 +22,11 @@ type Props = {
operator: Operator
options: SubjectOptions
hideOperator?: boolean
statements: Array<{
subject: string
verb: string
object?: unknown
error?: string
}>
statements: StatementProp[]
subjectPlaceholder: string
}

const Conditions: React.FC<Props> = ({
const Conditions = ({
canDelete,
statements,
options,
Expand All @@ -42,8 +38,8 @@ const Conditions: React.FC<Props> = ({
operator,
onChangeOperator,
onChangeStatements,
}) => {
const objectIsEmpty = object => {
}: Props) => {
const objectIsEmpty = (object: unknown) => {
if (object === undefined) return true
if (object === null) return true
if (object === '') return true
Expand Down Expand Up @@ -73,15 +69,18 @@ const Conditions: React.FC<Props> = ({
onChangeStatements([...statements, emptyStatement])
}

const handleRemoveStatement = index => {
const handleRemoveStatement = (index: number) => {
const updatedStatements = statements
.slice(0, index)
.concat(statements.slice(index + 1))

onChangeStatements(updatedStatements)
}

const handleUpdatestatement = (newStatement, statementIndex) => {
const handleUpdatestatement = (
newStatement: StatementProp,
statementIndex: number
) => {
const newStatements = statements.map((statement, idx) =>
idx === statementIndex ? newStatement : statement
)
Expand All @@ -95,8 +94,10 @@ const Conditions: React.FC<Props> = ({
<StrategySelector
isRtl={isRtl}
operator={operator}
labels={labels}
onChangeOperator={operator => onChangeOperator(operator)}
labels={labels as Labels}
onChangeOperator={(newOperator: Operator) =>
onChangeOperator(newOperator)
}
/>
</div>
)}
Expand Down Expand Up @@ -136,6 +137,17 @@ const Conditions: React.FC<Props> = ({
{}
)

const handleKeyDown = ({
key,
}: React.KeyboardEvent<HTMLDivElement>) => {
const SPACE = ' '
const ENTER = 'Enter'

if (key === SPACE || key === ENTER) {
handleRemoveStatement(statementIndex)
}
}

const statementContent = [
<div key="1" className="flex-grow-1">
<Statement
Expand All @@ -152,6 +164,9 @@ const Conditions: React.FC<Props> = ({
canDelete &&
(!isFullWidth ? (
<div
role="button"
tabIndex={0}
onKeyDown={handleKeyDown}
key="2"
className="ma3 c-muted-2 pointer hover-c-danger"
onClick={() => handleRemoveStatement(statementIndex)}
Expand All @@ -167,7 +182,7 @@ const Conditions: React.FC<Props> = ({
icon={<IconClose className="c-on-action-primary" />}
onClick={() => handleRemoveStatement(statementIndex)}
>
{labels.delete ?? DEFAULT_LABELS.delete}
{labels?.delete ?? DEFAULT_LABELS.delete}
</ButtonWithIcon>
</div>
)),
Expand All @@ -192,8 +207,8 @@ const Conditions: React.FC<Props> = ({
<Separator
label={
operator === 'all'
? labels.operatorAnd ?? DEFAULT_LABELS.operatorAnd
: labels.operatorOr ?? DEFAULT_LABELS.operatorOr
? labels?.operatorAnd ?? DEFAULT_LABELS.operatorAnd
: labels?.operatorOr ?? DEFAULT_LABELS.operatorOr
}
/>
)}
Expand All @@ -213,7 +228,7 @@ const Conditions: React.FC<Props> = ({
disabled={!canAddNewCondition()}
onClick={handleAddNewCondition}
>
{labels.addNewCondition ?? DEFAULT_LABELS.addNewCondition}
{labels?.addNewCondition ?? DEFAULT_LABELS.addNewCondition}
</ButtonWithIcon>
</div>
</div>
Expand Down
43 changes: 24 additions & 19 deletions react/components/Statement/index.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
import React from 'react'

import SubjectAtom, { SubjectOptions } from './Atoms/SubjectAtom'
import VerbAtom from './Atoms/VerbAtom'
import VerbAtom, { VerbOption } from './Atoms/VerbAtom'
import ObjectAtom from './Atoms/ObjectAtom'
import { StatementProp } from './typings'

type Props = {
type StatementProps = {
isFullWidth?: boolean
isRtl?: boolean
omitSubject?: boolean
omitVerbs?: boolean
onChangeStatement: (statement: Props['statement']) => void
onChangeStatement: (statement: StatementProp) => void
options: SubjectOptions
statement?: {
subject: string
verb: string
object?: unknown
error?: string
}
statement?: StatementProp
subjectPlaceholder: string
}

const Statement: React.FC<Props> = ({
const NoopComponent = () => <></>

const Statement: React.FC<StatementProps> = ({
isFullWidth = false,
isRtl,
omitSubject,
omitVerbs,
onChangeStatement,
options,
statement = { subject: '', verb: '', object: null, error: null },
statement = { subject: '', verb: '', object: null },
subjectPlaceholder,
}) => {
const verbOptions =
statement.subject &&
options[statement.subject].verbs.find(verb => verb.value === statement.verb)
let verbOptions: VerbOption = {
label: '',
value: '',
object: NoopComponent,
}
const searchedVerbOption = options[statement.subject].verbs.find(
verb => verb.value === statement.verb
)
if (statement.subject && searchedVerbOption) {
verbOptions = searchedVerbOption
}

const statementAtoms = [
!omitSubject && (
<SubjectAtom
Expand All @@ -43,8 +50,6 @@ const Statement: React.FC<Props> = ({
...statement,
subject,
verb: options[subject].verbs[0].value,
object: null,
error: null,
}
onChangeStatement(newStatement)
}}
Expand All @@ -62,8 +67,8 @@ const Statement: React.FC<Props> = ({
const newStatement = {
...statement,
verb,
object: null,
error: null,
object: undefined,
error: undefined,
}
onChangeStatement(newStatement)
}}
Expand All @@ -76,7 +81,7 @@ const Statement: React.FC<Props> = ({
disabled={!statement.verb}
error={statement.error}
object={statement.object}
onChange={(object, error = null) => {
onChange={(object, error) => {
const newStatement = {
...statement,
object,
Expand Down
7 changes: 7 additions & 0 deletions react/components/Statement/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export type SelectedOption = {
group: SelectOptionGroup['label']
option: SelectOption
}

export interface StatementProp {
subject: string
verb: string
object?: unknown
error?: string
}

0 comments on commit d13367c

Please sign in to comment.