Skip to content

Commit

Permalink
sort of works
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 16, 2024
1 parent 76193bc commit 95fb491
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 72 deletions.
32 changes: 0 additions & 32 deletions papermerge/core/routers/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,6 @@ def get_document_details(
return doc


@router.post("/{document_id}/custom-fields")
@utils.docstring_parameter(scope=scopes.NODE_UPDATE)
def add_document_custom_field_values(
document_id: uuid.UUID,
custom_fields_add: schemas.DocumentCustomFieldsAdd,
user: Annotated[
schemas.User, Security(get_current_user, scopes=[scopes.NODE_UPDATE])
],
db_session: db.Session = Depends(db.get_session),
) -> list[schemas.CustomFieldValue]:
"""
Associates document type to specified `document_type_id` and set it custom field
value(s). This API will create a NEW custom field value
All custom fields must be part of `DocumentType` specified by `document_type_id`,
otherwise response will return error 400 - invalid request.
Required scope: `{scope}`
"""
try:
added_entries = db.add_document_custom_field_values(
db_session,
id=document_id,
custom_fields_add=custom_fields_add,
user_id=user.id,
)
except NoResultFound:
raise HTTPException(status_code=404, detail="Document not found")

return added_entries


@router.patch("/{document_id}/custom-fields")
@utils.docstring_parameter(scope=scopes.NODE_UPDATE)
def update_document_custom_field_values(
Expand Down
2 changes: 1 addition & 1 deletion papermerge/core/schemas/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,6 @@ class DocumentCustomFieldsUpdateValue(BaseModel):


class DocumentCustomFieldsUpdate(BaseModel):
custom_field_value_id: UUID | None
custom_field_value_id: UUID | None = None
key: CFNameType
value: CFValueType
9 changes: 8 additions & 1 deletion ui2/src/features/document/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type GetDocsByTypeArgs = {

type UpdateDocumentTypeArgs = {
document_id?: string
invalidatesTags: {
documentTypeID?: string
}
body: {
document_type_id: string | null
}
Expand Down Expand Up @@ -178,10 +181,14 @@ export const apiSliceWithDocuments = apiSlice.injectEndpoints({
updateDocumentType: builder.mutation<void, UpdateDocumentTypeArgs>({
query: data => ({
url: `/documents/${data.document_id}/type`,
method: "PATCH",
body: data.body
}),
invalidatesTags: (_result, _error, arg) => {
return [{type: "Document", id: arg.document_id}]
return [
{type: "Document", id: arg.document_id},
{type: "DocumentCFV", id: arg.invalidatesTags.documentTypeID}
]
}
}),
getDocumentCustomFields: builder.query<CFV[], string>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ import type {CFV, PanelMode} from "@/types"
import {Button, ComboboxItem, Select, Skeleton, TextInput} from "@mantine/core"

export default function CustomFields() {
const [showSaveButton, setShowSaveButton] = useState<boolean>(false)
const {data: allDocumentTypes = [], isSuccess: isSuccessAllDocumentTypes} =
useGetDocumentTypesQuery()
const mode: PanelMode = useContext(PanelContext)
const docID = useAppSelector(s => selectCurrentNodeID(s, mode))
const {currentData: doc, isLoading} = useGetDocumentQuery(docID ?? skipToken)
const [showSaveButton, setShowSaveButton] = useState<boolean>(false)
const [customFieldValues, setCustomFieldValues] = useState<CFV[]>([])
const [documentTypeID, setDocumentTypeID] = useState<ComboboxItem | null>(
null
)

const docID = useAppSelector(s => selectCurrentNodeID(s, mode))

const {data: allDocumentTypes = [], isSuccess: isSuccessAllDocumentTypes} =
useGetDocumentTypesQuery()
const {currentData: doc, isLoading} = useGetDocumentQuery(docID ?? skipToken)
const {currentData: documentType} = useGetDocumentTypeQuery(
documentTypeID?.value ?? skipToken
)
const [customFieldValues, setCustomFieldValues] = useState<CFV[]>([])
const [updateDocumentCustomFields, {error}] =
useUpdateDocumentCustomFieldsMutation()
const [updateDocumentType] = useUpdateDocumentTypeMutation()

const {data: documentCustomFields, isSuccess: isSuccessDocumentCustomFields} =
useGetDocumentCustomFieldsQuery(docID ?? skipToken)

Expand Down Expand Up @@ -127,47 +128,33 @@ export default function CustomFields() {
))

const onDocumentTypeChange = async (_: any, option: ComboboxItem) => {
if (documentTypeID == null) {
// means document does not have a document type yet ->
// no CFV associated to lose
const data = {
document_id: docID!,
body: {
document_type_id: documentTypeID
}
const documentTypeIDToInvalidate =
doc?.document_type_id || (option ? option.value : undefined)

const data = {
document_id: docID!,
invalidatesTags: {
documentTypeID: documentTypeIDToInvalidate
},
body: {
document_type_id: option ? option.value : null
}
await updateDocumentType(data)
setDocumentTypeID(option)
return
}
await updateDocumentType(data)

setDocumentTypeID(option)
if (option && option.value != doc?.document_type_id) {
setShowSaveButton(true)
} else {
setShowSaveButton(false)
}
if (
documentTypeID &&
documentTypeID.value == doc?.document_type_id &&
isSuccessDocumentCustomFields &&
documentCustomFields &&
documentCustomFields.length > 0
) {
const initialCustFieldValues = documentCustomFields.map(i => {
return {...i, value: i.value}
})
setCustomFieldValues(initialCustFieldValues)
}
setCustomFieldValues([])
setShowSaveButton(false)
}

const onClear = () => {
const onClearDocumentType = () => {
setDocumentTypeID(null)
setShowSaveButton(true)
setCustomFieldValues([])
}

const onSave = async () => {
if (documentCustomFields && documentCustomFields.length > 0) {
if (customFieldValues && customFieldValues.length > 0) {
// document already has custom fields associated
// we need to update existing custom field value
const content = customFieldValues.map(i => {
Expand Down Expand Up @@ -203,7 +190,7 @@ export default function CustomFields() {
value={documentTypeID ? documentTypeID.value : null}
placeholder="Pick Value"
onChange={onDocumentTypeChange}
onClear={onClear}
onClear={onClearDocumentType}
clearable
/>
{genericCustomFieldsComponents}
Expand Down
2 changes: 2 additions & 0 deletions ui2/src/features/document/components/customFields/Date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default function CustomFieldDate({
const DATE_FORMAT = "YYYY-MM-DD"
const strValue = d.format(DATE_FORMAT)
onChange({customField, value: strValue})
} else {
onChange({customField, value: ""})
}
setValue(value)
}
Expand Down

0 comments on commit 95fb491

Please sign in to comment.