Browse Source

Add data upload button

pull/341/head
Hironsan 5 years ago
parent
commit
abb1e53b70
5 changed files with 155 additions and 3 deletions
  1. 30
      frontend/components/containers/DocumentUploadButton.vue
  2. 111
      frontend/components/organisms/DocumentUploadForm.vue
  3. 5
      frontend/pages/projects/_id/dataset/index.vue
  4. 10
      frontend/rules/index.js
  5. 2
      frontend/store/documents.js

30
frontend/components/containers/DocumentUploadButton.vue

@ -0,0 +1,30 @@
<template>
<base-modal
text="Upload"
:is-create="true"
>
<template v-slot="slotProps">
<document-upload-form
:upload-document="uploadDocument"
@close="slotProps.close"
/>
</template>
</base-modal>
</template>
<script>
import { mapActions } from 'vuex'
import BaseModal from '@/components/molecules/BaseModal'
import DocumentUploadForm from '@/components/organisms/DocumentUploadForm'
export default {
components: {
BaseModal,
DocumentUploadForm
},
methods: {
...mapActions('documents', ['uploadDocument'])
}
}
</script>

111
frontend/components/organisms/DocumentUploadForm.vue

@ -0,0 +1,111 @@
<template>
<base-card
title="Upload Data"
agree-text="Upload"
cancel-text="Cancel"
:disabled="!valid"
@agree="create"
@cancel="cancel"
>
<template #content>
<v-form
ref="form"
v-model="valid"
>
<v-radio-group
v-model="selectedFormat"
:rules="fileFormatRules"
>
<v-radio
v-for="(format, i) in formats"
:key="i"
:label="format.text"
:value="format"
/>
</v-radio-group>
<v-file-input
v-model="file"
:accept="acceptType"
:rules="uploadFileRules"
label="File input"
/>
</v-form>
</template>
</base-card>
</template>
<script>
import BaseCard from '@/components/molecules/BaseCard'
import { fileFormatRules, uploadFileRules } from '@/rules/index'
export default {
components: {
BaseCard
},
props: {
uploadDocument: {
type: Function,
default: () => {},
required: true
}
},
data() {
return {
valid: false,
file: null,
selectedFormat: null,
formats: [
{
type: 'csv',
text: 'Upload a CSV file from your computer',
accept: '.csv'
},
{
type: 'plain',
text: 'Upload text items from your computer',
accept: '.txt'
},
{
type: 'json',
text: 'Upload a JSON file from your computer',
accept: '.json,.jsonl'
}
],
fileFormatRules,
uploadFileRules
}
},
computed: {
acceptType() {
if (this.selectedFormat) {
return this.selectedFormat.accept
} else {
return '.txt,.csv,.json,.jsonl'
}
}
},
methods: {
cancel() {
this.$emit('close')
},
validate() {
return this.$refs.form.validate()
},
reset() {
this.$refs.form.reset()
},
create() {
if (this.validate()) {
this.uploadDocument({
format: this.selectedFormat,
file: this.file
})
this.reset()
this.cancel()
}
}
}
}
</script>

5
frontend/pages/projects/_id/dataset/index.vue

@ -1,6 +1,7 @@
<template>
<v-card>
<v-card-title>
<document-upload-button />
<document-deletion-button />
</v-card-title>
<document-list />
@ -9,14 +10,14 @@
<script>
import DocumentList from '@/components/containers/DocumentList'
// import DocumentCreationButton from '@/components/containers/DocumentCreationButton'
import DocumentUploadButton from '@/components/containers/DocumentUploadButton'
import DocumentDeletionButton from '@/components/containers/DocumentDeletionButton'
export default {
layout: 'project',
components: {
DocumentList,
// DocumentCreationButton,
DocumentUploadButton,
DocumentDeletionButton
},
validate({ params }) {

10
frontend/rules/index.js

@ -31,3 +31,13 @@ export const descriptionRules = [
export const projectTypeRules = [
v => !!v || 'Project type is required'
]
// Rules for Document.
export const fileFormatRules = [
v => !!v || 'File format is required'
]
export const uploadFileRules = [
v => !!v || 'File is required',
v => !v || v.size < 1000000 || 'File size should be less than 1 MB!'
]

2
frontend/store/documents.js

@ -51,7 +51,7 @@ export const actions = {
commit('setLoading', false)
})
},
createDocument({ commit }, data) {
uploadDocument({ commit }, data) {
DocumentService.addDocument(data.projectId, data)
.then((response) => {
commit('addDocument', response)

Loading…
Cancel
Save