Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 173 additions & 90 deletions src/components/TableDialog.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<v-dialog
v-model="tableDialog"
width="700"
width="800"
>
<template v-slot:activator="{ on }">
<v-btn
Expand All @@ -16,97 +16,155 @@
</v-icon>
</v-btn>
</template>
<v-card>
<v-card-title
class="headline pb-0 pt-3"
primary-title
>
Create Table
</v-card-title>
<v-card>
<v-card-text class="px-4 pt-4 pb-1">
<v-layout wrap>
<v-flex>
<v-file-input
:error-messages="fileUploadError"
label="Upload File"
prepend-inner-icon="attach_file"
prepend-icon=""
single-line
clearable
dense
outlined
@change="handleFileInput"
/>
</v-flex>
</v-layout>
<v-layout wrap>
<v-flex>
<v-text-field
v-model="fileName"
:error-messages="tableCreationError"
label="Table Name"
outlined
dense
/>
</v-flex>
</v-layout>
<v-row no-gutters>
<v-col cols="4">
<v-text-field
v-model="keyField"
label="Key Column"
append-icon="restore"
outlined
dense
@click:append="restoreKeyField"
/>
</v-col>
<v-col cols="4">
<v-checkbox
v-model="overwrite"
class="mt-1 ml-2"
dense
label="Overwrite Default Key"
outlined
/>
</v-col>
</v-row>

<v-row>
<v-col
v-for="(type, field) in columnType"
:key="field"
cols="3"
>
<v-select
v-model="columnType[field]"
outlined
dense
:label="field"
:items="multinetTypes"
/>
</v-col>
</v-row>
</v-card-text>

<v-stepper v-model="step">
<v-stepper-header>
<v-stepper-step
:complete="step > 1"
step="1"
>
Select Dataset
</v-stepper-step>

<v-divider />

<v-card-actions class="px-4 py-3">
<v-spacer />
<v-btn
:disabled="createDisabled"
@click="createTable"
<v-stepper-step
:complete="step > 2"
step="2"
>
Set Column Types
</v-stepper-step>
</v-stepper-header>

<v-stepper-items>
<v-stepper-content step="1">
<v-card>
<v-card>
<v-card-text class="px-4 pt-4 pb-1">
<v-layout wrap>
<v-flex>
<v-file-input
:error-messages="fileUploadError"
label="Upload File"
prepend-inner-icon="attach_file"
prepend-icon=""
single-line
clearable
dense
outlined
@change="handleFileInput"
/>
</v-flex>
</v-layout>
<v-layout wrap>
<v-flex>
<v-text-field
v-model="fileName"
:error-messages="tableCreationError"
label="Table Name"
outlined
dense
/>
</v-flex>
</v-layout>
<v-row no-gutters>
<v-col cols="4">
<v-text-field
v-model="keyField"
label="Key Column"
append-icon="restore"
outlined
dense
@click:append="restoreKeyField"
/>
</v-col>
<v-col cols="4">
<v-checkbox
v-model="overwrite"
class="mt-1 ml-2"
dense
label="Overwrite Default Key"
outlined
/>
</v-col>
</v-row>
</v-card-text>

<v-divider />

<v-card-actions class="px-4 py-3">
<v-spacer />
<v-btn
:disabled="createDisabled"
@click="step = 2"
>
Next
</v-btn>
</v-card-actions>

<v-progress-linear
v-if="uploading"
:value="uploadProgress"
/>
</v-card>
</v-card>
</v-stepper-content>

<v-stepper-content
class="pa-0"
step="2"
>
<v-data-table
class="upload-preview"
fixed-header
:headers="headers"
hide-default-header
:items="rowSample"
>
Create
</v-btn>
</v-card-actions>
<v-progress-linear
v-if="uploading"
:value="uploadProgress"
/>
</v-card>
</v-card>
<template v-slot:header="{ props: { headers } }">
<thead dark>
<tr>
<th
v-for="(header, i) in headers"
:key="i"
class="pt-2 pb-4"
>
{{ header.text }}
<v-select
v-model="columnType[header.text]"
outlined
dark
dense
hide-details
:items="multinetTypes"
/>
</th>
</tr>
</thead>
</template>
</v-data-table>

<v-footer class="py-3">
<v-spacer />
<v-btn
color="primary"
text
@click="step = 1"
>
Back
</v-btn>

<v-btn
color="primary"
depressed
@click="createTable"
>
Create Table
</v-btn>
</v-footer>
</v-stepper-content>
</v-stepper-items>
</v-stepper>
</v-dialog>
</template>

Expand Down Expand Up @@ -139,6 +197,22 @@ export default defineComponent({
},
},
setup(props, { emit }) {
// Stepper control
const step: Ref<number> = ref(1);
const rowSample: Ref<Array<{}>> = ref([]);
const headers = computed(() => {
if (rowSample.value.length === 0) {
return [];
}

const keys = Object.keys(rowSample.value[0]);

return keys.map((key) => ({
text: key,
value: key,
}));
});

// Type recommendation
const columnType: Ref<{[key: string]: CSVColumnType}> = ref({});

Expand All @@ -165,9 +239,11 @@ export default defineComponent({
}

const typeRecs = await csvFileTypeRecommendations(file);
columnType.value = Array.from(typeRecs.keys()).reduce(
(acc, key) => ({ ...acc, [key]: typeRecs.get(key) }), {},
columnType.value = Array.from(typeRecs.typeRecs.keys()).reduce(
(acc, key) => ({ ...acc, [key]: typeRecs.typeRecs.get(key) }), {},
);

rowSample.value = [...typeRecs.rowSample];
}

// Upload options
Expand Down Expand Up @@ -217,6 +293,9 @@ export default defineComponent({
}

return {
step,
headers,
rowSample,
columnType,
multinetTypes,
fileName,
Expand All @@ -241,4 +320,8 @@ export default defineComponent({
margin: 49px 10px 0 0;
z-index: 1;
}
.upload-preview table th {
background-color: #1976d2 !important;
color: #fff !important;
}
</style>
19 changes: 16 additions & 3 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ interface TypeScore {
total: number;
}

async function csvFileTypeRecommendations(file: File): Promise<Map<string, CSVColumnType>> {
const parsePromise: Promise<Map<string, CSVColumnType>> = new Promise((resolve) => {
interface Recommendation {
typeRecs: Map<string, CSVColumnType>;
rowSample: Array<{}>;
}

async function csvFileTypeRecommendations(file: File): Promise<Recommendation> {
const parsePromise: Promise<Recommendation> = new Promise((resolve) => {
const columnTypes = new Map<string, TypeScore>();
const typeRecs = new Map<string, CSVColumnType>();
const rowSample = [] as Array<{}>;

Papa.parse(file, {
header: true,
Expand All @@ -58,6 +64,10 @@ async function csvFileTypeRecommendations(file: File): Promise<Map<string, CSVCo
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = row.data as { [key: string]: any };

if (rowSample.length !== 30) {
rowSample.push({ ...data });
}

Object.keys(data).forEach((key: string) => {
if (!columnTypes.has(key)) {
columnTypes.set(key, {
Expand Down Expand Up @@ -121,7 +131,10 @@ async function csvFileTypeRecommendations(file: File): Promise<Map<string, CSVCo
typeRecs.set(field, rec);
});

resolve(typeRecs);
resolve({
typeRecs,
rowSample,
});
},
});
});
Expand Down