From 31cda8ee67a8fc9831a28c51bc386c0ea41315cf Mon Sep 17 00:00:00 2001 From: Eugen Ciur Date: Tue, 26 Nov 2024 08:06:51 +0100 Subject: [PATCH] add boolean type cf UI --- papermerge/core/features/document/schema.py | 7 +++++ ui2/src/features/document/apiSlice.ts | 2 +- .../DocumentDetails/CustomFields.tsx | 22 +++++++++++---- .../components/customFields/Boolean.tsx | 28 +++++++++++++++++++ .../components/customFields/Monetary.tsx | 7 +++-- .../document/components/customFields/types.ts | 2 +- ui2/src/types.ts | 2 +- 7 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 ui2/src/features/document/components/customFields/Boolean.tsx diff --git a/papermerge/core/features/document/schema.py b/papermerge/core/features/document/schema.py index e652fc843..f42e5e979 100644 --- a/papermerge/core/features/document/schema.py +++ b/papermerge/core/features/document/schema.py @@ -44,6 +44,13 @@ def convert_value(cld, value, info: ValidationInfo) -> CFValueType: if value and info.data["type"] == CustomFieldType.monetary: return float(value) + if value and info.data["type"] == CustomFieldType.boolean: + if value.lower() == "f": + return False + + if value.lower() == "false": + return False + return value diff --git a/ui2/src/features/document/apiSlice.ts b/ui2/src/features/document/apiSlice.ts index 6516c7cc2..25dfabc47 100644 --- a/ui2/src/features/document/apiSlice.ts +++ b/ui2/src/features/document/apiSlice.ts @@ -72,7 +72,7 @@ type UpdateDocumentCustomFields = { body: Array<{ custom_field_value_id?: string key: string - value: string + value: string | boolean }> } diff --git a/ui2/src/features/document/components/DocumentDetails/CustomFields.tsx b/ui2/src/features/document/components/DocumentDetails/CustomFields.tsx index ce603658a..66a090949 100644 --- a/ui2/src/features/document/components/DocumentDetails/CustomFields.tsx +++ b/ui2/src/features/document/components/DocumentDetails/CustomFields.tsx @@ -22,9 +22,11 @@ import { ComboboxItem, Select, Skeleton, + Stack, Text, TextInput } from "@mantine/core" +import CustomFieldBoolean from "../customFields/Boolean" export default function CustomFields() { const mode: PanelMode = useContext(PanelContext) @@ -93,7 +95,7 @@ export default function CustomFields() { value }: { customField: CFV - value: string + value: string | boolean }) => { const newCustomFieldValues = customFieldValues.map(cf => { if (cf.name == customField.name) { @@ -183,7 +185,7 @@ export default function CustomFields() { onClear={onClearDocumentType} clearable /> - {genericCustomFieldsComponents} + {genericCustomFieldsComponents} {isErrorGetDocCF && ( Error while fetching custom fields. Error code cf98g62m. @@ -203,7 +205,13 @@ export default function CustomFields() { interface GenericCustomFieldArg { customField: CFV documentID?: string - onChange: ({customField, value}: {customField: CFV; value: string}) => void + onChange: ({ + customField, + value + }: { + customField: CFV + value: string | boolean + }) => void } function GenericCustomField({ @@ -211,7 +219,7 @@ function GenericCustomField({ documentID, onChange }: GenericCustomFieldArg) { - const [value, setValue] = useState(customField.value) + const [value, setValue] = useState(customField.value) const onLocalChange = (e: React.ChangeEvent) => { setValue(e.currentTarget.value) @@ -234,10 +242,14 @@ function GenericCustomField({ return } + if (customField.type == "boolean") { + return + } + return ( ) diff --git a/ui2/src/features/document/components/customFields/Boolean.tsx b/ui2/src/features/document/components/customFields/Boolean.tsx new file mode 100644 index 000000000..c1f8bfbd5 --- /dev/null +++ b/ui2/src/features/document/components/customFields/Boolean.tsx @@ -0,0 +1,28 @@ +import {Checkbox} from "@mantine/core" +import {useEffect, useState} from "react" +import {CustomFieldArgs} from "./types" + +export default function CustomFieldBoolean({ + customField, + onChange +}: CustomFieldArgs) { + const [value, setValue] = useState(Boolean(customField.value)) + + const onLocalChange = (event: React.ChangeEvent) => { + const value = event.currentTarget.checked + setValue(value) + onChange({customField, value: value}) + } + + useEffect(() => { + setValue(Boolean(customField.value)) + }, [customField.value]) + + return ( + + ) +} diff --git a/ui2/src/features/document/components/customFields/Monetary.tsx b/ui2/src/features/document/components/customFields/Monetary.tsx index cf0488799..5871f4399 100644 --- a/ui2/src/features/document/components/customFields/Monetary.tsx +++ b/ui2/src/features/document/components/customFields/Monetary.tsx @@ -34,17 +34,18 @@ export default function CustomFieldMonetary({ customField, onChange }: CustomFieldArgs) { - const [value, setValue] = useState(customField.value) + const [value, setValue] = useState( + customField.value.toString() + ) const currency = getCurrency(customField.extra_data) const onLocalChange = (v: number | string) => { setValue(v) onChange({customField, value: v.toString()}) } - console.log(`Monetary state=${value} CFV=${customField.value}`) useEffect(() => { - setValue(customField.value) + setValue(customField.value.toString()) }, [customField.value]) return ( diff --git a/ui2/src/features/document/components/customFields/types.ts b/ui2/src/features/document/components/customFields/types.ts index aaf28de0f..d7e3a5f28 100644 --- a/ui2/src/features/document/components/customFields/types.ts +++ b/ui2/src/features/document/components/customFields/types.ts @@ -2,7 +2,7 @@ import type {CFV} from "@/types" export type onChangeArgs = { customField: CFV - value: string + value: string | boolean } export type onChangeType = ({customField, value}: onChangeArgs) => void diff --git a/ui2/src/types.ts b/ui2/src/types.ts index bf74a95d7..ba977c3a1 100644 --- a/ui2/src/types.ts +++ b/ui2/src/types.ts @@ -440,7 +440,7 @@ export type CFV = { name: string type: string extra_data?: string - value: string + value: string | boolean } export type DocumentCFV = {