validate email & phone

This commit is contained in:
Warunee Tamkoo 2024-11-14 14:14:07 +07:00
parent 8da57869fb
commit 241341ea2b

View file

@ -3,7 +3,6 @@ import { useCounterMixin } from "@/stores/mixin";
import { useDataStore } from "@/stores/data"; import { useDataStore } from "@/stores/data";
import { useQuasar, type QTableColumn, type QTableProps } from "quasar"; import { useQuasar, type QTableColumn, type QTableProps } from "quasar";
import { ref, reactive, onMounted, computed } from "vue"; import { ref, reactive, onMounted, computed } from "vue";
import { useRegistryInFormationStore } from "@/modules/10_registry/store/registry";
import http from "@/plugins/http"; import http from "@/plugins/http";
import config from "@/app.config"; import config from "@/app.config";
@ -13,9 +12,7 @@ import type { InformationData } from "@/interface/Main";
const link = ref<string>(""); const link = ref<string>("");
const $q = useQuasar(); const $q = useQuasar();
const store = useRegistryInFormationStore();
const dataStore = useDataStore(); const dataStore = useDataStore();
const officerLink = computed(() => dataStore.officerLink);
const mixin = useCounterMixin(); const mixin = useCounterMixin();
const { const {
showLoader, showLoader,
@ -30,6 +27,8 @@ const editPhone = ref<boolean>(false);
const editEmail = ref<boolean>(false); const editEmail = ref<boolean>(false);
const rowsHistory = ref<any[]>([]); const rowsHistory = ref<any[]>([]);
const modalHistory = ref<boolean>(false); const modalHistory = ref<boolean>(false);
const phone = ref<string>("");
const email = ref<string>("");
/** ตัวแปรข้อมูล */ /** ตัวแปรข้อมูล */
const formDataInformation = reactive<InformationData>({ const formDataInformation = reactive<InformationData>({
@ -265,6 +264,9 @@ async function getData() {
formDataInformation.phone = data.phone; formDataInformation.phone = data.phone;
formDataInformation.email = data.email ? data.email.split("@")[0] : ""; formDataInformation.email = data.email ? data.email.split("@")[0] : "";
email.value = data.email ? data.email.split("@")[0] : "";
phone.value = data.phone;
emailVerify.value = data.statusEmail; emailVerify.value = data.statusEmail;
hideLoader(); hideLoader();
}) })
@ -297,60 +299,70 @@ function getHistory() {
}); });
} }
function onUndo(type: string) { const isValidPhone = ref<boolean>(true); // validate
if (type == "phone") { const isValidEmail = ref<boolean>(true); // validate
editPhone.value = false;
} else {
editEmail.value = false;
}
}
function onSubmitEdit(type: string) {
dialogConfirm(
$q,
() => {
if (type == "phone") {
onSavePhone();
} else {
onSaveEmail();
}
},
`ยืนยันการบันทึก${type == "phone" ? "(เบอร์โทร)" : "(Email)"}`
);
}
/** บันทึกเบอร์โทร */ /** บันทึกเบอร์โทร */
async function onSavePhone() { async function onSavePhone() {
await http if (!formDataInformation.phone || formDataInformation.phone.length != 10) {
.put(config.API.upDateNumberByType(link.value), { isValidPhone.value = false;
phone: formDataInformation.phone, return;
}) } else {
.then(async (res) => { isValidPhone.value = true;
await getData();
editPhone.value = false; dialogConfirm(
success($q, "บันทึกข้อมูลสำเร็จ"); $q,
}) async () => {
.catch((e) => { showLoader();
messageError($q, e); await http
}) .put(config.API.upDateNumberByType(link.value), {
.finally(() => {}); phone: formDataInformation.phone,
})
.then(async (res) => {
editPhone.value = false;
success($q, "บันทึกข้อมูลสำเร็จ");
hideLoader();
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
getData();
});
},
"ยืนยันการแก้ไขเบอร์โทร"
);
}
} }
/** บันทึก email */ /** บันทึก email */
async function onSaveEmail() { async function onSaveEmail() {
await http if (!formDataInformation.email) {
.put(config.API.updateEmailByType(link.value), { isValidEmail.value = false;
email: formDataInformation.email + `@bangkok.go.th`, return;
}) } else {
.then(async (res) => { isValidEmail.value = true;
await getData();
editEmail.value = false; dialogConfirm(
success($q, "บันทึกข้อมูลสำเร็จ"); $q,
}) async () => {
.catch((e) => { await http
messageError($q, e); .put(config.API.updateEmailByType(link.value), {
}) email: formDataInformation.email + `@bangkok.go.th`,
.finally(() => {}); })
.then(async (res) => {
await getData();
editEmail.value = false;
success($q, "บันทึกข้อมูลสำเร็จ");
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {});
},
"ยืนยันการแก้ไขอีเมล"
);
}
} }
onMounted(async () => { onMounted(async () => {
@ -497,8 +509,11 @@ onMounted(async () => {
:readonly="!editPhone" :readonly="!editPhone"
mask="##########" mask="##########"
:class="editPhone ? 'inputgreen' : ''" :class="editPhone ? 'inputgreen' : ''"
> hide-bottom-space
</q-input> error-message="กรุณากรอกเบอร์โทร"
:error="!isValidPhone"
@change="() => (isValidPhone = true)"
/>
</div> </div>
<div v-if="editPhone == false" class="self-center col-3"> <div v-if="editPhone == false" class="self-center col-3">
@ -520,7 +535,7 @@ onMounted(async () => {
round round
icon="save" icon="save"
color="primary" color="primary"
@click="onSubmitEdit('phone')" @click="onSavePhone"
> >
<q-tooltip>นท</q-tooltip> <q-tooltip>นท</q-tooltip>
</q-btn> </q-btn>
@ -530,7 +545,13 @@ onMounted(async () => {
round round
icon="undo" icon="undo"
color="red" color="red"
@click="onUndo('phone')" @click="
() => {
editPhone = false;
formDataInformation.phone = phone;
isValidPhone = true;
}
"
> >
<q-tooltip>ยกเล</q-tooltip> <q-tooltip>ยกเล</q-tooltip>
</q-btn> </q-btn>
@ -551,8 +572,11 @@ onMounted(async () => {
type="email" type="email"
suffix="@bangkok.go.th" suffix="@bangkok.go.th"
:class="editEmail ? 'inputgreen' : ''" :class="editEmail ? 'inputgreen' : ''"
> hide-bottom-space
</q-input> error-message="กรุณากรอกอีเมล"
:error="!isValidEmail"
@change="isValidEmail = true"
/>
</div> </div>
<div v-if="emailVerify == null" class="self-center col-3"> <div v-if="emailVerify == null" class="self-center col-3">
<div v-if="editEmail == false"> <div v-if="editEmail == false">
@ -574,7 +598,7 @@ onMounted(async () => {
round round
icon="save" icon="save"
color="primary" color="primary"
@click="onSubmitEdit('email')" @click="onSaveEmail"
> >
<q-tooltip>นท</q-tooltip> <q-tooltip>นท</q-tooltip>
</q-btn> </q-btn>
@ -584,7 +608,13 @@ onMounted(async () => {
round round
icon="undo" icon="undo"
color="red" color="red"
@click="onUndo('email')" @click="
() => {
editEmail = false;
formDataInformation.email = email;
isValidEmail = true;
}
"
> >
<q-tooltip>ยกเล</q-tooltip> <q-tooltip>ยกเล</q-tooltip>
</q-btn> </q-btn>