summary refs log tree commit diff stats
path: root/client/src/views/CreateSmiles.vue
blob: 7c285f0a5dd9dc104abe05aecf18f40b19ee641a (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
136
137
138
139
140
141
<template>
<div class="new-smile">
  <p id="subtitle">Create Smiles :)</p>

  <div class="card">
    <Message v-if="smileCreated" severity="success">Smile Created.</Message>
    <div class="grid">
      <div class="col-12 md:col-6">
        <div v-if="smileCreated">
          <ul>
            <li>Click to copy the links.</li>
            <li>Share the <strong>public</strong> link with your friends.</li>
            <li>Keep the <strong>private</strong> link for yourself.</li>
          </ul>
        </div>
        <div v-else>
          <p>
            Filling this form will create a new "Smile Collection" for
            you, you can add your smiles to the collection using
            the <strong>private</strong> and your friends can view them
            using the <strong>public</strong> link.
          </p>
        </div>
      </div>
      <div class="col-12 md:col-6 flex align-items-center
                  justify-content-center">
        <div v-if="smileCreated" class="smile-links">
          <div class="grid">
            <div class="col-12 lg:col-6">
              <h5>Public</h5>
              <span class="p-input-icon-left">
                <i class="pi pi-share-alt" />
                <InputText @click="copyPublic" v-model="id"
                           type="text" placeholder="ID" readonly />
              </span>
            </div>
            <div class="col-12 lg:col-6">
              <h5>Private</h5>
              <span class="p-input-icon-left">
                <i class="pi pi-lock" />
                <InputText @click="copyPrivate" v-model="auth"
                           type="text" placeholder="Auth" readonly />
              </span>
            </div>
          </div>
        </div>
        <div v-else class="p-inputgroup">
          <InputText v-model="name" placeholder="Name" />
          <Button @click="createSmile"
                  icon="pi pi-user-plus" label="Create"
                  class="p-button-success" />
        </div>
      </div>
    </div>
    <div v-if="smileCreated" style="text-align: center;">
      <router-link :to="{ name: `ManageSmiles`, params: { id: `${id}`, auth: `${auth}` } }">
        <Button label="Upload Smiles"  icon="pi pi-upload" />
      </router-link>
    </div>
    <ProgressBar v-if="showProgress" mode="indeterminate" style="height: .4em;" />
  </div>

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

<script>
export default {
    data() {
        return {
            name: '',
            showProgress: false,
            id: '',
            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});
        },
        copyPrivate() {
            navigator.clipboard.writeText(this.privateLink());
            this.$toast.add({severity: 'success',
                             summary: 'Copied Private link to clipboard',
                             life: 2000});
        },
        publicLink() {
            return window.location.href.replace('/create', '') + '/' + this.id;
        },
        privateLink() {
            return window.location.href.replace('/create', '') + '/' + this.id + '/' + this.auth;
        },
        createSmile() {
            if (this.name === '') {
                this.$toast.add({severity: 'warn', summary: 'Invalid Name',
                                 detail: 'Empty', life: 2000});
                return;
            }

            const data = { name: this.name };
            const toast = this.$toast;

            this.showProgress = true;
            fetch('http://localhost:9090/create', {
                method: 'POST',
                cache: 'no-cache',
                headers: { 'Content-Type': 'application/json' },
                referrerPolicy: 'no-referrer',
                body: JSON.stringify(data)
            }).then(response => {
                if (!response.ok)
                    throw new Error('HTTP error, status = ' + response.status);
                return response.json();
            }).then(res => {
                this.id = res.id;
                this.auth = res.auth;
            }).catch(function(error) {
                console.log(error)
                toast.add({severity: 'error', summary: 'Service error', detail: error});
            }).finally(() => {
                this.showProgress = false;
            });
            // <<<< -- fat arrows mess with polymode formatting.
        }
    },
    computed: {
        smileCreated() {
            return this.id.length > 0 && this.auth.length > 0;
        },
    }
}
</script>

<style scoped>
.new-smile li {
    line-height: 1.6;
}
</style>