Skip to content

Commit

Permalink
fix: fix get aggregate value
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 19, 2024
1 parent ba053d0 commit 93fc93f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import { ClipboardCopyIcon, CopyIcon, Maximize2Icon, Trash2Icon } from "lucide-svelte"
import { gridViewStore, isRowSelected, isSelectedCell } from "./grid-view.store"
import SelectedRecordsButton from "./selected-records-button.svelte"
import { aggregatesStore } from "$lib/store/aggregates.store"
import ViewPagination from "../view/view-pagination.svelte"
import { createMutation } from "@tanstack/svelte-query"
import { trpc } from "$lib/trpc/client"
Expand Down Expand Up @@ -62,9 +61,6 @@
$: colorSpec = $view.color.into(undefined)?.getSpec($t.schema).into(undefined)
$: getTableAggregates = aggregatesStore.getTableAggregates
$: aggregates = $getTableAggregates($t.id.value)
let store = getRecordsStore()
let hasRecord = store.hasRecord
let count = store.count
Expand Down Expand Up @@ -135,12 +131,11 @@
},
footer: createRender(GridViewFooter, {
field,
aggregateResult: aggregates?.[field.id.value],
readonly,
}),
plugins: {
resize: {
initialWidth: $view.type === 'grid' ? $view.getFieldWidth(field.id.value) : 200,
initialWidth: $view.type === "grid" ? $view.getFieldWidth(field.id.value) : 200,
disable: readonly,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { invalidate } from "$app/navigation"
import * as Select from "$lib/components/ui/select/index.js"
import { aggregatesStore } from "$lib/store/aggregates.store"
import { getTable, viewId } from "$lib/store/table.store"
import { trpc } from "$lib/trpc/client"
import { cn } from "$lib/utils"
Expand All @@ -12,12 +13,16 @@
const table = getTable()
export let field: Field
export let readonly: boolean
export let aggregateResult: AggregateResult | undefined = undefined
let aggregates = derived(
[aggregatesStore, table, viewId],
([$aggregates, $table, $viewId]) => $aggregates[$viewId ?? $table.views.getDefaultView()?.id.value],
)
$: aggregateResult = $aggregates?.[field.id.value]
$: options =
// @ts-ignore
field.aggregate.options?.map((value) => ({ value, label: $LL.table.aggregateFns[value as IFieldAggregate]() })) ??
[]
$: value = $table.views.getViewById($viewId).aggregate.into(undefined)?.value?.[field.id.value]
Expand Down
13 changes: 13 additions & 0 deletions apps/frontend/src/lib/components/blocks/grid-view/grid-view.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { queryParam, ssp } from "sveltekit-search-params"
import GridViewDataTable from "./grid-view-data-table.svelte"
import { preferences } from "$lib/store/persisted.store"
import { aggregatesStore } from "$lib/store/aggregates.store"
export let readonly = false
Expand Down Expand Up @@ -47,6 +48,18 @@
$: if ($getRecords.isSuccess) {
store.setRecords(Records.fromJSON($t, records), $getRecords.dataUpdatedAt)
}
const getAggregates = createQuery(
derived([t], ([$table]) => ({
queryKey: ["aggregates", $table?.id.value],
queryFn: () => trpc.record.aggregate.query({ tableId: $table.id.value, viewId: $viewId }),
enabled: !!$table,
})),
)
$: if ($getAggregates.data && $t) {
aggregatesStore.updateTableAggregates($viewId ?? $t.views.getDefaultView()?.id.value, $getAggregates.data)
}
</script>

<GridViewDataTable
Expand Down
10 changes: 5 additions & 5 deletions apps/frontend/src/lib/store/aggregates.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { derived, writable } from "svelte/store"
type IAggregates = Record<string, string | number | null>

export const createAggregatesStore = () => {
const store = writable<Map<string, IAggregates>>(new Map())
const store = writable<Record<string, IAggregates>>({})
const { subscribe, update, set } = store

const updateTableAggregates = (tableId: string, aggregates: IAggregates) => {
update((map) => map.set(tableId, aggregates))
const updateTableAggregates = (viewId: string, aggregates: IAggregates) => {
update((map) => ({ ...map, [viewId]: aggregates }))
}

const getTableAggregates = derived(store, ($store) => {
return (tableId: string) => {
const aggregates = $store.get(tableId)
return (viewId: string) => {
const aggregates = $store[viewId]
return aggregates
}
})
Expand Down
20 changes: 11 additions & 9 deletions apps/frontend/src/routes/(share)/s/v/[shareId]/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { page } from "$app/stores"
import { TableCreator, TableDo } from "@undb/table"
import { setTable } from "$lib/store/table.store"
import { setTable, viewId } from "$lib/store/table.store"
import type { LayoutData } from "./$types"
import { writable } from "svelte/store"
import { derived, writable } from "svelte/store"
import { shareStore } from "$lib/store/share.store"
import { aggregatesStore } from "$lib/store/aggregates.store"
import { createQuery } from "@tanstack/svelte-query"
Expand All @@ -28,14 +28,16 @@
$shareStore.set(share.id, share)
}
const getAggregates = createQuery({
queryKey: [share?.id, "aggregates", tableDTO?.id],
queryFn: () => trpc.record.aggregate.query({ tableId: tableDTO!.id, viewId: share!.target.id }),
enabled: !!share && !!tableDTO,
})
const getAggregates = createQuery(
derived([table], ([$table]) => ({
queryKey: [share?.id, "aggregates", tableDTO?.id],
queryFn: () => trpc.record.aggregate.query({ tableId: $table.id.value, viewId: share!.target.id }),
enabled: !!share && !!$table,
})),
)
if ($getAggregates.data && tableDTO) {
aggregatesStore.updateTableAggregates(tableDTO.id, $getAggregates.data)
$: if ($getAggregates.data && $table) {
aggregatesStore.updateTableAggregates($viewId ?? $table.views.getDefaultView()?.id.value, $getAggregates.data)
}
</script>

Expand Down
6 changes: 3 additions & 3 deletions packages/trpc/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ import {
updateaccountCommand,
} from "@undb/commands"
import { getCurrentSpaceId } from "@undb/context/server"
import { CommandBus, QueryBus } from "@undb/cqrs"
import { CommandBus,QueryBus } from "@undb/cqrs"
import { container } from "@undb/di"
import type { ICommandBus, IQueryBus } from "@undb/domain"
import type { ICommandBus,IQueryBus } from "@undb/domain"
import {
CountRecordsQuery,
GetAggregatesQuery,
Expand Down Expand Up @@ -136,7 +136,7 @@ import {
import { tableDTO } from "@undb/table"
import { z } from "@undb/zod"
import { authz } from "./authz.middleware"
import { privateProcedure, publicProcedure, t } from "./trpc"
import { privateProcedure,publicProcedure,t } from "./trpc"

const commandBus = container.resolve<ICommandBus>(CommandBus)
const queryBus = container.resolve<IQueryBus>(QueryBus)
Expand Down

0 comments on commit 93fc93f

Please sign in to comment.