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

Refactor UGN-244 improve useMemo dependencies #37

Merged
merged 1 commit into from
Feb 25, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/steps/ValidationStep/ValidationStep.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,16 +23,11 @@ export const ValidationStep = <T,>({ initialData, onSubmit }: Props<T>) => {
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<ReadonlySet<number | string>>(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))
Expand All @@ -41,13 +36,24 @@ export const ValidationStep = <T,>({ initialData, onSubmit }: Props<T>) => {
}
}

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)
}
return data
}, [data, filterByErrors])

const rowKeyGetter = useCallback((row: T & Meta) => row.__index, [])

return (
<>
<Box display="flex" p="2rem" pb={0} flexDirection="column" flex={1} overflow="auto" height="100%">
Expand All @@ -65,7 +71,7 @@ export const ValidationStep = <T,>({ initialData, onSubmit }: Props<T>) => {
</Box>
</Box>
<Table
rowKeyGetter={(row) => row.__index}
rowKeyGetter={rowKeyGetter}
rows={tableData}
onRowsChange={updateRow}
columns={columns}
Expand Down
60 changes: 35 additions & 25 deletions src/steps/ValidationStep/components/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,42 @@ export const generateColumns = <T,>(fields: Fields<T>) => [
editOnClick: true,
},
formatter: ({ row, onRowChange }) => {
const component =
column.fieldType.type === "checkbox" ? (
<Box
display="flex"
alignItems="center"
height="100%"
onClick={(event) => {
event.stopPropagation()
}}
>
<Switch
isChecked={row[column.key] as boolean}
onChange={() => {
onRowChange({ ...row, [column.key]: !row[column.key] })
let component

switch (column.fieldType.type) {
case "checkbox":
component = (
<Box
display="flex"
alignItems="center"
height="100%"
onClick={(event) => {
event.stopPropagation()
}}
/>
</Box>
) : column.fieldType.type === "select" ? (
<Box minWidth="100%" minHeight="100%" overflow="hidden" textOverflow="ellipsis">
{column.fieldType.options.find((option) => option.value === row[column.key])?.label || null}
</Box>
) : (
<Box minWidth="100%" minHeight="100%" overflow="hidden" textOverflow="ellipsis">
{row[column.key]}
</Box>
)
>
<Switch
isChecked={row[column.key] as boolean}
onChange={() => {
onRowChange({ ...row, [column.key]: !row[column.key] })
}}
/>
</Box>
)
break
case "select":
component = (
<Box minWidth="100%" minHeight="100%" overflow="hidden" textOverflow="ellipsis">
{column.fieldType.options.find((option) => option.value === row[column.key])?.label || null}
</Box>
)
break
default:
component = (
<Box minWidth="100%" minHeight="100%" overflow="hidden" textOverflow="ellipsis">
{row[column.key]}
</Box>
)
}

if (row.__errors?.[column.key]) {
return (
Expand Down
3 changes: 1 addition & 2 deletions src/steps/ValidationStep/stories/Validation.stories.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/steps/ValidationStep/types.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down