Skip to content

Commit

Permalink
feat: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
rboixaderg committed Apr 21, 2024
1 parent 1aa5f6d commit fd8de59
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/guillo-gmi/actions/add_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function AddItem(props: Props) {
const { type } = props
const { getForm } = Ctx.registry

const Form = getForm(type)
const Form = getForm(type, undefined)

const setActive = () => {
Ctx.cancelAction()
Expand Down
12 changes: 4 additions & 8 deletions src/guillo-gmi/components/panel/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ import { Input } from '../input/input'
import { SelectVocabulary } from '../input/select_vocabulary'
import { useIntl } from 'react-intl'
import { genericMessages } from '../../locales/generic_messages'

import {
ItemColumn,
RegistrySchemaFilter,
SearchItem,
} from '../../types/guillotina'
import { RegistrySchemaFilter, SearchItem } from '../../types/guillotina'

interface InitialState {
page: number
Expand Down Expand Up @@ -124,7 +119,8 @@ export function PanelItems() {
const fnName = getSearchEngineQueryParamsFunction(SearchEngine)
if (sortParsed === undefined) {
const defaultSortValue = Ctx.registry.getDefaultSortValue(
Ctx.context['@type']
Ctx.context['@type'],
undefined
)
sortParsed = parser(
`_sort_${defaultSortValue.direction}=${defaultSortValue.key}`
Expand Down Expand Up @@ -339,7 +335,7 @@ export function PanelItems() {
<th>
<AllItemsCheckbox />
</th>
{columns.map((column: ItemColumn) => (
{columns.map((column) => (
<th
key={`table-col-${column.label}`}
onClick={() => column.isSortable && onSort(column.key)}
Expand Down
3 changes: 2 additions & 1 deletion src/guillo-gmi/contexts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GuillotinaReducerActionTypes,
} from '../reducers/guillotina'
import { GuillotinaCommonObject } from '../types/guillotina.js'
import { IManageRegistry } from '../hooks/useRegistry.js'

export const AuthContext = createContext({})

Expand All @@ -20,7 +21,7 @@ interface PropsTraversal {
type: GuillotinaReducerActionTypes
payload: IndexSignature
}>
registry: IndexSignature
registry: IManageRegistry
flash?: (action: string, result: string) => void
url: string
children?: React.ReactNode
Expand Down
5 changes: 3 additions & 2 deletions src/guillo-gmi/forms/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import { stringToSlug } from '../lib/helpers'
import { useIntl } from 'react-intl'
import { genericMessages } from '../locales/generic_messages'

interface Props {
export interface BaseFormProps {
onSubmit: (data: { title: string; id: string }) => void
actionName?: string
title?: string
dataTest?: string
loading?: boolean
type: string
}
export function BaseForm({
onSubmit,
actionName,
title,
dataTest,
loading,
}: Props) {
}: BaseFormProps) {
const intl = useIntl()
const [name, setName] = useState('')
const [id, setId] = useState('')
Expand Down
54 changes: 37 additions & 17 deletions src/guillo-gmi/hooks/useRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { RemoveItems } from '../actions/remove_items'
import { RemoveItem } from '../actions/remove_item'
import { AddItem } from '../actions/add_item'
import { ChangePassword } from '../actions/change_pass'
import { BaseForm } from '../forms/base'
import { BaseForm, BaseFormProps } from '../forms/base'
import { UserForm } from '../forms/users'
import { IAttachment } from '../components/behaviors/iattachment'
import { IDublinCore } from '../components/behaviors/idublincore'
Expand All @@ -35,7 +35,18 @@ import {
ItemColumn,
RegistrySchema,
} from '../types/guillotina'
import { buildQs, parser } from '../lib/search'

export interface RegistrySortValue {
direction: 'asc' | 'des'
key: string
}
export interface RegistryProperties {
Buttons: React.ReactElement
Panels: React.ReactElement
default: string[]
ignoreField: string[]
}
export interface IRegistry {
paths: {
[key: string]: React.FC
Expand All @@ -59,7 +70,7 @@ export interface IRegistry {
[key: string]: RegistrySchema
}
properties: {
[key: string]: React.FC
[key: string]: RegistryProperties
}
components: {
[key: string]: (props: any) => React.ReactNode | null | undefined
Expand All @@ -70,11 +81,11 @@ export interface IRegistry {
fieldsToFilter: {
[key: string]: string[]
}
parsedSearchQueryParam: {
[key: string]: (query: string, type: string) => string
}
defaultSortValue: {
[key: string]: {
direction: 'asc' | 'des'
key: string
}
[key: string]: RegistrySortValue
}
}

Expand Down Expand Up @@ -136,6 +147,7 @@ const registry: IRegistry = {
fieldsToFilter: {
UserManager: ['id', 'email', 'user_name'],
},
parsedSearchQueryParam: {},
defaultSortValue: {},
}

Expand All @@ -148,20 +160,18 @@ export interface IManageRegistry {
) => React.ComponentType<any>
getComponent: (name: string) => React.ComponentType<any>
getView: (name: string) => React.ComponentType<any>
getForm: (type: string, fallback: React.FC) => React.FC
getForm: (type: string, fallback?: React.FC) => React.FC<BaseFormProps>
getAction: (type: string, fallback?: React.FC) => React.FC
getBehavior: (type: string, fallback: React.FC) => React.FC
getProperties: (type: string) => React.FC
getBehavior: (type: string, fallback?: React.FC) => React.FC<any>
getProperties: (type: string) => RegistryProperties
getItemsColumn: (type: string) => ItemColumn[] | undefined
getSchemas: (type: string) => RegistrySchema
getFieldsToFilter: (type: string, fallback?: string[]) => string[]
getParsedSearchQueryParam: (query: string, type: string) => string
getDefaultSortValue: (
type: string,
fallback: { direction: 'asc' | 'des'; key: string }
) => {
key: string
direction: 'asc' | 'des'
}
fallback?: RegistrySortValue
) => RegistrySortValue
getSearchEngineQueryParamsFunction: (type: string) => string
}

Expand Down Expand Up @@ -202,15 +212,15 @@ const getView = (name: string) => {
return registry.views[name]
}

const getForm = (type: string, fallback: React.FC) => {
const getForm = (type: string, fallback?: React.FC<BaseFormProps>) => {
return registry.forms[type] || fallback || BaseForm
}

const getAction = (type: string, fallback?: React.FC) => {
return registry.actions[type] || fallback
}

const getBehavior = (type: string, fallback: React.FC) => {
const getBehavior = (type: string, fallback?: React.FC) => {
return registry.behaviors[type] || fallback
}

Expand All @@ -228,7 +238,7 @@ const getFieldsToFilter = (type: string, fallback: string[] = ['title']) => {

const getDefaultSortValue = (
type: string,
fallback = {
fallback: RegistrySortValue | undefined = {
key: 'id',
direction: 'des',
}
Expand All @@ -240,6 +250,15 @@ const getSearchEngineQueryParamsFunction = (type: string) => {
return registry.searchEngineQueryParamsFunction[type]
}

const getParsedSearchQueryParam = (query: string, type: string) => {
const parsedFunction = registry.parsedSearchQueryParam[type]
if (!parsedFunction) {
const fieldsToFilter = getFieldsToFilter(type)
return buildQs(parser(query, fieldsToFilter))
}
return parsedFunction(query, type)
}

export const defaultComponent = (context: GuillotinaCommonObject) => {
return context.is_folderish ? FolderCtx : ItemCtx
}
Expand Down Expand Up @@ -268,6 +287,7 @@ export function useRegistry(data: Partial<IRegistry>): IManageRegistry {
getProperties,
getItemsColumn,
getFieldsToFilter,
getParsedSearchQueryParam,
getDefaultSortValue,
getSchemas,
getView,
Expand Down
4 changes: 3 additions & 1 deletion src/guillo-gmi/lib/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Auth } from './auth'
import {
GuillotinaGroup,
GuillotinaUser,
ItemColumn,
ItemColumnChild,
ReturnSearchCompatible,
} from '../types/guillotina'
Expand Down Expand Up @@ -127,12 +128,13 @@ export class GuillotinaClient {
return result
}

getItemsColumn() {
getItemsColumn(): ItemColumn[] {
const smallcss = { width: 25 }
const mediumcss = { width: 120 }

return [
{
key: 'icon',
label: '',
isSortable: false,
child: ({ model }: ItemColumnChild) => (
Expand Down
5 changes: 4 additions & 1 deletion src/guillo-gmi/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const SEP = '='
const DEFAULT_FIELD = 'title__in'
const CLEANER = '||'

export function parser(qs: string, defaultField = DEFAULT_FIELD): string[][] {
export function parser(
qs: string,
defaultField: string | string[] | undefined = DEFAULT_FIELD
): string[][] {
if (qs.includes(SEP) && Array.isArray(defaultField)) {
throw new Error('This option is not supported')
}
Expand Down

0 comments on commit fd8de59

Please sign in to comment.