Skip to content

Commit

Permalink
feat: add anon id and search uuid to catalog search request (#2214)
Browse files Browse the repository at this point in the history
* feat: add anon id and search uuid to catalog search request

* test: mock the getAnalytics to fix the test

* fix: add safe operator when getting anonId

* fix: safe operator to avoid fn call if not available
  • Loading branch information
juanmahidalgo committed Apr 11, 2024
1 parent 62995de commit f587aa0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context as ResponsiveContext } from 'react-responsive'
import { fireEvent, waitFor, within } from '@testing-library/react'
import { BodyShape, NFTCategory, Network, Rarity, WearableCategory } from '@dcl/schemas'
import { getAnalytics } from 'decentraland-dapps/dist/modules/analytics/utils'
import { Asset, AssetType } from '../../../modules/asset/types'
import { getAssetUrl } from '../../../modules/asset/utils'
import { locations } from '../../../modules/routing/locations'
Expand All @@ -20,6 +21,7 @@ import {
import { LOCAL_STORAGE_RECENT_SEARCHES_KEY, SearchBarDropdown } from './SearchBarDropdown'
import { SearchBarDropdownProps } from './SearchBarDropdown.types'

jest.mock('decentraland-dapps/dist/modules/analytics/utils')
jest.mock('decentraland-dapps/dist/containers/Profile', () => {
return {
__esModule: true,
Expand All @@ -29,6 +31,8 @@ jest.mock('decentraland-dapps/dist/containers/Profile', () => {
jest.mock('../../../modules/vendor/decentraland/catalog/api')
jest.mock('../../../modules/vendor/decentraland/builder/api')

const getAnalyticsMock = getAnalytics as jest.Mock

const mockLocalStorage = () =>
(() => {
let store = {} as Storage
Expand Down Expand Up @@ -140,6 +144,16 @@ function renderSearchDropBarDropdown(props: Partial<SearchBarDropdownProps> = {}
)
}

beforeEach(() => {
getAnalyticsMock.mockReturnValue({
page: jest.fn(),
track: jest.fn(),
user: () => ({
anonymousId: () => 'anAnonymousId'
})
})
})

describe('SearchBarDropdown', () => {
describe('when providing a search term', () => {
let props: Partial<SearchBarDropdownProps>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,21 @@ export const SearchBarDropdown = ({
let cancel = false
if (searchTerm) {
const searchUUID = uuidv5(searchTerm, UUID_NAMESPACE)
const anonId = getAnalytics()?.user()?.anonymousId?.() || ''
if (currentSearchTab === SearchTab.EMOTES || currentSearchTab === SearchTab.WEARABLES) {
setIsLoading(true)
catalogAPI
.get({
search: searchTerm,
category: category,
first: MAX_AMOUNT_OF_RESULTS
})
.get(
{
search: searchTerm,
category: category,
first: MAX_AMOUNT_OF_RESULTS
},
{
'x-search-uuid': searchUUID,
'x-anonymous-id': anonId
}
)
.then(response => {
if (!cancel) {
setResults(response.data)
Expand Down
10 changes: 6 additions & 4 deletions webapp/src/modules/vendor/decentraland/catalog/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Item, CatalogFilters } from '@dcl/schemas'
import { BaseClient } from 'decentraland-dapps/dist/lib/BaseClient'
import { NFT_SERVER_URL } from '../nft'
import { MARKETPLACE_SERVER_URL } from '../marketplace/api'
import { retryParams } from '../utils'

export class CatalogAPI extends BaseClient {
async get(filters: CatalogFilters = {}): Promise<{ data: Item[] }> {
async get(filters: CatalogFilters = {}, headers?: Record<string, string>): Promise<{ data: Item[] }> {
const queryParams = this.buildItemsQueryString(filters)
return this.fetch(`/v1/catalog?${queryParams}`)
return this.fetch(`/v1/catalog?${queryParams}`, {
headers
})
}

private buildItemsQueryString(filters: CatalogFilters): string {
Expand Down Expand Up @@ -145,7 +147,7 @@ export class CatalogAPI extends BaseClient {
}
}

export const catalogAPI = new CatalogAPI(NFT_SERVER_URL, {
export const catalogAPI = new CatalogAPI(MARKETPLACE_SERVER_URL, {
retries: retryParams.attempts,
retryDelay: retryParams.delay
})

0 comments on commit f587aa0

Please sign in to comment.