aboutsummaryrefslogtreecommitdiff
path: root/front-end/src/views/Register/index.vue
blob: 92a59921dfe3a539d3a1f891143fabe238b6c40a (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<template>
  <div id="reg-template" class="app-component-entry">
    <div class="container flex flex-col m-auto my-2 duration-150 ease-linear lg:mt-16">
      <div class="text-center">
        <h2>Sign Up</h2>
      </div>
      <div class="mt-4 content-container">
        <form v-if="formState === 0" @submit.prevent="OnSubmit" :disabled="waiting">
          <fieldset class="input-group">
            <div class="input-container">
              <label for="reg-email" class="pl-1 text-sm">Email Address</label>
              <input
                id="reg-email" 
                v-model="v$.emailAddress.$model"
                type="email"
                placeholder="user@example.com" 
                required
                class="input-field primary"
                @input="onInput"
              >
            </div>
          </fieldset>
          <fieldset class="flex flex-row justify-between mt-6">
            <div>
              <label class="checkbox primary">
                <input v-model="acceptedTerms" type="checkbox">
                <span class="check" />
                <span class="mx-2 text-sm"> 
                  I agree to the
                  <a class="link" href="#">Terms of Service</a>
                </span>
              </label>
            </div>
            <div>
              <button type="submit" :disabled="!acceptedTerms || waiting" class="btn primary">
                Submit
              </button>
            </div>
          </fieldset>
        </form>
        <complete-reg
          v-else-if="formState === 1"
          :reg-path="regPath"
          :token="token"
          @cancel="formState = 0"
          @complete="formState = 2"
        />
        <div v-else>
          <div class="text-center">
            <h3>Success</h3>
            <fa-icon
              :icon="['fa','check-circle']"
              class="text-primary-500 dark:text-primary-600"
              size="3x"
            />
            <p class="mt-4">
              You may log in now.
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { isNil } from 'lodash';
import useVuelidate from '@vuelidate/core'
import { required, maxLength, email, helpers } from '@vuelidate/validators'
import { ref, reactive, watch } from 'vue'
import { useSession, apiCall, useMessage, useWait, useTitle, useVuelidateWrapper } from '@vnuge/vnlib.browser'
import CompleteReg from './components/CompleteReg.vue'
import { useRouter } from 'vue-router';
import { useRouteQuery } from '@vueuse/router';

const regPath = "/account/registration"

useTitle('Registration')

const { setMessage, onInput } = useMessage()
const { waiting } = useWait()
const { browserId } = useSession()
const router = useRouter();

//Token is the t query argument
const token = useRouteQuery('t', null)

const acceptedTerms = ref(false)
const formState = ref(isNil(token.value) ? 0 : 1)

const vState = reactive({
  emailAddress: ''
})

const rules = {
  emailAddress: {
    required: helpers.withMessage('Email cannot be empty', required),
    email: helpers.withMessage('Your email address is not valid', email),
    maxLength: helpers.withMessage('Email address must be less than 50 characters', maxLength(50))
  }
}
const v$ = useVuelidate(rules, vState, { $lazy: true })
const { validate } = useVuelidateWrapper(v$)

const OnSubmit = async function () {
  if (!acceptedTerms.value) {
    setMessage('You must accept the terms of service to continue.')
    return
  }
  if (!await validate()) {
    return
  }
  await apiCall(async ({ axios, toaster }) => {
    const response = await axios.put(regPath, {
      username: v$.value.emailAddress.$model,
      clientid: browserId.value,
      localtime: new Date().toISOString()
    })
    if (response.data.success) {
      toaster.form.success({
          id: 'logout-success',
          title: 'Success',
          text: response.data.result,
          duration: 5000
        }
      )

      acceptedTerms.value = false
      v$.value.emailAddress.$model = '';
      //Clear form
      v$.value.$reset()
    } else {
      setMessage(response.data.result)
    }
  })
}

watch(formState, () => {
  //Clear token if formState is not 1
  v$.value.$reset()
  acceptedTerms.value = false
  v$.value.emailAddress.$model = '';
  router.push({ query: {} })
})

</script>

<style>
#reg-template .content-container{
    @apply mx-auto p-4 bg-white dark:bg-dark-700 border border-gray-200 dark:border-dark-500;
    @apply sm:p-6 sm:rounded-md sm:shadow-sm w-full max-w-sm;
}

#reg-template input.input-field {
    @apply block w-full p-2 border-b-2 my-2;
}

.content-container fieldset.input-group {
    @apply mx-auto;
}
</style>