This repository was archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
ref: useUserAddressFactory #381
Open
defudef
wants to merge
9
commits into
master
Choose a base branch
from
rfc-use-user-address-factory
base: master
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
9 commits
Select commit
Hold shift + click to select a range
a265b82
rfc proposition
b84723f
Merge branch 'master' of github.com:DivanteLtd/next
1aa9638
rfc proposition
8c4e73a
removed not related rfc
19a56f3
Merge branch 'master' of github.com:DivanteLtd/next into rfc-use-user…
6a639e9
updated rfc
b9c2d19
removed useUser from useUSerFactory
55cdac0
updated rfc according to decisions made
f9e28cd
updated rfc according to decision making meeting
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 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 | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,120 @@ | ||||||||
| # UseUserAddressFactory with Commerce Tools composable use case | ||||||||
|
|
||||||||
| ## Motivation | ||||||||
|
|
||||||||
| We need to extend useUserAddress type to show and modify user addresses. | ||||||||
| Also, some eCommerce platforms requires updating user entity with every address modification (for example: CommerceTools). | ||||||||
|
|
||||||||
| ## Composable interface | ||||||||
| ```TS | ||||||||
| export interface UseUserAddress<ADDRESS, SEARCH_PARAMS, ADDRESS_OPTIONS = unknown> { | ||||||||
| addresses: ComputedProperty<ADDRESS[]>; | ||||||||
| shippingAddresses: ComputedProperty<ADDRESS[]>; | ||||||||
| billingAddresses: ComputedProperty<ADDRESS[]>; | ||||||||
| loadAddresses: (params?: SEARCH_PARAMS) => Promise<void>; | ||||||||
| addAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<void>; | ||||||||
|
Contributor
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. what are these |
||||||||
| updateAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<void>; | ||||||||
|
Comment on lines
+15
to
+16
Contributor
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.
Suggested change
|
||||||||
| deleteAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<void>; | ||||||||
| loading: ComputedProperty<boolean>; | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ## Factory params interface | ||||||||
| ```TS | ||||||||
| // factory params schema | ||||||||
| export type UseUserAddressFactoryParams<ADDRESS, SEARCH_PARAMS, ADDRESS_OPTIONS = unknown> = { | ||||||||
| loadAddresses: (params?: SEARCH_PARAMS) => Promise<ADDRESS[]>; | ||||||||
| getBillingAddresses: (addresses: ADDRESS[]) => ADDRESS[]; | ||||||||
| getShippingAddresses: (addresses: ADDRESS[]) => ADDRESS[]; | ||||||||
| addAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<ADDRESS[]>; | ||||||||
| updateAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<ADDRESS[]>; | ||||||||
| deleteAddress: (address: ADDRESS, options?: ADDRESS_OPTIONS) => Promise<ADDRESS[]>; | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ## Other changes requirements: | ||||||||
| We need to add a possibility to update a user | ||||||||
| ```TS | ||||||||
| // FACTORY INTERFACE | ||||||||
| export interface UseUser | ||||||||
| < | ||||||||
| USER, | ||||||||
| UPDATE_USER_PARAMS | ||||||||
| > { | ||||||||
| // ...leave all the stuff as they are now | ||||||||
|
|
||||||||
| // needed to add this: | ||||||||
| setUser: (newUser: USER) => void; | ||||||||
|
Contributor
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. It shouldn't be a part of params, we have access to this object from the inside therefore we can just return this fn
Contributor
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. Please take a look at @andrzejewsky RFC |
||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ```TS | ||||||||
| // FACTORY IMPLEMENTATION | ||||||||
| export function useUserFactory<USER, UPDATE_USER_PARAMS, REGISTER_USER_PARAMS extends { email: string; password: string }>( | ||||||||
| factoryParams: UseUserFactoryParams<USER, UPDATE_USER_PARAMS, REGISTER_USER_PARAMS> | ||||||||
| ) { | ||||||||
| // ...leave all the stuff as they are now | ||||||||
|
|
||||||||
| // needed to add this: | ||||||||
| const setUser = (newUser: USER) => { | ||||||||
| user.value = newUser; | ||||||||
| }; | ||||||||
|
|
||||||||
| return { | ||||||||
| user: computed(() => user.value), | ||||||||
| setUser, // required to be added | ||||||||
| updateUser, | ||||||||
| register, | ||||||||
| login, | ||||||||
| logout, | ||||||||
| isAuthenticated, | ||||||||
| changePassword, | ||||||||
| refreshUser, | ||||||||
| loading: computed(() => loading.value) | ||||||||
| }; | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ## Commerce Tools Integration | ||||||||
| In order to integrate CT we need a user instance to update it. It's required, because addresses are being updated through user update endpoint. | ||||||||
|
|
||||||||
| Below example when adding an address: | ||||||||
|
|
||||||||
| ### Factory params | ||||||||
| ```TS | ||||||||
| import { setUser } from './useUser'; | ||||||||
|
|
||||||||
| const factoryParams = { | ||||||||
| addAddress: async (address) => { | ||||||||
| const { user } = await apiAddAddress(address); | ||||||||
|
|
||||||||
| setUser(user); | ||||||||
|
|
||||||||
| return user.addresses; | ||||||||
| } | ||||||||
| }; | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Factory content | ||||||||
| ```TS | ||||||||
| export function useUserAddressFactory = (factoryParams) => { | ||||||||
| const addresses: ref([]); | ||||||||
| const loading: ref(false); | ||||||||
|
|
||||||||
| return function useUserAddress(): UseUserAddress<ADDRESS, SEARCH_PARAMS, ADDRESS_OPTIONS> { | ||||||||
| const addAddress: async (address) => { | ||||||||
| loading.value = true; | ||||||||
| addresses.value = await factoryParams.addAddress(address); | ||||||||
| loading.value = false; | ||||||||
| } | ||||||||
|
|
||||||||
| // and other methods | ||||||||
|
|
||||||||
| return { | ||||||||
| addresses: computed(() => addresses.value); | ||||||||
| addAddress, | ||||||||
| // ...And other stuff mentioned in interfaces section | ||||||||
| } | ||||||||
| } | ||||||||
| }; | ||||||||
| ``` | ||||||||
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.
can we have multiple shipping addressess and billing addresses? Does every address have to be in a specific category?