Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
remove useSearchQueryAttributes hook
Browse files Browse the repository at this point in the history
  • Loading branch information
AlicjaSzu committed May 14, 2020
1 parent f351aab commit 4092559
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useEffect } from "react";
import {
useProductVariantsAttributes,
useProductVariantsAttributesValuesSelection,
useSearchQueryAttributes,
} from "@hooks";
import { ProductVariantAttributeSelect } from "./ProductVariantAttributeSelect";
import * as S from "./styles";
Expand All @@ -15,6 +14,7 @@ export const ProductVariantPicker: React.FC<IProps> = ({
onChange,
selectSidebar = false,
selectSidebarTarget,
updateUrlWithAttributes,
}: IProps) => {
const productVariantsAttributes = useProductVariantsAttributes(
productVariants
Expand All @@ -23,7 +23,6 @@ export const ProductVariantPicker: React.FC<IProps> = ({
productVariantsAttributesSelectedValues,
selectProductVariantsAttributesValue,
] = useProductVariantsAttributesValuesSelection(productVariantsAttributes);
const { updateUrlWithAttributes } = useSearchQueryAttributes();

useEffect(() => {
const selectedVariant = productVariants.find(productVariant => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const PROPS = {
onChange: action("onChange"),
productVariants: PRODUCT_VARIANTS,
queryAttributes: {},
updateUrlWithAttributes: action("update url params"),
};

storiesOf("@components/organisms/ProductVariantPicker", module)
Expand Down
6 changes: 5 additions & 1 deletion src/@next/components/organisms/ProductVariantPicker/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ if (!portalRoot) {
document.body.appendChild(portalRoot);
}

const PROPS = { productVariants, queryAttributes: {} };
const PROPS = {
productVariants,
queryAttributes: {},
updateUrlWithAttributes: jest.fn(),
};

describe("<ProductVariantPicker />", () => {
it("exists", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IProps {
selectSidebar?: boolean;
selectSidebarTarget?: HTMLElement | null;
queryAttributes: Record<string, string>;
updateUrlWithAttributes: (slug: string, value: string) => void;
}
1 change: 0 additions & 1 deletion src/@next/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export * from "./useProductVariantsAttributesValuesSelection";
export * from "./useSelectableProductVariantsAttributeValues";
export * from "./useCheckoutStepState";
export * from "./useCheckoutStepFromPath";
export * from "./useSearchQueryAttributes";
30 changes: 0 additions & 30 deletions src/@next/hooks/useSearchQueryAttributes.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions src/components/ProductDescription/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface ProductDescriptionProps {
queryAttributes: Record<string, string>;
addToCart(varinatId: string, quantity?: number): void;
setVariantId(variantId: string);
updateUrlWithAttributes(slug: string, value: string): void;
}

interface ProductDescriptionState {
Expand Down Expand Up @@ -149,6 +150,7 @@ class ProductDescription extends React.Component<
onChange={this.onVariantPickerChange}
selectSidebar={true}
queryAttributes={this.props.queryAttributes}
updateUrlWithAttributes={this.props.updateUrlWithAttributes}
/>
</div>
<div className="product-description__quantity-input">
Expand Down
11 changes: 5 additions & 6 deletions src/views/Product/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ const populateBreadcrumbs = product => [
},
];

const Page: React.FC<IProps & { queryAttributes: Record<string, string> }> = ({
add,
product,
items,
queryAttributes,
}) => {
const Page: React.FC<IProps & {
queryAttributes: Record<string, string>;
updateUrlWithAttributes: (slug: string, value: string) => void;
}> = ({ add, product, items, queryAttributes, updateUrlWithAttributes }) => {
const productGallery: React.RefObject<HTMLDivElement> = React.useRef();

const [variantId, setVariantId] = React.useState("");
Expand Down Expand Up @@ -60,6 +58,7 @@ const Page: React.FC<IProps & { queryAttributes: Record<string, string> }> = ({
queryAttributes={queryAttributes}
addToCart={add}
setVariantId={setVariantId}
updateUrlWithAttributes={updateUrlWithAttributes}
/>
);

Expand Down
29 changes: 25 additions & 4 deletions src/views/Product/View.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "./scss/index.scss";

import { isEmpty } from "lodash";
import queryString from "query-string";
import React, { useEffect, useState } from "react";
import { RouteComponentProps } from "react-router";
import { useHistory } from "react-router-dom";

import { useSearchQueryAttributes } from "@hooks";
import { ProductDetails_product } from "@sdk/queries/types/ProductDetails";
import { useCart, useProductDetails } from "@sdk/react";

Expand Down Expand Up @@ -50,7 +51,21 @@ const extractMeta = (product: ProductDetails_product) => ({

const PageWithQueryAttributes: React.FC<IProps> = props => {
const { product } = props;
const { clearUrl, searchQueryAttributes } = useSearchQueryAttributes();
const history = useHistory();
const search = history.location.search;
const searchQueryAttributes = queryString.parse(search);

const updateUrlWithAttributes = (slug: string, value: string) => {
history.replace(
queryString.stringifyUrl(
{
query: { [slug]: value },
url: `${history.location.pathname}${history.location.search}`,
},
{ skipEmptyString: true }
)
);
};
const [queryAttributes, setQueryAttributes] = useState({});

useEffect(() => {
Expand Down Expand Up @@ -85,10 +100,16 @@ const PageWithQueryAttributes: React.FC<IProps> = props => {
}, [product.variants.length]);

useEffect(() => {
clearUrl();
history.replace(history.location.pathname);
}, [queryAttributes]);

return <Page {...props} queryAttributes={queryAttributes} />;
return (
<Page
{...props}
queryAttributes={queryAttributes}
updateUrlWithAttributes={updateUrlWithAttributes}
/>
);
};

const View: React.FC<RouteComponentProps<{ id: string }>> = ({ match }) => {
Expand Down

0 comments on commit 4092559

Please sign in to comment.