Skip to content

Commit

Permalink
feat(adapter): update adapter's config in order to use the new platfo… (
Browse files Browse the repository at this point in the history
#2)

feat(adapter): update adapter's config in order to use the new platform's api

EX-3785
  • Loading branch information
Pedro López Mareque authored Jun 9, 2021
1 parent 0081f4f commit 2d051f0
Show file tree
Hide file tree
Showing 10 changed files with 3,376 additions and 3,694 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": ["plugin:@empathy/x/all"]
"extends": ["plugin:@empathyco/x/all"]
}
6,878 changes: 3,247 additions & 3,631 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 14 additions & 27 deletions public/snippet-script.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
function getURLParameter(name) {
return decodeURIComponent(
(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
return decodeURIComponent(
(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
}

var instance = getURLParameter('instance') || 'juguettos';
var instance = getURLParameter('instance') || undefined;
var env = getURLParameter('env') || undefined;
var scope = getURLParameter('scope') || 'default';
var lang = getURLParameter('lang') || 'es';
var scope = getURLParameter('scope') || 'desktop';
var lang = getURLParameter('lang') || 'en';
var device = getURLParameter('device') || 'mobile';
var searchLang = getURLParameter('searchLang') || lang;
var currency = getURLParameter('currency') || 'EUR';
var consent = getURLParameter('consent') === 'true' || false;
var documentDirection = getURLParameter('doc-dir') || 'ltr';

/*window.initX = function() {
return {
instance,
env,
scope,
lang,
searchLang,
currency,
consent,
documentDirection
};
};*/

window.initX = {
instance,
env,
scope,
lang,
device,
searchLang,
currency,
consent,
documentDirection
instance,
env,
scope,
lang,
device,
searchLang,
currency,
consent,
documentDirection
};

23 changes: 23 additions & 0 deletions src/adapters/demo-request-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { EmpathySearchRequest, SearchRequest } from '@empathy/search-adapter';

/**
* This mapper modifies the request sent to the API.
*
* @param rawRequest - The initial
* {@link @empathy/search-adapter#SearchRequest | SearchRequest} object without
* any modification.
* @param request - The {@link @empathy/search-adapter#EmpathySearchRequest |
* empathy's search request object} with the changes done in previous hooks or
* mappers.
*
* @returns A new {@link @empathy/search-adapter#EmpathySearchRequest |
* empathy's search request object}.
*/
export function customRequestMapper(
rawRequest: SearchRequest,
request: EmpathySearchRequest
): EmpathySearchRequest {
return Object.assign(request, {
query: rawRequest.query
});
}
65 changes: 65 additions & 0 deletions src/adapters/demo-result.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { EmpathyResult } from '@empathy/search-adapter';
import { Result } from '@empathy/search-types';

/**
* Custom platform result which extends the base {@link Result}.
*/
interface CustomResult {
/** Gender of the result. */
gender: string;
/** Color of the result. */
color: string;
/** Year of the result. */
year: string;
/** List of category paths of the result. */
categoryPaths: string[];
/** Usage of the result. */
usage: string;
/** Group ID of the result. */
groupId: string;
/** Type of the result. */
type: string;
/** Score of the result. */
score: number;
/** List of category ids of the result. */
categoryIds: string[];
/** Size of the result. */
size: string;
/** Season of the result. */
season: string;
/** List of categories of the result. */
categories: string[];
}

declare module '@empathy/search-types' {
interface Result extends CustomResult {}
}

declare module '@empathy/search-adapter' {
interface EmpathyResult extends CustomResult {}
}

/**
* This mapper adapts the response from the api to X Components.
*
* @param rawResult - This is the raw response form the API.
* @param result - This is the Empathy result handle by the app.
*
* @returns A new object which can be handled by XComponents.
*/
export function resultMapper(rawResult: EmpathyResult, result: Result): Result {
return Object.assign<Result, Partial<Result>>(result, {
gender: rawResult.gender,
color: rawResult.color,
year: rawResult.year,
usage: rawResult.usage,
groupId: rawResult.groupId,
type: rawResult.type,
score: rawResult.score,
size: rawResult.size,
season: rawResult.season,
categories: rawResult.categories,
categoryIds: rawResult.categoryIds,
categoryPaths: rawResult.categoryPaths
});
}
2 changes: 1 addition & 1 deletion src/components/body/sort.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
}
})
export default class SortComponent extends Vue {
public sortValues: Sort[] = ['', 'priceSort asc', 'priceSort desc'];
public sortValues: Sort[] = ['', 'price asc', 'price desc'];
}
</script>
10 changes: 5 additions & 5 deletions src/i18n/messages/en.messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"title": "Recommendations"
},
"facets": {
"hierarchical_category": "Categories",
"rootCategories_facet": "Root Categories",
"brand_facet": "Brands",
"age_facet": "Age",
"price_facet": "Price"
"gender": "Gender",
"categoryPaths": "Categories",
"price": "Price",
"usage": "Usage",
"season": "Season"
}
},
"tablet": {
Expand Down
10 changes: 5 additions & 5 deletions src/i18n/messages/es.messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"title": "Recomendaciones"
},
"facets": {
"hierarchical_category": "Categorías",
"rootCategories_facet": "Categorías",
"brand_facet": "Marcas",
"age_facet": "Edad",
"price_facet": "Precio"
"gender": "Género",
"categoryPaths": "Categorías",
"price": "Precio",
"usage": "Uso",
"season": "Temporada"
}
},
"tablet": {
Expand Down
35 changes: 11 additions & 24 deletions src/x-components/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { EmpathyAdapterBuilder } from '@empathy/search-adapter';
import { Result } from '@empathy/search-types';
import { customRequestMapper } from '@/adapters/demo-request-mapper';
import { resultMapper } from '@/adapters/demo-result.mapper';

export const adapter = new EmpathyAdapterBuilder()
.setInstance('juguettos')
.setEnvironment('staging')
.addMapper((_, result: Result) => {
result.url = `./product_page.html?productId=${result.id.toString()}`;
result.identifier.value = `${result.id.toString()}`;
return result;
}, 'results')
.addRequestMapper(customRequestMapper)
.addMapper(resultMapper, 'results')
.setFeatureConfig('search', {
endpoint: 'https://api.empathybroker.com/search/v1/query/juguettos/searchv2'
endpoint: 'https://search.internal.test.empathy.co/query/empathy/search',
responsePaths: {
results: 'catalog.content',
facets: 'catalog.facets',
totalResults: 'catalog.numFound'
}
})
.setFacetConfig(
{
modelName: 'HierarchicalFacet'
},
'hierarchical_category'
)
.setFacetConfig(
{
modelName: 'NumberRangeFacet',
template: '<!tag=price_facet>priceSort:[<min> TO <max>]'
},
'price_facet'
)
.setLang('es')
.setScope('desktop')
.setInstance('platform')
.build();
4 changes: 4 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": []
}

0 comments on commit 2d051f0

Please sign in to comment.