mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
working filtering
This commit is contained in:
1 parent
42c8e915a1
commit
2664b54be0
2 files changed
+157
-38
No files matched your search
+10
-1
@@ -11,6 +11,10 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
|
||||
const view = getView(url, route, View.Grid);
|
||||
const offset = pageToOffset(page, limit);
|
||||
|
||||
const queries = url.searchParams.get('query');
|
||||
const parsedQueries = queries ? JSON.parse(queries) : [];
|
||||
console.log(parsedQueries);
|
||||
|
||||
return {
|
||||
offset,
|
||||
limit,
|
||||
@@ -18,7 +22,12 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
|
||||
documents: await sdk.forProject.databases.listDocuments(
|
||||
params.database,
|
||||
params.collection,
|
||||
[Query.limit(limit), Query.offset(offset), Query.orderDesc('$createdAt')]
|
||||
[
|
||||
Query.limit(limit),
|
||||
Query.offset(offset),
|
||||
Query.orderDesc('$createdAt'),
|
||||
...parsedQueries
|
||||
]
|
||||
)
|
||||
};
|
||||
};
|
||||
+147
-37
@@ -1,15 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { Drop } from '$lib/components';
|
||||
import { Button } from '$lib/elements/forms';
|
||||
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
|
||||
import type { Writable } from 'svelte/store';
|
||||
import { columns } from './store';
|
||||
import { Query } from '@appwrite.io/console';
|
||||
import InputText from '$lib/elements/forms/inputText.svelte';
|
||||
|
||||
let showFilters = false;
|
||||
|
||||
const columnTypes = ['string', 'number'] as const;
|
||||
<script lang="ts" context="module">
|
||||
const columnTypes = ['string', 'number', 'relationship'] as const;
|
||||
type ColumnType = (typeof columnTypes)[number];
|
||||
|
||||
type StoreValues<Store> = Store extends Writable<infer T> ? T : never;
|
||||
@@ -17,31 +7,115 @@
|
||||
type: ColumnType;
|
||||
};
|
||||
|
||||
type Operator = {
|
||||
toTag: (attribute: string, input: string) => string;
|
||||
toQuery: (attribute: string, input: string) => string;
|
||||
types: ColumnType[];
|
||||
};
|
||||
|
||||
function initQueries() {
|
||||
const queries = writable(
|
||||
new Map<string, string>([
|
||||
['**name** starts with **Th**', Query.startsWith('name', 'Th')]
|
||||
])
|
||||
);
|
||||
|
||||
type AddFilterArgs = {
|
||||
operator: Operator;
|
||||
column: Column;
|
||||
value: string;
|
||||
};
|
||||
|
||||
function addFilter({ column, operator, value }: AddFilterArgs) {
|
||||
queries.update((map) => {
|
||||
map.set(operator.toTag(column.id, value), operator.toQuery(column.id, value));
|
||||
return map;
|
||||
});
|
||||
}
|
||||
|
||||
function removeFilter(tag: string) {
|
||||
queries.update((map) => {
|
||||
map.delete(tag);
|
||||
return map;
|
||||
});
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
queries.set(new Map());
|
||||
}
|
||||
|
||||
function apply() {
|
||||
const queryParam = JSON.stringify(Array.from(get(queries)).map(([_, v]) => v));
|
||||
|
||||
const encodedQueryParam = encodeURIComponent(queryParam);
|
||||
const currentLocation = window.location.pathname;
|
||||
goto(`${currentLocation}?query=${encodedQueryParam}`);
|
||||
}
|
||||
|
||||
return {
|
||||
...queries,
|
||||
addFilter,
|
||||
removeFilter,
|
||||
clearAll,
|
||||
apply
|
||||
};
|
||||
}
|
||||
|
||||
const queries = initQueries();
|
||||
const tags = derived(queries, ($queries) => Array.from($queries.keys()));
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { Drop } from '$lib/components';
|
||||
import { Button } from '$lib/elements/forms';
|
||||
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
|
||||
import { derived, get, writable, type Writable } from 'svelte/store';
|
||||
import { columns } from './store';
|
||||
import { Query } from '@appwrite.io/console';
|
||||
import InputText from '$lib/elements/forms/inputText.svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { tick } from 'svelte';
|
||||
|
||||
let showFilters = false;
|
||||
|
||||
let columnId: string | null = null;
|
||||
$: column = $columns.find((c) => c.id === columnId) as Column;
|
||||
|
||||
const operators: Array<{
|
||||
label: string;
|
||||
function: (attribute: string, input: string) => string;
|
||||
types: ColumnType[];
|
||||
}> = [
|
||||
{
|
||||
label: 'starts with',
|
||||
function: Query.startsWith,
|
||||
const operators: Record<string, Operator> = {
|
||||
'starts with': {
|
||||
toQuery: Query.startsWith,
|
||||
toTag: (attribute, input) => `**${attribute}** starts with **${input}**`,
|
||||
types: ['string']
|
||||
},
|
||||
'ends with': {
|
||||
toQuery: Query.endsWith,
|
||||
toTag: (attribute, input) => `**${attribute}** ends with **${input}**`,
|
||||
types: ['string']
|
||||
},
|
||||
'greater than': {
|
||||
toQuery: Query.greaterThan,
|
||||
toTag: (attribute, input) => `**${attribute}** greater than **${input}**`,
|
||||
types: ['number']
|
||||
},
|
||||
'less than': {
|
||||
toQuery: Query.lessThan,
|
||||
toTag: (attribute, input) => `**${attribute}** less than **${input}**`,
|
||||
types: ['number']
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
$: operatorsForColumn = operators
|
||||
.filter((o) => o.types.includes(column?.type))
|
||||
.map((o) => ({
|
||||
label: o.label,
|
||||
value: o.label
|
||||
$: operatorsForColumn = Object.entries(operators)
|
||||
.filter(([_, v]) => v.types.includes(column?.type))
|
||||
.map(([k]) => ({
|
||||
label: k,
|
||||
value: k
|
||||
}));
|
||||
let operator: string | null = null;
|
||||
|
||||
let operatorKey: string | null = null;
|
||||
$: operator = operatorKey ? operators[operatorKey] : null;
|
||||
$: {
|
||||
columnId;
|
||||
operator = null;
|
||||
operatorKey = null;
|
||||
}
|
||||
|
||||
let value: string | null = null;
|
||||
@@ -50,14 +124,17 @@
|
||||
value = null;
|
||||
}
|
||||
|
||||
let filters = [] as string[];
|
||||
// This Map is keyed by tags, and has a query as the value
|
||||
|
||||
function addFilter() {
|
||||
if (column && operator && value) {
|
||||
filters.push(`${column.id}:${operator}:${value}`);
|
||||
filters = filters;
|
||||
queries.addFilter({ column, operator, value });
|
||||
}
|
||||
}
|
||||
|
||||
function tagFormat(node: HTMLElement) {
|
||||
node.innerHTML = node.innerHTML.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>');
|
||||
}
|
||||
</script>
|
||||
|
||||
<Drop bind:show={showFilters} noArrow>
|
||||
@@ -80,22 +157,42 @@
|
||||
id="operator"
|
||||
options={operatorsForColumn}
|
||||
placeholder="Select operator"
|
||||
bind:value={operator} />
|
||||
bind:value={operatorKey} />
|
||||
</div>
|
||||
{#if column && operator}
|
||||
{#if column && operatorKey}
|
||||
<div class="u-margin-block-start-16">
|
||||
<InputText id="value" bind:value />
|
||||
<InputText id="value" bind:value placeholder="Enter value" />
|
||||
</div>
|
||||
{/if}
|
||||
<Button text disabled={!value} class="u-margin-block-start-12" on:click={addFilter}>
|
||||
<i class="icon-plus" />
|
||||
Add filter
|
||||
</Button>
|
||||
{filters}
|
||||
<ul class="tags">
|
||||
{#each $tags as tag (tag)}
|
||||
<button
|
||||
class="tag"
|
||||
on:click={() => {
|
||||
queries.removeFilter(tag);
|
||||
tick().then(() => {
|
||||
showFilters = true;
|
||||
});
|
||||
}}>
|
||||
<span class="text" use:tagFormat>
|
||||
{tag}
|
||||
</span>
|
||||
<i class="icon-x" />
|
||||
</button>
|
||||
{/each}
|
||||
</ul>
|
||||
<hr />
|
||||
<div class="u-flex u-margin-block-start-16 u-main-end u-gap-8">
|
||||
<Button text>Clear all</Button>
|
||||
<Button>Apply</Button>
|
||||
<Button text on:click={queries.clearAll}>Clear all</Button>
|
||||
<Button
|
||||
on:click={() => {
|
||||
queries.apply();
|
||||
showFilters = false;
|
||||
}}>Apply</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Drop>
|
||||
@@ -123,6 +220,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
.tags {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
|
||||
margin-block-start: 1rem;
|
||||
|
||||
:global(b) {
|
||||
font-weight: bold;
|
||||
}
|
||||
}
|
||||
|
||||
hr {
|
||||
height: 1px;
|
||||
width: calc(100% + 2rem);
|
||||
|
||||
Reference in new issue
Block a user