From e34f10013800cf5aafa77dee59998b113b5755de Mon Sep 17 00:00:00 2001 From: puriphatt Date: Fri, 2 Aug 2024 08:26:55 +0000 Subject: [PATCH] fix(01): edit image profile --- src/pages/01_branch-management/MainPage.vue | 77 +++++++++------------ 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/src/pages/01_branch-management/MainPage.vue b/src/pages/01_branch-management/MainPage.vue index 92128247..afcbce52 100644 --- a/src/pages/01_branch-management/MainPage.vue +++ b/src/pages/01_branch-management/MainPage.vue @@ -23,7 +23,6 @@ import FormBranchInformation from 'components/01_branch-management/FormBranchInf import FormLocation from 'components/01_branch-management/FormLocation.vue'; import FormQr from 'components/01_branch-management/FormQr.vue'; import FormBranchContact from 'components/01_branch-management/FormBranchContact.vue'; -import FormImage from 'components/01_branch-management/FormImage.vue'; import DrawerInfo from 'components/DrawerInfo.vue'; import InfoForm from 'components/02_personnel-management/InfoForm.vue'; import TreeCompoent from 'src/components/TreeCompoent.vue'; @@ -103,32 +102,14 @@ const formMenuIcon = ref<{ icon: string; color: string; bgColor: string }[]>([ }, ]); +const refImageUpload = ref(); const isImageEdit = ref(false); -const profileFileImg = ref(null); -const imageUrl = ref(''); +const profileFileImg = ref(undefined); +const imageUrl = ref(''); +const prevImageUrl = ref(''); const currentNode = ref(); const imageDialog = ref(false); -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(undefined); const qrCodeimageUrl = ref(''); @@ -311,6 +292,7 @@ async function fetchBranchById(id: string) { if (res) { qrCodeimageUrl.value = res.qrCodeImageUrl; imageUrl.value = res.imageUrl; + prevImageUrl.value = res.imageUrl; formData.value = { headOfficeId: res.headOfficeId, @@ -337,7 +319,7 @@ async function fetchBranchById(id: string) { function clearData() { formData.value = structuredClone(defaultFormData); - imageUrl.value = null; + imageUrl.value = ''; qrCodeimageUrl.value = null; currentEdit.value = { id: '', @@ -348,6 +330,7 @@ function clearData() { async function undo() { formType.value = 'view'; + imageUrl.value = prevImageUrl.value; formData.value = prevFormData.value; } @@ -493,8 +476,8 @@ async function onSubmit() { ); await branchStore.fetchList({ pageSize: 99999 }); + if (!imageDialog.value) modalDrawer.value = false; modal.value = false; - modalDrawer.value = false; } if (formType.value === 'create') { @@ -567,16 +550,16 @@ function handleHold(node: BranchWithChildren) { }; } -function openImageDialog(isEdit?: boolean) { - if (isEdit) isImageEdit.value = true; - else isImageEdit.value = false; - imageDialog.value = true; -} - -function handleImageUpload(file: File | null, url: string | null) { - imageUrl.value = url; +async function handleImageUpload(file: File | null, url: string | null) { + if (formType.value === 'view') { + formType.value = 'edit'; + await onSubmit(); + formType.value = 'view'; + } + if (formType.value === 'edit') { + await onSubmit(); + } imageDialog.value = false; - // profileFileImg = file; } watch( @@ -1448,21 +1431,25 @@ watch(currentHq, () => { onSubmit(); } " - :close="() => ((modal = false), (profileFileImg = null))" + :close=" + () => ( + (modal = false), (profileFileImg = undefined), (isImageEdit = false) + ) + " >
@@ -1596,14 +1583,17 @@ watch(currentHq, () => { :editData="() => drawerEdit()" :submit="() => onSubmit()" :delete-data="() => triggerDelete(currentEdit.id)" - :close="() => ((modalDrawer = false), flowStore.rotate())" + :close=" + () => ((modalDrawer = false), flowStore.rotate(), (isImageEdit = false)) + " :statusBranch="formData.status" >
{ icon="mdi-office-building-outline" color="hsla(var(--pink-6-hsl)/1)" bgColor="hsla(var(--pink-6-hsl)/0.15)" - @view="openImageDialog" - @edit="inputFileImg.click()" + @view="imageDialog = true" + @edit="refImageUpload && refImageUpload.browse()" :readonly="formType === 'view'" :menu="formMenuIcon" /> @@ -1872,8 +1862,9 @@ watch(currentHq, () => {