feat: เพิ่ม อัปโหลดรูป ภาพสำนักงานได้

This commit is contained in:
Net 2024-04-18 17:30:29 +07:00
parent f84d280685
commit 118bcc6066

View file

@ -16,6 +16,7 @@ import FormBranchInformation from 'src/components/01_branch-management/FormBranc
import FormLocation from 'src/components/01_branch-management/FormLocation.vue'; import FormLocation from 'src/components/01_branch-management/FormLocation.vue';
import FormQr from 'src/components/01_branch-management/FormQr.vue'; import FormQr from 'src/components/01_branch-management/FormQr.vue';
import FormBranchContact from 'src/components/01_branch-management/FormBranchContact.vue'; import FormBranchContact from 'src/components/01_branch-management/FormBranchContact.vue';
import FormImage from 'src/components/01_branch-management/FormImage.vue';
import { BranchWithChildren, BranchCreate } from 'stores/branch/types'; import { BranchWithChildren, BranchCreate } from 'stores/branch/types';
import { watch } from 'vue'; import { watch } from 'vue';
@ -25,6 +26,29 @@ const { t } = useI18n();
const modal = ref<boolean>(false); const modal = ref<boolean>(false);
const profileFileImg = ref<File | undefined>(undefined);
const imageUrl = ref<string | null>('');
const inputFileImg = (() => {
const element = document.createElement('input');
element.type = 'file';
element.accept = 'image/*';
const reader = new FileReader();
reader.addEventListener('load', () => {
if (typeof reader.result === 'string') imageUrl.value = reader.result;
});
element.addEventListener('change', () => {
profileFileImg.value = element.files?.[0];
if (profileFileImg.value) {
reader.readAsDataURL(profileFileImg.value);
}
});
return element;
})();
const profileFile = ref<File | undefined>(undefined); const profileFile = ref<File | undefined>(undefined);
const qrCodeimageUrl = ref<string | null>(''); const qrCodeimageUrl = ref<string | null>('');
@ -132,7 +156,7 @@ const formDialogRef = ref();
const formType = ref<'create' | 'edit' | 'delete' | 'view'>('create'); const formType = ref<'create' | 'edit' | 'delete' | 'view'>('create');
const formTypeBranch = ref<'headOffice' | 'subBranch'>('headOffice'); const formTypeBranch = ref<'headOffice' | 'subBranch'>('headOffice');
const codeHq = ref<{ id: string; code: string }>({ id: '', code: '' }); const codeHq = ref<{ id: string; code: string }>({ id: '', code: '' });
const formData = ref<Omit<BranchCreate, 'qrCodeImage'>>( const formData = ref<Omit<BranchCreate, 'qrCodeImage' | 'imageUrl'>>(
structuredClone(defaultFormData), structuredClone(defaultFormData),
); );
@ -144,7 +168,10 @@ async function fetchBranchById(id: string) {
const res = await branchStore.fetchById(id); const res = await branchStore.fetchById(id);
if (res) { if (res) {
console.log(res);
qrCodeimageUrl.value = res.qrCodeImageUrl; qrCodeimageUrl.value = res.qrCodeImageUrl;
imageUrl.value = res.imageUrl;
formData.value = { formData.value = {
headOfficeId: res.headOfficeId, headOfficeId: res.headOfficeId,
taxNo: res.taxNo, taxNo: res.taxNo,
@ -169,6 +196,8 @@ async function fetchBranchById(id: string) {
function clearData() { function clearData() {
formData.value = structuredClone(defaultFormData); formData.value = structuredClone(defaultFormData);
imageUrl.value = null;
qrCodeimageUrl.value = null;
profileFile.value = undefined; profileFile.value = undefined;
} }
@ -244,25 +273,27 @@ function triggerDelete(id: string) {
async function onSubmit() { async function onSubmit() {
if (formType.value === 'edit') { if (formType.value === 'edit') {
if (!profileFile) { await branchStore.editById(
await branchStore.editById(codeHq.value.id, formData.value); codeHq.value.id,
} else { formData.value,
await branchStore.editById( profileFile.value,
codeHq.value.id, profileFileImg.value,
formData.value, );
profileFile.value,
);
}
await branchStore.fetchList({ pageSize: 99999 }); await branchStore.fetchList({ pageSize: 99999 });
modal.value = false; modal.value = false;
} }
if (formTypeBranch.value === 'headOffice') { if (formTypeBranch.value === 'headOffice') {
if (formType.value === 'create' && profileFile.value) { if (
formType.value === 'create' &&
profileFile.value &&
profileFileImg.value
) {
await branchStore.create({ await branchStore.create({
...formData.value, ...formData.value,
qrCodeImage: profileFile.value, qrCodeImage: profileFile.value,
imageUrl: profileFileImg.value,
}); });
await branchStore.fetchList({ pageSize: 99999 }); await branchStore.fetchList({ pageSize: 99999 });
modal.value = false; modal.value = false;
@ -270,12 +301,17 @@ async function onSubmit() {
} }
if (formTypeBranch.value === 'subBranch') { if (formTypeBranch.value === 'subBranch') {
if (formType.value === 'create' && profileFile.value) { if (
formType.value === 'create' &&
profileFile.value &&
profileFileImg.value
) {
formData.value.headOfficeId = codeHq.value.id; formData.value.headOfficeId = codeHq.value.id;
await branchStore.create({ await branchStore.create({
...formData.value, ...formData.value,
qrCodeImage: profileFile.value, qrCodeImage: profileFile.value,
imageUrl: profileFileImg.value,
}); });
await branchStore.fetchList({ pageSize: 99999 }); await branchStore.fetchList({ pageSize: 99999 });
modal.value = false; modal.value = false;
@ -766,11 +802,24 @@ watch(locale, () => {
<template #location> <template #location>
<FormLocation <FormLocation
:separator="true"
v-model:latitude="formData.latitude" v-model:latitude="formData.latitude"
v-model:longitude="formData.longitude" v-model:longitude="formData.longitude"
title="formDialogTitleLocation" title="formDialogTitleLocation"
/> />
</template> </template>
<template #by-type>
<FormImage
v-model:image="imageUrl"
@upload="
() => {
inputFileImg.click();
}
"
:title="$t('formDialogTitleImg')"
/>
</template>
</FormDialog> </FormDialog>
</template> </template>