-
Notifications
You must be signed in to change notification settings - Fork 115
refactor(composable): refactor useReview #729
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63e1307
refactor(composable): refactor useReview
sethidden 2b72a52
refactor(theme): change to new refactored usereview composable
sethidden 1559d92
refactor(composable): deprecate useReview factory aswell
sethidden c309904
refactor(theme): move commands into separate files and change naming …
sethidden 3d770c6
refactor(theme): add spaces for readability
sethidden df274e2
refactor(theme): add typings for commands
sethidden 0a42bc3
refactor(theme): fix useReview imports
sethidden df004d6
fix(theme): fix TypeScript errors after adding context.d.ts $magento …
sethidden 11bee3f
refactor(theme): adjust to lack of state refs in composable
sethidden 7c26ec7
refactor(composable): remove unused params
sethidden b311a23
refactor(composables): add composable command types
sethidden 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 |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| /* istanbul ignore file */ | ||
|
|
||
| /** | ||
| * @deprecated since version 1.0.0 | ||
| */ | ||
| import { | ||
| ComposableFunctionArgs, | ||
| Context, | ||
|
|
||
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
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { ApiClientMethods, IntegrationContext } from '@vue-storefront/core'; | ||
| import { ClientInstance, Config, MagentoApiMethods } from '@vue-storefront/magento-api'; | ||
|
|
||
| declare module '@vue-storefront/core' { | ||
| export interface Context { | ||
| $magento: IntegrationContext<ClientInstance, Config, ApiClientMethods<MagentoApiMethods>>; | ||
| } | ||
| } |
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
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
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
20 changes: 20 additions & 0 deletions
20
packages/theme/composables/useReview/commands/addReviewCommand.ts
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,20 @@ | ||
| import { Context, Logger } from '@vue-storefront/core'; | ||
| import {CreateProductReviewInput } from '~/modules/GraphQL/types'; | ||
| import { ComposableFunctionArgs } from '~/composables/types'; | ||
|
|
||
| export const addReviewCommand = { | ||
| execute: async (context: Context, params: ComposableFunctionArgs<CreateProductReviewInput>) => { | ||
| Logger.debug('[Magento] add review params input:', JSON.stringify(params, null, 2)); | ||
|
|
||
| const { | ||
| customQuery, | ||
| ...input | ||
| } = params; | ||
|
|
||
| const { data } = await context.$magento.api.createProductReview(input); | ||
|
|
||
| Logger.debug('[Result]:', { data }); | ||
|
|
||
| return data?.createProductReview?.review ?? {}; | ||
| }, | ||
| }; |
14 changes: 14 additions & 0 deletions
14
packages/theme/composables/useReview/commands/loadCustomerReviewsCommand.ts
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,14 @@ | ||
| import { ComposableFunctionArgs, Context, Logger } from '@vue-storefront/core'; | ||
| import { CustomerProductReviewParams } from '@vue-storefront/magento-api'; | ||
|
|
||
| export const loadCustomerReviewsCommand = { | ||
| execute: async (context: Context, params?: ComposableFunctionArgs<CustomerProductReviewParams>) => { | ||
| Logger.debug('[Magento] load customer review based on:', { params }); | ||
|
|
||
| const { data } = await context.$magento.api.customerProductReview(params); | ||
|
|
||
| Logger.debug('[Result]:', { data }); | ||
|
|
||
| return data?.customer ?? {}; | ||
| }, | ||
| }; |
13 changes: 13 additions & 0 deletions
13
packages/theme/composables/useReview/commands/loadReviewMetadataCommand.ts
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,13 @@ | ||
| import { Context, Logger } from '@vue-storefront/core'; | ||
|
|
||
| export const loadReviewMetadataCommand = { | ||
| execute: async (context: Context) => { | ||
sethidden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Logger.debug('[Magento] load review metadata'); | ||
|
|
||
| const { data } = await context.$magento.api.productReviewRatingsMetadata(); | ||
|
|
||
| Logger.debug('[Result]:', { data }); | ||
|
|
||
| return data?.productReviewRatingsMetadata?.items ?? []; | ||
| }, | ||
| }; | ||
20 changes: 20 additions & 0 deletions
20
packages/theme/composables/useReview/commands/searchReviewsCommand.ts
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,20 @@ | ||
| import { Context, Logger } from '@vue-storefront/core'; | ||
| import { ComposableFunctionArgs } from '~/composables/types'; | ||
| import { GetProductSearchParams } from '~/composables/useProduct/useProduct'; | ||
|
|
||
| export const searchReviewsCommand = { | ||
| execute: async (context: Context, params?: ComposableFunctionArgs<GetProductSearchParams>) => { | ||
| Logger.debug('[Magento] search review params input:', JSON.stringify(params, null, 2)); | ||
|
|
||
| const { | ||
| customQuery, | ||
| ...input | ||
| } = params; | ||
|
|
||
| const { data } = await context.$magento.api.productReview(input as GetProductSearchParams); | ||
|
|
||
| Logger.debug('[Result]:', { data }); | ||
|
|
||
| return data?.products?.items ?? []; | ||
| }, | ||
| }; |
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,93 @@ | ||
| /* eslint-disable consistent-return */ | ||
| import { ref, useContext } from '@nuxtjs/composition-api'; | ||
| import { ComposableFunctionArgs, Context, Logger } from '@vue-storefront/core'; | ||
| import { CreateProductReviewInput } from '~/modules/GraphQL/types'; | ||
| import { UseReviewErrors } from './useReview'; | ||
| import { addReviewCommand } from './commands/addReviewCommand'; | ||
| import { loadCustomerReviewsCommand } from './commands/loadCustomerReviewsCommand'; | ||
| import { loadReviewMetadataCommand } from './commands/loadReviewMetadataCommand'; | ||
| import { searchReviewsCommand } from './commands/searchReviewsCommand'; | ||
| import { GetProductSearchParams } from '../useProduct/useProduct'; | ||
|
|
||
| export const useReview = () => { | ||
| const loading = ref(false); | ||
| const error = ref<UseReviewErrors>({ | ||
| search: null, | ||
| addReview: null, | ||
| loadReviewMetadata: null, | ||
| loadCustomerReviews: null, | ||
| }); | ||
|
|
||
| const { app } = useContext(); | ||
Frodigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const context = app.$vsf as Context; | ||
|
|
||
| const search = async (searchParams: ComposableFunctionArgs<GetProductSearchParams>) => { | ||
| Logger.debug('useReview/search', searchParams); | ||
|
|
||
| try { | ||
| loading.value = true; | ||
| error.value.search = null; | ||
| return await searchReviewsCommand.execute(context, searchParams); | ||
| } catch (err) { | ||
| error.value.search = err; | ||
| Logger.error('useReview/search', err); | ||
| } finally { | ||
| loading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| const loadCustomerReviews = async () => { | ||
| Logger.debug('useReview/loadCustomerReviews'); | ||
|
|
||
| try { | ||
| loading.value = true; | ||
| error.value.loadCustomerReviews = null; | ||
| return await loadCustomerReviewsCommand.execute(context); | ||
| } catch (err) { | ||
| error.value.loadCustomerReviews = err; | ||
| Logger.error('useReview/loadCustomerReviews', err); | ||
| } finally { | ||
| loading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| const loadReviewMetadata = async () => { | ||
| Logger.debug('useReview/loadReviewMetadata'); | ||
|
|
||
| try { | ||
| loading.value = true; | ||
| error.value.loadReviewMetadata = null; | ||
| return await loadReviewMetadataCommand.execute(context); | ||
| } catch (err) { | ||
| error.value.loadReviewMetadata = err; | ||
| Logger.error('useReview/loadReviewMetadata', err); | ||
| } finally { | ||
| loading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| const addReview = async (params: ComposableFunctionArgs<CreateProductReviewInput>) => { | ||
| Logger.debug('useReview/addReview', params); | ||
| try { | ||
| loading.value = true; | ||
| error.value.addReview = null; | ||
| return await addReviewCommand.execute(context, params); | ||
| } catch (err) { | ||
| error.value.addReview = err; | ||
| Logger.error('useReview/addReview', err); | ||
| } finally { | ||
| loading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| search, | ||
| addReview, | ||
| loadReviewMetadata, | ||
| loadCustomerReviews, | ||
| loading, | ||
| error, | ||
| }; | ||
| }; | ||
|
|
||
| export default useReview; | ||
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,6 @@ | ||
| export interface UseReviewErrors { | ||
| search: Error; | ||
| addReview: Error; | ||
| loadReviewMetadata: Error; | ||
| loadCustomerReviews: Error; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.