From c7a917ce407dbb99e3e2f29dd80ff7c198dab3f7 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 1 Oct 2020 14:48:07 +0100 Subject: [PATCH 1/9] csv import functional --- .../nav/ModelNavigator/CreateTable.svelte | 52 +++- packages/server/package.json | 1 + packages/server/src/api/controllers/model.js | 15 +- packages/server/src/api/controllers/static.js | 35 +++ packages/server/src/api/routes/record.js | 6 +- packages/server/src/api/routes/static.js | 1 + packages/server/yarn.lock | 277 +++++++++++++++++- 7 files changed, 377 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/components/nav/ModelNavigator/CreateTable.svelte b/packages/builder/src/components/nav/ModelNavigator/CreateTable.svelte index 3c05b0de00..705da1365c 100644 --- a/packages/builder/src/components/nav/ModelNavigator/CreateTable.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/CreateTable.svelte @@ -2,19 +2,28 @@ import { goto } from "@sveltech/routify" import { backendUiStore } from "builderStore" import { notifier } from "builderStore/store/notifications" - import { DropdownMenu, Button, Icon, Input, Select } from "@budibase/bbui" + import { + DropdownMenu, + Button, + Heading, + Icon, + Input, + Select, + Dropzone, + } from "@budibase/bbui" + import api from "builderStore/api" import analytics from "analytics" - export let table - let anchor let dropdown let name + let recordImport = {} async function saveTable() { const model = await backendUiStore.actions.models.save({ name, - schema: {}, + schema: recordImport.schema || {}, + recordImport, }) notifier.success(`Table ${name} created successfully.`) $goto(`./model/${model._id}`) @@ -27,6 +36,38 @@ name = "" dropdown.hide() } + + function handleFileTooLarge(fileSizeLimit) { + notifier.danger( + `Files cannot exceed ${fileSizeLimit / + BYTES_IN_MB}MB. Please try again with smaller files.` + ) + } + + async function processFiles(fileList) { + const fileArray = Array.from(fileList) + let data = new FormData() + for (var i = 0; i < fileList.length; i++) { + data.append("file", fileList[i]) + } + + const response = await fetch("/api/csv/validate", { + method: "POST", + body: data, + headers: { + Accept: "application/json", + }, + }) + + recordImport = await response.json() + + if (response.status !== 200) { + notifier.danger("CSV Invalid, please try another CSV file") + return [] + } + + return fileArray.map(file => ({ ...file, extension: "csv" })) + }
@@ -40,6 +81,9 @@ placeholder="Table Name" thin bind:value={name} /> + + Create Table from CSV (Optional) +