-
Couldn't load subscription status.
- Fork 38
feat(VCST-3928): save for later optimistic response #1956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuskithedeveloper
wants to merge
11
commits into
dev
Choose a base branch
from
feat/VCST-3928-save-for-later-optimistic-response
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4554133
feat: optimistic responses
yuskithedeveloper a911126
Merge branch 'dev' into feat/VCST-3928-save-for-later-optimistic-resp…
yuskithedeveloper f233264
Merge branch 'dev' into feat/VCST-3928-save-for-later-optimistic-resp…
yuskithedeveloper 808d9f1
feat: removed batching, getSavedForLater changed to apollo cached query
yuskithedeveloper e9c68fe
Merge branch 'dev' into feat/VCST-3928-save-for-later-optimistic-resp…
yuskithedeveloper fa17118
chore: removed not used code
yuskithedeveloper 15eb43a
Merge branch 'feat/VCST-3928-save-for-later-optimistic-response' of h…
yuskithedeveloper 4e9ca92
refactor: undefined checks
yuskithedeveloper f1fd410
Merge branch 'dev' into feat/VCST-3928-save-for-later-optimistic-resp…
yuskithedeveloper 67c84ea
fix: accidentally removed file
yuskithedeveloper 9ad902b
chore: removed not used code
yuskithedeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
client-app/core/api/graphql/cart/queries/getSavedForLater/index.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,68 @@ | ||
| import { ApolloError } from "@apollo/client/core"; | ||
| import { useMutation } from "@vue/apollo-composable"; | ||
| import { useMutation, useQuery } from "@vue/apollo-composable"; | ||
| import { createSharedComposable } from "@vueuse/core"; | ||
| import { ref, computed } from "vue"; | ||
| import { computed } from "vue"; | ||
| import { AbortReason } from "@/core/api/common/enums"; | ||
| import { getSavedForLater as getSavedForLaterQuery } from "@/core/api/graphql/cart/queries/getSavedForLater"; | ||
| import { MoveToSavedForLaterDocument, MoveFromSavedForLaterDocument } from "@/core/api/graphql/types"; | ||
| import { useMutationBatcher } from "@/core/composables/useMutationBatcher"; | ||
| import { | ||
| MoveToSavedForLaterDocument, | ||
| MoveFromSavedForLaterDocument, | ||
| GetSavedForLaterDocument, | ||
| } from "@/core/api/graphql/types"; | ||
| import { globals } from "@/core/globals"; | ||
| import { Logger } from "@/core/utilities"; | ||
| import type { SavedForLaterListFragment } from "@/core/api/graphql/types"; | ||
| import { useFullCart } from "@/shared/cart"; | ||
|
|
||
| function _useSavedForLater() { | ||
| const { storeId, currencyCode, cultureName, userId } = globals; | ||
|
|
||
| const savedForLaterList = ref<SavedForLaterListFragment>(); | ||
| const { cart } = useFullCart(); | ||
|
|
||
| const { mutate: _moveToSavedForLater, loading: _moveToSavedForLaterLoading } = | ||
| useMutation(MoveToSavedForLaterDocument); | ||
| const savedForLaterList = computed(() => _savedForLaterQueryResult.value?.getSavedForLater); | ||
|
|
||
| const { | ||
| add: _moveToSavedForLaterBatched, | ||
| overflowed: moveToSavedForLaterOverflowed, | ||
| loading: _moveToSavedForLaterBatchedLoading, | ||
| } = useMutationBatcher(_moveToSavedForLater); | ||
| refetch: getSavedForLater, | ||
| loading: _getSavedForLaterLoading, | ||
| result: _savedForLaterQueryResult, | ||
| } = useQuery( | ||
| GetSavedForLaterDocument, | ||
| { | ||
| storeId, | ||
| userId, | ||
| cultureName, | ||
| currencyCode, | ||
| }, | ||
| { | ||
| notifyOnNetworkStatusChange: true, | ||
| fetchPolicy: "cache-first", | ||
| }, | ||
| ); | ||
|
|
||
| const { mutate: _moveToSavedForLater, loading: _moveToSavedForLaterLoading } = useMutation( | ||
| MoveToSavedForLaterDocument, | ||
| { | ||
| optimisticResponse(vars, { IGNORE }) { | ||
| const movedItemIds = vars.command?.lineItemIds; | ||
|
|
||
| if (!movedItemIds?.length || !cart.value || !savedForLaterList.value) { | ||
| return IGNORE; | ||
| } | ||
|
|
||
| return { | ||
| moveToSavedForLater: { | ||
| cart: { | ||
| ...cart.value, | ||
| items: cart.value.items.filter((item) => !movedItemIds.includes(item.id)), | ||
| }, | ||
| list: savedForLaterList.value, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| async function moveToSavedForLater(cartId: string, itemIds: string[]) { | ||
| try { | ||
| const moveResult = await _moveToSavedForLaterBatched({ | ||
| await _moveToSavedForLater({ | ||
| command: { | ||
| storeId, | ||
| userId, | ||
|
|
@@ -36,8 +72,6 @@ function _useSavedForLater() { | |
| lineItemIds: itemIds, | ||
| }, | ||
| }); | ||
|
|
||
| savedForLaterList.value = moveResult?.data?.moveToSavedForLater?.list; | ||
| } catch (err) { | ||
| if (err instanceof ApolloError && err.networkError?.toString() === (AbortReason.Explicit as string)) { | ||
| return; | ||
|
|
@@ -46,18 +80,32 @@ function _useSavedForLater() { | |
| } | ||
| } | ||
|
|
||
| const { mutate: _moveFromSavedForLater, loading: _moveFromSavedForLaterLoading } = | ||
| useMutation(MoveFromSavedForLaterDocument); | ||
| const { mutate: _moveFromSavedForLater, loading: _moveFromSavedForLaterLoading } = useMutation( | ||
| MoveFromSavedForLaterDocument, | ||
| { | ||
| optimisticResponse(vars, { IGNORE }) { | ||
| const movedItemIds = vars.command?.lineItemIds; | ||
|
|
||
| const { | ||
| add: _moveFromSavedForLaterBatched, | ||
| overflowed: moveFromSavedForLaterOverflowed, | ||
| loading: _moveFromSavedForLaterBatchedLoading, | ||
| } = useMutationBatcher(_moveFromSavedForLater); | ||
| if (!movedItemIds?.length || !cart.value || !savedForLaterList.value) { | ||
| return IGNORE; | ||
| } | ||
|
|
||
| return { | ||
| moveFromSavedForLater: { | ||
| cart: cart.value, | ||
| list: { | ||
| ...savedForLaterList.value, | ||
| items: savedForLaterList.value.items.filter((item) => !movedItemIds.includes(item.id)), | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| async function moveFromSavedForLater(cartId: string, itemIds: string[]) { | ||
| try { | ||
| const moveResult = await _moveFromSavedForLaterBatched({ | ||
| await _moveFromSavedForLater({ | ||
| command: { | ||
| storeId, | ||
| userId, | ||
|
|
@@ -67,8 +115,6 @@ function _useSavedForLater() { | |
| lineItemIds: itemIds, | ||
| }, | ||
| }); | ||
|
|
||
| savedForLaterList.value = moveResult?.data?.moveFromSavedForLater?.list; | ||
| } catch (err) { | ||
| if (err instanceof ApolloError && err.networkError?.toString() === (AbortReason.Explicit as string)) { | ||
| return; | ||
|
|
@@ -77,29 +123,15 @@ function _useSavedForLater() { | |
| } | ||
| } | ||
|
|
||
| async function getSavedForLater() { | ||
| try { | ||
| savedForLaterList.value = await getSavedForLaterQuery(); | ||
| } catch (err) { | ||
| Logger.error(`useSavedForLater.${getSavedForLater.name}`, err); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| savedForLaterList, | ||
|
|
||
| moveToSavedForLater, | ||
| moveToSavedForLaterOverflowed, | ||
| moveFromSavedForLater, | ||
| moveFromSavedForLaterOverflowed, | ||
| getSavedForLater, | ||
|
|
||
| loading: computed( | ||
| () => | ||
| _moveToSavedForLaterLoading.value || | ||
| _moveToSavedForLaterBatchedLoading.value || | ||
| _moveFromSavedForLaterLoading.value || | ||
| _moveFromSavedForLaterBatchedLoading.value, | ||
| () => _getSavedForLaterLoading.value || _moveToSavedForLaterLoading.value || _moveFromSavedForLaterLoading.value, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Items Temporarily Disappear During Move
The optimistic responses for
moveToSavedForLaterandmoveFromSavedForLaterare incomplete. Items are correctly removed from their source list (cart or saved for later) but aren't added to their destination. This makes items temporarily disappear from the UI, creating a confusing experience until the server response updates the state.Additional Locations (1)
client-app/shared/cart/composables/useSaveForLater.ts#L85-L104