refactor: 02 => optimize screen.xs fetch scroll logic

This commit is contained in:
puriphatt 2025-01-30 16:54:28 +07:00
parent 790c45e532
commit e2ede08319

View file

@ -74,6 +74,7 @@ const imageDialog = ref(false);
const infoDrawerEdit = ref(false); const infoDrawerEdit = ref(false);
const refreshImageState = ref(false); const refreshImageState = ref(false);
const refFilter = ref<InstanceType<typeof QSelect>>(); const refFilter = ref<InstanceType<typeof QSelect>>();
const firstScroll = ref(false);
const inputSearch = ref(''); const inputSearch = ref('');
const currentTab = ref<string>('ALL'); const currentTab = ref<string>('ALL');
@ -434,7 +435,7 @@ async function onSubmit(excludeDialog?: boolean) {
} }
} }
await fetchUserList(); $q.screen.xs ? await fetchUserEditXs() : await fetchUserList();
typeStats.value = await userStore.typeStats(); typeStats.value = await userStore.typeStats();
const res = await branchStore.userStats(formData.value.userType); const res = await branchStore.userStats(formData.value.userType);
if (res) { if (res) {
@ -466,9 +467,14 @@ async function onSubmit(excludeDialog?: boolean) {
} }
} }
currentTab.value = formData.value.userType; if (
currentTab.value === formData.value.userType &&
currentTab.value !== 'ALL'
) {
$q.screen.gt.xs ? await fetchUserEditXs() : await fetchUserList();
}
await fetchUserList(); currentTab.value = formData.value.userType;
typeStats.value = await userStore.typeStats(); typeStats.value = await userStore.typeStats();
const res = await branchStore.userStats(formData.value.userType); const res = await branchStore.userStats(formData.value.userType);
@ -658,6 +664,9 @@ async function fetchUserList() {
if (!userData.value) { if (!userData.value) {
userData.value = ret; userData.value = ret;
} else { } else {
userData.value.page = ret.page;
userData.value.pageSize = ret.pageSize;
userData.value.total = ret.total;
userData.value?.result.push(...ret.result); userData.value?.result.push(...ret.result);
} }
} else { } else {
@ -666,6 +675,26 @@ async function fetchUserList() {
} }
} }
async function fetchUserEditXs() {
const ret = await userStore.fetchList({
includeBranch: true,
pageSize: userData.value?.result.length || 0,
page: 1,
query: !!inputSearch.value ? inputSearch.value : undefined,
userType: currentTab.value === 'ALL' ? undefined : currentTab.value,
status:
statusFilter.value === 'all'
? undefined
: statusFilter.value === 'statusACTIVE'
? 'ACTIVE'
: 'INACTIVE',
});
if (ret) {
userData.value = ret;
}
}
function noPersonnel() { function noPersonnel() {
const number = const number =
typeStats.value && typeStats.value &&
@ -700,8 +729,11 @@ onMounted(async () => {
watch( watch(
() => currentTab.value, () => currentTab.value,
async (label) => { async (label) => {
firstScroll.value = true;
mapUserType(label); mapUserType(label);
await fetchUserList(); if (userData.value) userData.value.result = [];
currentPage.value = 1;
if ($q.screen.gt.xs) await fetchUserList();
const res = await branchStore.userStats(label); const res = await branchStore.userStats(label);
if (res) { if (res) {
userStats.value = res; userStats.value = res;
@ -734,7 +766,12 @@ watch(
}, },
); );
watch([inputSearch, statusFilter, pageSize], async () => await fetchUserList()); watch([inputSearch, statusFilter, pageSize], async () => {
if (userData.value) userData.value.result = [];
currentPage.value = 1;
await fetchUserList();
});
watch( watch(
() => $q.screen.lt.md, () => $q.screen.lt.md,
@ -1083,74 +1120,79 @@ watch(
class="flex full-width full-height q-pa-md surface-2 column col scroll" class="flex full-width full-height q-pa-md surface-2 column col scroll"
> >
<q-infinite-scroll <q-infinite-scroll
:key="currentTab"
:offset="100"
@load=" @load="
(_, done) => { (_, done) => {
if ($q.screen.gt.xs || currentPage === currentMaxPage) return; if ($q.screen.gt.xs) return;
currentPage = currentPage + 1; currentPage = firstScroll ? 1 : currentPage + 1;
fetchUserList().then(() => done(currentPage >= currentMaxPage)); fetchUserList().then(() => {
firstScroll = false;
done(currentPage >= currentMaxPage);
});
} }
" "
> >
<template v-if="userData && userData.total > 0"> <q-table
<q-table v-if="userData && userData.total > 0"
flat flat
bordered bordered
:grid="modeView" :grid="modeView"
:rows="userData.result" :rows="userData.result"
:columns="columns" :columns="columns"
class="full-width" class="full-width"
card-container-class="q-col-gutter-md" card-container-class="q-col-gutter-md"
row-key="name" row-key="name"
:rows-per-page-options="[0]" :rows-per-page-options="[0]"
hide-pagination hide-pagination
:visible-columns="fieldSelected" :visible-columns="fieldSelected"
> >
<template v-slot:header="props"> <template v-slot:header="props">
<q-tr <q-tr
style="background-color: hsla(var(--info-bg) / 0.07)" style="background-color: hsla(var(--info-bg) / 0.07)"
:props="props" :props="props"
> >
<q-th <q-th v-for="col in props.cols" :key="col.name" :props="props">
v-for="col in props.cols" {{ $t(col.label) }}
:key="col.name" </q-th>
:props="props" <q-th auto-width />
> </q-tr>
{{ $t(col.label) }} </template>
</q-th>
<q-th auto-width />
</q-tr>
</template>
<template v-slot:body="props"> <template v-slot:body="props">
<q-tr <q-tr
:class="{ :class="{
'app-text-muted': props.row.status === 'INACTIVE', 'app-text-muted': props.row.status === 'INACTIVE',
'status-active': props.row.status !== 'INACTIVE', 'status-active': props.row.status !== 'INACTIVE',
'status-inactive': props.row.status === 'INACTIVE', 'status-inactive': props.row.status === 'INACTIVE',
}" }"
:props="props" :props="props"
:style=" :style="
props.rowIndex % 2 !== 0 props.rowIndex % 2 !== 0
? $q.dark.isActive ? $q.dark.isActive
? 'background: hsl(var(--gray-11-hsl)/0.2)' ? 'background: hsl(var(--gray-11-hsl)/0.2)'
: `background: #f9fafc` : `background: #f9fafc`
: '' : ''
" "
>
<q-td
class="text-center"
v-if="fieldSelected.includes('orderNumber')"
> >
<q-td {{
class="text-center" $q.screen.xs
v-if="fieldSelected.includes('orderNumber')" ? props.rowIndex + 1
> : (currentPage - 1) * pageSize + props.rowIndex + 1
{{ (currentPage - 1) * pageSize + props.rowIndex + 1 }} }}
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('name')"> <q-td v-if="fieldSelected.includes('name')">
<div class="row items-center" style="flex-wrap: nowrap"> <div class="row items-center" style="flex-wrap: nowrap">
<div style="display: flex"> <div style="display: flex">
<div class="q-mr-md"> <div class="q-mr-md">
<div <div
:style="` :style="`
border-radius: 50%; border-radius: 50%;
border-style: solid; border-style: solid;
border-width: 2px; border-width: 2px;
@ -1164,245 +1206,251 @@ watch(
'--pink-8-hsl' '--pink-8-hsl'
} }
`" `"
class="q-pa-xs" class="q-pa-xs"
> >
<q-avatar size="md"> <q-avatar size="md">
<q-img <q-img
class="text-center" class="text-center"
:ratio="1" :ratio="1"
:src="`${baseUrl}/user/${props.row.id}/profile-image/${props.row.selectedImage}?ts=${Date.now()}`" :src="`${baseUrl}/user/${props.row.id}/profile-image/${props.row.selectedImage}?ts=${Date.now()}`"
> >
<template #error> <template #error>
<div <div
class="no-padding full-width full-height flex items-center justify-center" class="no-padding full-width full-height flex items-center justify-center"
:style="`${props.row.gender ? 'background: white' : 'background: linear-gradient(135deg,rgba(43, 137, 223, 1) 0%, rgba(230, 51, 81, 1) 100%);'}`" :style="`${props.row.gender ? 'background: white' : 'background: linear-gradient(135deg,rgba(43, 137, 223, 1) 0%, rgba(230, 51, 81, 1) 100%);'}`"
> >
<q-img <q-img
v-if="props.row.gender" v-if="props.row.gender"
:src=" :src="
props.row.gender === 'male' props.row.gender === 'male'
? '/no-img-man.png' ? '/no-img-man.png'
: '/no-img-female.png' : '/no-img-female.png'
" "
/> />
<q-icon <q-icon
v-else v-else
size="sm" size="sm"
name="mdi-account-outline" name="mdi-account-outline"
style="color: white" style="color: white"
/> />
</div> </div>
</template> </template>
</q-img> </q-img>
<q-badge <q-badge
class="absolute-bottom-right no-padding" class="absolute-bottom-right no-padding"
style=" style="
border-radius: 50%; border-radius: 50%;
min-width: 8px; min-width: 8px;
min-height: 8px; min-height: 8px;
" "
:style="{ :style="{
background: `var(--${props.row.status === 'INACTIVE' ? 'stone-5' : 'green-6'})`, background: `var(--${props.row.status === 'INACTIVE' ? 'stone-5' : 'green-6'})`,
}" }"
></q-badge> ></q-badge>
</q-avatar> </q-avatar>
</div>
</div> </div>
</div> </div>
<div class="column"> </div>
<div class="col ellipsis" style="max-width: 20vw"> <div class="column">
<div class="col ellipsis" style="max-width: 20vw">
{{
locale === 'eng'
? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim()
: `${props.row.firstName} ${props.row.lastName}`.trim()
}}
<q-tooltip
anchor="bottom left"
self="center left"
:delay="300"
>
{{ {{
locale === 'eng' locale === 'eng'
? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim() ? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim()
: `${props.row.firstName} ${props.row.lastName}`.trim() : `${props.row.firstName} ${props.row.lastName}`.trim()
}} }}
<q-tooltip </q-tooltip>
anchor="bottom left"
self="center left"
:delay="300"
>
{{
locale === 'eng'
? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim()
: `${props.row.firstName} ${props.row.lastName}`.trim()
}}
</q-tooltip>
<q-icon <q-icon
class="q-pl-xs" class="q-pl-xs"
:class="{ :class="{
'symbol-gender': props.row.gender, 'symbol-gender': props.row.gender,
'symbol-gender__male': 'symbol-gender__male': props.row.gender === 'male',
props.row.gender === 'male', 'symbol-gender__female':
'symbol-gender__female': props.row.gender === 'female',
props.row.gender === 'female', }"
}" :name="`mdi-gender-${props.row.gender === 'male' ? 'male' : 'female'}`"
:name="`mdi-gender-${props.row.gender === 'male' ? 'male' : 'female'}`" width="24px"
width="24px" />
/> </div>
</div> <div class="app-text-muted">
<div class="app-text-muted"> {{ props.row.code || '-' }}
{{ props.row.code || '-' }}
</div>
</div> </div>
</div> </div>
</q-td> </div>
</q-td>
<q-td v-if="fieldSelected.includes('telephoneNo')"> <q-td v-if="fieldSelected.includes('telephoneNo')">
{{ props.row.telephoneNo || '-' }} {{ props.row.telephoneNo || '-' }}
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('birthDate')"> <q-td v-if="fieldSelected.includes('birthDate')">
{{ calculateAge(props.row.birthDate) || '-' }} {{ calculateAge(props.row.birthDate) || '-' }}
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('email')"> <q-td v-if="fieldSelected.includes('email')">
{{ props.row.email || '-' }} {{ props.row.email || '-' }}
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('userRole')"> <q-td v-if="fieldSelected.includes('userRole')">
<span class="tags tags__purple"> <span class="tags tags__purple">
{{ mapRole(props.row.userRole) }} {{ mapRole(props.row.userRole) }}
</span> </span>
</q-td> </q-td>
<q-td> <q-td>
<q-btn <q-btn
:id="`btn-view-${props.row.username}`" :id="`btn-view-${props.row.username}`"
icon="mdi-eye-outline" icon="mdi-eye-outline"
size="sm" size="sm"
dense dense
round round
flat flat
@click.stop=" @click.stop="
() => { () => {
openDialog('INFO', props.row.id); openDialog('INFO', props.row.id);
} }
"
/>
<KebabAction
:id-name="props.row.username"
:status="props.row.status"
@view="
() => {
openDialog('INFO', props.row.id);
}
"
@edit="
() => {
openDialog('INFO', props.row.id, true);
}
"
@delete="onDelete(props.row.id)"
@change-status="
async () => {
triggerChangeStatus(props.row.id, props.row.status);
}
"
/>
</q-td>
</q-tr>
</template>
<template v-slot:item="props">
<div class="col-md-3 col-sm-6 col-12">
<PersonCard
:prefix-id="props.row.username"
:data="{
code: props.row.code,
name:
locale === 'eng'
? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim()
: `${props.row.firstName} ${props.row.lastName}`.trim(),
img: props.row.profileImageUrl
? props.row.profileImageUrl
: props.row.userRole.includes('system')
? '/img-admin.png'
: props.row.gender === 'male'
? '/no-img-man.png'
: '/no-img-female.png',
male: props.row.gender === 'male',
female: props.row.gender === 'female',
detail: [
{
icon: 'mdi-phone-outline',
value: props.row.telephoneNo,
},
{
icon: 'mdi-clock-outline',
value:
(props.row.birthDate &&
calculateAge(props.row.birthDate)) ||
'-',
},
],
}"
:tag="[
{
color:
{
USER: 'cyan',
MESSENGER: 'yellow',
DELEGATE: 'red',
AGENCY: 'magenta',
}[props.row.userType as string] || 'pink',
value: $t(`personnel.${props.row.userType}`),
},
]"
:disabled="props.row.status === 'INACTIVE'"
@update-card="(a, b) => openDialog(a, props.row.id, b)"
@delete-card="onDelete(props.row.id)"
@enter-card="(a) => openDialog(a, props.row.id)"
@toggle-status="
triggerChangeStatus(props.row.id, props.row.status)
" "
> />
<template #img> <KebabAction
<q-img :id-name="props.row.username"
class="text-center" :status="props.row.status"
:ratio="1" @view="
:src="`${baseUrl}/user/${props.row.id}/profile-image/${props.row.selectedImage}?ts=${Date.now()}`" () => {
style=" openDialog('INFO', props.row.id);
object-fit: cover; }
width: 100%; "
height: 100%; @edit="
border-radius: 50%; () => {
" openDialog('INFO', props.row.id, true);
> }
<template #error> "
<div @delete="onDelete(props.row.id)"
class="no-padding full-width full-height flex items-center justify-center" @change-status="
style=" async () => {
background: linear-gradient( triggerChangeStatus(props.row.id, props.row.status);
135deg, }
rgba(43, 137, 223, 1) 0%, "
rgba(230, 51, 81, 1) 100% />
); </q-td>
</q-tr>
</template>
<template v-slot:item="props">
<div class="col-md-3 col-sm-6 col-12">
<PersonCard
:prefix-id="props.row.username"
:data="{
code: props.row.code,
name:
locale === 'eng'
? `${props.row.firstNameEN} ${props.row.lastNameEN}`.trim()
: `${props.row.firstName} ${props.row.lastName}`.trim(),
img: props.row.profileImageUrl
? props.row.profileImageUrl
: props.row.userRole.includes('system')
? '/img-admin.png'
: props.row.gender === 'male'
? '/no-img-man.png'
: '/no-img-female.png',
male: props.row.gender === 'male',
female: props.row.gender === 'female',
detail: [
{
icon: 'mdi-phone-outline',
value: props.row.telephoneNo,
},
{
icon: 'mdi-clock-outline',
value:
(props.row.birthDate &&
calculateAge(props.row.birthDate)) ||
'-',
},
],
}"
:tag="[
{
color:
{
USER: 'cyan',
MESSENGER: 'yellow',
DELEGATE: 'red',
AGENCY: 'magenta',
}[props.row.userType as string] || 'pink',
value: $t(`personnel.${props.row.userType}`),
},
]"
:disabled="props.row.status === 'INACTIVE'"
@update-card="(a, b) => openDialog(a, props.row.id, b)"
@delete-card="onDelete(props.row.id)"
@enter-card="(a) => openDialog(a, props.row.id)"
@toggle-status="
triggerChangeStatus(props.row.id, props.row.status)
"
>
<template #img>
<q-img
class="text-center"
:ratio="1"
:src="`${baseUrl}/user/${props.row.id}/profile-image/${props.row.selectedImage}?ts=${Date.now()}`"
style="
object-fit: cover;
width: 100%;
height: 100%;
border-radius: 50%;
"
>
<template #error>
<div
class="no-padding full-width full-height flex items-center justify-center"
style="
background: linear-gradient(
135deg,
rgba(43, 137, 223, 1) 0%,
rgba(230, 51, 81, 1) 100%
);
"
>
<q-img
v-if="props.row.gender"
:src="
props.row.gender === 'male'
? '/no-img-man.png'
: '/no-img-female.png'
" "
> />
<q-img <q-icon
v-if="props.row.gender" v-else
:src=" size="lg"
props.row.gender === 'male' name="mdi-account-outline"
? '/no-img-man.png' style="color: white"
: '/no-img-female.png' />
" </div>
/> </template>
<q-icon </q-img>
v-else </template>
size="lg" </PersonCard>
name="mdi-account-outline" </div>
style="color: white" </template>
/> </q-table>
</div> <template v-slot:loading>
</template> <div
</q-img> v-if="$q.screen.lt.sm && currentPage !== currentMaxPage"
</template> class="row justify-center"
</PersonCard> >
</div> <q-spinner-dots color="primary" size="40px" />
</template> </div>
</q-table>
</template> </template>
</q-infinite-scroll> </q-infinite-scroll>