Only clear change cache for keys which have been saved and haven't been further altered since the request started!

This commit is contained in:
Andrew Kingston 2024-05-29 09:51:01 +01:00
parent 35842525c5
commit e1ee233aaf
1 changed files with 17 additions and 6 deletions

View File

@ -404,8 +404,11 @@ export const createActions = context => {
// Save change
try {
// Mark as in progress
inProgressChanges.update(state => ({ ...state, [rowId]: true }))
// Incremenet change count for this row
inProgressChanges.update(state => ({
...state,
[rowId]: (state[rowId] || 0) + 1,
}))
// Update row
const changes = get(rowChangeCache)[rowId]
@ -423,17 +426,25 @@ export const createActions = context => {
await refreshRow(saved.id)
}
// Wipe row change cache now that we've saved the row
// Wipe row change cache for any values which have been saved
const liveChanges = get(rowChangeCache)[rowId]
rowChangeCache.update(state => {
delete state[rowId]
Object.keys(changes || {}).forEach(key => {
if (changes[key] === liveChanges?.[key]) {
delete state[rowId][key]
}
})
return state
})
} catch (error) {
handleValidationError(rowId, error)
}
// Mark as completed
inProgressChanges.update(state => ({ ...state, [rowId]: false }))
// Decrement change count for this row
inProgressChanges.update(state => ({
...state,
[rowId]: (state[rowId] || 1) - 1,
}))
}
// Updates a value of a row