Skip to content

Commit

Permalink
refactor: remove component from list config
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Oct 11, 2023
1 parent 25568ed commit ebec718
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 38 deletions.
7 changes: 1 addition & 6 deletions src/components/sectionList/SectionListWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ export const SectionListWrapper = <Model extends IdentifiableObject>({
<ModelValue
path={path}
schema={schema}
component={
headerColumns.find(
(c) => c.path === path
)?.component
}
model={model}
sectionModel={model}
/>
)
}}
Expand Down
4 changes: 2 additions & 2 deletions src/components/sectionList/listView/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { ModelPropertyDescriptor } from '../../../lib'
export interface ModelListView {
name: string
sectionModel: string
columns: Array<ModelPropertyDescriptor>
filters: Array<ModelPropertyDescriptor>
columns: ReadonlyArray<ModelPropertyDescriptor>
filters: ReadonlyArray<ModelPropertyDescriptor>
}

export type ModelListViews = {
Expand Down
3 changes: 2 additions & 1 deletion src/components/sectionList/listView/useModelListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export const useModelListView = () => {
select,
})

if (query.error) {
// 404 errors are expected when user havent saved any views
if (query.error && (query.error as any).details?.httpStatusCode !== 404) {
console.error(query.error)
}

Expand Down
27 changes: 9 additions & 18 deletions src/components/sectionList/modelValue/ModelValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import { ModelValueRenderer } from './ModelValueRenderer'
type ModelValueProps = {
schema: Schema
path: string
model: unknown
// override renderer
component?: React.ComponentType<{
value: unknown
schemaProperty: SchemaFieldProperty
}>
sectionModel: unknown
}

const ModelValueError = () => {
Expand All @@ -39,27 +34,23 @@ const getSchemaProperty = (
return schemaProperty
}

export const ModelValue = ({
component,
schema,
path,
model,
}: ModelValueProps) => {
export const ModelValue = ({ schema, path, sectionModel }: ModelValueProps) => {
const schemaProperty = getSchemaProperty(schema, path)

const value = getIn(model, path)
const value = getIn(sectionModel, path)

if (!schemaProperty || value == undefined) {
console.warn(
`Property ${path} not found in schema, value not rendered: ${value}`
)
console.warn(`Property ${path} not found, value not rendered: ${value}`)
return null
}

const Component = component || ModelValueRenderer
return (
<ErrorBoundary FallbackComponent={ModelValueError}>
<Component value={value} schemaProperty={schemaProperty} />
<ModelValueRenderer
path={path}
value={value}
schemaProperty={schemaProperty}
/>
</ErrorBoundary>
)
}
12 changes: 11 additions & 1 deletion src/components/sectionList/modelValue/ModelValueRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import { SchemaFieldProperty } from '../../../lib'
import { BooleanValue } from './BooleanValue'
import { ConstantValue } from './ConstantValue'
import { DateValue } from './DateValue'
import { PublicAccessValue } from './PublicAccess'
import { TextValue } from './TextValue'

export type ValueDetails = {
schemaProperty: SchemaFieldProperty
value: unknown
path: string
}

export const ModelValueRenderer = ({ value, schemaProperty }: ValueDetails) => {
export const ModelValueRenderer = ({
path,
value,
schemaProperty,
}: ValueDetails) => {
if (path === 'sharing.public' && typeof value === 'string') {
return <PublicAccessValue value={value} />
}

if (schemaProperty.propertyType === 'CONSTANT') {
return <ConstantValue value={value as string} />
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/sectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type SelectedColumn = {
path: string
}

export type SelectedColumns = SelectedColumn[]
export type SelectedColumns = ReadonlyArray<SelectedColumn>

export type CheckBoxOnChangeObject = {
checked: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './sectionListViews'
export * from './sectionListViewsConfig'
export * from './sections'
export * from './translatedModelConstants'
export * from './translatedModelProperties'
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import i18n from '@dhis2/d2-i18n'
// import { PublicAccessValue } from '../../components/sectionList/modelValue/PublicAccess'
import { uniqueBy } from '../utils'
import type { SectionName } from './sections'
import { getTranslatedProperty } from './translatedModelProperties'

export interface ModelPropertyDescriptor {
label: string
path: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
component?: React.FC<{ value: any }>
}

type ModelPropertyConfig = string | ModelPropertyDescriptor
interface ViewConfigPart {
available?: ModelPropertyConfig[]
available?: ReadonlyArray<ModelPropertyConfig>
overrideDefaultAvailable?: boolean
default?: ModelPropertyConfig[]
default?: ReadonlyArray<ModelPropertyConfig>
}

interface ViewConfig {
Expand All @@ -24,8 +21,8 @@ interface ViewConfig {
}

interface ResolvedViewConfigPart {
available: ModelPropertyDescriptor[]
default: ModelPropertyDescriptor[]
available: ReadonlyArray<ModelPropertyDescriptor>
default: ReadonlyArray<ModelPropertyDescriptor>
}
interface ResolvedViewConfig {
columns: ResolvedViewConfigPart
Expand All @@ -52,7 +49,6 @@ const defaultModelViewConfig = {
{
label: i18n.t('Public access'),
path: 'sharing.public',
// component: PublicAccessValue,
},
],
default: ['name', 'sharing.public', 'lastUpdated'],
Expand Down

0 comments on commit ebec718

Please sign in to comment.