Skip to content

Commit

Permalink
feat: add new fields and update render field and edit component, also…
Browse files Browse the repository at this point in the history
… improve move and delete item action
  • Loading branch information
rboixaderg committed Mar 2, 2024
1 parent 08a9ce8 commit 1006a3a
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
"type": "object",
"properties": {
"items": {
"type": "array"
}
"type": "array",
"title": "Array in json"
},
"text": {
"type": "string",
"title": "Text in json"
},
"second_level": {
"type": "object",
"title": "Two levels",
"properties": {
"first_item_second_level": {
"type": "string",
"title": "Item second level text",
},
},
},
}
}
)
Expand All @@ -25,6 +40,9 @@ class IGMI(interfaces.IFolder):
index_field("text_field", type="searchabletext")
text_field = schema.Text(title="Text field", required=False)

index_field("textarea_field", type="searchabletext")
textarea_field = schema.Text(title="Text area field", required=True, widget="textarea")


text_line_field = schema.TextLine(title="Text line field")
index_field("number_field", type="int")
Expand All @@ -44,6 +62,7 @@ class IGMI(interfaces.IFolder):
choice_field_vocabulary = schema.Choice(
title="Choice field vocabulary",
vocabulary="gmi_vocabulary",
required=True,
)

index_field("choice_field", type="keyword")
Expand Down Expand Up @@ -83,6 +102,7 @@ class IGMI(interfaces.IFolder):
widget="search_list",
labelProperty="title",
typeNameQuery="GMI",
required=True
)

brother_gmi = schema.Text(
Expand Down
1 change: 0 additions & 1 deletion src/guillo-gmi/actions/move_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function MoveItem(props: Props) {
Ctx.flash(`Failed to move item!: ${errorMessage}`, 'danger')
}

Ctx.refresh()
Ctx.cancelAction()
}

Expand Down
1 change: 0 additions & 1 deletion src/guillo-gmi/actions/remove_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function RemoveItem(props: Props) {
Ctx.flash(`Failed to delete item!: ${errorMessage}`, 'danger')
}

Ctx.refresh()
Ctx.cancelAction()
}

Expand Down
57 changes: 56 additions & 1 deletion src/guillo-gmi/components/fields/editComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SearchInput } from '../input/search_input'
import { useTraversal } from '../../contexts'
import { Ref, forwardRef } from 'react'
import { GuillotinaItemsProperty } from '../../types/guillotina'
import { IndexSignature } from '../../types/global'

