Skip to content

Commit

Permalink
Merge pull request #2 from shaniit-org/dev
Browse files Browse the repository at this point in the history
update on algolia record update
  • Loading branch information
Riley1101 authored Nov 22, 2023
2 parents 75f69a6 + ad528d2 commit 8609ebe
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 59 deletions.
118 changes: 59 additions & 59 deletions src/lib/utils/algolia.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,80 @@ import algoliasearch from 'algoliasearch/lite';

import { autocomplete, getAlgoliaResults } from '@algolia/autocomplete-js';

const APP_ID = import.meta.env.FLY_ALGOLIA_ID;
const API = import.meta.env.FLY_ALGOLIA_API;
const APP_ID = import.meta.env.FLY_ALGOLIA_ID || import.meta.env.VITE_ALGOLIA_ID;
const API = import.meta.env.FLY_ALGOLIA_API || import.meta.env.VITE_ALGOLIA_API;

const searchClient = algoliasearch(APP_ID, API);
export const searchClient = algoliasearch(APP_ID, API);

export function init_autocomplete() {
autocomplete({
placeholder: 'Search',
classNames: {
detachedSearchButton: 'aa-input-d',
detachedSearchButtonIcon: 'aa-input-d-i',
detachedSearchButtonQuery: 'aa-input-d-q',
detachedSearchButtonPlaceholder: 'aa-input-d-p',
inputWrapper: 'form-group'
},
detachedMediaQuery: '',
container: '#autocomplete',
insights: true,
getSources({ query }) {
return [
{
sourceId: 'news',
getItems() {
return getAlgoliaResults({
searchClient,
queries: [
{
indexName: 'news',
query,
params: {
hitsPerPage: 3,
attributesToSnippet: ['title:10', 'description:35'],
snippetEllipsisText: '…'
}
},
autocomplete({
placeholder: 'Search',
classNames: {
detachedSearchButton: 'aa-input-d',
detachedSearchButtonIcon: 'aa-input-d-i',
detachedSearchButtonQuery: 'aa-input-d-q',
detachedSearchButtonPlaceholder: 'aa-input-d-p',
inputWrapper: 'form-group'
},
detachedMediaQuery: '',
container: '#autocomplete',
insights: true,
getSources({ query }) {
return [
{
sourceId: 'news',
getItems() {
return getAlgoliaResults({
searchClient,
queries: [
{
indexName: 'news',
query,
params: {
hitsPerPage: 3,
attributesToSnippet: ['title:10', 'description:35'],
snippetEllipsisText: '…'
}
},

{
indexName: 'events',
query,
params: {
hitsPerPage: 3,
attributesToSnippet: ['title:10', 'description:35'],
snippetEllipsisText: '…'
}
}
]
});
},
templates: {
item({ item, components, html }) {
return html`<a
{
indexName: 'events',
query,
params: {
hitsPerPage: 3,
attributesToSnippet: ['title:10', 'description:35'],
snippetEllipsisText: '…'
}
}
]
});
},
templates: {
item({ item, components, html }) {
return html`<a
href="/${item.__autocomplete_indexName}/${item.slug}"
class="flex flex-col cursor-pointer p-2"
>
<div class="text-theme-dark">
${components.Highlight({
hit: item,
attribute: 'title'
})}
hit: item,
attribute: 'title'
})}
<span class="ml-3 px-2 py-1 bx--tag bx--tag--blue bx--tax--sm max-w-max my-2 ">
${item.__autocomplete_indexName}
</span>
</div>
<div class="text-theme-dark/80 text-sm">
${components.Snippet({
hit: item,
attribute: 'description'
})}
hit: item,
attribute: 'description'
})}
</div>
</a>`;
}
}
}
];
}
});
}
}
}
];
}
});
}
51 changes: 51 additions & 0 deletions src/routes/api/algolia/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { searchClient } from '$lib/utils/algolia';
import { sanityClient } from '$lib/sanity/client';
const query = `{
'news':*[_type =="news"][0...1000]{
"objectID" : _id,
"slug": slug.current,
title,
description
},
"events":*[_type =="event"][0...1000]{
"objectID" : _id,
"slug": slug.current,
title,
description
}
}`;

/** @type {import('./$types').RequestHandler} */
export async function GET({ url }) {
let secret = url.searchParams.get('secret');
if (secret !== import.meta.env.FLY_ALGOLIA_SECRET) {
return new Response(
JSON.stringify({
status: 403,
body: 'forbidden'
})
);
}
try {
const { news, events } = await sanityClient.fetch(query);
const newsIndex = searchClient.initIndex('news');
const eventsIndex = searchClient.initIndex('events');
// @ts-ignore
await newsIndex.saveObjects(news);
// @ts-ignore
await eventsIndex.saveObjects(events);
return new Response(
JSON.stringify({
status: 200,
body: 'success'
})
);
} catch (e) {
return new Response(
JSON.stringify({
status: 500,
body: e
})
);
}
}

0 comments on commit 8609ebe

Please sign in to comment.