From 9860c9aa287ea0aa6a51ec36bd6c20fe3d61e44a Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Tue, 9 Jan 2024 15:16:03 -0500 Subject: [PATCH 1/4] Adds modified property to paginated instance API response. Updates widget query updates to include modified key --- .../app/classes/materia/widget/instance/manager.php | 1 + src/components/hooks/useCopyWidget.jsx | 6 ++++-- src/components/hooks/useDeleteWidget.jsx | 3 ++- src/components/hooks/useUpdateWidget.jsx | 13 +++++++++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/fuel/app/classes/materia/widget/instance/manager.php b/fuel/app/classes/materia/widget/instance/manager.php index d6ef5f140..81c28c23d 100644 --- a/fuel/app/classes/materia/widget/instance/manager.php +++ b/fuel/app/classes/materia/widget/instance/manager.php @@ -90,6 +90,7 @@ public static function get_paginated_for_user($user_id, $page_number = 0) $data = [ 'pagination' => $displayable_inst, + 'modified' => time(), ]; if ($has_next_page) $data['next_page'] = $page_number + 1; diff --git a/src/components/hooks/useCopyWidget.jsx b/src/components/hooks/useCopyWidget.jsx index 61811706a..a37934bb2 100644 --- a/src/components/hooks/useCopyWidget.jsx +++ b/src/components/hooks/useCopyWidget.jsx @@ -36,7 +36,8 @@ export default function useCopyWidget() { pages: previous.pages.map((page, index) => { if (index == 0) return { ...page, pagination: [ newInst, ...page.pagination] } else return page - }) + }), + modified: Math.floor(Date.now() / 1000) })) return { previousValue } @@ -51,7 +52,8 @@ export default function useCopyWidget() { return inst }) } else return page - }) + }), + modified: Math.floor(Date.now() / 1000) })) variables.successFunc(data) }, diff --git a/src/components/hooks/useDeleteWidget.jsx b/src/components/hooks/useDeleteWidget.jsx index 036b564cc..c956f4d11 100644 --- a/src/components/hooks/useDeleteWidget.jsx +++ b/src/components/hooks/useDeleteWidget.jsx @@ -19,7 +19,8 @@ export default function useDeleteWidget() { pages: previous.pages.map((page) => ({ ...page, pagination: page.pagination.filter(widget => widget.id !== inst.instId) - })) + })), + modified: Math.floor(Date.now() / 1000) } }) diff --git a/src/components/hooks/useUpdateWidget.jsx b/src/components/hooks/useUpdateWidget.jsx index 39321e331..284952168 100644 --- a/src/components/hooks/useUpdateWidget.jsx +++ b/src/components/hooks/useUpdateWidget.jsx @@ -16,7 +16,7 @@ export default function useUpdateWidget() { widgetList = queryClient.getQueryData('widgets') // widgetList is passed to onSuccess or onError depending on resolution of mutation function - return { widgetList } + return { ...widgetList } }, onSuccess: (updatedInst, variables) => { @@ -26,7 +26,7 @@ export default function useUpdateWidget() { if (inst.id === variables.args[0]) { inst.open_at = `${variables.args[4]}` inst.close_at = `${variables.args[5]}` - inst.attempts = `${variables.args[6]}` + inst.attempts = parseInt(variables.args[6]) inst.guest_access = variables.args[7] inst.embedded_only = variables.args[8] break @@ -34,9 +34,14 @@ export default function useUpdateWidget() { } } - // update query cache for widgets. This does NOT invalidate the cache, forcing a re-fetch!! - queryClient.setQueryData('widgets', widgetList) + queryClient.setQueryData('widgets', previous => { + return { + ...widgetList, + modified: Math.floor(Date.now() / 1000) + } + }) + queryClient.invalidateQueries(['user-perms', variables.args[0]]) variables.successFunc(updatedInst) From 5279fc56b1f8e3ed2df26d13c7954caddef7a460 Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Tue, 9 Jan 2024 16:45:44 -0500 Subject: [PATCH 2/4] Modified addl instance properties to be cast as ints instead of strings in useUpdateWidget hook --- src/components/hooks/useUpdateWidget.jsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/components/hooks/useUpdateWidget.jsx b/src/components/hooks/useUpdateWidget.jsx index 284952168..ba8ed155c 100644 --- a/src/components/hooks/useUpdateWidget.jsx +++ b/src/components/hooks/useUpdateWidget.jsx @@ -15,17 +15,21 @@ export default function useUpdateWidget() { await queryClient.cancelQueries('widgets') widgetList = queryClient.getQueryData('widgets') + console.log(widgetList) + // widgetList is passed to onSuccess or onError depending on resolution of mutation function return { ...widgetList } }, onSuccess: (updatedInst, variables) => { + console.log(widgetList) + // update successful - insert new values into our local copy of widgetList for (const page of widgetList?.pages) { for (const inst of page?.pagination) { if (inst.id === variables.args[0]) { - inst.open_at = `${variables.args[4]}` - inst.close_at = `${variables.args[5]}` + inst.open_at = parseInt(variables.args[4]) + inst.close_at = parseInt(variables.args[5]) inst.attempts = parseInt(variables.args[6]) inst.guest_access = variables.args[7] inst.embedded_only = variables.args[8] From eaddcdfd9105e43c613cf2c1d40a83207fd77ef2 Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Tue, 9 Jan 2024 17:05:06 -0500 Subject: [PATCH 3/4] Modified property in API response unnecessary, pulling it back out --- fuel/app/classes/materia/widget/instance/manager.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fuel/app/classes/materia/widget/instance/manager.php b/fuel/app/classes/materia/widget/instance/manager.php index 81c28c23d..ff345715f 100644 --- a/fuel/app/classes/materia/widget/instance/manager.php +++ b/fuel/app/classes/materia/widget/instance/manager.php @@ -89,8 +89,7 @@ public static function get_paginated_for_user($user_id, $page_number = 0) $displayable_inst = array_slice($displayable_inst, $offset, $widgets_per_page); $data = [ - 'pagination' => $displayable_inst, - 'modified' => time(), + 'pagination' => $displayable_inst ]; if ($has_next_page) $data['next_page'] = $page_number + 1; From c7b5d7a533290995b59b7b39efe5a4f8955cb896 Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Tue, 9 Jan 2024 17:09:35 -0500 Subject: [PATCH 4/4] Forgot console logs --- src/components/hooks/useUpdateWidget.jsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/components/hooks/useUpdateWidget.jsx b/src/components/hooks/useUpdateWidget.jsx index ba8ed155c..96c563369 100644 --- a/src/components/hooks/useUpdateWidget.jsx +++ b/src/components/hooks/useUpdateWidget.jsx @@ -15,15 +15,11 @@ export default function useUpdateWidget() { await queryClient.cancelQueries('widgets') widgetList = queryClient.getQueryData('widgets') - console.log(widgetList) - // widgetList is passed to onSuccess or onError depending on resolution of mutation function return { ...widgetList } }, onSuccess: (updatedInst, variables) => { - console.log(widgetList) - // update successful - insert new values into our local copy of widgetList for (const page of widgetList?.pages) { for (const inst of page?.pagination) {