Skip to content

Commit

Permalink
refactor: rename to address
Browse files Browse the repository at this point in the history
  • Loading branch information
eoaksnes committed Oct 26, 2023
1 parent 98346d1 commit 4d3babc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
"name": "List",
"type": "CORE:UiRecipe",
"plugin": "table-plugin",
"plugin": "@development-framework/dm-core-plugins/list",
"dimensions": "*",
"config": {
"type": "PLUGINS:dm-core-plugins/list/ListPluginConfig",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
import React, { useMemo, useState } from 'react'
import { Button } from '@equinor/eds-core-react'

const TablePlugin = (props: IUIPlugin) => {
const { idReference } = props
const TestUseListPlugin = (props: IUIPlugin) => {
const { idReference: address } = props

const {
items,
Expand All @@ -26,7 +26,7 @@ const TablePlugin = (props: IUIPlugin) => {
save,
dirtyState,
updateAttribute,
} = useList<TGenericObject>(idReference)
} = useList<TGenericObject>(address)

const [paginationPage, setPaginationPage] = useState(0)
const [paginationRowsPerPage, setPaginationRowsPerPage] = useState(10)
Expand Down Expand Up @@ -113,4 +113,4 @@ const TablePlugin = (props: IUIPlugin) => {
)
}

export { TablePlugin }
export { TestUseListPlugin }
6 changes: 3 additions & 3 deletions example/src/plugins/table/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TUiPluginMap } from '@development-framework/dm-core'

import { TablePlugin } from './TablePlugin'
import { TestUseListPlugin } from './TestUseListPlugin'

export default {
'table-plugin': {
component: TablePlugin,
'test-use-list-plugin': {
component: TestUseListPlugin,
},
} as TUiPluginMap
49 changes: 26 additions & 23 deletions packages/dm-core/src/hooks/useList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,8 @@ export function useList<T>(idReference: string): IUseListReturnType<T> {
address: idReference,
depth: 0,
})
.then(async (response: any) => {
if (!attribute.contained) {
const resolved = await dmssAPI.documentGet({
address: idReference,
depth: 1,
})
const items = Object.values(response.data).map((data, index) => ({
key: crypto.randomUUID(),
index: index,
// @ts-ignore
data: resolved.data[index],
reference: data,
}))
// @ts-ignore
setItems(items)
setError(null)
} else {
.then(async (response: AxiosResponse) => {
if (attribute.contained) {
const items = Object.values(response.data).map((data, index) => ({
key: crypto.randomUUID(),
index: index,
Expand All @@ -90,6 +75,23 @@ export function useList<T>(idReference: string): IUseListReturnType<T> {
// @ts-ignore
setItems(items)
setError(null)
} else {
const resolved = await dmssAPI.documentGet({
address: idReference,
depth: 1,
})
if (Array.isArray(resolved.data)) {
const items = Object.values(response.data).map((data, index) => ({
key: crypto.randomUUID(),
index: index,
// @ts-ignore
data: resolved.data[index],
reference: data,
}))
// @ts-ignore
setItems(items)
setError(null)
}
}
})
.catch((error: AxiosError<ErrorResponse>) => {
Expand Down Expand Up @@ -199,12 +201,13 @@ export function useList<T>(idReference: string): IUseListReturnType<T> {
const index = items.findIndex(
(item: TItem<T>) => item.key === itemToUpdate.key
)
const address = attribute?.contained
? `${idReference}[${index}]`
: items[index].reference?.address
if (!address) return
return dmssAPI
.documentUpdate({
// @ts-ignore
idAddress: attribute?.contained
? `${idReference}[${index}]`
: items[index].reference?.address,
idAddress: address,
data: JSON.stringify(newDocument),
updateUncontained: false,
})
Expand Down Expand Up @@ -261,16 +264,16 @@ export function useList<T>(idReference: string): IUseListReturnType<T> {
}

return {
items: items,
items,
attribute,
isLoading,
error,
dirtyState,
addItem,
removeItem,
addReference,
updateItem,
save,
dirtyState,
updateAttribute,
}
}

0 comments on commit 4d3babc

Please sign in to comment.