Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add persistent query and paging #70

Merged
merged 5 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const router = createRouter({
path: "/search",
name: "search",
component: () => import("../views/search/SearchView.vue"),
props: (route) => ({
offset: route.query.offset,
limit: route.query.limit,
query: route.query.query,
}),
},
{
path: "/imprint",
Expand Down
50 changes: 49 additions & 1 deletion src/views/search/SearchView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,25 @@ import {
ref,
unref,
type Ref,
watch,
} from "vue";
import ExportProgress from "./ExportProgress.vue";
import { downloadFullTsv, type ProgressEvent } from "./ExportTsv";
import ResultTable from "./ResultTable.vue";
import { useRoute, useRouter } from "vue-router";
const pageState = usePageState();
const searchState = usePageState();
const entries: Ref<BakrepSearchResultEntry[]> = ref([]);

const api = useApi();
const route = useRoute();
const router = useRouter();

const pagination: Ref<PaginationData> = ref(empty());
const query: Ref<CompoundQuery> = ref({ op: "and", value: [] });
const ordering: Ref<SortOption[]> = ref([{ field: "id", ord: "asc" }]);
const searchinfo: Ref<SearchInfo> = ref({ fields: [] });

function init() {
pageState.value.setState(State.Loading);
api
Expand All @@ -52,9 +58,50 @@ function init() {
searchinfo.value = r;
pageState.value.setState(State.Main);
})
.then(populateVariables)
.catch((err) => pageState.value.setError(err));
}

function encodeQuery(): string {
return btoa(JSON.stringify({ query: query.value, ordering: ordering.value }));
}

function decodeQuery(encodedQuery: string): {
query: CompoundQuery;
ordering: SortOption[];
} {
return JSON.parse(atob(encodedQuery));
}

function populateVariables() {
if (route.query.query) {
const decodedQuery = decodeQuery(route.query.query as string);
query.value = decodedQuery.query;
ordering.value = decodedQuery.ordering;
pagination.value.offset = Number.parseInt(route.query.offset as string);
pagination.value.limit = Number.parseInt(route.query.limit as string);
search(pagination.value.offset);
}
}

watch(
() => route.query,
() => {
populateVariables();
},
);

function updateQuery(offset = 0) {
router.push({
name: "search",
query: {
offset: offset,
limit: pagination.value.limit,
query: encodeQuery(),
},
});
}

function searchinfo2querybuilderrules(f: SearchInfoField): Rule {
const config = fieldNames[f.field];
const field = config ? config.label : f.field;
Expand Down Expand Up @@ -170,6 +217,7 @@ function search(offset = 0) {
searchState.value.setState(State.Main);
if (r.offset) pagination.value.offset = r.offset;
pagination.value.total = r.total;
updateQuery(r.offset);
})
.catch((err) => pageState.value.setError(err));
}
Expand Down Expand Up @@ -294,7 +342,7 @@ onBeforeUnmount(() => {
v-if="pagination.total > 0"
class="mt-3"
:value="pagination"
@update:offset="search"
@update:offset="updateQuery"
/>
</div>
</Loading>
Expand Down