Skip to content

Commit 7a1e17d

Browse files
author
Sanjeev Yadav
committed
feat: Extract product list in fragment & create useSearch hook
1 parent 30f9ec3 commit 7a1e17d

File tree

15 files changed

+201
-46
lines changed

15 files changed

+201
-46
lines changed
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { gql } from '@apollo/client';
2+
import { ProductInListType, PRODUCTS_FRAGMENT } from './productsFragment';
23

34
export interface GetCategoryProductsVars {
45
id: string;
@@ -9,26 +10,7 @@ export interface GetCategoryProductsVars {
910
export interface CategoryProductsDataType {
1011
products: {
1112
total_count: number;
12-
items: Array<CategoryProductType>;
13-
};
14-
}
15-
16-
export interface CategoryProductType {
17-
id: number;
18-
sku: string;
19-
name: string;
20-
small_image: {
21-
url: String;
22-
};
23-
price_range: PriceRangeType;
24-
}
25-
26-
export interface PriceRangeType {
27-
maximum_price: {
28-
final_price: {
29-
currency: string;
30-
value: number;
31-
};
13+
items: Array<ProductInListType>;
3214
};
3315
}
3416

@@ -40,22 +22,8 @@ export const GET_CATGEORY_PRODUCTS = gql`
4022
currentPage: $currentPage
4123
) {
4224
total_count
43-
items {
44-
id
45-
sku
46-
name
47-
small_image {
48-
url
49-
}
50-
price_range {
51-
maximum_price {
52-
final_price {
53-
currency
54-
value
55-
}
56-
}
57-
}
58-
}
25+
...ProductListFragment
5926
}
6027
}
28+
${PRODUCTS_FRAGMENT}
6129
`;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { gql } from '@apollo/client';
2+
import { CategoryProductsDataType } from './getCategoryProducts';
3+
import { PRODUCTS_FRAGMENT } from './productsFragment';
4+
5+
export interface GetSearchProductsVars {
6+
searchText: string;
7+
pageSize: number;
8+
currentPage: number;
9+
}
10+
11+
export interface SearchProductsDataType extends CategoryProductsDataType {}
12+
13+
export const GET_SEARCH_PRODUCTS = gql`
14+
query GetSearchProducts(
15+
$searchText: String!
16+
$pageSize: Int!
17+
$currentPage: Int!
18+
) {
19+
products(
20+
search: $searchText
21+
pageSize: $pageSize
22+
currentPage: $currentPage
23+
) {
24+
total_count
25+
...ProductListFragment
26+
}
27+
}
28+
${PRODUCTS_FRAGMENT}
29+
`;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { gql } from '@apollo/client';
2+
3+
export interface ProductInListType {
4+
id: number;
5+
sku: string;
6+
name: string;
7+
small_image: {
8+
url: String;
9+
};
10+
price_range: PriceRangeType;
11+
}
12+
13+
export interface PriceRangeType {
14+
maximum_price: {
15+
final_price: {
16+
currency: string;
17+
value: number;
18+
};
19+
};
20+
}
21+
22+
export const PRODUCTS_FRAGMENT = gql`
23+
fragment ProductListFragment on Products {
24+
items {
25+
id
26+
sku
27+
name
28+
small_image {
29+
url
30+
}
31+
price_range {
32+
maximum_price {
33+
final_price {
34+
currency
35+
value
36+
}
37+
}
38+
}
39+
}
40+
}
41+
`;

src/i18n/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"yes": "Yes",
77
"no": "No",
88
"menu": "Menu",
9+
"search": "Search",
910
"user": "User",
1011
"dearUser": "Dear User",
1112
"login": "Login",

src/logic/cart/useCart.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import { useEffect, useState } from 'react';
2-
import { useMutation } from '@apollo/client';
2+
import { useMutation, useQuery } from '@apollo/client';
33
import {
44
CreateCartDataType,
55
CREATE_CART,
66
} from '../../apollo/mutations/createCart';
7+
import {
8+
IsLoggedInDataType,
9+
IS_LOGGED_IN,
10+
} from '../../apollo/queries/isLoggedIn';
711

812
interface Result {
913
cartId: string | null | undefined;
1014
}
1115

1216
export const useCart = (): Result => {
1317
const [cartId, setCartId] = useState<string | null | undefined>(null);
18+
const { data: { isLoggedIn = false } = {} } = useQuery<IsLoggedInDataType>(
19+
IS_LOGGED_IN,
20+
);
1421
const [fetchCartId] = useMutation<CreateCartDataType>(CREATE_CART);
1522

1623
useEffect(() => {
17-
if (!cartId) {
24+
if (isLoggedIn && !cartId) {
1825
createCart();
1926
}
20-
}, []);
27+
}, [isLoggedIn]);
2128

2229
const createCart = async () => {
2330
try {

src/logic/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './app/useForm';
22
export * from './categories/useCategories';
33
export * from './products/useCategoryProducts';
4+
export * from './products/useSearch';
45
export * from './auth/useLogin';
56
export * from './auth/useLogout';
67
export * from './auth/useSignup';

src/logic/products/useCategoryProducts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { useEffect, useState } from 'react';
22
import { useLazyQuery, ApolloError } from '@apollo/client';
33
import {
44
GET_CATGEORY_PRODUCTS,
5-
CategoryProductType,
65
GetCategoryProductsVars,
76
CategoryProductsDataType,
87
} from '../../apollo/queries/getCategoryProducts';
8+
import { ProductInListType } from '../../apollo/queries/productsFragment';
99

1010
interface Props {
1111
categoryId: string;
1212
}
1313

1414
interface Result {
15-
products: Array<CategoryProductType>;
15+
products: Array<ProductInListType>;
1616
getCategoryProducts(): void;
1717
loading: boolean;
1818
error: ApolloError | undefined;
@@ -24,7 +24,7 @@ interface Result {
2424
const PAGE_SIZE = 10;
2525

2626
export const useCategoryProducts = ({ categoryId: id }: Props): Result => {
27-
const [products, setProducts] = useState<Array<CategoryProductType>>([]);
27+
const [products, setProducts] = useState<Array<ProductInListType>>([]);
2828
const [currentPage, setCurrentPage] = useState<number>(1);
2929

3030
const [getCategoryProducts, { loading, error }] = useLazyQuery<

src/logic/products/useSearch.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useState, useEffect } from 'react';
2+
import { useLazyQuery } from '@apollo/client';
3+
import {
4+
GetSearchProductsVars,
5+
GET_SEARCH_PRODUCTS,
6+
SearchProductsDataType,
7+
} from '../../apollo/queries/getSearchProducts';
8+
9+
const PAGE_SIZE = 10;
10+
const AUTO_SUGGESTION_API_TIME_DELAY = 1000;
11+
12+
export const useSearch = () => {
13+
const [searchText, handleChange] = useState<string>('');
14+
const [currentPage, setCurrentPage] = useState<number>(1);
15+
const [getSearchProducts, { loading, error, data }] = useLazyQuery<
16+
SearchProductsDataType,
17+
GetSearchProductsVars
18+
>(GET_SEARCH_PRODUCTS);
19+
20+
useEffect(() => {
21+
if (searchText.trim() === '' || searchText.trim().length < 3) {
22+
// can do something
23+
return;
24+
}
25+
const task = setTimeout(
26+
() =>
27+
getSearchProducts({
28+
variables: {
29+
searchText,
30+
pageSize: PAGE_SIZE,
31+
currentPage,
32+
},
33+
}),
34+
AUTO_SUGGESTION_API_TIME_DELAY,
35+
);
36+
37+
// eslint-disable-next-line consistent-return
38+
return () => {
39+
clearTimeout(task);
40+
};
41+
}, [searchText]);
42+
43+
console.log({ loading, error, data });
44+
return {
45+
data,
46+
loading,
47+
searchText,
48+
handleChange,
49+
getSearchProducts,
50+
};
51+
};

src/navigation/StackNavigator.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Routes } from './routeNames';
77
import { AppStackParamList } from './routeParams';
88
import { translate } from '../i18n';
99
import {
10+
SearchScreen,
1011
CategoriesScreen,
1112
ProductListScreen,
1213
ProductDetailsScreen,
@@ -30,6 +31,17 @@ const StackNavigator = () => (
3031
/>
3132
</CustomHeaderButtons>
3233
),
34+
headerRight: () => (
35+
<CustomHeaderButtons>
36+
<CustomHeaderItem
37+
title={translate('common.search')}
38+
iconName="search"
39+
onPress={() =>
40+
navigation.navigate(Routes.NAVIGATION_TO_SEARCH_SCREEN)
41+
}
42+
/>
43+
</CustomHeaderButtons>
44+
),
3345
})}
3446
/>
3547
<Stack.Screen
@@ -75,6 +87,13 @@ const StackNavigator = () => (
7587
};
7688
}}
7789
/>
90+
<Stack.Screen
91+
name={Routes.NAVIGATION_TO_SEARCH_SCREEN}
92+
component={SearchScreen}
93+
options={{
94+
headerShown: false,
95+
}}
96+
/>
7897
</Stack.Navigator>
7998
);
8099

src/navigation/routeNames.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export enum Routes {
88
NAVIGATION_TO_AUTHENTICATION_SPLASH_SCREEN = 'AuthenticationSplashScreen',
99
NAVIGATION_TO_LOGIN_SCREEN = 'LoginScreen',
1010
NAVIGATION_TO_SIGNUP_SCREEN = 'SignupScreen',
11+
NAVIGATION_TO_SEARCH_SCREEN = 'SearchScreen',
1112
}

0 commit comments

Comments
 (0)