Skip to content

Commit

Permalink
feat: display value
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 28, 2024
1 parent 39388a2 commit 661a4bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import * as Popover from "$lib/components/ui/popover"
export let value: string | Array<string | null>
export let displayValue: string | null
export let field: RollupField
export let isEditing: boolean
export let isSelected: boolean
Expand Down
59 changes: 38 additions & 21 deletions packages/persistence/src/record/record-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@undb/table"
import { format } from "date-fns/fp"
import { isNumber, isString } from "radash"
import { match } from "ts-pattern"

const formatter = format("yyyy-MM-dd")

Expand Down Expand Up @@ -121,7 +122,7 @@ export function getRecordDTOFromEntity(table: TableDo, entity: any, foreignTable
continue
}

if (field.type === "rollup" && field.fn === "lookup") {
if (field.type === "rollup") {
const foreignTable = field.getForeignTable(table, foreignTables)
let lookupField: Field | undefined = undefined
if (foreignTable) {
Expand All @@ -131,29 +132,45 @@ export function getRecordDTOFromEntity(table: TableDo, entity: any, foreignTable
}
}

function handleValue(value: (string | number)[]) {
if (lookupField?.type === "date") {
return value
.map((v: string | number) => (v ? new Date(v).toISOString() : null))
.map((v) => (v ? formatter(v) : null))
} else if (lookupField?.type === "select") {
return value.map((v) => lookupField.getOptionById(v)?.name ?? null)
} else {
return value
if (field.fn === "lookup") {
function handleValue(value: (string | number)[]) {
return match(lookupField)
.with({ type: "date" }, () =>
value
.map((v: string | number) => (v ? new Date(v).toISOString() : null))
.map((v) => (v ? formatter(v) : null)),
)
.with({ type: "select" }, (field) => value.map((v) => field.getOptionById(v)?.name ?? null))
.otherwise(() => value)
}
}

if (Array.isArray(value)) {
values[key] = handleValue(value)
continue
} else if (isString(value)) {
try {
values[key] = JSON.parse(value)
values[key] = handleValue(values[key])
} catch (error) {
logger.warn({ error, value }, "Error parsing JSON")
values[key] = [value]
if (Array.isArray(value)) {
values[key] = handleValue(value)
continue
} else if (isString(value)) {
try {
values[key] = JSON.parse(value)
values[key] = handleValue(values[key])
} catch (error) {
logger.warn({ error, value }, "Error parsing JSON")
values[key] = [value]
}
continue
}
} else if (field.fn === "sum" || field.fn === "average" || field.fn === "min" || field.fn === "max") {
function handleValue(value: number) {
return match(lookupField)
.with({ type: "currency" }, (field) => field.format(value))
.otherwise(() => null)
}
values[key] = value
const displayValue = handleValue(value as number)
if (displayValue !== null) {
displayValues[key] = displayValue
}
continue
} else {
values[key] = value
continue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class CurrencyField extends AbstractField<CurrencyFieldValue, CurrencyFie
return this.#constraint.props.min
}

format(value: number) {
return `${this.symbol} ${value}`
}

override get valueSchema() {
return this.#constraint.schema
}
Expand Down

0 comments on commit 661a4bd

Please sign in to comment.