FileUpload.vue 1.01 KB
Newer Older
abergavenny's avatar
abergavenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script setup>
import { useApartmentStore } from '@/stores/apartments'

defineProps(['label'])

const apartments = useApartmentStore()

async function handleFileUpload(event) {
  try {
    const fileBlob = event.target.files
    const formData = new FormData()

    formData.append('image', fileBlob[0])

    const response = await apartments.uploadFile(apartments.active, formData)

    if (response.success) {
      await apartments.fetchFile(apartments.active, true)
    }
  } catch (error) {
    console.error('ERROR:', error.name)
  }
}
</script>

<template>
  <div class="file-upload button primary">
    <input type="file" @change="handleFileUpload" />
    <div class="file-upload__label">
      <span>{{ label }}</span>
    </div>
  </div>
</template>

<style scoped>
.file-upload {
  position: relative;
  display: flex;
}

.file-upload>input[type=file] {
  position: absolute;
  appearance: none;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.file-upload__label {
  pointer-events: none;
}
</style>