-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sergio222-dev/develop
* refactor search card
- Loading branch information
Showing
16 changed files
with
220 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { HTMLAttributes} from "@builder.io/qwik"; | ||
import { component$, Slot } from "@builder.io/qwik"; | ||
|
||
export const Chip = component$<HTMLAttributes<HTMLDivElement>>(({ class: className, ...props }) => { | ||
return ( | ||
<div class={`rounded-3xl bg-secondary px-2 py-1 text-black ${className}`} {...props}> | ||
<Slot /> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { HTMLAttributes, InputHTMLAttributes, JSXOutput, QRL } from "@builder.io/qwik"; | ||
import { $, useSignal } from "@builder.io/qwik"; | ||
import { Slot } from "@builder.io/qwik"; | ||
import { component$ } from "@builder.io/qwik"; | ||
import { ButtonIcon } from "~/components/button"; | ||
import { Chip } from "~/components/chip/Chip"; | ||
import { Icon } from "~/components/icons/Icon"; | ||
|
||
interface FilterChipProps extends HTMLAttributes<HTMLDivElement> { | ||
} | ||
|
||
export const ChipFilter = component$<FilterChipProps>(({ ...props }) => { | ||
return ( | ||
<Chip class="flex gap-1 hover:bg-red-500 cursor-pointer px-3 py-2 text-white text-sm" {...props}> | ||
<ButtonIcon class="bg-transparent"> | ||
<Icon name={'close'} width={8} height={8}/> | ||
</ButtonIcon> | ||
<Slot/> | ||
</Chip> | ||
) | ||
}); | ||
|
||
interface FilterFieldProps { | ||
onSubmit?: QRL<(this: FilterFieldProps, value: string | undefined) => Promise<void>>; | ||
children?: JSXOutput[]; | ||
} | ||
|
||
export const FilterField = component$<FilterFieldProps>( | ||
({ | ||
onSubmit, | ||
}) => { | ||
const r = useSignal<HTMLFormElement>(); | ||
|
||
const handleSubmit = $(() => { | ||
// get value | ||
const value = new FormData(r.value).get('contains'); | ||
|
||
r.value?.reset(); | ||
|
||
onSubmit?.(value?.toString()); | ||
}) | ||
|
||
return ( | ||
<div class="relative w-full rounded-3xl p-2 ring-primary ring-4 focus-within:ring-secondary flex gap-2 flex-wrap"> | ||
<Slot/> | ||
<form | ||
ref={r} | ||
class="w-full flex-1 min-w-[200px]" | ||
onSubmit$={handleSubmit} | ||
preventdefault:submit> | ||
<input | ||
name="contains" | ||
autocomplete="off" | ||
class="focus:outline-none flex-1 w-full" | ||
type="text" | ||
/> | ||
</form> | ||
</div> | ||
) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,42 @@ | ||
import { $, component$, useContext, useStore, useTask$ } from '@builder.io/qwik'; | ||
import { Menu } from '~/components/menu'; | ||
import { MenuTw } from '~/components/menuTw/MenuTw'; | ||
import { MenuTwItem } from '~/components/menuTw/MenuTwItem'; | ||
import { cardFilter, sortDirection } from '~/config/cardFilter'; | ||
import { useSubtypeLoader } from '~/routes/cards'; | ||
import type { CardFilters } from '~/stores/filterContext'; | ||
import { FilterContext } from '~/stores/filterContext'; | ||
import { getDefaultFilter } from '~/utils/cardFilters'; | ||
import { useDebounce } from '~/utils/useDebounce'; | ||
import { Pagination } from './Pagination'; | ||
import { $, component$, useContext } from '@builder.io/qwik'; | ||
import { ChipFilter, FilterField } from "~/components/filterField/FilterField"; | ||
import type { Filter } from "~/models/filters/Filter"; | ||
import { FILTERS_TYPES } from "~/models/filters/Filter"; | ||
import { FilterContext } from '~/stores/filterContext'; | ||
import { Pagination } from './Pagination'; | ||
|
||
// TODO: refactor this components, move to common components folder and remove all loaders and context | ||
export const CardFilter = component$(() => { | ||
// Loaders | ||
const subtypes = useSubtypeLoader(); | ||
|
||
// Context | ||
const c = useContext(FilterContext); | ||
|
||
// State | ||
const filters = useStore<CardFilters>(getDefaultFilter(1)); | ||
|
||
// Side effects | ||
useTask$(({ track }) => { | ||
track(() => filters.sortBy); | ||
track(() => filters.sortDirection); | ||
track(() => filters.name); | ||
track(() => filters.types); | ||
// Handlers | ||
const addContainsFilter = $(async (value: string | undefined) => { | ||
if (!value) return; | ||
|
||
void c.updateFilters(filters); | ||
}); | ||
const textFilter: Filter = { | ||
id: Math.random().toString(), | ||
label: `Contains: "${value}"`, | ||
value: value, | ||
field: 'text', | ||
filterType: FILTERS_TYPES.LIKE, | ||
isDisjunctive: true, | ||
} | ||
|
||
// Handlers | ||
const debounceUpdateName = useDebounce(250, $((event: Event) => { | ||
const value = (event.target as HTMLInputElement).value; | ||
// await c.addFilter(nameFilter); | ||
await c.addFilter(textFilter); | ||
}) | ||
|
||
void c.updateName(value); | ||
})); | ||
|
||
// Render | ||
return ( | ||
<div class="flex justify-between"> | ||
<form | ||
preventdefault:submit | ||
> | ||
<Menu | ||
onChange$={(e) => filters.sortBy = (e.target as HTMLInputElement).value} | ||
name="sortBy"> | ||
{cardFilter.map(s => ( | ||
<option key={s.value} value={s.value}>{s.label}</option> | ||
))} | ||
</Menu> | ||
|
||
<Menu | ||
onChange$={(e) => { | ||
filters.sortDirection = (e.target as HTMLInputElement).value as 'asc' | 'desc'; | ||
}} | ||
name="sortDirection"> | ||
{sortDirection.map(s => ( | ||
<option key={s.value} value={s.value}>{s.label}</option> | ||
))} | ||
</Menu> | ||
|
||
<MenuTw | ||
onChange={$((s) => { | ||
filters.types = s; | ||
})} | ||
// form={el.value} onChange={$((s) => { subtypesFilter.value = s })} | ||
> | ||
{subtypes.value.map(s => ( | ||
<MenuTwItem key={s} label={s} value={s} /> | ||
))} | ||
</MenuTw> | ||
|
||
<input name="name" onInput$={e => { | ||
void debounceUpdateName(e); | ||
}} type="text" placeholder="Name" /> | ||
</form> | ||
<Pagination /> | ||
<div class="flex justify-between items-center"> | ||
<FilterField onSubmit={addContainsFilter}> | ||
{c.filters.map(f => ( | ||
<ChipFilter key={f.id} onClick$={() => c.removeFilter(f.id)}>{f.label}</ChipFilter> | ||
))} | ||
</FilterField> | ||
<Pagination/> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const FILTERS_TYPES = { | ||
LIKE: 'LIKE', | ||
IN: 'IN', | ||
} | ||
|
||
export interface Filter { | ||
id: string; | ||
label: string; | ||
value: string; | ||
field: string; | ||
filterType: typeof FILTERS_TYPES[keyof typeof FILTERS_TYPES]; | ||
isDisjunctive?: boolean; | ||
} | ||
|
||
export function convertToFilter(filter: Filter): [string, string, string] { | ||
switch (filter.filterType) { | ||
case FILTERS_TYPES.LIKE: | ||
return [filter.field, 'ilike', '%' + filter.value + '%']; | ||
case FILTERS_TYPES.IN: | ||
return [filter.field, 'in', `(${filter.value})`]; | ||
default: | ||
throw new Error('Filter type not found'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Filter } from "~/models/filters/Filter"; | ||
|
||
export interface FetchCardsPayload { | ||
page: number; | ||
size: number; | ||
sortBy: string; | ||
sortDirection: 'asc' | 'desc'; | ||
filters: Filter[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.