Skip to content
Snippets Groups Projects
SignUp.vue 21 KiB
Newer Older
moon's avatar
moon committed
<template>
    <base-toaster :open="toasterIsOpen" @close="hideDialog">
      <div class="flex justify-between space-x-4 items-center">
        <font-awesome-icon
          class="ml-3"
          icon="fa-solid fa-thumbs-up "
          size="lg"
        />

moon's avatar
moon committed
        <h4>
raghosh's avatar
raghosh committed
          Sign up request successful. An activation link has been sent to the
          email {{ email.val }}
raghosh's avatar
raghosh committed
          . Please click the link to activate your account.
moon's avatar
moon committed
        </h4>
        <button class="btn btn-outline" @click="hideDialog">
          <font-awesome-icon icon="fa-regular fa-circle-xmark" size="2x" />
moon's avatar
moon committed
    <div class="flex items-center justify-center">
          bg-white bg-opacity-75
raghosh's avatar
raghosh committed
          <h1
            class="
              font-bold
              text-left
              font-montserrat
              text-3xl
              sm:text-5xl
              mb-7
            "
          >
moon's avatar
moon committed
            Sign Up to join CD-Code
moon's avatar
moon committed
          </h1>
raghosh's avatar
raghosh committed
          <form @submit.prevent="register">
            <div class="my-4" :class="{ invalid: !name.isValid }">
raghosh's avatar
raghosh committed
              <div class="flex justify-between">
                <h1
                  class="
                    text-left
                    font-bold
                    mb-2
                    text-xl
                    sm:text-2xl
                    font-montserrat
                  "
                >
                  Name*
                </h1>
                <h1
                  class="
                    text-left text-gray-400
                    mb-2
                    text-lg
                    sm:text-2xl
                    font-montserrat
                  "
                >
                  Note: * fields are mandatory.
                </h1>
raghosh's avatar
raghosh committed
              </div>
moon's avatar
moon committed
              <input
raghosh's avatar
raghosh committed
                v-model.trim="name.val"
moon's avatar
moon committed
                type="text"
raghosh's avatar
raghosh committed
                class="
                  text-xl
                  outline-none
                  px-4
                  py-5
                  w-full
                  bg-transparent
                  border
                  rounded-lg
                  border-gray-400
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Enter full name."
moon's avatar
moon committed
                @blur="clearValidity('name')"
              />
              <p v-if="!name.isValid" class="text-red-500">
moon's avatar
moon committed
            </div>
            <div class="my-4" :class="{ invalid: !email.isValid }">
raghosh's avatar
raghosh committed
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Email*
moon's avatar
moon committed
              </h1>
              <input
raghosh's avatar
raghosh committed
                v-model.trim="email.val"
moon's avatar
moon committed
                type="email"
raghosh's avatar
raghosh committed
                class="
                  text-xl
                  outline-none
                  w-full
                  bg-transparent
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Enter email."
moon's avatar
moon committed
                @blur="clearValidity('email')"
              />
              <p v-if="!email.isValid" class="text-red-500">
              <p v-if="error && errorMsg" class="text-red-500">
moon's avatar
moon committed
            </div>
            <div class="my-4" :class="{ invalid: !password.isValid }">
raghosh's avatar
raghosh committed
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Password*
moon's avatar
moon committed
              </h1>
              <input
raghosh's avatar
raghosh committed
                v-model.trim="password.val"
moon's avatar
moon committed
                type="password"
raghosh's avatar
raghosh committed
                class="
                  text-xl
                  outline-none
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  w-full
                  bg-transparent
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Password must be minimum 6 character."
moon's avatar
moon committed
                @blur="clearValidity('password')"
              />
              <p v-if="!password.isValid" class="text-red-500">
                Password must not be empty or less than 6 character.
              </p>
moon's avatar
moon committed
            </div>
            <div class="my-4" :class="{ invalid: !username.isValid }">
raghosh's avatar
raghosh committed
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Username*
moon's avatar
moon committed
              </h1>
              <input
raghosh's avatar
raghosh committed
                v-model.trim="username.val"
moon's avatar
moon committed
                type="text"
raghosh's avatar
raghosh committed
                class="
                  text-xl
                  outline-none
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  w-full
                  bg-transparent
                  border-b
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Username must be minimum 3 character."
moon's avatar
moon committed
                @blur="clearValidity('username')"
              />
              <p v-if="!username.isValid" class="text-red-500">
