-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from vuestorefront-community/dev
chore: release version 1.2.0
- Loading branch information
Showing
12 changed files
with
653 additions
and
93 deletions.
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
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,26 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import gql from 'graphql-tag'; | ||
import { Context, CustomQuery } from '@vue-storefront/core'; | ||
import mutation from './subscribeNewsLetterMutation'; | ||
import ApolloClient from 'apollo-client'; | ||
import { GraphQlAddAddressParams, DefaultGraphQlMutationResponse } from '../../index'; | ||
import { FetchResult } from 'apollo-link/lib/types'; | ||
|
||
export default async function subscribeNewsLetter( | ||
context: Context, | ||
params: GraphQlAddAddressParams, | ||
customQuery?: CustomQuery | ||
): Promise<FetchResult<DefaultGraphQlMutationResponse>> { | ||
const apolloClient = context.client.apollo as ApolloClient<any>; | ||
|
||
const { subscribeNewsLetter } = context.extendQuery( | ||
customQuery, { subscribeNewsLetter: { mutation, variables: params } } | ||
); | ||
|
||
const response = await apolloClient.mutate({ | ||
mutation: gql`${subscribeNewsLetter.mutation}`, | ||
variables: subscribeNewsLetter.variables | ||
}); | ||
|
||
return response; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/api-client/src/api/subscribeNewsLetter/subscribeNewsLetterMutation.ts
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,8 @@ | ||
import gql from 'graphql-tag'; | ||
|
||
export default gql` | ||
mutation($email: String){ | ||
newsletterSubscribe(email: $email){ | ||
subscribed | ||
} | ||
}`; |
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
26 changes: 26 additions & 0 deletions
26
packages/composables/src/composables/useNewsLetter/index.ts
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,26 @@ | ||
import { | ||
Context, Logger | ||
} from '@vue-storefront/core'; | ||
import useUser from '../useUser'; | ||
import { useNewsletterFactory, UseNewsletterFactoryParams } from '../../factories/useNewsLetterFactory'; | ||
|
||
const factoryParams: UseNewsletterFactoryParams<any, string> = { | ||
provide() { | ||
return { | ||
user: useUser() | ||
}; | ||
}, | ||
sendSubscription: async (context: Context, params) => { | ||
Logger.debug('[Magento]: Update user newsletter subscription', { params }); | ||
|
||
const { data } = await context.$odoo.api.subscribeNewsLetter( | ||
{ | ||
email: params.email | ||
} | ||
); | ||
|
||
return data.newsletterSubscribe; | ||
} | ||
}; | ||
|
||
export default useNewsletterFactory<any, string>(factoryParams); |
65 changes: 65 additions & 0 deletions
65
packages/composables/src/factories/useNewsLetterFactory.ts
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,65 @@ | ||
import { Ref, computed } from '@vue/composition-api'; | ||
import { | ||
ComposableFunctionArgs, | ||
configureFactoryParams, | ||
Context, | ||
FactoryParams, | ||
Logger, | ||
sharedRef | ||
} from '@vue-storefront/core'; | ||
import { Composable, ComputedProperty, PlatformApi } from '@vue-storefront/core/lib/src/types'; | ||
|
||
export interface UseNewsletterErrors { | ||
sendSubscription: Error; | ||
} | ||
|
||
export interface UseNewsletter<SEND_NEWSLETTER_PARAMS, API extends PlatformApi = any> extends Composable<API> { | ||
error: ComputedProperty<UseNewsletterErrors>; | ||
loading: ComputedProperty<boolean>; | ||
sendSubscription: (params: ComposableFunctionArgs<{ email: SEND_NEWSLETTER_PARAMS }>) => Promise<void>; | ||
} | ||
|
||
export interface UseNewsletterFactoryParams<NEWSLETTER, SEND_NEWSLETTER_PARAMS, API extends PlatformApi = any> extends FactoryParams<API> { | ||
sendSubscription: (context: Context, params: ComposableFunctionArgs<{ email: SEND_NEWSLETTER_PARAMS }>) => Promise<NEWSLETTER>; | ||
} | ||
|
||
export const useNewsletterFactory = <NEWSLETTER, SEND_NEWSLETTER_PARAMS, API extends PlatformApi = any>( | ||
factoryParams: UseNewsletterFactoryParams<NEWSLETTER, SEND_NEWSLETTER_PARAMS, API> | ||
) => function useNewsletter(): UseNewsletter<SEND_NEWSLETTER_PARAMS, API> { | ||
const errorsFactory = (): UseNewsletterErrors => ({ | ||
sendSubscription: null | ||
}); | ||
|
||
const loading: Ref<boolean> = sharedRef(false, 'useNewsletter-loading'); | ||
// eslint-disable-next-line @typescript-eslint/naming-convention,no-underscore-dangle | ||
const _factoryParams = configureFactoryParams(factoryParams); | ||
const error: Ref<UseNewsletterErrors> = sharedRef(errorsFactory(), 'useNewsletter-error'); | ||
|
||
const resetErrorValue = () => { | ||
error.value = errorsFactory(); | ||
}; | ||
|
||
const sendSubscription = async (params: ComposableFunctionArgs<{ email: SEND_NEWSLETTER_PARAMS }>) => { | ||
Logger.debug('useNewsletterFactory.sendSubscription', params); | ||
resetErrorValue(); | ||
|
||
try { | ||
loading.value = true; | ||
error.value.sendSubscription = null; | ||
console.log(await _factoryParams.sendSubscription(params)); | ||
|
||
return await _factoryParams.sendSubscription(params); | ||
} catch (err) { | ||
error.value.sendSubscription = err; | ||
Logger.error('useNewsletter/sendSubscription', err); | ||
} finally { | ||
loading.value = false; | ||
} | ||
}; | ||
|
||
return { | ||
error: computed(() => error.value), | ||
loading: computed(() => loading.value), | ||
sendSubscription | ||
}; | ||
}; |
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
Oops, something went wrong.