-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify product data handling in ProductDetails component
feat: enhance loading state handling in ProductDetails component feat: implement offer fetching and aggregation functionality feat: add revalidation for static props in page component feat: update offer fetcher to use secure subdomain for API requests feat: update offer fetcher to use dynamic base URL for API requests feat: refactor ProductDetails component to improve data structure and validation handling feat: update fetcher to include credentials in API requests feat: update offer fetcher to use dynamic store URL and conditionally append workspace parameter feat: update revalidation settings to use dynamic configuration feat: enhance error handling in useOffer hook to return initial state on fetch errors feat: update useOffer hook to return error state on fetch failures feat: refactor offer fetching logic to separate URL generation and fetching functions feat: set fetch priority to high for offer URLs in page component feat: upgrade swr to version 2.2.5 and update offer fetching logic feat: remove fetcherOffer export and related preload calls in useProductLink feat: update product search URL to remove unnecessary versioning chore: update yarn.lock to remove deprecated dependencies and clean up versioning
- Loading branch information
1 parent
63d9790
commit 0695f89
Showing
13 changed files
with
283 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,52 @@ | ||
import { Item, Seller } from '@faststore/api' | ||
import { EnhancedCommercialOffer } from './enhance' | ||
import { inStock, price } from './sort' | ||
|
||
type Root = EnhancedCommercialOffer<Seller, Item> | ||
|
||
const withTax = ( | ||
price: number, | ||
tax: number = 0, | ||
unitMultiplier: number = 1 | ||
) => { | ||
const unitTax = tax / unitMultiplier | ||
return Math.round((price + unitTax) * 100) / 100 | ||
} | ||
|
||
const getHighPrice = ( | ||
offers: Root[], | ||
options: { includeTaxes: boolean } = { includeTaxes: false } | ||
) => { | ||
const availableOffers = offers.filter(inStock) | ||
const highOffer = availableOffers[availableOffers.length - 1] | ||
const highPrice = highOffer ? price(highOffer) : 0 | ||
if (!options.includeTaxes) { | ||
return highPrice | ||
} | ||
|
||
return withTax(highPrice, highOffer?.Tax, highOffer?.product?.unitMultiplier) | ||
} | ||
|
||
const getLowPrice = ( | ||
offers: Root[], | ||
options: { includeTaxes: boolean } = { includeTaxes: false } | ||
) => { | ||
const [lowOffer] = offers.filter(inStock) | ||
|
||
const lowPrice = lowOffer ? price(lowOffer) : 0 | ||
|
||
if (!options.includeTaxes) { | ||
return lowPrice | ||
} | ||
|
||
return withTax(lowPrice, lowOffer?.Tax, lowOffer?.product?.unitMultiplier) | ||
} | ||
|
||
export function aggregateOffer(offers: Root[]) { | ||
return { | ||
highPrice: getHighPrice(offers), | ||
lowPrice: getLowPrice(offers), | ||
lowPriceWithTaxes: getLowPrice(offers, { includeTaxes: true }), | ||
offerCount: offers.length, | ||
} | ||
} |
This file contains 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 { CommertialOffer } from '@faststore/api' | ||
|
||
export type EnhancedCommercialOffer<S, P> = CommertialOffer & { | ||
seller: S | ||
product: P | ||
} | ||
|
||
export const enhanceCommercialOffer = <S, P>({ | ||
offer, | ||
seller, | ||
product, | ||
}: { | ||
offer: CommertialOffer | ||
seller: S | ||
product: P | ||
}): EnhancedCommercialOffer<S, P> => ({ | ||
...offer, | ||
product, | ||
seller, | ||
}) |
This file contains 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,23 @@ | ||
import { ProductSearchResult } from '@faststore/api' | ||
import { api, storeUrl } from '../../../discovery.config' | ||
|
||
const IS_PROD = process.env.NODE_ENV === 'production' | ||
|
||
export function getUrl(skuId: string) { | ||
const base = IS_PROD | ||
? storeUrl | ||
: `https://${api.storeId}.${api.environment}.com.br` | ||
const url = new URL(`${base}/api/intelligent-search/product_search`) | ||
url.searchParams.append('query', `sku.id:${skuId}`) | ||
if (IS_PROD) { | ||
url.searchParams.append('workspace', 'chrs') | ||
} | ||
|
||
return url.toString() | ||
} | ||
|
||
export async function fetcher(skuId: string) { | ||
return fetch(getUrl(skuId)).then((res) => | ||
res.json() | ||
) as Promise<ProductSearchResult> | ||
} |
This file contains 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,45 @@ | ||
import useSWR from 'swr' | ||
import { aggregateOffer } from './aggregate' | ||
import { enhanceCommercialOffer } from './enhance' | ||
import { fetcher } from './fetcher' | ||
import { bestOfferFirst } from './sort' | ||
export { getUrl as getOfferUrl } from './fetcher' | ||
|
||
const ERROR_DATA = { offers: {}, isValidating: false } | ||
|
||
export function useOffer(args: { skuId: string }) { | ||
const { data, error, isValidating } = useSWR(args.skuId, fetcher) | ||
|
||
if (error || !data || data.products.length === 0) { | ||
console.warn('Error or no data fetching offer to SKU', args.skuId, error) | ||
return ERROR_DATA | ||
} | ||
|
||
const product = data.products[0] | ||
|
||
if (!product || product.items.length === 0) { | ||
console.warn('Product not found or has no items for SKU', args.skuId) | ||
return ERROR_DATA | ||
} | ||
|
||
const item = product.items.find((item) => item.itemId === args.skuId) | ||
|
||
if (!item) { | ||
console.warn('Item not found for SKU', args.skuId) | ||
return ERROR_DATA | ||
} | ||
|
||
const sellers = item.sellers | ||
.map((seller) => | ||
enhanceCommercialOffer({ | ||
offer: seller.commertialOffer, | ||
seller, | ||
product: item, | ||
}) | ||
) | ||
.sort(bestOfferFirst) | ||
|
||
const offers = aggregateOffer(sellers) | ||
|
||
return { offers, isValidating } | ||
} |
Oops, something went wrong.