summary refs log tree commit diff stats
path: root/client/src/views/ManageSmiles.vue
blob: 70c9e6b3fb29134e6d664ff6b1505d7d421b5cd2 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
<div class="manage-smile">
  <p id="subtitle">Manage Smiles :)</p>
  <ProgressBar v-if="showLoading" mode="indeterminate" style="height: .4em;" />

  <div v-if="!verified && !showLoading" style="text-align: center;">
    <Button @click="verify" label="Try Again"
            class="p-button-outlined  p-button-info p-button-lg" icon="pi pi-refresh" />
  </div>

  <div v-if="verified" class="card">
    <div class="grid">
      <div class="col-12 md:col-6">
        <div class="col-12">
          <span class="p-input-icon-left">
            <i class="pi pi-user" />
            <InputText v-model="name" type="text" readonly />
          </span>
        </div>
        <div class="col-12">
          <span class="p-input-icon-left">
            <i class="pi pi-share-alt" />
            <InputText @click="copyPublic" v-model="id" type="text" readonly />
          </span>
        </div>
      </div>
      <div class="col-12 md:col-6">
        <FileUpload @error="onError" @before-send="beforeSend" @upload="onUpload"
                    name="images" url="../../upload"
                    :multiple="true" accept="image/*"
                    :maxFileSize="2097152"> <!-- 1024 * 1024 * 2 -->
          <template #empty>
            <p>Drag and drop files to here to upload.</p>
          </template>
        </FileUpload>
      </div>
    </div>
  </div>

  <Toast />
</div>
</template>

<script>
export default {
    data() {
        return {
            name: '',
            verified: false,
            showLoading: true,
            id: this.$route.params.id,
            auth: this.$route.params.auth,
        }
    },
    methods: {
        copyPublic() {
            navigator.clipboard.writeText(this.publicLink());
            this.$toast.add({severity: 'success', icon: 'pi-user-plus',
                             summary: 'Copied Public link to clipboard',
                             life: 2000});
        },
        publicLink() {
            let link = window.location.href.split('/').slice(0, -2);
            link.push(this.id);
            return link.join('/');
        },
        beforeSend(xhr) {
            xhr.formData.append('id', this.id);
            xhr.formData.append('auth', this.auth);
        },
        onError(error) {
            this.$toast.add(
                {severity: 'error',
                 summary: 'Upload failed',
                 detail: `${error.xhr.status} ${error.xhr.statusText} | ${error.xhr.response}`});
        },
        onUpload(res) {
            const response = JSON.parse(res.xhr.response);
            for(let i = 0; i < response.length; i++) {
                const x = response[i];
                if (x.stored === false) {
                    this.$toast.add(
                        {
                            severity: 'error',
                            summary: 'Upload failed',
                            detail: `${x.filename}: ${x.messages}`
                        }
                    );
                } else if (x.messages.length !== 0) {
                    this.$toast.add(
                        {
                            severity: 'warn',
                            summary: 'Upload Issues',
                            detail: `${x.filename}: ${x.messages}`
                        }
                    );
                }
            }
        },
        verify() {
            const data = { id: this.id, auth: this.auth };
            const toast = this.$toast;

            this.showLoading = true;
            fetch('../../verify', {
                method: 'POST',
                cache: 'no-cache',
                headers: { 'Content-Type': 'application/json' },
                referrerPolicy: 'no-referrer',
                body: JSON.stringify(data)
            }).then(response => {
                if (response.status === 401)
                    throw new Error(response.status + ' # ' + 'Authentication Failed');
                if (response.status === 404)
                    throw new Error(response.status + ' # ' + 'Smiles deleted or invalid link');
                if (!response.ok)
                    throw new Error('HTTP error, status = ' + response.status);
                return response.json();
            }).then(res => {
                this.name = res.name;
                this.verified = true;
            }).catch(error => {
                console.log(error)
                toast.add({severity: 'error', summary: 'Service error', detail: error});
            }).finally(() => {
                this.showLoading = false;
                // <<<< -- fat arrows mess with polymode formatting.
            });
        },
    },
    beforeMount() {
        this.verify();
    },
}
</script>