Skip to content
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

fix skeleton inter-route imports #1131

Merged
merged 1 commit into from
Jul 21, 2023
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
58 changes: 45 additions & 13 deletions templates/skeleton/app/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,45 @@ import {Pagination} from '@shopify/hydrogen';
import React, {useRef, useEffect} from 'react';
import {Image, Money} from '@shopify/hydrogen-react';
import {useFetchers} from '@remix-run/react';
import {
type NormalizedPredictiveSearch,
type NormalizedPredictiveSearchResults,
type NormalizedPredictiveSearchResultItem,
} from '~/routes/api.predictive-search';

import type {SearchQuery} from 'storefrontapi.generated';
import type {
PredictiveProductFragment,
PredictiveCollectionFragment,
PredictiveArticleFragment,
SearchQuery,
} from 'storefrontapi.generated';

type PredicticeSearchResultItemImage =
| PredictiveCollectionFragment['image']
| PredictiveArticleFragment['image']
| PredictiveProductFragment['variants']['nodes'][0]['image'];

type PredictiveSearchResultItemPrice =
| PredictiveProductFragment['variants']['nodes'][0]['price'];

export type NormalizedPredictiveSearchResultItem = {
__typename: string | undefined;
handle: string;
id: string;
image?: PredicticeSearchResultItemImage;
price?: PredictiveSearchResultItemPrice;
styledTitle?: string;
title: string;
url: string;
};

export const NO_PREDICTIVE_SEARCH_RESULTS: NormalizedPredictiveSearchResults = [
{type: 'queries', items: []},
{type: 'products', items: []},
{type: 'collections', items: []},
{type: 'pages', items: []},
{type: 'articles', items: []},
];
export type NormalizedPredictiveSearchResults = Array<
| {type: 'queries'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'products'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'collections'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'pages'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'articles'; items: Array<NormalizedPredictiveSearchResultItem>}
>;

export type NormalizedPredictiveSearch = {
results: NormalizedPredictiveSearchResults;
totalResults: number;
};

type FetchSearchResultsReturn = {
searchResults: {
Expand All @@ -33,6 +57,14 @@ type FetchSearchResultsReturn = {
searchTerm: string;
};

export const NO_PREDICTIVE_SEARCH_RESULTS: NormalizedPredictiveSearchResults = [
{type: 'queries', items: []},
{type: 'products', items: []},
{type: 'collections', items: []},
{type: 'pages', items: []},
{type: 'articles', items: []},
];

export function SearchForm({searchTerm}: {searchTerm: string}) {
const inputRef = useRef<HTMLInputElement | null>(null);

Expand Down
54 changes: 13 additions & 41 deletions templates/skeleton/app/routes/api.predictive-search.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,37 @@
import {json, type LoaderArgs} from '@shopify/remix-oxygen';
import type {
NormalizedPredictiveSearch,
NormalizedPredictiveSearchResults,
} from '~/components/Search';
import {NO_PREDICTIVE_SEARCH_RESULTS} from '~/components/Search';

import type {
PredictiveSearchQuery,
PredictiveArticleFragment,
PredictiveCollectionFragment,
PredictivePageFragment,
PredictiveProductFragment,
PredictiveCollectionFragment,
PredictiveArticleFragment,
PredictiveQueryFragment,
PredictiveSearchQuery,
} from 'storefrontapi.generated';

type PredictiveSearchResultItem =
| PredictiveProductFragment
| PredictiveArticleFragment
| PredictiveCollectionFragment
| PredictivePageFragment
| PredictiveArticleFragment;

type PredicticeSearchResultItemImage =
| PredictiveCollectionFragment['image']
| PredictiveArticleFragment['image']
| PredictiveProductFragment['variants']['nodes'][0]['image'];

type PredictiveSearchResultItemPrice =
| PredictiveProductFragment['variants']['nodes'][0]['price'];

export type NormalizedPredictiveSearchResultItem = {
__typename: string | undefined;
handle: string;
id: string;
image?: PredicticeSearchResultItemImage;
price?: PredictiveSearchResultItemPrice;
styledTitle?: string;
title: string;
url: string;
};

export type NormalizedPredictiveSearchResults = Array<
| {type: 'queries'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'products'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'collections'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'pages'; items: Array<NormalizedPredictiveSearchResultItem>}
| {type: 'articles'; items: Array<NormalizedPredictiveSearchResultItem>}
>;

export type NormalizedPredictiveSearch = {
results: NormalizedPredictiveSearchResults;
totalResults: number;
};
| PredictiveProductFragment;

type PredictiveSearchTypes =
| 'PRODUCT'
| 'ARTICLE'
| 'COLLECTION'
| 'PAGE'
| 'ARTICLE'
| 'PRODUCT'
| 'QUERY';

const DEFAULT_SEARCH_TYPES: PredictiveSearchTypes[] = [
'PRODUCT',
'ARTICLE',
'COLLECTION',
'PAGE',
'ARTICLE',
'PRODUCT',
'QUERY',
];

Expand Down