bugfix. Closes #122
This commit is contained in:
parent
a1f15da39f
commit
f7b74453ab
@ -16,12 +16,12 @@ export default class Algolia {
|
|||||||
|
|
||||||
this.applicationID = config.get('applicationID');
|
this.applicationID = config.get('applicationID');
|
||||||
this.apiKey = config.get('apiKey');
|
this.apiKey = config.get('apiKey');
|
||||||
this.searchURL = `https://${this.applicationID}-dsn.algolia.net/1`;
|
this.searchURL = `https://${ this.applicationID }-dsn.algolia.net/1`;
|
||||||
|
|
||||||
this.entriesCache = {
|
this.entriesCache = {
|
||||||
collection: null,
|
collection: null,
|
||||||
page: null,
|
page: null,
|
||||||
entries: []
|
entries: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ export default class Algolia {
|
|||||||
'X-Algolia-API-Key': this.apiKey,
|
'X-Algolia-API-Key': this.apiKey,
|
||||||
'X-Algolia-Application-Id': this.applicationID,
|
'X-Algolia-Application-Id': this.applicationID,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...headers
|
...headers,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,11 +48,11 @@ export default class Algolia {
|
|||||||
const params = [];
|
const params = [];
|
||||||
if (options.params) {
|
if (options.params) {
|
||||||
for (const key in options.params) {
|
for (const key in options.params) {
|
||||||
params.push(`${key}=${encodeURIComponent(options.params[key])}`);
|
params.push(`${ key }=${ encodeURIComponent(options.params[key]) }`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (params.length) {
|
if (params.length) {
|
||||||
path += `?${params.join('&')}`;
|
path += `?${ params.join('&') }`;
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ export default class Algolia {
|
|||||||
request(path, options = {}) {
|
request(path, options = {}) {
|
||||||
const headers = this.requestHeaders(options.headers || {});
|
const headers = this.requestHeaders(options.headers || {});
|
||||||
const url = this.urlFor(path, options);
|
const url = this.urlFor(path, options);
|
||||||
return fetch(url, { ...options, headers: headers }).then((response) => {
|
return fetch(url, { ...options, headers }).then((response) => {
|
||||||
const contentType = response.headers.get('Content-Type');
|
const contentType = response.headers.get('Content-Type');
|
||||||
if (contentType && contentType.match(/json/)) {
|
if (contentType && contentType.match(/json/)) {
|
||||||
return this.parseJsonResponse(response);
|
return this.parseJsonResponse(response);
|
||||||
@ -72,14 +72,14 @@ export default class Algolia {
|
|||||||
|
|
||||||
search(collections, searchTerm, page) {
|
search(collections, searchTerm, page) {
|
||||||
const searchCollections = collections.map(collection => (
|
const searchCollections = collections.map(collection => (
|
||||||
{ indexName: collection, params: `query=${searchTerm}&page=${page}` }
|
{ indexName: collection, params: `query=${ searchTerm }&page=${ page }` }
|
||||||
));
|
));
|
||||||
|
|
||||||
return this.request(`${this.searchURL}/indexes/*/queries`, {
|
return this.request(`${ this.searchURL }/indexes/*/queries`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ requests: searchCollections })
|
body: JSON.stringify({ requests: searchCollections }),
|
||||||
}).then(response => {
|
}).then((response) => {
|
||||||
const entries = response.results.map((result, index) => result.hits.map(hit => {
|
const entries = response.results.map((result, index) => result.hits.map((hit) => {
|
||||||
const slug = hit.slug || getSlug(hit.path);
|
const slug = hit.slug || getSlug(hit.path);
|
||||||
return createEntry(collections[index], slug, hit.path, { data: hit.data, partial: true });
|
return createEntry(collections[index], slug, hit.path, { data: hit.data, partial: true });
|
||||||
}));
|
}));
|
||||||
@ -89,11 +89,11 @@ export default class Algolia {
|
|||||||
}
|
}
|
||||||
|
|
||||||
searchBy(field, collection, query) {
|
searchBy(field, collection, query) {
|
||||||
return this.request(`${this.searchURL}/indexes/${collection}`, {
|
return this.request(`${ this.searchURL }/indexes/${ collection }`, {
|
||||||
params: {
|
params: {
|
||||||
restrictSearchableAttributes: field,
|
restrictSearchableAttributes: field,
|
||||||
query
|
query,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,14 +101,14 @@ export default class Algolia {
|
|||||||
if (this.entriesCache.collection === collection && this.entriesCache.page === page) {
|
if (this.entriesCache.collection === collection && this.entriesCache.page === page) {
|
||||||
return Promise.resolve({ page: this.entriesCache.page, entries: this.entriesCache.entries });
|
return Promise.resolve({ page: this.entriesCache.page, entries: this.entriesCache.entries });
|
||||||
} else {
|
} else {
|
||||||
return this.request(`${this.searchURL}/indexes/${collection.get('name')}`, {
|
return this.request(`${ this.searchURL }/indexes/${ collection.get('name') }`, {
|
||||||
params: { page }
|
params: { page },
|
||||||
}).then(response => {
|
}).then((response) => {
|
||||||
const entries = response.hits.map(hit => {
|
const entries = response.hits.map((hit) => {
|
||||||
const slug = hit.slug || getSlug(hit.path);
|
const slug = hit.slug || getSlug(hit.path);
|
||||||
return createEntry(collection.get('name'), slug, hit.path, { data: hit.data, partial: true });
|
return createEntry(collection.get('name'), slug, hit.path, { data: hit.data, partial: true });
|
||||||
});
|
});
|
||||||
this.entriesCache = { collection, page, entries };
|
this.entriesCache = { collection, pagination: response.page, entries };
|
||||||
return { entries, pagination: response.page };
|
return { entries, pagination: response.page };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ export default class Algolia {
|
|||||||
|
|
||||||
getEntry(collection, slug) {
|
getEntry(collection, slug) {
|
||||||
return this.searchBy('slug', collection.get('name'), slug).then((response) => {
|
return this.searchBy('slug', collection.get('name'), slug).then((response) => {
|
||||||
const entry = response.hits.filter((hit) => hit.slug === slug)[0];
|
const entry = response.hits.filter(hit => hit.slug === slug)[0];
|
||||||
return createEntry(collection.get('name'), slug, entry.path, { data: entry.data, partial: true });
|
return createEntry(collection.get('name'), slug, entry.path, { data: entry.data, partial: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -31,16 +31,16 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
|
|||||||
collection = action.payload.collection;
|
collection = action.payload.collection;
|
||||||
loadedEntries = action.payload.entries;
|
loadedEntries = action.payload.entries;
|
||||||
page = action.payload.page;
|
page = action.payload.page;
|
||||||
|
|
||||||
return state.withMutations((map) => {
|
return state.withMutations((map) => {
|
||||||
loadedEntries.forEach(entry => (
|
loadedEntries.forEach(entry => (
|
||||||
map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
|
map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
|
||||||
));
|
));
|
||||||
|
|
||||||
const ids = List(loadedEntries.map(entry => entry.slug));
|
const ids = List(loadedEntries.map(entry => entry.slug));
|
||||||
|
|
||||||
map.setIn(['pages', collection], Map({
|
map.setIn(['pages', collection], Map({
|
||||||
page,
|
page,
|
||||||
ids: page === 0 ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids),
|
ids: (!page || page === 0) ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids),
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user