interface Props {
schema: GuillotinaItemsProperty
Expand All @@ -19,10 +20,24 @@ interface Props {
dataTest?: string
className?: string
placeholder?: string
id?: string
required?: boolean
}

export const EditComponent = forwardRef(
({ schema, val, setValue, dataTest, className, placeholder }: Props, ref) => {
(
{
schema,
val,
setValue,
dataTest,
className,
placeholder,
id,
required,
}: Props,
ref
) => {
const traversal = useTraversal()

if (schema?.widget === 'search_list') {
Expand Down Expand Up @@ -71,6 +86,8 @@ export const EditComponent = forwardRef(
onChange={(ev) => setValue(ev)}
ref={ref as Ref<HTMLTextAreaElement>}
dataTest={dataTest}
placeholder={placeholder}
id={id}
/>
)
} else if (schema?.type === 'boolean') {
Expand All @@ -94,6 +111,8 @@ export const EditComponent = forwardRef(
dataTest={dataTest}
onChange={setValue}
multiple
placeholder={placeholder}
id={id}
/>
)
} else if (schema?.items?.vocabulary) {
Expand All @@ -111,6 +130,8 @@ export const EditComponent = forwardRef(
})}
multiple
onChange={setValue}
placeholder={placeholder}
id={id}
/>
)
}
Expand Down Expand Up @@ -146,6 +167,8 @@ export const EditComponent = forwardRef(
dataTest={dataTest}
onChange={setValue}
vocabularyName={get(schema, 'vocabularyName', null)}
placeholder={placeholder}
id={id}
/>
)
}
Expand All @@ -164,8 +187,35 @@ export const EditComponent = forwardRef(
}
})}
onChange={setValue}
placeholder={placeholder}
id={id}
/>
)
} else if (schema?.type === 'object' && schema.widget !== 'file') {
const value = val as IndexSignature
return (
<>
{schema.title && <h4 className="subtitle mt-2">{schema.title}</h4>}
{Object.keys(get(schema, 'properties', {})).map((key) => {
const subSchema = get(schema, 'properties', {})[key]
const requiredFields: string[] = get(schema, 'required', [])
return (
<EditComponent
key={`${id}[${key}]`}
id={`${id}[${key}]`}
schema={subSchema}
val={value && key in value ? value[key] : ''}
placeholder={subSchema?.title ?? ''}
required={requiredFields.includes(key)}
setValue={(ev) => {
setValue({ ...value, [key]: ev })
}}
dataTest={`${key}TestInput`}
/>
)
})}
</>
)
}
const getInputType = () => {
switch (schema?.type) {
Expand All @@ -175,6 +225,8 @@ export const EditComponent = forwardRef(
return 'date'
case 'datetime':
return 'datetime-local'
case 'time':
return 'time'
default:
return 'text'
}
Expand All @@ -187,6 +239,9 @@ export const EditComponent = forwardRef(
onChange={(ev) => setValue(ev)}
ref={ref as Ref<HTMLInputElement>}
type={getInputType()}
required={required}
placeholder={placeholder}
id={id}
/>
)
}
Expand Down
17 changes: 11 additions & 6 deletions src/guillo-gmi/components/fields/renderField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export function RenderField({ value, Widget, schema }: RenderFieldProps) {
))
}
return Object.keys(value).map((key) => (
<FieldValue field={key} value={value[key]} key={key} />
<FieldValue
field={get(schema, `properties.${key}.title`, key)}
schema={get(schema, `properties.${key}`, {})}
value={value[key]}
key={key}
/>
))
}
return <p>No render for {JSON.stringify(value)}</p>
Expand All @@ -48,12 +53,13 @@ export function RenderField({ value, Widget, schema }: RenderFieldProps) {
interface FieldValueProps {
field: string
value: unknown
schema: GuillotinaSchemaProperty
}
const FieldValue = ({ field, value }: FieldValueProps) => (
const FieldValue = ({ field, value, schema }: FieldValueProps) => (
<div className="field">
<div className="label">{field}</div>
<div className="value">
<RenderField value={value} />
<RenderFieldComponent val={value} schema={schema} field={field} />
</div>
</div>
)
Expand Down Expand Up @@ -197,7 +203,7 @@ interface RenderFieldComponentProps {
schema: GuillotinaSchemaProperty
field: string
val: any
modifyContent: boolean
modifyContent?: boolean
}
export function RenderFieldComponent({
schema,
Expand All @@ -215,6 +221,7 @@ export function RenderFieldComponent({
(modifyContent
? DEFAULT_VALUE_EDITABLE_FIELD
: DEFAULT_VALUE_NO_EDITABLE_FIELD),
schema: schema,
}
if (val && schema?.widget === 'file') {
renderProps['value'] = {
Expand All @@ -228,14 +235,12 @@ export function RenderFieldComponent({
renderProps['value'] = new Date(val).toLocaleString()
} else if (schema?.items?.vocabularyName || schema?.vocabularyName) {
renderProps['Widget'] = VocabularyRenderField
renderProps['schema'] = schema
} else if (
schema?.widget === 'search' ||
schema?.widget === 'search_list'
) {
renderProps['Widget'] = SearchRenderField
renderProps['value'] = val
renderProps['schema'] = schema
}
return renderProps
}
Expand Down
3 changes: 2 additions & 1 deletion src/guillo-gmi/components/input/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { classnames, generateUID } from '../../lib/helpers'
import { useIntl } from 'react-intl'
import { genericMessages } from '../../locales/generic_messages'
import { forwardRef, useState } from 'react'
import { IndexSignature } from '../../types/global'
// @ TODO implement hasErrors

interface Props {
Expand All @@ -19,7 +20,7 @@ interface Props {
onChange?: (value: string | string[]) => void
options: { text: string; value: string }[]
appendDefault?: boolean
style?: any
style?: IndexSignature
dataTest?: string
value?: string | string[]
}
Expand Down
1 change: 0 additions & 1 deletion src/guillo-gmi/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function Layout({ children, onLogout, auth }: Props) {
</a>
<a
id="burger"
href="/"
role="button"
className="navbar-burger burger"
aria-label="menu"
Expand Down
14 changes: 12 additions & 2 deletions src/guillo-gmi/hooks/useRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { NotFound } from '../components/notfound'
import { Path } from '../components/path'
import { EditComponent } from '../components/fields/editComponent'
import { RenderFieldComponent } from '../components/fields/renderField'
import { GuillotinaSchema } from '../types/guillotina'

export interface IRegistry {
paths: {
Expand All @@ -48,8 +49,17 @@ export interface IRegistry {
behaviors: {
[key: string]: React.FC
}
itemsColumn: any
schemas: any
itemsColumn: {
[key: string]: () => {
label: string
key: string
isSortable?: boolean
child: React.ReactNode
}[]
}
schemas: {
[key: string]: GuillotinaSchema
}
properties: {
[key: string]: React.FC
}
Expand Down
1 change: 1 addition & 0 deletions src/guillo-gmi/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class Auth {
if (!authToken) return {}

if (this.willExpire(expires) && this.retryRefresh < this.maxRetry) {
// eslint-disable-next-line no-extra-semi
;(async () => await this.refreshToken())()
}

Expand Down
2 changes: 1 addition & 1 deletion src/guillo-gmi/lib/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class GuillotinaClient {
result = [
...parser(start.toString(), '_from'),
...parser(pageSize.toString(), 'size'),
...parser('*', '_metadata=*'),
...parser('*', '_metadata'),
]

if (withDepth) {
Expand Down
1 change: 1 addition & 0 deletions src/guillo-gmi/types/guillotina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface GuillotinaItemsProperty {
labelProperty?: string
typeNameQuery?: string
items: GuillotinaItemsProperty
title: string
}
export interface GuillotinaSchemaProperty {
type: string
Expand Down

0 comments on commit 1006a3a

Please sign in to comment.