Skip to content

Commit f074c5e

Browse files
committed
Fixes to widget copy issues in my-widgets component
1 parent d68fd86 commit f074c5e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/components/my-widgets-page.jsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ const MyWidgetsPage = () => {
182182
noAccess: true,
183183
})
184184
}
185+
} else if (state.pendingWidgetHash && widgetList.page == widgetList.totalPages) {
186+
// special case to handle widget copy behavior
187+
// because state.widgetHash and widgetList.instances are both updated concurrently, race conditions could occur
188+
// to resolve this, set a special flag that's addressed AFTER the instance list is re-fetched
189+
// widgetHash is updated only once widgetList.instances is fully populated
190+
// unfortunately this is less responsive than the normal loading of instances, but oh well
191+
let hash = state.pendingWidgetHash
192+
let {pendingWidgetHash, ...stateCopy} = state
193+
setState({
194+
...stateCopy,
195+
widgetHash: hash
196+
})
185197
}
186198
}, [widgetList.instances, state.widgetHash])
187199

@@ -246,11 +258,13 @@ const MyWidgetsPage = () => {
246258
{
247259
// Still waiting on the widget list to refresh, return to a 'loading' state and indicate a post-fetch change is coming.
248260
onSettled: newInstId => {
249-
// Setting selectedInst to null again to avoid race conditions.
261+
// race conditions require we reset selectedInst and widgetHash to null prior to updating those values
262+
// instead, pendingWidgetHash will be used to update widgetHash once the instance list has fully reloaded
250263
setState({
251264
...state,
252265
selectedInst: null,
253-
widgetHash: newInstId
266+
widgetHash: null,
267+
pendingWidgetHash: newInstId
254268
})
255269

256270
setWidgetList({

0 commit comments

Comments
 (0)