Skip to content

Commit

Permalink
fix: fix import table data csv
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 17, 2024
1 parent 48d2c72 commit 373b396
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 27 deletions.
14 changes: 7 additions & 7 deletions apps/frontend/src/lib/components/blocks/base/base-detail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export let getTableUrl: (tableId: string) => string
</script>

<main class="h-full flex-1 px-4 py-4">
<main class="flex flex-1 flex-col overflow-hidden px-4 py-4">
{#if $hasPermission("table:create")}
<div class="flex items-center gap-4">
<button
Expand Down Expand Up @@ -50,19 +50,19 @@
</div>
{/if}

<section class="pt-3">
<section class="flex flex-1 flex-col overflow-hidden pt-3">
<h3 class="text-xl font-normal text-gray-600">Tables</h3>

<Table.Root>
<Table.Header>
<Table.Row>
<Table.Root class="flex w-full flex-1 flex-col overflow-y-auto">
<Table.Header class="flex w-full">
<Table.Row class="w-full">
<Table.Head>Name</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Body class="w-full flex-1">
{#each base.tables as table}
{#if table}
<Table.Row class="cursor-pointer" on:click={() => goto(getTableUrl(table.id))}>
<Table.Row class="flex w-full cursor-pointer" on:click={() => goto(getTableUrl(table.id))}>
<Table.Cell class="flex items-center font-medium">
<DatabaseIcon class="mr-2 h-4 w-4" />
{table.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Label } from "$lib/components/ui/label"
import { Checkbox } from "$lib/components/ui/checkbox"
import { parse, type ImportDataExtensions, type SheetData } from "$lib/import/import.helper"
import { FileIcon, XIcon, ArrowRightIcon, ArrowLeftIcon } from "lucide-svelte"
import { FileIcon, XIcon, ArrowRightIcon, ArrowLeftIcon, LoaderCircleIcon } from "lucide-svelte"
import * as Table from "$lib/components/ui/table"
import { invalidate, goto } from "$app/navigation"
import { baseId, currentBase } from "$lib/store/base.store"
Expand All @@ -17,7 +17,6 @@
FieldIdVo,
inferCreateFieldType,
systemFieldNames,
systemFieldTypes,
TableIdVo,
type ICreateRecordDTO,
type ICreateSchemaDTO,
Expand All @@ -36,7 +35,6 @@
let firstRowAsHeader = true
let importData = true
let tableName: string | undefined = undefined
let rs: string[][] = []
const createRecords = createMutation({
mutationKey: ["table", "import", "records"],
Expand All @@ -56,7 +54,8 @@
const createTable = createMutation({
mutationKey: ["table", "import"],
mutationFn: trpc.table.create.mutate,
async onSuccess(data) {
async onSuccess(tableId) {
const rs = data?.data.slice(1).map((r) => r.map((v) => String(v))) ?? []
if (importData && rs.length) {
const records = rs.map((r, i) => {
const record: ICreateRecordDTO = { values: {} }
Expand All @@ -72,7 +71,7 @@
})
$createRecords.mutate({
tableId: data,
tableId: tableId,
records,
})
} else {
Expand All @@ -96,7 +95,10 @@
tableId = TableIdVo.create().value
file = f
tableName = getNextName(tableNames, file.name)
let parsed = await parse(file)
console.log(parsed)
if (firstRowAsHeader) {
const names = parsed.data[0].reduce((acc, cur) => {
if (!cur) {
Expand Down Expand Up @@ -161,7 +163,6 @@
id: FieldIdVo.create().value,
display: i === 0,
})) as ICreateSchemaDTO
$: console.log(schema)
</script>

{#if step === 0}
Expand Down Expand Up @@ -248,10 +249,17 @@
Back
</Button>
{/if}
<Button disabled={(step === 0 && !file) || (step === 1 && schema.length < 1)} on:click={handleClickImport} size="sm">
<Button
disabled={(step === 0 && !file) || (step === 1 && schema.length < 1) || $createTable.isPending}
on:click={handleClickImport}
size="sm"
>
{#if step === 0}
Next step <ArrowRightIcon class="ml-2 h-4 w-4" />
{:else}
{#if $createTable.isPending}
<LoaderCircleIcon class="mr-2 h-4 w-4 animate-spin" />
{/if}
Import
{/if}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@
>
<a
href={`/t/${table.id}`}
title={table.name}
class={cn(
"text-primary flex h-full flex-1 items-center font-normal",
"text-primary flex h-full flex-1 items-center truncate font-normal",
active && !viewId && "text-background font-medium",
)}
>
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/lib/import/import.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const parseCsv = async (file: File, options?: ParseDataOption): Promise<SheetDat
return new Promise((resolve) => {
Papa.parse<string[]>(file, {
complete(results) {
resolve(results.data)
resolve(results.data.filter((r) => !(r.length === 1 && r[0] === "")))
},
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
</script>

<main class="flex flex-col">
<main class="flex h-screen flex-col">
{#if base}
<BaseHeader {base} />
<UpdateBaseDialog {base} />
Expand Down
8 changes: 4 additions & 4 deletions packages/persistence/src/record/record.mutate-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCurrentUserId, mustGetCurrentSpaceId } from "@undb/context/server"
import type { ISpecification, ISpecVisitor } from "@undb/domain"
import { getCurrentUserId,mustGetCurrentSpaceId } from "@undb/context/server"
import type { ISpecification,ISpecVisitor } from "@undb/domain"
import {
CurrencyEqual,
DateIsEmpty,
Expand Down Expand Up @@ -53,10 +53,10 @@ import {
type UserEmpty,
type UserEqual,
} from "@undb/table"
import { sql, type ExpressionBuilder } from "kysely"
import { sql,type ExpressionBuilder } from "kysely"
import { unique } from "radash"
import { AbstractQBMutationVisitor } from "../abstract-qb.visitor"
import type { IQueryBuilder, IRecordQueryBuilder } from "../qb"
import type { IQueryBuilder,IRecordQueryBuilder } from "../qb"
import { JoinTable } from "../underlying/reference/join-table"

export class RecordMutateVisitor extends AbstractQBMutationVisitor implements IRecordVisitor {
Expand Down
11 changes: 7 additions & 4 deletions packages/table/src/modules/schema/fields/field-value.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { None, Option, Some } from "@undb/domain"
import { match } from "ts-pattern"
import type { JsonValue } from "type-fest"
import type { Field, FieldValue, MutableFieldValue } from "./field.type"
import type { IOptionId } from "./option/option-id.vo"
import {
AutoIncrementFieldValue,
ButtonFieldValue,
Expand All @@ -27,7 +26,7 @@ import { EmailFieldValue } from "./variants/email-field"
import { LongTextFieldValue } from "./variants/long-text-field/long-text-field-value.vo"
import { PercentageFieldValue } from "./variants/percentage-field" // 新增导入
import { RatingFieldValue } from "./variants/rating-field"
import { SelectFieldValue } from "./variants/select-field"
import { type SelectField, SelectFieldValue } from "./variants/select-field"
import { UserFieldValue } from "./variants/user-field"

export class FieldValueFactory {
Expand All @@ -36,7 +35,11 @@ export class FieldValueFactory {
.with({ type: "number" }, (field) => Some(new NumberFieldValue(field.valueSchema.parse(value))))
.with({ type: "rating" }, (field) => Some(new RatingFieldValue(field.valueSchema.parse(value))))
.with({ type: "string" }, (field) => Some(new StringFieldValue(field.valueSchema.parse(value))))
.with({ type: "select" }, (field) => Some(new SelectFieldValue(field.valueSchema.parse(value))))
.with({ type: "select" }, (field) => {
const parsedValue = SelectFieldValue.parseValue(value, field as SelectField)
console.log(parsedValue)
return Some(new SelectFieldValue(field.valueSchema.parse(parsedValue)))
})
.with({ type: "reference" }, (field) => Some(new ReferenceFieldValue(field.valueSchema.parse(value))))
.with({ type: "email" }, (field) => Some(new EmailFieldValue(field.valueSchema.parse(value))))
.with({ type: "url" }, (field) => Some(new UrlFieldValue(field.valueSchema.parse(value))))
Expand Down Expand Up @@ -66,7 +69,7 @@ export class FieldValueFactory {
.with("updatedBy", () => Some(new UpdatedByFieldValue(value as string)))
.with("reference", () => Some(new ReferenceFieldValue(value as string[])))
.with("rollup", () => Some(new RollupFieldValue(value as number | Date)))
.with("select", () => Some(new SelectFieldValue(value as IOptionId)))
.with("select", () => Some(new SelectFieldValue(SelectFieldValue.parseValue(value, field as SelectField))))
.with("email", () => Some(new EmailFieldValue(value as string)))
.with("url", () => Some(new UrlFieldValue(value as string)))
.with("attachment", () => Some(new AttachmentFieldValue(value as IAttachmentFieldValue)))
Expand Down
3 changes: 3 additions & 0 deletions packages/table/src/modules/schema/fields/field.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export const inferCreateFieldType = (values: (string | number | null | object |
options: Options.fromStrings(distinctValues.map((value) => value.toString() ?? "")).toJSON(),
},
type: "select",
constraint: {
max: 1,
},
}) as Omit<ICreateSelectFieldDTO, "id" | "name">,
)
.with(P.array(P.string), () => ({ type: "string" }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ export class SelectFieldValue extends FieldValueObject<ISelectFieldValue> {
super(Array.isArray(option) ? option : { value: option })
}

static parseValue(value: string | string[] | null, field: SelectField): IOptionId | IOptionId[] | null {
if (value === null || value === undefined) {
return null
}
if (Array.isArray(value)) {
return value
.map((v) => field.options.find((op) => op.id === v || op.name === v))
.filter((v) => !!v)
.map((v) => v.id)
}
return field.options.find((op) => op.id === value || op.name === value)?.id ?? null
}

isEmpty() {
if (Array.isArray(this.props)) {
return this.props.length === 0
Expand Down
4 changes: 2 additions & 2 deletions packages/trpc/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const p = t.procedure
requestId,
responseTime,
type,
input,
rawInput,
// input,
// rawInput,
path,
}
if (result.ok) {
Expand Down

0 comments on commit 373b396

Please sign in to comment.