Skip to content

Commit

Permalink
Merge pull request #909 from searchspring/redirect-single-result
Browse files Browse the repository at this point in the history
Redirect single result
  • Loading branch information
korgon authored Oct 4, 2023
2 parents e5680dc + 359e901 commit 717339a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,57 @@ describe('Autocomplete Controller', () => {
});
});

it('can redirect url when singleResult', async () => {
document.body.innerHTML = '<div><input type="text" id="search_query"></div>';
acConfig = {
...acConfig,
selector: '#search_query',
action: '/search',
settings: {
redirects: {
singleResult: true,
},
},
};

const controller = new AutocompleteController(acConfig, {
client: new MockClient(globals, {}),
store: new AutocompleteStore(acConfig, services),
urlManager,
eventManager: new EventManager(),
profiler: new Profiler(),
logger: new Logger(),
tracker: new Tracker(globals),
});
(controller.client as MockClient).mockData.updateConfig({ autocomplete: 'singleResult', siteId: '8uyt2m' });

const query = 'dress';
controller.urlManager = controller.urlManager.set('query', query);

await controller.bind();
const inputEl: HTMLInputElement = document.querySelector(controller.config.selector)!;
expect(inputEl).toBeDefined();

inputEl.value = query;

await controller.search();
expect(controller.store.results.length).toBe(1);

// @ts-ignore
delete window.location;
window.location = {
...window.location,
href: '', // jest does not support window location changes
};

inputEl.focus();
inputEl.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, keyCode: KEY_ENTER }));

await waitFor(() => {
expect(window.location.href).toContain(controller.store.results[0].mappings.core!.url);
});
});

it('tests bind method without form (using config.action)', async () => {
acConfig = {
...acConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const defaultConfig: AutocompleteControllerConfig = {
},
redirects: {
merchandising: true,
singleResult: true,
},
},
};
Expand Down Expand Up @@ -108,6 +109,15 @@ export class AutocompleteController extends AbstractController {
window.location.href = redirectURL;
return false;
}

if (this.config?.settings?.redirects?.singleResult) {
const { results } = (ac.controller as AutocompleteController).store;
const singleResultUrl = results.length === 1 && results[0].type === 'product' && results[0].mappings.core?.url;
if (singleResultUrl) {
window.location.href = singleResultUrl;
return false;
}
}
});
// attach config plugins and event middleware
this.use(this.config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"pagination": {
"totalResults": 1,
"page": 1,
"pageSize": 30,
"totalPages": 1
},
"results": [
{
"id": "182146",
"mappings": {
"core": {
"uid": "182146",
"sku": "C-AD-W1-1869P",
"name": "Stripe Out White Off-The-Shoulder Dress",
"url": "/product/C-AD-W1-1869P",
"addToCartUrl": "/product/C-AD-W1-1869P",
"price": 48,
"msrp": 50,
"imageUrl": "https://searchspring-demo-content.s3.amazonaws.com/demo/fashion/product_images_large/4468_copyright_reddressboutique_2017__large.jpg",
"secureImageUrl": "https://searchspring-demo-content.s3.amazonaws.com/demo/fashion/product_images_large/4468_copyright_reddressboutique_2017__large.jpg",
"thumbnailImageUrl": "https://searchspring-demo-content.s3.amazonaws.com/demo/fashion/product_images_thumb_med/4468_copyright_reddressboutique_2017__thumb_med.jpg",
"rating": "5",
"ratingCount": "1111",
"description": "Are you Stripe Out of ideas for what to wear this weekend on that trip you've got coming up with your friends? Afraid you'll be the odd one out and everyone else will be all cute and trendy and there you'll be ... not trendy and wearing the same old things you've been wearing on this annual getaway for years? Lucky for you, here's the dress you've been searching for. Doesn't matter what else you pack (it does, you'll want to continue to shop with us, we were just being nice) this is the piece that will set you apart from everyone else (that is absolutely true, you will be a Goddess among women). Take that, bad fashion moments of the past! Striped dress features 3/4 sleeve bell sleeves with a partially elastic/open back. Model is wearing a small. • 97% Cotton 3% Spandex • Machine Wash Cold • Lined • Made in the USA",
"stockMessage": "In stock",
"brand": "Adrienne",
"popularity": "4461",
"caption": "Captions!"
}
},
"attributes": {
"id": "b419ddfcad5f87ee49c786eb5f8621fd",
"intellisuggestData": "eJyyKK0sMcplYEgpSi0uZnDWdXTRDTfUNbQwswxgMGQwZDBgMDSyNGdIL8pMAQQAAP__9rsKZQ",
"intellisuggestSignature": "be0783431378a1700c8e0e498aa928696c08495713585d89fd975e3ba40c0c4d",
"product_type_unigram": "dress"
},
"children": []
}
],
"filters": [],
"facets": [],
"sorting": [],
"merchandising": {
"campaigns": [],
"redirect": "",
"content": {}
},
"search": {
"query": "dress"
},
"autocomplete": {
"query": "dress",
"suggested": {
"text": "dress",
"type": "exact",
"source": "popular-query"
},
"alternatives": [
{
"text": "red dress"
}
]
}
}
47 changes: 25 additions & 22 deletions packages/snap-store-mobx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,36 @@ export type FinderFieldConfig = {
levels?: string[];
};

export type AutocompleteStoreConfigSettings = {
integratedSpellCorrection?: boolean;
initializeFromUrl?: boolean;
syncInputs?: boolean;
serializeForm?: boolean;
facets?: FacetStoreConfig & {
fields?: {
[field: string]: FacetStoreConfig;
};
};
trending?: {
limit: number;
showResults?: boolean;
};
history?: {
limit: number;
showResults?: boolean;
};
redirects?: {
merchandising?: boolean;
singleResult?: boolean;
};
};

// Autocomplete config
export type AutocompleteStoreConfig = StoreConfig & {
globals?: Partial<AutocompleteRequestModel>;
selector: string;
action?: string;
settings?: {
integratedSpellCorrection?: boolean;
initializeFromUrl?: boolean;
syncInputs?: boolean;
serializeForm?: boolean;
facets?: FacetStoreConfig & {
fields?: {
[field: string]: FacetStoreConfig;
};
};
trending?: {
limit: number;
showResults?: boolean;
};
history?: {
limit: number;
showResults?: boolean;
};
redirects?: {
merchandising?: boolean;
};
};
settings?: AutocompleteStoreConfigSettings;
};

// Recommendation config
Expand Down

0 comments on commit 717339a

Please sign in to comment.