Skip to content

Commit

Permalink
refactor(file): implement new alexandria search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz committed Dec 9, 2024
1 parent 2afbd8e commit d0616af
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 38 deletions.
12 changes: 0 additions & 12 deletions addon/adapters/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,5 @@ export default function (BaseClass) {

return ajaxOptions;
}

// Overwrite and replicate the query function,
// because ember doesnt pass adapterOptions to urlForQuery
query(_, type, query, __, options) {
let url = this.buildURL(type.modelName, null, null, "query", query);

if (options?.adapterOptions?.customEndpoint) {
url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`;
}

return this.ajax(url, "GET", { data: query });
}
};
}
15 changes: 15 additions & 0 deletions addon/adapters/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function (BaseClass) {
return class SearchResultAdapter extends BaseClass {
// Overwrite and replicate the query function,
// because ember doesnt pass adapterOptions to urlForQuery
query(_, type, query, __, options) {
let url = this.buildURL(type.modelName, null, null, "query", query);

if (options?.adapterOptions?.customEndpoint) {
url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`;
}

return this.ajax(url, "GET", { data: query });
}
};
}
1 change: 0 additions & 1 deletion addon/components/document-view.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
>
<div
class="document-view"
{{did-insert (perform this.initialiseDocumentSelection)}}
>
{{! List & Grid View }}
{{#if this.listView}}
Expand Down
44 changes: 33 additions & 11 deletions addon/components/document-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,50 @@ export default class DocumentViewComponent extends Component {

@task
*fetchDocuments() {
const documents = yield this.store.query("document", {
include: "category,files,tags",
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
let documents = [];
if (this.args.filters.query) {
this.args.filters.only_newest = true;
const searchResult = yield this.store.query(
"search-result",
{
include: "document,matched_file",
filter: this.args.filters || {},
page: { number: 1 },
},
{
adapterOptions: {
customEndpoint: "search",
},
},
);

documents = searchResult.reduce((acc, result) => {
if (!acc.some((doc) => doc.id === result.document.id)) {
acc.push(result.document);
}
return acc;
}, []);
} else {
documents = yield this.store.query("document", {
include: "category,files,tags",
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
}

this.initialiseDocumentSelection(documents);

return yield this.config.documentsPostProcess(documents);
}

@task
*initialiseDocumentSelection() {
initialiseDocumentSelection(docs) {
let docIds = [];
if (this.router.externalRouter.currentRoute?.queryParams?.document) {
docIds = decodeURIComponent(
this.router.externalRouter.currentRoute.queryParams.document,
).split(",");
}
if (docIds.length !== 0) {
const docs = yield this.store.query("document", {
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
const selectedDocs = [...docs].filter((doc) => docIds.includes(doc.id));
selectedDocs.forEach((doc) => this.documents.selectDocument(doc));
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tracked } from "@glimmer/tracking";
import { task } from "ember-concurrency";
import { trackedFunction } from "reactiveweb/function";

export default class DocumentViewComponent extends Component {
export default class SearchViewComponent extends Component {
@service store;
@service("alexandria-config") config;
@service("alexandria-documents") documents;
Expand All @@ -22,10 +22,10 @@ export default class DocumentViewComponent extends Component {
return [];
}

const files = await this.store.query(
"file",
const search = await this.store.query(
"search-result",
{
include: "document,renderings",
include: "document,matched_file",
filter: this.args.filters || {},
page: { number: 1 },
},
Expand All @@ -36,14 +36,7 @@ export default class DocumentViewComponent extends Component {
},
);

const documents = Array.from(
new Map(
files.map((file) => [
file.document.id,
this.store.peekRecord("document", file.document.id),
]),
).values(),
);
const documents = search.map((result) => result.document);

return await this.config.documentsPostProcess(documents);
});
Expand Down
2 changes: 1 addition & 1 deletion addon/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class IndexController extends Controller {
category: this.category,
tags: this.tags.length ? this.tags.join(",") : undefined,
marks: this.marks.length ? this.marks.join(",") : undefined,
search: this.search,
query: this.search,
activeGroup: this.activeGroup,
};

Expand Down
1 change: 1 addition & 0 deletions addon/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class SearchController extends Controller {

get filters() {
const filters = {
onlyNewest: true,
query: this.search,
};

Expand Down
11 changes: 11 additions & 0 deletions addon/models/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Model, { attr, belongsTo } from "@ember-data/model";

export default class SearchResultModel extends Model {
@attr searchRank;
@attr searchContext;
@attr fileName;
@attr documentName;

@belongsTo("document", { inverse: null, async: false }) document;
@belongsTo("matched-file", { inverse: null, async: false }) file;
}
3 changes: 3 additions & 0 deletions addon/serializers/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LocalizedSerializer } from "ember-localized-model";

export default class SearchResultSerializer extends LocalizedSerializer {}
2 changes: 1 addition & 1 deletion addon/templates/search.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div
class="alexandria-container uk-flex uk-flex-1 uk-height-1-1 uk-border uk-background-default uk-width-1 uk-flex-column uk-overflow-hidden"
>
<FileSearch
<SearchView
@filters={{this.filters}}
class="uk-border-top"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ApplicationAdapter from "./application";

import adapterFactory from "ember-alexandria/adapters/search-result";

export default adapterFactory(ApplicationAdapter);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/models/search-result";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/serializers/search-result";
5 changes: 5 additions & 0 deletions tests/dummy/app/adapters/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ApplicationAdapter from "./application";

import adapterFactory from "ember-alexandria/adapters/search-result";

export default adapterFactory(ApplicationAdapter);
1 change: 1 addition & 0 deletions tests/dummy/app/models/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/models/search-result";
1 change: 1 addition & 0 deletions tests/dummy/app/serializers/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/serializers/search-result";

0 comments on commit d0616af

Please sign in to comment.