moon's avatar
moon committed
            </div>
            <div class="my-4" :class="{ invalid: !current_role.isValid }">
raghosh's avatar
raghosh committed
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Current Role*
moon's avatar
moon committed
              </h1>
moon's avatar
moon committed
              <select
                id="inline-current-role"
raghosh's avatar
raghosh committed
                v-model="current_role.val"
                class="
                  bg-white
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  w-full
                  text-gray-700
                  outline-none
                  bg-transparent
                  border-b
                  hover:border-blue-700
                "
moon's avatar
moon committed
                @blur="clearValidity('current_role')"
moon's avatar
moon committed
              >
                <option id="phd" value="PhD">PhD</option>
                <option id="postdoc" value="PostDoc">PostDoc</option>
                <option id="research_scientist" value="Research_Scientist">
moon's avatar
moon committed
                  Research Scientist
                </option>
                <option
                  id="principal_investigator"
                  value="Principal_Investigator"
                >
                  Principal Investigator
                </option>
              <p v-if="!current_role.isValid" class="text-red-500">
            <div
              class="my-4"
              :class="{ invalid: !scientific_discipline.isValid }"
            >
              <h1
raghosh's avatar
raghosh committed
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Scientific Disciplines*
              </h1>
              <div
                v-for="options in scientificDisciplineOptions"
                :key="options.id"
              >
                <input
                  :id="options.id"
                  v-model="scientific_discipline.val"
                  type="checkbox"
                  class="h-6 w-6"
                  :name="options"
                  :value="options.discipline"
                  @blur="clearValidity('scientific_discipline')"
                />
                <label class="mx-3" :for="options.id">{{
              <p v-if="!scientific_discipline.isValid" class="text-red-500">
                Please select atleast one scientific discipline.
              </p>
raghosh's avatar
raghosh committed
            </div>
            <div class="my-4" :class="{ invalid: !affiliation.isValid }">
raghosh's avatar
raghosh committed
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Affiliation*
              </h1>
              <input
                v-model.trim="affiliation.val"
                type="text"
                class="
                  text-xl
                  outline-none
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  w-full
                  bg-transparent
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Institute name currently associated with."
moon's avatar
moon committed
                @blur="clearValidity('affiliation')"
              />
              <p v-if="!affiliation.isValid" class="text-red-500">
                Affiliation must not be empty.
              </p>
            </div>
            <div class="my-4" :class="{ invalid: !profileLink.isValid }">
raghosh's avatar
raghosh committed
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Profile Link
              </h1>
              <input
                v-model.trim="profileLink.val"
                type="text"
                class="
                  text-xl
                  outline-none
                  px-4
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  w-full
                  bg-transparent
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="eg- https://www.google.com (optional)"
moon's avatar
moon committed
                @blur="clearValidity('profileLink')"
              />
              <p v-if="!profileLink.isValid" class="text-red-500">
                The url entered is invalid. Please check it again!
              </p>
              <p
                v-if="profileLinkError && profileLinkErrMsg"
                class="text-red-500"
              >
                {{ profileLinkErrMsg }}
              </p>
            </div>
            <div class="my-4" :class="{ invalid: !motivation.isValid }">
raghosh's avatar
raghosh committed
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
                Motivation*
moon's avatar
moon committed
              </h1>
              <textarea
raghosh's avatar
raghosh committed
                v-model.trim="motivation.val"
moon's avatar
moon committed
                rows="5"
                type="text"
raghosh's avatar
raghosh committed
                class="
                  text-xl
                  outline-none
                  w-full
                  bg-transparent
raghosh's avatar
raghosh committed
                  py-5
                  rounded-lg
raghosh's avatar
raghosh committed
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Your motivation text."
moon's avatar
moon committed
                @blur="clearValidity('motivation')"
moon's avatar
moon committed
              />
            <p v-if="!motivation.isValid" class="text-red-500">
              Motivation text must not be empty.
            </p>
            <p v-if="!formIsValid" class="font-bold text-red-400">
              Required fields were not provided.
            </p>
            <p v-show="error" class="text-red-500">
              {{ errorMsg }}
            </p>
            <div v-if="isLoading">
              <base-spinner />
            </div>
moon's avatar
moon committed
            <button
              type="submit"
              class="
                bg-blue-500
                text-2xl
                font-bold
                rounded-lg
                p-5
                mr-4
                w-full
                text-white
                hover:bg-blue-700
              "
moon's avatar
moon committed
            >
              Create Account
            </button>
moon's avatar
moon committed
              <h4 class="mt-6">
                Already have an account?
                <a
                  class="text-pink-base hover:text-pink-base hover:no-underline"
moon's avatar
moon committed
                  @click="$router.go(-1)"
                  >Sign in</a
                >
moon's avatar
moon committed
              </h4>
moon's avatar
moon committed
            </div>
moon's avatar
moon committed
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BaseSpinner from "../components/UI/BaseSpinner.vue";
import BaseToaster from "../components/UI/BaseToaster.vue";
let host = require("@/components/js/const").apiHost;
moon's avatar
moon committed
export default {
  name: "Register",
raghosh's avatar
raghosh committed
  components: { BaseToaster, BaseSpinner },
moon's avatar
moon committed
  data() {
    return {
raghosh's avatar
raghosh committed
      name: {
        val: "",
raghosh's avatar
raghosh committed
      },
      email: {
        val: "",
raghosh's avatar
raghosh committed
      },
      password: {
        val: "",
raghosh's avatar
raghosh committed
      },
      username: {
        val: "",
raghosh's avatar
raghosh committed
      },
      motivation: {
        val: "",
raghosh's avatar
raghosh committed
      },
      affiliation: {
        val: "",
raghosh's avatar
raghosh committed
      },
      current_role: {
        val: "",
raghosh's avatar
raghosh committed
      },
      profileLink: {
        val: "",
raghosh's avatar
raghosh committed
      },
      scientific_discipline: {
raghosh's avatar
raghosh committed
      },
moon's avatar
moon committed
      error: false,
raghosh's avatar
raghosh committed
      errorMsg: ``,
      isDev: process.env.NODE_ENV === "development",
raghosh's avatar
raghosh committed
      toasterIsOpen: false,
      scientificDisciplineOptions: [
        { id: "1", discipline: "Physics" },
        { id: "2", discipline: "Chemistry" },
        { id: "3", discipline: "Biology" },
        { id: "4", discipline: "Computer Science" },
        { id: "5", discipline: "Mathematics" },
raghosh's avatar
raghosh committed
      ],
      profileLinkError: false,
      profileLinkErrMsg: "",
raghosh's avatar
raghosh committed
    };
moon's avatar
moon committed
  methods: {
    clearValidity(input) {
      this[input].isValid = true;
raghosh's avatar
raghosh committed
    },
    validateForm() {
      this.formIsValid = true;
      if (this.name.val === "") {
        this.name.isValid = false;
        this.formIsValid = false;
      }
      if (this.email.val === "") {
        this.email.isValid = false;
        this.formIsValid = false;
      }
      if (this.password.val === "" || this.password.val.length < 6) {
        this.password.isValid = false;
        this.formIsValid = false;
      }
      if (this.username.val === "" || this.username.val.length < 3) {
        this.username.isValid = false;
        this.formIsValid = false;
      }
      if (this.current_role.val === "") {
        this.current_role.isValid = false;
        this.formIsValid = false;
      }
      if (this.scientific_discipline.val.length === 0) {
        this.scientific_discipline.isValid = false;
        this.formIsValid = false;
      }
      if (this.affiliation.val === "") {
        this.affiliation.isValid = false;
        this.formIsValid = false;
      }
      if (this.isValidHttpUrl() === false) {
        this.profileLink.isValid = false;
        this.formIsValid = false;
      }
      if (this.motivation.val === "") {
        this.motivation.isValid = false;
        this.formIsValid = false;
      }
raghosh's avatar
raghosh committed
    },
    isValidHttpUrl() {
      let timer = null;
        "^(https?:\\/\\/)?" + // protocol
          "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
          "((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
          "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
          "(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
          "(\\#[-a-z\\d_]*)?$",
        "i"
      ); // fragment locator
      let isValid;
      if (this.profileLink.val === "") {
        console.log("profile link empty");
        return true;
      } else {
        isValid = pattern.test(this.profileLink.val);
        console.log("lint is there", isValid);
      }
      return isValid;
      //     if(!isValid){
      //       console.log("triggered");
      // this.profileLinkError= true;
      // this.profileLinkErrMsg="The url entered is invalid. Please check it again!"
      //     } else{
      // this.profileLinkError=false;
      // this.profileLinkErrMsg=""
      //     }
      // clearTimeout(timer);
      // timer= setTimeout(()=>{
      //   let isValid= pattern.test(this.profileLink.val);
      //     if(!isValid){
      //       console.log("triggered");
      // this.profileLinkError= true;
      // this.profileLinkErrMsg="The url entered is invalid. Please check it again!"
      //     } else{
      // this.profileLinkError=false;
      // this.profileLinkErrMsg=""
      //     }
raghosh's avatar
raghosh committed

raghosh's avatar
raghosh committed
    showDialog() {
      this.toasterIsOpen = true;
    },
    hideDialog() {
      this.toasterIsOpen = false;
      this.$router.push("login");
moon's avatar
moon committed
    async register(e) {
raghosh's avatar
raghosh committed
      const vm = this;
      vm.validateForm();
      if (!vm.formIsValid) {
        return;
      }
raghosh's avatar
raghosh committed
      if (vm.isDev) {
        host = require("@/components/js/const").devApiHost;
raghosh's avatar
raghosh committed
      }
      this.isLoading = true;
      const res = await fetch(`${host}/api/auth/local/register`, {
        method: "POST",
        mode: "cors", // no-cors, *cors, same-origin
        cache: "no-cache", // *default, no-cache, reload, force-cache,
          "Content-Type": "application/json",
          // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: JSON.stringify({
raghosh's avatar
raghosh committed
          password: vm.password.val,
          email: vm.email.val,
          username: vm.username.val,
          motivation_text: vm.motivation.val,
          current_role: vm.current_role.val,
          scientific_discipline: vm.scientific_discipline.val,
          current_affiliation: vm.affiliation.val,
          profile_link: vm.profileLink.val,
        }),
      });
        this.isLoading = false;
        this.error = true;
moon's avatar
moon committed
        this.errorMsg = `${res.status} Internal Error, please write a mail to CDCode Admin`;

        return;
      }
      if (!res.ok && res.status === 405) {
        this.isLoading = false;
        this.error = true;
moon's avatar
moon committed
        this.errorMsg = `${res.status} Internal Error, please write a mail to CDCode Admin`;

        return;
      }
      const response = await res.json();
      if (
        !res.ok &&
        response.error.status === 400 &&
        response.error.message === "Email is already taken"
        this.isLoading = false;
        this.error = true;
          "Email Address already registered with an existing account. You can reset your password by clicking forget password from sign up page.";
        this.password.val = "";
      vm.toasterIsOpen = true;
      this.error = false;
      this.errorMsg = "";
      // try {
      //   await vm.axios.post(`${host}/api/auth/local/register`, {
      //     full_name: vm.name.val,
      //     password: vm.password.val,
      //     email: vm.email.val,
      //     username: vm.username.val,
      //     motivation_text: vm.motivation.val,
      //     current_role: vm.current_role.val,
      //     scientific_discipline: vm.scientific_discipline.val,
      //     current_affiliation: vm.affiliation.val,
      //     profile_link: vm.profileLink.val,
      //   });

      //   this.isLoading = false;
      //   vm.toasterIsOpen = true;
      //   this.error = false;
      //   this.errorMsg = "";
      //   //vm.$router.push('login')
      // } catch (e) {
      //   console.error(e);
      //   this.isLoading = false;
      //   if (e.message === "Request failed with status code 405") {
      //     console.log(e.message);
      //   }
      //   if (e.message === "Request failed with status code 400") {
      //     vm.error = true;
      //     vm.errorMsg =
      //       "Email Address already registered with an existing account. You can reset your password by clicking forget password from sign up page.";
      //   }

      // }
raghosh's avatar
raghosh committed
    },
  },
};
moon's avatar
moon committed
</script>
raghosh's avatar
raghosh committed
@import url("~@/assets/bootstrap.css");
raghosh's avatar
raghosh committed
.invalid h1,
raghosh's avatar
raghosh committed
  color: red;
}
.invalid input,
.invalid textarea,
raghosh's avatar
raghosh committed
  border: 1px solid red;
}
moon's avatar
moon committed
</style>