Use existing types

This commit is contained in:
Adria Navarro 2024-12-24 14:33:58 +01:00
parent d176325179
commit f6db307ebc
2 changed files with 17 additions and 26 deletions

View File

@ -1,21 +1,23 @@
import { get, derived, Writable, Readable } from "svelte/store" import { get, derived, Writable, Readable } from "svelte/store"
import { import {
ArrayOperator,
BasicOperator,
FieldType, FieldType,
UIColumn, UIColumn,
UIFilter, UILegacyFilter,
UIInlineFilter,
UILogicalOperator, UILogicalOperator,
UISearchFilter,
} from "@budibase/types" } from "@budibase/types"
import { Store as StoreContext } from "." import { Store as StoreContext } from "."
import { memo } from "../../../utils/memo" import { memo } from "../../../utils/memo"
export interface FilterStore { export interface FilterStore {
filter: Writable<UIFilter> filter: Writable<UISearchFilter | undefined>
inlineFilters: Writable<UIInlineFilter[]> inlineFilters: Writable<UILegacyFilter[]>
} }
export interface FilterDerivedStore { export interface FilterDerivedStore {
allFilters: Readable<UIFilter> allFilters: Readable<UISearchFilter | undefined>
} }
export type Store = FilterStore & FilterDerivedStore export type Store = FilterStore & FilterDerivedStore
@ -42,7 +44,7 @@ export const deriveStores = (context: StoreContext): FilterDerivedStore => {
if (!$inlineFilters?.length) { if (!$inlineFilters?.length) {
return $filter return $filter
} }
let allFilters = { const allFilters: UISearchFilter = {
logicalOperator: UILogicalOperator.ALL, logicalOperator: UILogicalOperator.ALL,
groups: [ groups: [
{ {
@ -51,12 +53,13 @@ export const deriveStores = (context: StoreContext): FilterDerivedStore => {
}, },
], ],
} }
// Just use inline if no filter // Just use inline if no filter
if (!$filter?.groups?.length) { if (!$filter?.groups?.length) {
return allFilters return allFilters
} }
// Join them together if both // Join them together if both
allFilters.groups = [...allFilters.groups, ...$filter.groups] allFilters.groups = [...allFilters.groups!, ...$filter.groups]
return allFilters return allFilters
} }
) )
@ -72,10 +75,10 @@ export const createActions = (context: StoreContext) => {
const addInlineFilter = (column: UIColumn, value: string) => { const addInlineFilter = (column: UIColumn, value: string) => {
const filterId = `inline-${column.name}` const filterId = `inline-${column.name}`
const type = column.schema.type const type = column.schema.type
const inlineFilter: UIInlineFilter = { const inlineFilter: UILegacyFilter = {
field: column.name, field: column.name,
id: filterId, id: filterId,
operator: "string", operator: BasicOperator.STRING,
valueType: "value", valueType: "value",
type, type,
value, value,
@ -84,11 +87,11 @@ export const createActions = (context: StoreContext) => {
// Add overrides specific so the certain column type // Add overrides specific so the certain column type
if (type === FieldType.NUMBER) { if (type === FieldType.NUMBER) {
inlineFilter.value = parseFloat(value) inlineFilter.value = parseFloat(value)
inlineFilter.operator = "equal" inlineFilter.operator = BasicOperator.EQUAL
} else if (type === FieldType.BIGINT) { } else if (type === FieldType.BIGINT) {
inlineFilter.operator = "equal" inlineFilter.operator = BasicOperator.EQUAL
} else if (type === FieldType.ARRAY) { } else if (type === FieldType.ARRAY) {
inlineFilter.operator = "contains" inlineFilter.operator = ArrayOperator.CONTAINS
} }
inlineFilters.update($inlineFilters => { inlineFilters.update($inlineFilters => {

View File

@ -1,17 +1,5 @@
import { FieldType, UILogicalOperator } from "@budibase/types" import { LegacyFilter } from "@budibase/types"
export interface UIFilter { export type UILegacyFilter = LegacyFilter & {
groups: {
logicalOperator: UILogicalOperator
filters: UIInlineFilter[]
}[]
}
export interface UIInlineFilter {
field: string
type: FieldType
value: number | string
operator: string
id: string id: string
valueType: string
} }