-
Notifications
You must be signed in to change notification settings - Fork 17
Integrated basic info with API #236
base: master
Are you sure you want to change the base?
Changes from all commits
4c0d657
1935190
965654d
f347abd
9c4c3ca
560d668
779a269
d685e7c
b9e27dc
f23ce85
7095769
f5cc0cf
feaa09e
05c67c5
04428cd
e8f8c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Customer, AddressInput } from '../../types/GraphQL'; | ||
| import { apolloClient } from '../../index'; | ||
| import updateMyCustomer from '../updateMyCustomer/defaultMutation'; | ||
| import { UpdateMyCustomerResponse } from '../../types/Api'; | ||
|
|
||
| const addCustomerAddress = (customer: Customer, address: AddressInput): Promise<UpdateMyCustomerResponse> => apolloClient.mutate({ | ||
| mutation: updateMyCustomer, | ||
| variables: { | ||
| version: customer.version, | ||
| actions: [{ | ||
| addAddress: { address } | ||
| }] | ||
| }, | ||
| fetchPolicy: 'no-cache' | ||
| }); | ||
|
|
||
| export default addCustomerAddress; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Customer, Address } from '../../types/GraphQL'; | ||
| import { UpdateMyCustomerResponse } from '../../types/Api'; | ||
| import updateMyCustomer from '../updateMyCustomer/defaultMutation'; | ||
| import { apolloClient } from '../..'; | ||
|
|
||
| const getActionKey = (type: 'billing' | 'shipping'): string => { | ||
| switch (type) { | ||
| case 'billing': | ||
| return 'addBillingAddressId'; | ||
| case 'shipping': | ||
| return 'addShippingAddressId'; | ||
| default: | ||
| throw new Error('Unknown action type'); | ||
| } | ||
| }; | ||
|
|
||
| const assignCustomerAddress = (customer: Customer, address: Address, type: 'billing' | 'shipping'): Promise<UpdateMyCustomerResponse> => apolloClient.mutate({ | ||
| mutation: updateMyCustomer, | ||
| variables: { | ||
| version: customer.version, | ||
| actions: [{ | ||
| [getActionKey(type)]: { | ||
| addressId: address.id | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| export default assignCustomerAddress; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Customer, AddressInput } from '../../types/GraphQL'; | ||
| import { UpdateMyCustomerResponse } from '../../types/Api'; | ||
| import { apolloClient } from '../../index'; | ||
| import updateMyCustomer from '../updateMyCustomer/defaultMutation'; | ||
|
|
||
| const removeCustomerAddress = (customer: Customer, address: AddressInput): Promise<UpdateMyCustomerResponse> => apolloClient.mutate({ | ||
| mutation: updateMyCustomer, | ||
| variables: { | ||
| version: customer.version, | ||
| actions: [{ | ||
| removeAddress: { | ||
| addressId: address.id | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| export default removeCustomerAddress; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { Customer, AddressInput } from '../../types/GraphQL'; | ||
| import { UpdateMyCustomerResponse } from '../../types/Api'; | ||
| import { apolloClient } from '../../index'; | ||
| import updateMyCustomer from '../updateMyCustomer/defaultMutation'; | ||
|
|
||
| const updateCustomerAddress = (customer: Customer, address: AddressInput): Promise<UpdateMyCustomerResponse> => apolloClient.mutate({ | ||
| mutation: updateMyCustomer, | ||
| variables: { | ||
| version: customer.version, | ||
| actions: [{ | ||
| changeAddress: { | ||
| addressId: address.id, | ||
| address | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| export default updateCustomerAddress; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { Customer } from '../../types/GraphQL'; | ||
| import { UpdateMyCustomerResponse } from '../../types/Api'; | ||
| import { apolloClient } from '../../index'; | ||
| import updateMyCustomer from '../updateMyCustomer/defaultMutation'; | ||
|
|
||
| interface PersonalDetails { | ||
| firstName?: string; | ||
| lastName?: string; | ||
| email?: string; | ||
| } | ||
|
|
||
| const generateActions = (personalDetails: PersonalDetails): any[] => { | ||
| const actions = []; | ||
|
|
||
| personalDetails.email && actions.push({ | ||
| changeEmail: { | ||
| email: personalDetails.email | ||
| } | ||
| }); | ||
|
|
||
| personalDetails.firstName && actions.push({ | ||
| setFirstName: { | ||
| firstName: personalDetails.firstName | ||
| } | ||
| }); | ||
|
|
||
| personalDetails.lastName && actions.push({ | ||
| setLastName: { | ||
| lastName: personalDetails.lastName | ||
| } | ||
| }); | ||
|
|
||
| return actions; | ||
| }; | ||
|
|
||
| const updateCustomerPersonalDetails = (customer: Customer, personalDetails: PersonalDetails): Promise<UpdateMyCustomerResponse> => apolloClient.mutate({ | ||
| mutation: updateMyCustomer, | ||
| variables: { | ||
| version: customer.version, | ||
| actions: generateActions(personalDetails) | ||
| } | ||
| }); | ||
|
|
||
| export default updateCustomerPersonalDetails; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import gql from 'graphql-tag'; | ||
| import { CustomerFragment, AddressFragment } from '../../fragments'; | ||
|
|
||
| export default gql` | ||
| ${AddressFragment} | ||
| ${CustomerFragment} | ||
|
|
||
| mutation updateMyCustomer($version: Long!, $actions: [MyCustomerUpdateAction!]!, $storeKey: KeyReferenceInput) { | ||
| updateMyCustomer(version: $version, actions: $actions, storeKey: $storeKey) { | ||
| shippingAddressIds | ||
| billingAddressIds | ||
| addresses { | ||
| ...DefaultAddress | ||
| } | ||
| ...DefaultCustomer | ||
| } | ||
| } | ||
| `; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||
| import {UseUserFactoryParams} from '@vue-storefront/factories'; | ||||||
| import {Customer} from '@vue-storefront/commercetools-api/lib//types/GraphQL'; | ||||||
| import { UseUserFactoryParams } from '@vue-storefront/factories'; | ||||||
| import { Customer } from '@vue-storefront/commercetools-api/lib//types/GraphQL'; | ||||||
|
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
|
||||||
| import { authenticate } from './authenticate'; | ||||||
| import { | ||||||
| updateCustomerPersonalDetails, | ||||||
| customerSignMeUp as apiCustomerSignMeUp, | ||||||
| customerSignMeIn as apiCustomerSignMeIn, | ||||||
| customerSignOut as apiCustomerSignOut, | ||||||
|
|
@@ -10,7 +11,9 @@ import { | |||||
| } from '@vue-storefront/commercetools-api'; | ||||||
| import useCart from '../useCart'; | ||||||
|
|
||||||
| export const params: UseUserFactoryParams<Customer, any, any> = { | ||||||
| export type UpdateUserParams = Customer & { clientSideOnly?: boolean }; | ||||||
|
Collaborator
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 is this
Contributor
Author
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. Yes. Prevent from updating user personal details for CT
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. can you elaborate more on that? |
||||||
|
|
||||||
| export const params: UseUserFactoryParams<Customer, UpdateUserParams, any> = { | ||||||
| loadUser: async () => { | ||||||
| try { | ||||||
| const profile = await apiGetMe({ customer: true }); | ||||||
|
|
@@ -27,9 +30,19 @@ export const params: UseUserFactoryParams<Customer, any, any> = { | |||||
| await useCart().refreshCart(); | ||||||
| await apiCustomerSignOut(); | ||||||
| }, | ||||||
| updateUser: async ({currentUser, updatedUserData}): Promise<Customer> => { | ||||||
| // Change code below if the apiClient receive userUpdate method | ||||||
| return Promise.resolve({currentUser, updatedUserData} as any); | ||||||
| updateUser: async ({ currentUser, updatedUserData }): Promise<Customer> => { | ||||||
| if (updatedUserData.clientSideOnly) { | ||||||
|
andrzejewsky marked this conversation as resolved.
|
||||||
| return Promise.resolve({currentUser, updatedUserData} as any); | ||||||
| } | ||||||
|
|
||||||
| const { firstName, lastName, email } = updatedUserData; | ||||||
|
|
||||||
| const { data } = await updateCustomerPersonalDetails(currentUser, { firstName, lastName, email }); | ||||||
|
|
||||||
| return { | ||||||
| ...currentUser, | ||||||
| ...data.updateMyCustomer | ||||||
| }; | ||||||
| }, | ||||||
| register: async ({email, password, firstName, lastName}) => { | ||||||
| return await authenticate({email, password, firstName, lastName}, apiCustomerSignMeUp); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { addCustomerAddress, assignCustomerAddress, removeCustomerAddress, updateCustomerAddress } from '@vue-storefront/commercetools-api'; | ||
| import { UseUserAddressFactoryParams, UpdatedUserAddresses } from '@vue-storefront/factories'; | ||
| import { Customer } from '@vue-storefront/commercetools-api/lib//types/GraphQL'; | ||
| import { Address } from '../types/GraphQL'; | ||
| import useUser from '../useUser'; | ||
| import { UpdateUserParams } from '../useUser/factoryParams'; | ||
| import { UserAddressType } from '@vue-storefront/interfaces'; | ||
|
|
||
| const userComposable = useUser(); | ||
|
andrzejewsky marked this conversation as resolved.
|
||
|
|
||
| const transformAddresses = (customer: Customer): UpdatedUserAddresses<Address, UpdateUserParams> => ({ | ||
| addresses: customer.addresses, | ||
| updateUserParams: { ...customer, clientSideOnly: true } | ||
| }); | ||
|
|
||
| export const params: UseUserAddressFactoryParams<Customer, Address, UserAddressType, { [x: string]: any }, UpdateUserParams> = { | ||
| userComposable, | ||
| addresses: userComposable.user.value && userComposable.user.value.id ? userComposable.user.value.addresses : [], | ||
|
andrzejewsky marked this conversation as resolved.
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.
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. You will need to partailly implement #342 for |
||
| addAddress: async (address, type) => { | ||
| const { data } = await addCustomerAddress(userComposable.user.value, address); | ||
| const newAddress = data.updateMyCustomer.addresses.pop(); | ||
|
|
||
| const { data: { updateMyCustomer } } = await assignCustomerAddress( | ||
| { ...userComposable.user.value, ...data.updateMyCustomer }, | ||
| newAddress, | ||
| type | ||
| ); | ||
|
|
||
| return transformAddresses(updateMyCustomer); | ||
| }, | ||
| updateAddress: async (address) => { | ||
| const { data } = await updateCustomerAddress(userComposable.user.value, address); | ||
|
|
||
|
andrzejewsky marked this conversation as resolved.
|
||
| return transformAddresses(data.updateMyCustomer); | ||
| }, | ||
| deleteAddress: async (address) => { | ||
| const { data } = await removeCustomerAddress(userComposable.user.value, address); | ||
|
|
||
| return transformAddresses(data.updateMyCustomer); | ||
| }, | ||
| getBillingAddresses: () => { | ||
| const { addresses, billingAddressIds } = userComposable.user.value; | ||
|
|
||
| return (addresses || []).filter(({ id }) => (billingAddressIds || []).includes(id)); | ||
| }, | ||
| getShippingAddresses: () => { | ||
| const { addresses, shippingAddressIds } = userComposable.user.value; | ||
|
|
||
| return (addresses || []).filter(({ id }) => (shippingAddressIds || []).includes(id)); | ||
| }, | ||
| searchAddresses: async () => userComposable.user.value.addresses | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { UseUserAddress } from '@vue-storefront/interfaces'; | ||
| import { Address } from '../types/GraphQL'; | ||
| import { useUserAddressFactory } from '@vue-storefront/factories'; | ||
| import { Customer } from '@vue-storefront/commercetools-api/lib//types/GraphQL'; | ||
| import { params } from './factoryParams'; | ||
|
|
||
| const useUserAddress: () => UseUserAddress<Address> = useUserAddressFactory<Customer, Address>(params); | ||
|
|
||
| export default useUserAddress; | ||
|
andrzejewsky marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.