"src/views/sections/src/views/sections/Register.vue" did not exist on "3f9b6187164b52d52fa4cc94272c673ca9e92ac4"
Register.vue 6.85 KB
Newer Older
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<template>
  <v-card
    class="elevation-12"
    style="margin: 8px"
  >
    <v-toolbar
      dark
      color="success"
    >
      <v-toolbar-title>
        Datos de Usuario
      </v-toolbar-title>
    </v-toolbar>
    <v-card-text>
      <v-form
        ref="form"
        v-model="valid"
        lazy-validation
      >
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
20
        <!-- <v-text-field
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
21
          v-model="data.name"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
22
          :rules="passwordRules"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
23 24
          prepend-icon="mdi-account"
          label="Nombre"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
25
        /> -->
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
26 27 28 29 30 31 32 33 34 35 36
        <v-text-field
          v-model="data.email"
          :rules="usernameRules"
          prepend-icon="mdi-at"
          name="login"
          label="Coreo Electrónico"
          required
          type="email"
        />
        <v-autocomplete
          v-model="data.role"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
37
          :rules="passwordRules"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
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
          :items="roles"
          prepend-icon="mdi-account"
          label="Rol"
          item-text="name"
          item-value="value"
          required
        />
        <v-text-field
          id="password"
          v-model="data.password"
          :rules="passwordRules"
          prepend-icon="mdi-lock"
          name="password"
          required
          label="Contraseña"
          type="password"
        />
        <v-text-field
          id="password"
          v-model="data.confirm_password"
          :rules="passwordRules"
          prepend-icon="mdi-lock"
          name="password"
          required
          label="Confirmar Contraseña"
          type="password"
        />
      </v-form>
    </v-card-text>
    <v-card-actions>
      <v-spacer />
      <v-btn
        color="red"
        text
        :disabled="!valid"
        submit
        :loading="tryLogin"
75
        @click="cancel"
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      >
        Cancelar
      </v-btn>
      <v-spacer />
      <v-btn
        color="green"
        dark
        :disabled="!valid"
        submit
        :loading="tryLogin"
        @click="submit"
      >
        Registrar
      </v-btn>
      <v-spacer />
    </v-card-actions>
  </v-card>
</template>
<script>
  import { register } from '@/axios/auth'
  import { mapMutations } from 'vuex'
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
97
  import { isLastName, isEmail } from '@/utils/regex'
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
98 99 100 101

  export default {
    name: 'Register',
    data: () => ({
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
102
      custom: true,
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      data: {
        name: null,
        email: null,
        password: null,
        confirm_password: null,
        role: null,
      },
      roles: [
        {
          value: 'MCP',
          name: 'Mesa Coordinadora Provincial',
        },
        {
          value: 'ERP',
          name: 'Equipo de Respuesta Rápida',
        },
        {
          value: 'MCM',
          name: 'Mesa Coordinadora Municipal',
        },
        {
          value: 'DCA',
          name: 'Director Centro Hospitalario',
        },
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
127 128 129 130
        {
          value: 'MCPREV',
          name: 'Revisor de Mesa Coordinadora Provincial',
        },
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
131 132 133 134
        {
          value: 'CA',
          name: 'Centro Aislamiento',
        },
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      ],
      tryLogin: false,
      valid: true,
      passwordRules: [
        v => !!v || 'Este campo es requerido',
      ],
      testRules: [
        v => !!v || 'Este campo es requerido',
      ],
      nameRules: [
        v => !!v || 'Este campo es requerido',
        v => isLastName(v) || 'Debe introducir un nombre válido',
      ],
      usernameRules: [
        v => !!v || 'Este campo es requerido',
        v => isEmail(v) || 'Debe introducir una dirección de correo válida',
      ],
    }),
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
153 154 155 156 157 158 159 160 161 162 163
    computed: {
      same () {
        return this.data.password === this.data.confirm_password
      },
      progress () {
        return Math.min(100, this.value.length * 10)
      },
      color () {
        return ['error', 'warning', 'success'][Math.floor(this.progress / 40)]
      },
    },
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
164 165
    methods: {
      ...mapMutations(['setUser']),
166 167 168
      cancel () {
        this.$router.push({ name: 'Control' })
      },
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
169 170 171 172 173
      async submit () {
        this.$refs.form.validate()
        if (this.$refs.form.validate(true)) {
          this.tryLogin = true
          try {
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
174
            this.data.name = this.data.email
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            const loginResponse = await register(this.data)
            const token = loginResponse.data.token
            const user = loginResponse.data.user
            this.setUser(user)
            localStorage.setItem('tkn', token)
            this.$router.push({ name: 'Control' })
          } catch (e) {
            if (e.toString() === 'Error: Network Error') {
              this.$toast.error('Error de Red, revise su conexión', {
                position: 'bottom-center',
                timeout: 3000,
                closeOnClick: true,
                pauseOnFocusLoss: false,
                pauseOnHover: true,
                draggable: true,
                draggablePercent: 0.6,
                showCloseButtonOnHover: false,
                hideProgressBar: true,
                closeButton: 'button',
                icon: true,
                rtl: false,
              })
              this.tryLogin = false
            }
            switch (e.response.status) {
              case 500: {
                this.$toast.error('Error interno del servidor, inténtelo más tarde', {
                  position: 'bottom-center',
                  timeout: 3000,
                  closeOnClick: true,
                  pauseOnFocusLoss: false,
                  pauseOnHover: true,
                  draggable: true,
                  draggablePercent: 0.6,
                  showCloseButtonOnHover: false,
                  hideProgressBar: true,
                  closeButton: 'button',
                  icon: true,
                  rtl: false,
                })
                break
              }
              case 401: {
                this.$toast.error('No autorizado', {
                  position: 'bottom-center',
                  timeout: 3000,
                  closeOnClick: true,
                  pauseOnFocusLoss: false,
                  pauseOnHover: true,
                  draggable: true,
                  draggablePercent: 0.6,
                  showCloseButtonOnHover: false,
                  hideProgressBar: true,
                  closeButton: 'button',
                  icon: true,
                  rtl: false,
                })
                break
              }
              default: {
                this.$toast.error('No autorizado', {
                  position: 'bottom-center',
                  timeout: 3000,
                  closeOnClick: true,
                  pauseOnFocusLoss: false,
                  pauseOnHover: true,
                  draggable: true,
                  draggablePercent: 0.6,
                  showCloseButtonOnHover: false,
                  hideProgressBar: true,
                  closeButton: 'button',
                  icon: true,
                  rtl: false,
                })
              }
            }
            this.tryLogin = false
          }
        }
      },
    },
  }
</script>