feat: handle item edit and remove
This commit is contained in:
parent
a46d4afa75
commit
3e5c2aa607
@ -426,7 +426,13 @@ const DocumentList: FC<IDocumentListProps> = ({
|
|||||||
const isQAMode = chunkingMode === ChunkingMode.qa
|
const isQAMode = chunkingMode === ChunkingMode.qa
|
||||||
const [localDocs, setLocalDocs] = useState<LocalDoc[]>(documents)
|
const [localDocs, setLocalDocs] = useState<LocalDoc[]>(documents)
|
||||||
const [enableSort, setEnableSort] = useState(true)
|
const [enableSort, setEnableSort] = useState(true)
|
||||||
const { isShowEditModal, showEditModal, hideEditModal } = useBatchEditDocumentMetadata({
|
const {
|
||||||
|
isShowEditModal,
|
||||||
|
showEditModal,
|
||||||
|
hideEditModal,
|
||||||
|
originalList,
|
||||||
|
handleSave,
|
||||||
|
} = useBatchEditDocumentMetadata({
|
||||||
list: documents,
|
list: documents,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -655,8 +661,8 @@ const DocumentList: FC<IDocumentListProps> = ({
|
|||||||
{isShowEditModal && (
|
{isShowEditModal && (
|
||||||
<EditMetadataBatchModal
|
<EditMetadataBatchModal
|
||||||
documentNum={selectedIds.length}
|
documentNum={selectedIds.length}
|
||||||
list={[]}
|
list={originalList}
|
||||||
onChange={() => { }}
|
onSave={handleSave}
|
||||||
onHide={hideEditModal}
|
onHide={hideEditModal}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import React, { useCallback, useState } from 'react'
|
import React, { useCallback, useState } from 'react'
|
||||||
import Modal from '../../../base/modal'
|
import Modal from '../../../base/modal'
|
||||||
import { DataType, type MetadataItemWithEdit } from '../types'
|
import type { MetadataItemInBatchEdit } from '../types'
|
||||||
|
import { DataType, type MetadataItemWithEdit, UpdateType } from '../types'
|
||||||
import EditMetadataBatchItem from './edit-row'
|
import EditMetadataBatchItem from './edit-row'
|
||||||
import AddedMetadataItem from './add-row'
|
import AddedMetadataItem from './add-row'
|
||||||
import Button from '../../../base/button'
|
import Button from '../../../base/button'
|
||||||
@ -13,30 +14,45 @@ import SelectMetadataModal from '../metadata-dataset/select-metadata-modal'
|
|||||||
import { RiQuestionLine } from '@remixicon/react'
|
import { RiQuestionLine } from '@remixicon/react'
|
||||||
import Divider from '@/app/components/base/divider'
|
import Divider from '@/app/components/base/divider'
|
||||||
import AddMetadataButton from '../add-metadata-button'
|
import AddMetadataButton from '../add-metadata-button'
|
||||||
|
import produce from 'immer'
|
||||||
|
|
||||||
const i18nPrefix = 'dataset.metadata.batchEditMetadata'
|
const i18nPrefix = 'dataset.metadata.batchEditMetadata'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
documentNum: number
|
documentNum: number
|
||||||
list: MetadataItemWithEdit[]
|
list: MetadataItemInBatchEdit[]
|
||||||
onChange: (list: MetadataItemWithEdit[], addedList: MetadataItemWithEdit[], isApplyToAllSelectDocument: boolean) => void
|
onSave: (list: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => void
|
||||||
onHide: () => void
|
onHide: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const EditMetadataBatchModal: FC<Props> = ({
|
const EditMetadataBatchModal: FC<Props> = ({
|
||||||
documentNum,
|
documentNum,
|
||||||
list,
|
list,
|
||||||
onChange,
|
onSave,
|
||||||
onHide,
|
onHide,
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const [templeList, setTempleList] = useState<MetadataItemWithEdit[]>(list)
|
const [templeList, setTempleList] = useState<MetadataItemWithEdit[]>(list)
|
||||||
const handleTemplesChange = useCallback((payload: MetadataItemWithEdit) => {
|
const handleTemplesChange = useCallback((payload: MetadataItemWithEdit) => {
|
||||||
const newTempleList = templeList.map(i => i.id === payload.id ? payload : i)
|
const newTempleList = produce(templeList, (draft) => {
|
||||||
|
const index = draft.findIndex(i => i.id === payload.id)
|
||||||
|
if (index !== -1) {
|
||||||
|
draft[index] = payload
|
||||||
|
draft[index].isUpdated = true
|
||||||
|
draft[index].updateType = UpdateType.changeValue
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
setTempleList(newTempleList)
|
setTempleList(newTempleList)
|
||||||
}, [templeList])
|
}, [templeList])
|
||||||
const handleTempleItemRemove = useCallback((id: string) => {
|
const handleTempleItemRemove = useCallback((id: string) => {
|
||||||
const newTempleList = templeList.filter(i => i.id !== id)
|
const newTempleList = produce(templeList, (draft) => {
|
||||||
|
const index = draft.findIndex(i => i.id === id)
|
||||||
|
if (index !== -1) {
|
||||||
|
draft[index].isUpdated = true
|
||||||
|
draft[index].updateType = UpdateType.delete
|
||||||
|
}
|
||||||
|
})
|
||||||
setTempleList(newTempleList)
|
setTempleList(newTempleList)
|
||||||
}, [templeList])
|
}, [templeList])
|
||||||
|
|
||||||
@ -63,8 +79,8 @@ const EditMetadataBatchModal: FC<Props> = ({
|
|||||||
const [isApplyToAllSelectDocument, setIsApplyToAllSelectDocument] = useState(false)
|
const [isApplyToAllSelectDocument, setIsApplyToAllSelectDocument] = useState(false)
|
||||||
|
|
||||||
const handleSave = useCallback(() => {
|
const handleSave = useCallback(() => {
|
||||||
onChange(templeList, addedList, isApplyToAllSelectDocument)
|
onSave([...templeList.filter(item => item.updateType !== UpdateType.delete), ...addedList], isApplyToAllSelectDocument)
|
||||||
}, [templeList, addedList, isApplyToAllSelectDocument, onChange])
|
}, [templeList, addedList, isApplyToAllSelectDocument, onSave])
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
title={t(`${i18nPrefix}.editMetadata`)}
|
title={t(`${i18nPrefix}.editMetadata`)}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ const useBatchEditDocumentMetadata = ({
|
|||||||
setFalse: hideEditModal,
|
setFalse: hideEditModal,
|
||||||
}] = useBoolean(false)
|
}] = useBoolean(false)
|
||||||
|
|
||||||
const originalList = useMemo(() => {
|
const originalList: MetadataItemInBatchEdit[] = useMemo(() => {
|
||||||
const idNameValue: Record<string, { value: string | number | null, isMultipleValue: boolean }> = {}
|
const idNameValue: Record<string, { value: string | number | null, isMultipleValue: boolean }> = {}
|
||||||
// TODO: mock backend data struct
|
// TODO: mock backend data struct
|
||||||
// const metaDataList: MetadataItemWithValue[][] = list.map((item, i) => {
|
// const metaDataList: MetadataItemWithValue[][] = list.map((item, i) => {
|
||||||
@ -72,13 +72,63 @@ const useBatchEditDocumentMetadata = ({
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
return res
|
return res
|
||||||
}, [list])
|
}, [])
|
||||||
|
|
||||||
|
const formateToBackendList = (editedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
|
||||||
|
const updatedList = editedList.filter((editedItem) => {
|
||||||
|
const originalItem = originalList.find(i => i.id === editedItem.id)
|
||||||
|
if (!originalItem) // added item
|
||||||
|
return true
|
||||||
|
if (editedItem.value !== originalItem.value)
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
const removedList = originalList.filter((originalItem) => {
|
||||||
|
const editedItem = editedList.find(i => i.id === originalItem.id)
|
||||||
|
if (!editedItem) // removed item
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
const res: { document_id: string, metadata_list: MetadataItemWithValue[] }[] = list.map((item, i) => {
|
||||||
|
// the new metadata will override the old one
|
||||||
|
const oldMetadataList = item.doc_metadata || testMetadataList[i] // TODO: used mock data
|
||||||
|
const newMetadataList: MetadataItemWithValue[] = oldMetadataList
|
||||||
|
.filter((item) => {
|
||||||
|
return !removedList.find(removedItem => removedItem.id === item.id)
|
||||||
|
})
|
||||||
|
.map((item) => {
|
||||||
|
const editedItem = updatedList.find(i => i.id === item.id)
|
||||||
|
if (editedItem)
|
||||||
|
return editedItem
|
||||||
|
return item
|
||||||
|
})
|
||||||
|
if (isApplyToAllSelectDocument) {
|
||||||
|
// add missing metadata item
|
||||||
|
updatedList.forEach((editedItem) => {
|
||||||
|
if (!newMetadataList.find(i => i.id === editedItem.id))
|
||||||
|
newMetadataList.push(editedItem)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
document_id: item.id,
|
||||||
|
metadata_list: newMetadataList,
|
||||||
|
}
|
||||||
|
}).filter(item => item.metadata_list.length > 0)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleSave = (editedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
|
||||||
|
const backendList = formateToBackendList(editedList, isApplyToAllSelectDocument)
|
||||||
|
console.log(backendList)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isShowEditModal,
|
isShowEditModal,
|
||||||
showEditModal,
|
showEditModal,
|
||||||
hideEditModal,
|
hideEditModal,
|
||||||
originalList,
|
originalList,
|
||||||
|
handleSave,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user