From d8a634e5073f306f17b891c9ec322c4565b5c9db Mon Sep 17 00:00:00 2001 From: masiulis Date: Fri, 25 Feb 2022 17:04:02 +0200 Subject: [PATCH] Refactor UGN-244 improve useMemo dependencies, change nested ternary to switch statement, wrap functions in useCallback --- src/steps/ValidationStep/ValidationStep.tsx | 22 ++++--- .../ValidationStep/components/columns.tsx | 60 +++++++++++-------- .../stories/Validation.stories.tsx | 3 +- src/steps/ValidationStep/types.ts | 2 +- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/steps/ValidationStep/ValidationStep.tsx b/src/steps/ValidationStep/ValidationStep.tsx index bb9545e8..3afba5aa 100644 --- a/src/steps/ValidationStep/ValidationStep.tsx +++ b/src/steps/ValidationStep/ValidationStep.tsx @@ -1,4 +1,4 @@ -import React, { useMemo, useState } from "react" +import React, { useCallback, useMemo, useState } from "react" import { Box, Button, Heading, Switch } from "@chakra-ui/react" import { ContinueButton } from "../../components/ContinueButton" import { useRsi } from "../../hooks/useRsi" @@ -23,16 +23,11 @@ export const ValidationStep = ({ initialData, onSubmit }: Props) => { const { fields, rowHook, tableHook, initialHook = (table) => table } = useRsi() const [data, setData] = useState<(T & Meta)[]>( - addErrorsAndRunHooks(addIndexes(initialHook(initialData)), fields, rowHook, tableHook), + useMemo(() => addErrorsAndRunHooks(addIndexes(initialHook(initialData)), fields, rowHook, tableHook), []), ) const [selectedRows, setSelectedRows] = useState>(new Set()) const [filterByErrors, setFilterByErrors] = useState(false) - const updateRow = (rows: typeof data) => { - setData(addErrorsAndRunHooks(rows, fields, rowHook, tableHook)) - } - const columns = useMemo(() => generateColumns(fields), []) - const deleteSelectedRows = () => { if (selectedRows.size) { const newData = data.filter((value) => !selectedRows.has(value.__index)) @@ -41,6 +36,15 @@ export const ValidationStep = ({ initialData, onSubmit }: Props) => { } } + const updateRow = useCallback( + (rows: typeof data) => { + setData(addErrorsAndRunHooks(rows, fields, rowHook, tableHook)) + }, + [setData, addErrorsAndRunHooks, rowHook, tableHook], + ) + + const columns = useMemo(() => generateColumns(fields), [fields, generateColumns]) + const tableData = useMemo(() => { if (filterByErrors) { return data.filter((value) => value?.__errors) @@ -48,6 +52,8 @@ export const ValidationStep = ({ initialData, onSubmit }: Props) => { return data }, [data, filterByErrors]) + const rowKeyGetter = useCallback((row: T & Meta) => row.__index, []) + return ( <> @@ -65,7 +71,7 @@ export const ValidationStep = ({ initialData, onSubmit }: Props) => { row.__index} + rowKeyGetter={rowKeyGetter} rows={tableData} onRowsChange={updateRow} columns={columns} diff --git a/src/steps/ValidationStep/components/columns.tsx b/src/steps/ValidationStep/components/columns.tsx index c59f9cfe..6c4e9713 100644 --- a/src/steps/ValidationStep/components/columns.tsx +++ b/src/steps/ValidationStep/components/columns.tsx @@ -81,32 +81,42 @@ export const generateColumns = (fields: Fields) => [ editOnClick: true, }, formatter: ({ row, onRowChange }) => { - const component = - column.fieldType.type === "checkbox" ? ( - { - event.stopPropagation() - }} - > - { - onRowChange({ ...row, [column.key]: !row[column.key] }) + let component + + switch (column.fieldType.type) { + case "checkbox": + component = ( + { + event.stopPropagation() }} - /> - - ) : column.fieldType.type === "select" ? ( - - {column.fieldType.options.find((option) => option.value === row[column.key])?.label || null} - - ) : ( - - {row[column.key]} - - ) + > + { + onRowChange({ ...row, [column.key]: !row[column.key] }) + }} + /> + + ) + break + case "select": + component = ( + + {column.fieldType.options.find((option) => option.value === row[column.key])?.label || null} + + ) + break + default: + component = ( + + {row[column.key]} + + ) + } if (row.__errors?.[column.key]) { return ( diff --git a/src/steps/ValidationStep/stories/Validation.stories.tsx b/src/steps/ValidationStep/stories/Validation.stories.tsx index 6df9f8c0..261d28d2 100644 --- a/src/steps/ValidationStep/stories/Validation.stories.tsx +++ b/src/steps/ValidationStep/stories/Validation.stories.tsx @@ -1,5 +1,4 @@ -import { Box, extendTheme } from "@chakra-ui/react" -import { colorSchemeOverrides, themeOverrides } from "../../../theme" +import { Box } from "@chakra-ui/react" import { editableTableInitialData, mockRsiValues } from "../../../stories/mockRsiValues" import { ValidationStep } from "../ValidationStep" import { Providers } from "../../../components/Providers" diff --git a/src/steps/ValidationStep/types.ts b/src/steps/ValidationStep/types.ts index 0aa70d34..e2bf03f2 100644 --- a/src/steps/ValidationStep/types.ts +++ b/src/steps/ValidationStep/types.ts @@ -1,4 +1,4 @@ -import type {Info} from "../../types"; +import type { Info } from "../../types" export type Data = { [key: string]: string | boolean | number | undefined } export type Meta = { __index: number; __errors?: Error | null }