feat(loading-state): add loading indicators and skeleton UI for data tables

- Implement loading state management across patient, doctor and satusehat lists
- Add skeleton loading UI for data tables during data fetching
- Refactor loading state variable naming for consistency
- Make search nav optional in header component
- Update icon sizing in header for better responsiveness
- Implement url search params query state at satusehat
This commit is contained in:
Khafid Prayoga
2025-08-25 13:33:28 +07:00
parent 92d6e2af10
commit 31084be5ce
5 changed files with 139 additions and 124 deletions
+10 -2
View File
@@ -15,6 +15,10 @@ const refSearchNav: RefSearchNav = {
}, },
} }
const isLoading = reactive({
dataListLoading: false,
})
const recId = ref<number>(0) const recId = ref<number>(0)
const recAction = ref<string>('') const recAction = ref<string>('')
const recItem = ref<any>(null) const recItem = ref<any>(null)
@@ -28,13 +32,16 @@ const headerPrep: HeaderPrep = {
}, },
} }
useAsyncData('getDoctor', () => xfetch('/api/v1/doctor'), { server: false, immediate: true })
async function getDoctorList() { async function getDoctorList() {
isLoading.dataListLoading = true
const resp = await xfetch('/api/v1/doctor') const resp = await xfetch('/api/v1/doctor')
if (resp.success) { if (resp.success) {
data.value = (resp.body as Record<string, any>).data data.value = (resp.body as Record<string, any>).data
} }
isLoading.dataListLoading = false
} }
onMounted(() => { onMounted(() => {
@@ -44,9 +51,10 @@ onMounted(() => {
provide('rec_id', recId) provide('rec_id', recId)
provide('rec_action', recAction) provide('rec_action', recAction)
provide('rec_item', recItem) provide('rec_item', recItem)
provide('pull_data', isLoading)
</script> </script>
<template> <template>
<PubNavHeaderPrep :prep="{ ...headerPrep }" :ref-search-nav="refSearchNav" /> <PubNavHeaderPrep :prep="{ ...headerPrep }" :ref-search-nav="refSearchNav" />
<AppDoctorList :data="data" /> <AppDoctorList v-if="!isLoading.dataListLoading" :data="data" />
</template> </template>
+5 -1
View File
@@ -20,8 +20,9 @@ const refSearchNav: RefSearchNav = {
// Loading state management // Loading state management
const isLoading = reactive({ const isLoading = reactive({
summary: false, summary: false,
table: false, dataListLoading: false,
}) })
const recId = ref<number>(0) const recId = ref<number>(0)
const recAction = ref<string>('') const recAction = ref<string>('')
const recItem = ref<any>(null) const recItem = ref<any>(null)
@@ -82,11 +83,13 @@ async function getPatientSummary() {
} }
async function getPatientList() { async function getPatientList() {
isLoading.dataListLoading = true
const resp = await xfetch('/api/v1/patient') const resp = await xfetch('/api/v1/patient')
console.log('data patient', resp) console.log('data patient', resp)
if (resp.success) { if (resp.success) {
data.value = (resp.body as Record<string, any>).data data.value = (resp.body as Record<string, any>).data
} }
isLoading.dataListLoading = false
} }
onMounted(() => { onMounted(() => {
@@ -97,6 +100,7 @@ onMounted(() => {
provide('rec_id', recId) provide('rec_id', recId)
provide('rec_action', recAction) provide('rec_action', recAction)
provide('rec_item', recItem) provide('rec_item', recItem)
provide('pull_data', isLoading)
</script> </script>
<template> <template>
+102 -105
View File
@@ -2,24 +2,27 @@
import type { ServiceStatus } from '~/components/pub/base/service-status.type' import type { ServiceStatus } from '~/components/pub/base/service-status.type'
import type { Summary } from '~/components/pub/base/summary-card.type' import type { Summary } from '~/components/pub/base/summary-card.type'
import type { HeaderPrep, RefSearchNav } from '~/components/pub/nav/types' import type { HeaderPrep, RefSearchNav } from '~/components/pub/nav/types'
import { useUrlSearchParams } from '@vueuse/core'
import { CircleCheckBig, CircleDashed, CircleX, Ellipsis, Search, Send } from 'lucide-vue-next' import { CircleCheckBig, CircleDashed, CircleX, Ellipsis, Search, Send } from 'lucide-vue-next'
// State management // State management
const data = ref([]) const data = ref([])
const isLoading = reactive({ const isLoading = reactive({
satusehatConn: true, satusehatConn: true,
dataList: false, dataListLoading: false,
}) })
// Filter states const params = useUrlSearchParams('history', {
const filters = reactive({ initialValue: {
search: '', search: '',
status: '', status: '',
resource_type: 'all', resource_type: 'all',
date_from: '', date_from: '',
date_to: '', date_to: '',
page: 1, page: 1,
limit: 10, limit: 10,
},
removeFalsyValues: true,
}) })
// Pagination state // Pagination state
@@ -35,17 +38,17 @@ const pagination = ref({
// API function to fetch data // API function to fetch data
async function fetchData() { async function fetchData() {
try { try {
isLoading.dataList = true isLoading.dataListLoading = true
const response: any = await $fetch('/api/v1/satusehat/list', { const response: any = await $fetch('/api/v1/satusehat/list', {
method: 'POST', method: 'POST',
body: { body: {
status: filters.status !== '' ? Number(filters.status) : undefined, status: params.status !== '' ? Number(params.status) : undefined,
resource_type: filters.resource_type, resource_type: params.resource_type,
date_from: filters.date_from, date_from: params.date_from,
date_to: filters.date_to, date_to: params.date_to,
search: filters.search, search: params.search,
page: filters.page, page: params.page,
limit: filters.limit, limit: params.limit,
}, },
}) })
@@ -56,26 +59,19 @@ async function fetchData() {
} catch (error) { } catch (error) {
console.error('Error fetching data:', error) console.error('Error fetching data:', error)
} finally { } finally {
isLoading.dataList = false isLoading.dataListLoading = false
} }
} }
// Debounced search function // Debounced search function
const debouncedFetchData = useDebounceFn(fetchData, 500)
const refSearchNav: RefSearchNav = { const refSearchNav: RefSearchNav = {
onClick: () => { onClick: () => {
// open filter modal // open filter modal
}, },
onInput: (val: string) => { onInput: (val: string) => {
filters.search = val
filters.page = 1
debouncedFetchData()
}, },
onClear: () => { onClear: () => {
filters.search = ''
filters.page = 1
fetchData()
}, },
} }
@@ -83,23 +79,9 @@ const recId = ref<number>(0)
const recAction = ref<string>('') const recAction = ref<string>('')
const recItem = ref<any>(null) const recItem = ref<any>(null)
// Filter change handlers
function onStatusChange(status: string) {
filters.status = status
filters.page = 1
fetchData()
}
function onResourceTypeChange(resourceType: string) { function onResourceTypeChange(resourceType: string) {
filters.resource_type = resourceType params.resource_type = resourceType
filters.page = 1 params.page = 1
fetchData()
}
function onDateRangeChange(dateFrom: string, dateTo: string) {
filters.date_from = dateFrom
filters.date_to = dateTo
filters.page = 1
fetchData() fetchData()
} }
@@ -167,7 +149,7 @@ async function callSatuSehat() {
} }
// Watch for tab changes // Watch for tab changes
watch(() => filters.resource_type, (newType) => { watch(() => params.resource_type, (newType) => {
onResourceTypeChange(newType) onResourceTypeChange(newType)
}) })
@@ -179,6 +161,7 @@ onMounted(async () => {
provide('rec_id', recId) provide('rec_id', recId)
provide('rec_action', recAction) provide('rec_action', recAction)
provide('rec_item', recItem) provide('rec_item', recItem)
provide('pull_data', isLoading)
const tabs = [ const tabs = [
{ {
@@ -201,36 +184,29 @@ const tabs = [
// Status filter options // Status filter options
const statusOptions = [ const statusOptions = [
{ value: '', label: 'Semua Status' },
{ value: '0', label: 'Failed' }, { value: '0', label: 'Failed' },
{ value: '1', label: 'Pending' }, { value: '1', label: 'Pending' },
{ value: '2', label: 'Success' }, { value: '2', label: 'Success' },
] ]
const actions = [ const actions = [
{
value: 'filter',
label: 'Filter',
icon: 'i-lucide-list-filter',
},
{ {
value: 'export', value: 'export',
label: 'Ekspor', label: 'Ekspor',
icon: 'i-lucide-download', icon: 'i-lucide-download',
}, },
] ]
// Sync activeTabFilter with filters.resource_type // Sync activeTabFilter with filters.resource_type
const activeTabFilter = computed({ const activeTabFilter = computed({
get: () => filters.resource_type, get: () => params.resource_type,
set: (value) => { set: (value) => {
filters.resource_type = value params.resource_type = value
}, },
}) })
</script> </script>
<template> <template>
<PubNavHeaderPrep :prep="headerPrep" :ref-search-nav="refSearchNav" /> <PubNavHeaderPrep :prep="headerPrep" />
<div class="my-4 flex flex-1 flex-col gap-3 md:gap-4"> <div class="my-4 flex flex-1 flex-col gap-3 md:gap-4">
<PubBaseServiceStatus v-bind="service" /> <PubBaseServiceStatus v-bind="service" />
<AppSatusehatCardSummary :is-loading="isLoading.satusehatConn" :summary-data="summaryData" /> <AppSatusehatCardSummary :is-loading="isLoading.satusehatConn" :summary-data="summaryData" />
@@ -240,83 +216,104 @@ const activeTabFilter = computed({
<Tabs v-model="activeTabFilter"> <Tabs v-model="activeTabFilter">
<div class="scrollbar-hide overflow-x-auto flex gap-2 justify-between"> <div class="scrollbar-hide overflow-x-auto flex gap-2 justify-between">
<TabsList> <TabsList>
<TabsTrigger <TabsTrigger v-for="tab in tabs" :key="tab.value" :value="tab.value"
v-for="tab in tabs" class="flex-shrink-0 px-4 py-2 text-sm font-medium data-[state=active]:bg-green-600 data-[state=inactive]:bg-gray-100 data-[state=active]:text-white data-[state=inactive]:text-gray-700">
:key="tab.value"
:value="tab.value"
class="flex-shrink-0 px-4 py-2 text-sm font-medium data-[state=active]:bg-green-600 data-[state=inactive]:bg-gray-100 data-[state=active]:text-white data-[state=inactive]:text-gray-700"
>
{{ tab.label }} {{ tab.label }}
</TabsTrigger> </TabsTrigger>
</TabsList> </TabsList>
<div class="flex gap-2 items-center"> <div class="flex gap-2 items-center">
<!-- Status Filter -->
<Select v-model="filters.status" @update:model-value="onStatusChange">
<SelectTrigger class="w-40 h-9">
<SelectValue placeholder="Status" />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="option in statusOptions" :key="option.value" :value="option.value">
{{ option.label }}
</SelectItem>
</SelectContent>
</Select>
<!-- Date Range Picker -->
<AppSatusehatPicker @date-change="onDateRangeChange" />
<!-- Search Input --> <!-- Search Input -->
<div class="relative w-full max-w-sm"> <div class="relative w-full max-w-sm">
<Input
id="search" <Dialog>
v-model="filters.search" <DialogTrigger>
type="text" <Input id="search" v-model="params.search" type="text" placeholder="Cari pasien..." class="pl-3 h-9" />
placeholder="Cari pasien..." </DialogTrigger>
class="pl-7 h-9" <DialogContent>
@input="refSearchNav.onInput(filters.search)" <DialogHeader class="border-b-1">
/> <DialogTitle class="pb-2">Pencarian</DialogTitle>
<span class="absolute start-0 inset-y-0 flex items-center justify-center px-2"> </DialogHeader>
<Search class="size-4 text-muted-foreground" /> <Form>
</span> <FormField v-slot="{ componentField }" name="username">
<FormItem>
<FormLabel>Pasien</FormLabel>
<FormControl>
<Input type="text" placeholder="nama pasien, id pasien" v-bind="componentField" />
</FormControl>
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="status">
<FormItem>
<FormLabel>Status</FormLabel>
<FormControl>
<Select>
<SelectTrigger class="bg-white border border-gray-300">
<SelectValue class="text-gray-400" placeholder="-- select item" />
</SelectTrigger>
<SelectContent class="bg-white ">
<SelectItem value="0">
Gagal
</SelectItem>
<SelectItem value="1">
Pending
</SelectItem>
<SelectItem value="2">
Terkirim
</SelectItem>
</SelectContent>
</Select>
</FormControl>
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="fhirId">
<FormItem>
<FormLabel>FHIR ID</FormLabel>
<FormControl>
<Input type="text" placeholder="fhir id" v-bind="componentField" />
</FormControl>
</FormItem>
</FormField>
</Form>
<DialogFooter>
<Button variant="outline">
Reset
</Button>
<Button>
Apply
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div> </div>
<!-- Action Buttons --> <!-- Action Buttons -->
<AppSatusehatButtonAction <AppSatusehatButtonAction v-for="action in actions" :key="action.value" :icon="action.icon"
v-for="action in actions" :text="action.label" />
:key="action.value"
:icon="action.icon"
:text="action.label"
/>
</div> </div>
</div> </div>
<div class="mt-4"> <div class="mt-4">
<TabsContent v-for="tab in tabs" :key="`content-${tab.value}`" :value="tab.value"> <TabsContent v-for="tab in tabs" :key="`content-${tab.value}`" :value="tab.value">
<div class="rounded-md border p-4"> <div class="rounded-md border p-4">
<Ellipsis v-if="isLoading.satusehatConn || isLoading.dataList" class="size-6 animate-pulse text-muted-foreground mx-auto" /> <!-- <Ellipsis v-if="isLoading.satusehatConn || isLoading.dataList" -->
<AppSatusehatList v-if="!isLoading.satusehatConn && !isLoading.dataList" :data="data" /> <!-- class="size-6 animate-pulse text-muted-foreground mx-auto" /> -->
<AppSatusehatList v-if="!isLoading.satusehatConn" :data="data" />
<!-- Pagination --> <!-- Pagination -->
<div v-if="!isLoading.satusehatConn && !isLoading.dataList && pagination.total > 0" class="mt-4 flex justify-between items-center"> <div v-if="!isLoading.satusehatConn && !isLoading.dataListLoading && pagination.total > 0"
class="mt-4 flex justify-between items-center">
<div class="text-sm text-muted-foreground"> <div class="text-sm text-muted-foreground">
Menampilkan {{ ((pagination.page - 1) * pagination.limit) + 1 }} - Menampilkan {{ ((pagination.page - 1) * pagination.limit) + 1 }} -
{{ Math.min(pagination.page * pagination.limit, pagination.total) }} {{ Math.min(pagination.page * pagination.limit, pagination.total) }}
dari {{ pagination.total }} data dari {{ pagination.total }} data
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<Button <Button v-if="pagination.has_prev" variant="outline" size="sm" @click="params.page--; fetchData()">
v-if="pagination.has_prev"
variant="outline"
size="sm"
@click="filters.page--; fetchData()"
>
Sebelumnya Sebelumnya
</Button> </Button>
<Button <Button v-if="pagination.has_next" variant="outline" size="sm" @click="params.page++; fetchData()">
v-if="pagination.has_next"
variant="outline"
size="sm"
@click="filters.page++; fetchData()"
>
Selanjutnya Selanjutnya
</Button> </Button>
</div> </div>
+15 -10
View File
@@ -10,30 +10,35 @@ defineProps<{
funcHtml: Record<string, (row: any) => string> funcHtml: Record<string, (row: any) => string>
funcComponent: Record<string, (row: any, idx: number) => any> funcComponent: Record<string, (row: any, idx: number) => any>
}>() }>()
const pullData = inject('pull_data') as any
</script> </script>
<template> <template>
<Table> <Table>
<TableHeader class="bg-gray-50"> <TableHeader class="bg-gray-50">
<TableRow> <TableRow>
<TableHead <TableHead v-for="(h, idx) in header[0]" :key="`head-${idx}`"
v-for="(h, idx) in header[0]" :key="`head-${idx}`" :style="{ width: cols[idx]?.width ? `${cols[idx].width}px` : undefined }">
:style="{ width: cols[idx]?.width ? `${cols[idx].width}px` : undefined }"
>
{{ h.label }} {{ h.label }}
</TableHead> </TableHead>
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody v-if="pullData.dataListLoading">
<!-- Loading state with 5 skeleton rows -->
<TableRow v-for="n in 5" :key="`skeleton-${n}`">
<TableCell v-for="(key, cellIndex) in keys" :key="`cell-skel-${n}-${cellIndex}`">
<Skeleton class="bg-gray-100 animate-pulse text-muted-foreground w-full h-6" />
</TableCell>
</TableRow>
</TableBody>
<TableBody v-else>
<TableRow v-for="(row, rowIndex) in rows" :key="`row-${rowIndex}`"> <TableRow v-for="(row, rowIndex) in rows" :key="`row-${rowIndex}`">
<TableCell v-for="(key, cellIndex) in keys" :key="`cell-${rowIndex}-${cellIndex}`"> <TableCell v-for="(key, cellIndex) in keys" :key="`cell-${rowIndex}-${cellIndex}`">
<!-- If funcComponent has a renderer --> <!-- If funcComponent has a renderer -->
<component <component :is="funcComponent[key](row, rowIndex).component" v-if="funcComponent[key]"
:is="funcComponent[key](row, rowIndex).component" v-if="funcComponent[key]" v-bind="funcComponent[key](row, rowIndex)" />
v-bind="funcComponent[key](row, rowIndex)"
/>
<!-- If funcParsed or funcHtml returns a value --> <!-- If funcParsed or funcHtml returns a value -->
<template v-else> <template v-else>
<!-- Use v-html for funcHtml to render HTML content --> <!-- Use v-html for funcHtml to render HTML content -->
+7 -6
View File
@@ -3,19 +3,19 @@ import type { HeaderPrep, RefSearchNav } from '../types.ts'
const props = defineProps<{ const props = defineProps<{
prep: HeaderPrep prep: HeaderPrep
refSearchNav: RefSearchNav refSearchNav?: RefSearchNav
}>() }>()
function emitSearchNavClick() { function emitSearchNavClick() {
props.refSearchNav.onClick() props.refSearchNav?.onClick()
} }
function onInput(event: Event) { function onInput(event: Event) {
props.refSearchNav.onInput((event.target as HTMLInputElement).value) props.refSearchNav?.onInput((event.target as HTMLInputElement).value)
} }
function btnClick() { function btnClick() {
props.prep.addNav?.onClick() props.prep?.addNav?.onClick()
} }
</script> </script>
@@ -24,12 +24,13 @@ function btnClick() {
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
<div class="flex items-center"> <div class="flex items-center">
<div class="ml-3 text-lg font-bold text-gray-900"> <div class="ml-3 text-lg font-bold text-gray-900">
<Icon :name="prep.icon" class="mr-2 h-4 w-4 align-middle" /> <Icon :name="prep.icon" class="mr-2 size-4 md:size-6 align-middle" />
{{ prep.title }} {{ prep.title }}
</div> </div>
</div> </div>
<div class="flex items-center"> <div class="flex items-center">
<div class="ml-3 text-lg text-gray-900"> <div v-if="props.refSearchNav"
class="ml-3 text-lg text-gray-900">
<Input <Input
type="text" type="text"
placeholder="Search" placeholder="Search"