123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <script setup lang="ts">
- import {
- defineAsyncComponent,
- ref,
- watch,
- reactive,
- computed,
- onMounted
- } from "vue";
- import { useStore } from "vuex";
- import { useRoute } from "vue-router";
- import Toast from "toasters";
- import _validation from "@/validation";
- const InputHelpBox = defineAsyncComponent(
- () => import("@/components/InputHelpBox.vue")
- );
- const SaveButton = defineAsyncComponent(
- () => import("@/components/SaveButton.vue")
- );
- const store = useStore();
- const route = useRoute();
- const { socket } = store.state.websockets;
- const saveButton = ref();
- const userId = computed(() => store.state.user.auth.userId);
- const originalUser = computed(() => store.state.settings.originalUser);
- const modifiedUser = computed(() => store.state.settings.modifiedUser);
- const validation = reactive({
- username: {
- entered: false,
- valid: false,
- message: "Please enter a valid username."
- },
- email: {
- entered: false,
- valid: false,
- message: "Please enter a valid email address."
- }
- });
- const updateOriginalUser = payload =>
- store.dispatch("settings/updateOriginalUser", payload);
- const openModal = payload =>
- store.dispatch("modalVisibility/openModal", payload);
- const onInput = inputName => {
- validation[inputName].entered = true;
- };
- const changeEmail = () => {
- const email = modifiedUser.value.email.address;
- if (!_validation.isLength(email, 3, 254))
- return new Toast("Email must have between 3 and 254 characters.");
- if (
- email.indexOf("@") !== email.lastIndexOf("@") ||
- !_validation.regex.emailSimple.test(email)
- )
- return new Toast("Invalid email format.");
- saveButton.value.saveStatus = "disabled";
- return socket.dispatch("users.updateEmail", userId.value, email, res => {
- if (res.status !== "success") {
- new Toast(res.message);
- saveButton.value.handleFailedSave();
- } else {
- new Toast("Successfully changed email address");
- updateOriginalUser({
- property: "email.address",
- value: email
- });
- saveButton.value.handleSuccessfulSave();
- }
- });
- };
- const changeUsername = () => {
- const { username } = modifiedUser.value;
- if (!_validation.isLength(username, 2, 32))
- return new Toast("Username must have between 2 and 32 characters.");
- if (!_validation.regex.azAZ09_.test(username))
- return new Toast(
- "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
- );
- if (username.replaceAll(/[_]/g, "").length === 0)
- return new Toast(
- "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
- );
- saveButton.value.saveStatus = "disabled";
- return socket.dispatch(
- "users.updateUsername",
- userId.value,
- username,
- res => {
- if (res.status !== "success") {
- new Toast(res.message);
- saveButton.value.handleFailedSave();
- } else {
- new Toast("Successfully changed username");
- updateOriginalUser({
- property: "username",
- value: username
- });
- saveButton.value.handleSuccessfulSave();
- }
- }
- );
- };
- const saveChanges = () => {
- const usernameChanged =
- modifiedUser.value.username !== originalUser.value.username;
- const emailAddressChanged =
- modifiedUser.value.email.address !== originalUser.value.email.address;
- if (usernameChanged) changeUsername();
- if (emailAddressChanged) changeEmail();
- if (!usernameChanged && !emailAddressChanged) {
- saveButton.value.handleFailedSave();
- new Toast("Please make a change before saving.");
- }
- };
- const removeActivities = () => {
- socket.dispatch("activities.removeAllForUser", res => {
- new Toast(res.message);
- });
- };
- onMounted(() => {
- if (
- route.query.removeAccount === "relinked-github" &&
- !localStorage.getItem("github_redirect")
- ) {
- openModal("removeAccount");
- // TODO fix/redo this logic, broke after composition refactor
- // setTimeout(() => {
- // const modal = this.$parent.$children.find(
- // child => child.name === "RemoveAccount"
- // );
- // modal.confirmGithubLink();
- // }, 50);
- }
- });
- watch(
- () => modifiedUser.value.username,
- value => {
- // const value = newModifiedUser.username;
- if (!_validation.isLength(value, 2, 32)) {
- validation.username.message =
- "Username must have between 2 and 32 characters.";
- validation.username.valid = false;
- } else if (
- !_validation.regex.azAZ09_.test(value) &&
- value !== originalUser.value.username // Sometimes a username pulled from GitHub won't succeed validation
- ) {
- validation.username.message =
- "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
- validation.username.valid = false;
- } else if (value.replaceAll(/[_]/g, "").length === 0) {
- validation.username.message =
- "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
- validation.username.valid = false;
- } else {
- validation.username.message = "Everything looks great!";
- validation.username.valid = true;
- }
- }
- );
- watch(
- () => modifiedUser.value.email.address,
- value => {
- // const value = newModifiedUser.email.address;
- if (!_validation.isLength(value, 3, 254)) {
- validation.email.message =
- "Email must have between 3 and 254 characters.";
- validation.email.valid = false;
- } else if (
- value.indexOf("@") !== value.lastIndexOf("@") ||
- !_validation.regex.emailSimple.test(value)
- ) {
- validation.email.message = "Invalid format.";
- validation.email.valid = false;
- } else {
- validation.email.message = "Everything looks great!";
- validation.email.valid = true;
- }
- }
- );
- </script>
- <template>
- <div class="content account-tab">
- <h4 class="section-title">Change account details</h4>
- <p class="section-description">Keep these details up-to-date</p>
- <hr class="section-horizontal-rule" />
- <p class="control is-expanded margin-top-zero">
- <label for="username">Username</label>
- <input
- class="input"
- id="username"
- type="text"
- placeholder="Enter username here..."
- v-model="modifiedUser.username"
- maxlength="32"
- autocomplete="off"
- @keypress="onInput('username')"
- @paste="onInput('username')"
- />
- <span v-if="modifiedUser.username" class="character-counter"
- >{{ modifiedUser.username.length }}/32</span
- >
- </p>
- <transition name="fadein-helpbox">
- <input-help-box
- :entered="validation.username.entered"
- :valid="validation.username.valid"
- :message="validation.username.message"
- />
- </transition>
- <p class="control is-expanded">
- <label for="email">Email</label>
- <input
- class="input"
- id="email"
- type="text"
- placeholder="Enter email address here..."
- v-if="modifiedUser.email"
- v-model="modifiedUser.email.address"
- @keypress="onInput('email')"
- @paste="onInput('email')"
- autocomplete="off"
- />
- </p>
- <transition name="fadein-helpbox">
- <input-help-box
- :entered="validation.email.entered"
- :valid="validation.email.valid"
- :message="validation.email.message"
- />
- </transition>
- <SaveButton ref="saveButton" @clicked="saveChanges()" />
- <div class="section-margin-bottom" />
- <h4 class="section-title">Remove any data we hold on you</h4>
- <p class="section-description">
- Permanently remove your account and/or data we store on you
- </p>
- <hr class="section-horizontal-rule" />
- <div class="row">
- <quick-confirm @confirm="removeActivities()">
- <a class="button is-warning">
- <i class="material-icons icon-with-button">cancel</i>
- Clear my activities
- </a>
- </quick-confirm>
- <a class="button is-danger" @click="openModal('removeAccount')">
- <i class="material-icons icon-with-button">delete</i>
- Remove my account
- </a>
- </div>
- </div>
- </template>
- <style lang="less" scoped>
- .control {
- margin-bottom: 2px !important;
- }
- .row {
- display: flex;
- }
- </style>
|