feat: filtering

This commit is contained in:
EETagent 2022-12-05 00:28:40 +01:00
parent 21d7ca9118
commit 0532a705a7
2 changed files with 35 additions and 12 deletions

View file

@ -70,16 +70,20 @@ export const apiLogout = async (fetchSsr?: Fetch) => {
// SSR compatible // SSR compatible
// List all candidates /admin/list/candidates // List all candidates /admin/list/candidates
export const apiListCandidates = async (fetchSsr?: Fetch): Promise<[CandidatePreview]> => { export const apiListCandidates = async (fetchSsr?: Fetch, field?: string): Promise<[CandidatePreview]> => {
const params = new URLSearchParams();
if (field) {
params.append('field', field);
}
try { try {
if (fetchSsr) { if (fetchSsr) {
const res = await fetchSsr(API_URL + '/admin/list/candidates', { const res = await fetchSsr(API_URL + '/admin/list/candidates?' + params.toString(), {
method: 'GET', method: 'GET',
credentials: 'include' credentials: 'include'
}); });
return await res.json(); return await res.json();
} }
const res = await axios.get(API_URL + '/admin/list/candidates', { const res = await axios.get(API_URL + '/admin/list/candidates?' + params.toString(), {
withCredentials: true withCredentials: true
}); });
return res.data; return res.data;

View file

@ -18,14 +18,13 @@
candidates = data.preview; candidates = data.preview;
}); });
const getCandidates = async () => { const getCandidates = async (field?: string) => {
try { try {
candidates = await apiListCandidates(); candidates = await apiListCandidates(undefined, field);
} catch { } catch {
console.log('error'); console.log('error');
} }
} };
type Filter = 'Vše' | 'KBB' | 'IT' | 'GYM'; type Filter = 'Vše' | 'KBB' | 'IT' | 'GYM';
@ -33,6 +32,25 @@
let activeFilter: Filter = 'Vše'; let activeFilter: Filter = 'Vše';
const changeFilter = (filter: Filter) => {
activeFilter = filter;
switch (activeFilter) {
case 'Vše':
getCandidates();
break;
case 'KBB':
getCandidates('KB');
break;
case 'IT':
getCandidates('IT');
break;
case 'GYM':
getCandidates('G');
break;
}
};
let scrollTop = 0; let scrollTop = 0;
let createCandidateModal: boolean = false; let createCandidateModal: boolean = false;
@ -43,7 +61,10 @@
</script> </script>
{#if createCandidateModal} {#if createCandidateModal}
<CreateCandidateModal on:created={getCandidates} on:close={() => (createCandidateModal = false)} /> <CreateCandidateModal
on:created={getCandidates}
on:close={() => (createCandidateModal = false)}
/>
{/if} {/if}
<div> <div>
@ -52,7 +73,7 @@
{#each filters as filter} {#each filters as filter}
<div class:selected={filter === activeFilter}> <div class:selected={filter === activeFilter}>
<Home /> <Home />
<button on:click={() => (activeFilter = filter)}>{filter}</button> <button on:click={() => changeFilter(filter)}>{filter}</button>
</div> </div>
{/each} {/each}
</div> </div>
@ -95,9 +116,7 @@
</thead> </thead>
<tbody> <tbody>
{#each candidates as candidate} {#each candidates as candidate}
<tr <tr class="border-b bg-white hover:cursor-pointer">
class="border-b bg-white hover:cursor-pointer"
>
<td class="whitespace-nowrap px-6 py-4 text-sm text-gray-900" <td class="whitespace-nowrap px-6 py-4 text-sm text-gray-900"
><a ><a
target="_blank" target="_blank"