Skip to content

Commit

Permalink
add boolean type cf UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Nov 26, 2024
1 parent 4b34b95 commit 31cda8e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
7 changes: 7 additions & 0 deletions papermerge/core/features/document/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion ui2/src/features/document/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type UpdateDocumentCustomFields = {
body: Array<{
custom_field_value_id?: string
key: string
value: string
value: string | boolean
}>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -183,7 +185,7 @@ export default function CustomFields() {
onClear={onClearDocumentType}
clearable
/>
{genericCustomFieldsComponents}
<Stack>{genericCustomFieldsComponents}</Stack>
{isErrorGetDocCF && (
<Text c="red">
Error while fetching custom fields. Error code cf98g62m.
Expand All @@ -203,15 +205,21 @@ 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({
customField,
documentID,
onChange
}: GenericCustomFieldArg) {
const [value, setValue] = useState<string>(customField.value)
const [value, setValue] = useState<string | boolean>(customField.value)

const onLocalChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.currentTarget.value)
Expand All @@ -234,10 +242,14 @@ function GenericCustomField({
return <CustomFieldMonetary customField={customField} onChange={onChange} />
}

if (customField.type == "boolean") {
return <CustomFieldBoolean customField={customField} onChange={onChange} />
}

return (
<TextInput
label={customField.name}
value={value}
value={value ? value.toString() : ""}
onChange={onLocalChange}
/>
)
Expand Down
28 changes: 28 additions & 0 deletions ui2/src/features/document/components/customFields/Boolean.tsx
Original file line number Diff line number Diff line change
@@ -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>(Boolean(customField.value))

const onLocalChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.currentTarget.checked
setValue(value)
onChange({customField, value: value})
}

useEffect(() => {
setValue(Boolean(customField.value))
}, [customField.value])

return (
<Checkbox
checked={value}
onChange={onLocalChange}
label={customField.name}
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ export default function CustomFieldMonetary({
customField,
onChange
}: CustomFieldArgs) {
const [value, setValue] = useState<string | number>(customField.value)
const [value, setValue] = useState<string | number>(
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 (
Expand Down
2 changes: 1 addition & 1 deletion ui2/src/features/document/components/customFields/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export type CFV = {
name: string
type: string
extra_data?: string
value: string
value: string | boolean
}

export type DocumentCFV = {
Expand Down

0 comments on commit 31cda8e

Please sign in to comment.