Skip to content

Commit

Permalink
fix: dont save the whole table on normal add
Browse files Browse the repository at this point in the history
  • Loading branch information
eoaksnes authored and awesthouse committed Dec 6, 2023
1 parent 151f3c9 commit 33e3fb7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
7 changes: 6 additions & 1 deletion e2e/tests/plugin-table-car_list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ test('Table car list example', async ({ page }) => {
await page.getByRole('menuitem').first().click()
await page.reload()
await navigate()
await expect(page.getByText('1 - 1 of 1')).toBeVisible()
await expect(page.getByText('1 - 1 of 1')).not.toBeVisible()
})

await test.step('Adding several cars to test pagination', async () => {
await expect(page.getByRole('button', { name: 'Next page' })).toBeDisabled()
await page.getByRole('button', { name: 'Add new row' }).click()
await expect(
page.getByRole('button', { name: 'Open expandable row' })
).toHaveCount(1)
await expect(page.getByText('1 - 1 of 1')).toBeVisible()
await page.getByRole('button', { name: 'Add new row' }).click()
await expect(
page.getByRole('button', { name: 'Open expandable row' })
).toHaveCount(2)
Expand Down
75 changes: 42 additions & 33 deletions packages/dm-core/src/hooks/useList/useList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,55 @@ export function useList<T extends object>(
.finally(() => setLoading(false))
}, [attribute, refresh])

const addItem = useCallback(
async (saveOnAdd: boolean = true, insertAtIndex?: number) => {
if (!attribute) throw new Error('Missing attribute')
if (!attribute.contained) {
throw new Error(
"Can't add item to a list that has uncontained items, need to use addReference method instead"
)
}
setLoading(true)
try {
setDirtyState(true)
const instantiateResponse = await dmssAPI.instantiateEntity({
entity: {
type: attribute?.attributeType,
},
})
async function addItem(saveOnAdd: boolean = true, insertAtIndex?: number) {
if (!attribute) throw new Error('Missing attribute')
if (!attribute.contained) {
throw new Error(
"Can't add item to a list that has uncontained items, need to use addReference method instead"
)
}
setLoading(true)
try {
setDirtyState(true)
const instantiateResponse = await dmssAPI.instantiateEntity({
entity: {
type: attribute?.attributeType,
},
})
const newItem: TItem<T> = utils.createNewItemObject(
instantiateResponse.data,
insertAtIndex || items.length,
saveOnAdd
)

if (insertAtIndex !== undefined) {
const itemsCopy = [...items]
const newItem: TItem<T> = utils.createNewItemObject(
instantiateResponse.data,
insertAtIndex || items.length,
saveOnAdd
)
const index = insertAtIndex !== undefined ? insertAtIndex : items.length
itemsCopy.splice(index, 0, newItem)
itemsCopy.splice(insertAtIndex, 0, newItem)
setItems(itemsCopy)
if (saveOnAdd) {
save(itemsCopy)
await save(itemsCopy)
setDirtyState(false)
}
} catch (error) {
if (isAxiosError(error)) {
setError(error.response?.data || { message: error.name, data: error })
} else {
if (saveOnAdd) {
await dmssAPI.documentAdd({
address: idReference,
document: JSON.stringify(newItem.data),
})
setDirtyState(false)
}
throw error
} finally {
setLoading(false)
const itemsCopy = [...items, newItem]
setItems(itemsCopy)
}
},
[attribute, items]
)
} catch (error) {
if (isAxiosError(error)) {
setError(error.response?.data || { message: error.name, data: error })
}
throw error
} finally {
setLoading(false)
}
}

const removeItem = async (
itemToDelete: TItem<T>,
Expand Down

0 comments on commit 33e3fb7

Please sign in to comment.