-
Notifications
You must be signed in to change notification settings - Fork 685
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
Add to Wishlist from PDP #3048
Merged
Merged
Add to Wishlist from PDP #3048
Changes from 8 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
1549954
initial work to add CE wishlist functionality on pdp
sirugh 4584c4f
Use 0 as wishlist id for CE
sirugh ee374fc
Adding configurableproduct via selected_options array
sirugh 022152b
Fixing logic for temporary item added
sirugh 520fd87
migrate logic to wishlist talons with compatible extension
sirugh d7ad759
Added to favorites ux is favorable over a technically challenging rem…
sirugh a01978f
bool name
sirugh 13563a0
EE workflow
sirugh 688bd0d
Feedback
sirugh 2d43447
styles
sirugh 95c1c87
fix gql warnings
sirugh 94c5fbf
Merge branch 'develop' into rugh/pwa-1267-add-to-wishlist-pdp
sirugh bb6bbbd
Add messages and some cleanup
sirugh c24cfb7
capitalization
sirugh 8f6cc19
id for i18n
sirugh 92984f1
Only close new list form when cancel is clicked
sirugh f3ae3ac
Update icon styles for wishlist button
sirugh e552a97
Add underline to mimic link
sirugh 9350eb1
wrap button in suspense to lazy load
sirugh cb5245e
Merge branch 'develop' into rugh/pwa-1267-add-to-wishlist-pdp
sirugh 5fd6431
Address feedback.
sirugh d4fea5a
id is required on customer
sirugh 4fe06c4
Remove unnecessary fragment
sirugh 286f051
Rename to createWishlistForm
sirugh 58ca5a8
Merge branch 'develop' into rugh/pwa-1267-add-to-wishlist-pdp
sirugh 9b03aa0
Fix a bug for simple items where configurable_options was null
sirugh cca24a7
Minimal fixes to existing tests to get passing.
sirugh 917aec1
Remove unused prop
sirugh a278ed1
Test changes to pfd talon
sirugh 064f68a
Move and test wishlistLineItem
sirugh ee14ca4
Test useWishlistButton.ce.js
sirugh a9d06c3
Test useWishlistButton.ee.js
sirugh f9f883f
Test useCreateWishlistForm
sirugh c95cbeb
test useWishlistDialog
sirugh e7b3ee0
Test productFullDetail
sirugh f53e537
Test wishlistButton.ce
sirugh e0e1c25
Test wishlistButton.ee
sirugh 128e742
Test CreateWishlistForm
sirugh 4a68c9c
Test WishlistDialog
sirugh 861bb7a
Merge remote-tracking branch 'origin/develop' into rugh/pwa-1267-add-…
sirugh de93f85
Merge branch 'develop' into rugh/pwa-1267-add-to-wishlist-pdp
dpatil-magento 2e5ee88
Add enclosing root div to button components and pack content around c…
sirugh 4d14627
fix snaps
sirugh 5620add
Use a single child of button to contain items
sirugh 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
This file contains 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 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ export const ProductDetailsFragment = gql` | |
id | ||
label | ||
values { | ||
uid | ||
default_label | ||
label | ||
store_label | ||
|
47 changes: 47 additions & 0 deletions
47
packages/peregrine/lib/talons/Wishlist/WishlistButton/useWishlistButton.ce.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useMutation } from '@apollo/client'; | ||
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge'; | ||
|
||
import DEFAULT_OPERATIONS from './wishlistButton.gql'; | ||
|
||
export const useWishlistButton = props => { | ||
const { itemOptions } = props; | ||
const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations); | ||
|
||
const { addProductToWishlistMutation } = operations; | ||
|
||
const [isItemAdded, setIsItemAdded] = useState(false); | ||
|
||
useEffect(() => { | ||
if (itemOptions.selected_options) setIsItemAdded(false); | ||
supernova-at marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, [itemOptions.selected_options]); | ||
|
||
const [ | ||
addProductToWishlist, | ||
{ loading: isAddLoading, error: addProductError } | ||
] = useMutation(addProductToWishlistMutation); | ||
|
||
const handleClick = useCallback(async () => { | ||
try { | ||
await addProductToWishlist({ | ||
variables: { | ||
// TODO: "0" will create a wishlist if doesn't exist, and add to one if it does, regardless of the user's single wishlist id. In 2.4.3 this will be "fixed" by removing the `wishlistId` param entirely because all users will have a wishlist created automatically in CE. So should only have to pass items and it will add correctly. | ||
wishlistId: '0', | ||
itemOptions | ||
} | ||
}); | ||
setIsItemAdded(true); | ||
} catch (err) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(err); | ||
} | ||
} | ||
}, [addProductToWishlist, itemOptions]); | ||
|
||
return { | ||
addProductError, | ||
handleClick, | ||
isDisabled: isItemAdded || isAddLoading, | ||
isItemAdded | ||
}; | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/peregrine/lib/talons/Wishlist/WishlistButton/useWishlistButton.ee.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
export const useWishlistButton = props => { | ||
const { itemOptions } = props; | ||
|
||
const [isItemAdded, setIsItemAdded] = useState(false); | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
|
||
useEffect(() => { | ||
if (itemOptions.selected_options) setIsItemAdded(false); | ||
}, [itemOptions.selected_options]); | ||
|
||
const handleButtonClick = useCallback(() => { | ||
setIsModalOpen(true); | ||
}, []); | ||
|
||
const handleModalClose = useCallback(success => { | ||
setIsModalOpen(false); | ||
|
||
// only set item added true if someone calls handleModalClose(true) | ||
if (success === true) { | ||
setIsItemAdded(true); | ||
} | ||
}, []); | ||
|
||
return { | ||
handleButtonClick, | ||
handleModalClose, | ||
isDisabled: isItemAdded || isModalOpen, | ||
isItemAdded, | ||
isModalOpen | ||
}; | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/peregrine/lib/talons/Wishlist/WishlistButton/wishlistButton.gql.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { gql } from '@apollo/client'; | ||
|
||
export const ADD_TO_WISHLIST = gql` | ||
mutation addProductToWishlist( | ||
$wishlistId: ID! | ||
$itemOptions: WishlistItemInput! | ||
) { | ||
addProductsToWishlist( | ||
wishlistId: $wishlistId | ||
wishlistItems: [$itemOptions] | ||
) { | ||
user_errors { | ||
code | ||
message | ||
} | ||
} | ||
} | ||
`; | ||
export default { | ||
addProductToWishlistMutation: ADD_TO_WISHLIST | ||
}; |
19 changes: 19 additions & 0 deletions
19
packages/peregrine/lib/talons/Wishlist/WishlistDialog/NewWishlistForm/newWishlistForm.gql.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { gql } from '@apollo/client'; | ||
|
||
/* https://devdocs.magento.com/guides/v2.4/graphql/mutations/create-wishlist.html */ | ||
export const CREATE_WISHLIST = gql` | ||
mutation createWishlist( | ||
$name: String! | ||
$visibility: WishlistVisibilityEnum! | ||
) { | ||
createWishlist(input: { name: $name, visibility: $visibility }) { | ||
wishlist { | ||
id | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export default { | ||
createWishlistMutation: CREATE_WISHLIST | ||
}; |
56 changes: 56 additions & 0 deletions
56
packages/peregrine/lib/talons/Wishlist/WishlistDialog/NewWishlistForm/useNewWishlistForm.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useMutation } from '@apollo/client'; | ||
|
||
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge'; | ||
|
||
import DEFAULT_OPERATIONS from './newWishlistForm.gql'; | ||
import { useFormState } from 'informed'; | ||
|
||
export const useNewWishlistForm = props => { | ||
const { onCreateList, isAddLoading } = props; | ||
const { values } = useFormState(); | ||
const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations); | ||
const [ | ||
createList, | ||
{ loading: isCreateLoading, error: createWishlistError } | ||
] = useMutation(operations.createWishlistMutation); | ||
|
||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const handleNewListClick = useCallback(() => { | ||
setIsOpen(true); | ||
}, []); | ||
|
||
const handleCancel = useCallback(() => { | ||
setIsOpen(false); | ||
}, []); | ||
|
||
const handleSave = useCallback(async () => { | ||
try { | ||
const { listname, visibility } = values; | ||
|
||
const { data } = await createList({ | ||
variables: { | ||
name: listname, | ||
visibility | ||
} | ||
}); | ||
const wishlistId = data.createWishlist.wishlist.id; | ||
|
||
onCreateList(wishlistId); | ||
} catch (err) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(err); | ||
} | ||
} | ||
}, [createList, onCreateList, values]); | ||
|
||
return { | ||
formErrors: [createWishlistError], | ||
handleCancel, | ||
handleNewListClick, | ||
handleSave, | ||
isOpen, | ||
isSaveDisabled: isCreateLoading || isAddLoading | ||
}; | ||
}; |
55 changes: 55 additions & 0 deletions
55
packages/peregrine/lib/talons/Wishlist/WishlistDialog/useWishlistDialog.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useCallback } from 'react'; | ||
import { useQuery, useMutation } from '@apollo/client'; | ||
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge'; | ||
|
||
import DEFAULT_OPERATIONS from './wishlistDialog.gql'; | ||
|
||
export const useWishlistDialog = props => { | ||
const { itemOptions, onClose } = props; | ||
const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations); | ||
|
||
const { data: wishlistsData } = useQuery(operations.getWishlistsQuery, { | ||
fetchPolicy: 'cache-and-network' | ||
}); | ||
|
||
const [ | ||
addProductToWishlist, | ||
{ loading: isAddLoading, error: addProductError } | ||
] = useMutation(operations.addProductToWishlistMutation); | ||
|
||
// enable_multiple_wishlists is a string "1" or "0". See documentation here: | ||
// https://devdocs.magento.com/guides/v2.4/graphql/mutations/create-wishlist.html | ||
const canCreateWishlist = | ||
wishlistsData && | ||
!!wishlistsData.storeConfig.enable_multiple_wishlists && | ||
wishlistsData.storeConfig.maximum_number_of_wishlists > | ||
wishlistsData.customer.wishlists.length; | ||
|
||
const handleAddToWishlist = useCallback( | ||
async wishlistId => { | ||
console.log('adding to id', wishlistId, itemOptions); | ||
try { | ||
await addProductToWishlist({ | ||
variables: { | ||
wishlistId, | ||
itemOptions | ||
} | ||
}); | ||
onClose(true); | ||
} catch (err) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(err); | ||
} | ||
} | ||
}, | ||
[addProductToWishlist, itemOptions, onClose] | ||
); | ||
|
||
return { | ||
formErrors: [addProductError], | ||
canCreateWishlist, | ||
handleAddToWishlist, | ||
isAddLoading, | ||
wishlistsData | ||
}; | ||
}; |
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.
Good use of
useMemo
to convert this array to a map. 👍Optionally, a small optimization would be to skip the intermediate array (faster, less memory):
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.
Done.