Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/composables/src/composables/useReview/index.ts
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,
Expand Down
3 changes: 3 additions & 0 deletions packages/composables/src/factories/useReviewFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @deprecated since version 1.0.0
*/
import { Ref, computed } from '@vue/composition-api';
import {
ComposableFunctionArgs,
Expand Down
13 changes: 6 additions & 7 deletions packages/theme/components/ProductAddReviewForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ import {
useRoute,
useContext,
} from '@nuxtjs/composition-api';
import { useReview } from '@vue-storefront/magento';
import { extend, ValidationObserver, ValidationProvider } from 'vee-validate';
import { min, oneOf, required } from 'vee-validate/dist/rules';
import {
SfInput, SfButton, SfSelect, SfTextarea,
} from '@storefront-ui/vue';
import { reviewGetters, userGetters } from '~/getters';
import { useUser } from '~/composables';
import { useUser, useReview } from '~/composables';

extend('required', {
...required,
Expand Down Expand Up @@ -168,16 +167,16 @@ export default defineComponent({
typeof $recaptcha !== 'undefined' && $config.isRecaptcha,
);
const {
loading, loadReviewMetadata, metadata, error,
} = useReview(
`productReviews-${id}`,
);
loading, loadReviewMetadata, error,
} = useReview();
const { isAuthenticated, user } = useUser();

const reviewSent = ref(false);

const form = ref(BASE_FORM(id));

const metadata = ref([]);

const ratingMetadata = computed(() => reviewGetters.getReviewMetadata([...metadata.value]));

const formSubmitValue = computed(() => {
Expand Down Expand Up @@ -234,7 +233,7 @@ export default defineComponent({
};

onBeforeMount(async () => {
await loadReviewMetadata();
metadata.value = await loadReviewMetadata();
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
import { waitFor } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import { useRoute } from '@nuxtjs/composition-api';
import { useUser, useReview } from '@vue-storefront/magento';
import {
render,
useUserMock,
useReviewMock,
} from '~/test-utils';

import { useReview, useUser } from '~/composables';
import ProductAddReviewForm from '../ProductAddReviewForm';

jest.mock('@vue-storefront/magento', () => {
const originalModule = jest.requireActual('@vue-storefront/magento');
return {
...originalModule,
useUser: jest.fn(),
useReview: jest.fn(),
};
});

Expand All @@ -30,6 +29,14 @@ jest.mock('@nuxtjs/composition-api', () => {
};
});

jest.mock('~/composables/useReview', () => {
const originalModule = jest.requireActual('~/composables/useReview');
return {
...originalModule,
useReview: jest.fn(),
};
});

describe.skip('<ProductAddReviewForm/>', () => {
it('Form fields are rendered and validated', async () => {
useUser.mockReturnValue(useUserMock());
Expand Down
8 changes: 8 additions & 0 deletions packages/theme/composables/context.d.ts
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>>;
}
}
1 change: 1 addition & 0 deletions packages/theme/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { default as useCart } from './useCart';
export { default as useContent } from './useContent';
export { default as useCategorySearch } from './useCategorySearch';
export { default as useProduct } from './useProduct';
export { default as useReview } from './useReview';
export { default as useShipping } from './useShipping';
export { default as useShippingProvider } from './useShippingProvider';
export { default as useRelatedProducts } from './useRelatedProducts';
2 changes: 1 addition & 1 deletion packages/theme/composables/useCart/useCart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface UseCartInterface<CART, CART_ITEM, PRODUCT> {
clear: (params: ComposableFunctionArgs<{ realCart?: boolean; }>) => Promise<void>;
applyCoupon: (params: ComposableFunctionArgs<{ couponCode: string; }>) => Promise<void>;
removeCoupon: (params: ComposableFunctionArgs<{}>) => Promise<void>;
isInCart: (context: Context, params: { currentCart: CART; product: PRODUCT }) => boolean;
isInCart: (params: { currentCart: CART; product: PRODUCT }) => boolean;
setCart: (newCart: CART) => void;
cart: ComputedRef<CART>;
loading: Ref<boolean>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export const getProductDetailsCommand = {
...searchParams,
} as GetProductSearchParams, customQuery);

return result.data?.products ?? [];
return result.data?.products;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const getProductListCommand = {
.api
.products(searchParams as GetProductSearchParams, customQuery);

return result.data?.products ?? [];
return result.data?.products;
},
};
5 changes: 3 additions & 2 deletions packages/theme/composables/useProduct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { ref, useContext } from '@nuxtjs/composition-api';
import { getProductListCommand } from '~/composables/useProduct/commands/getProductListCommand';
import { getProductDetailsCommand } from '~/composables/useProduct/commands/getProductDetailsCommand';
import { ProductsListQuery } from '~/modules/GraphQL/types';

export const useProduct = (id: string) => {
const loading = ref(false);
Expand All @@ -17,7 +18,7 @@ export const useProduct = (id: string) => {

const getProductList = async (searchParams) => {
Logger.debug(`useProduct/${id}/getProductList`, searchParams);
let products = [];
let products : ProductsListQuery['products'] = null;

try {
loading.value = true;
Expand All @@ -35,7 +36,7 @@ export const useProduct = (id: string) => {

const getProductDetails = async (searchParams) => {
Logger.debug(`useProduct/${id}/getProductDetails`, searchParams);
let products = [];
let products : ProductsListQuery['products'] = null;

try {
loading.value = true;
Expand Down
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 ?? {};
},
};
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 ?? {};
},
};
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) => {
Logger.debug('[Magento] load review metadata');

const { data } = await context.$magento.api.productReviewRatingsMetadata();

Logger.debug('[Result]:', { data });

return data?.productReviewRatingsMetadata?.items ?? [];
},
};
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 ?? [];
},
};
93 changes: 93 additions & 0 deletions packages/theme/composables/useReview/index.ts
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();
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;
6 changes: 6 additions & 0 deletions packages/theme/composables/useReview/useReview.d.ts
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;
}
13 changes: 7 additions & 6 deletions packages/theme/pages/MyAccount/MyReviews.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
import {
SfTabs, SfLoader, SfReview, SfRating,
} from '@storefront-ui/vue';
import { useReview } from '@vue-storefront/magento';
import { reviewGetters } from '~/getters';
import { computed, defineComponent, onMounted } from '@nuxtjs/composition-api';
import {
computed, defineComponent, onMounted, ref,
} from '@nuxtjs/composition-api';
import { useReview } from '~/composables';

export default defineComponent({
name: 'MyReviews',
Expand All @@ -68,14 +70,13 @@ export default defineComponent({
SfRating,
},
setup() {
const { reviews, loading, loadCustomerReviews } = useReview(
'productReviews-my-reviews',
);
const { loading, loadCustomerReviews } = useReview();
const reviews = ref([]);

const userReviews = computed(() => reviewGetters.getItems(reviews.value));

onMounted(async () => {
await loadCustomerReviews();
reviews.value = await loadCustomerReviews();
});

return {
Expand Down
Loading