Skip to content

Commit

Permalink
subject select table
Browse files Browse the repository at this point in the history
  • Loading branch information
volar committed Oct 19, 2023
1 parent d70eb77 commit 3491bfe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/components/subjectSelect/ASubjectSelectTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const props = withDefaults(
pagination: Pagination
submitFilter: Fn
resetFilter: Fn
loading?: boolean
minCount?: number
maxCount?: number
modelValue?: boolean | undefined // dialog
dialogTitleT?: string
paginationMode?: 'standard' | 'lazyLoad'
paginationMode?: 'standard' | 'more'
}>(),
{
loading: false,
minCount: 1,
maxCount: 1,
modelValue: undefined,
Expand Down Expand Up @@ -56,7 +58,6 @@ const paginationComputed = computed({
})
const sidebarLeft = ref(true)
const loading = ref(false)
const filterTouched = ref(false)
const { t } = useI18n()
Expand Down Expand Up @@ -111,7 +112,7 @@ const hasNextPage = computed(() => {
})
const autoloadOnIntersect = (isIntersecting: boolean) => {
if (isIntersecting && hasNextPage.value && !loading.value) {
if (isIntersecting && hasNextPage.value && !props.loading) {
fetchNextPage()
}
}
Expand Down Expand Up @@ -228,7 +229,7 @@ defineExpose({
<div class="subject-select__content">
<slot name="content" />
<div
v-if="paginationMode === 'lazyLoad'"
v-if="paginationMode === 'more'"
class="d-flex w-100 align-center justify-center pa-4"
>
<ABtnSecondary
Expand Down
24 changes: 21 additions & 3 deletions src/components/subjectSelect/useSubjectSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ref } from 'vue'
import type { Pagination } from '@/types/Pagination'
import type { FilterBag } from '@/types/Filter'
import { useFilterHelpers } from '@/composables/filter/filterHelpers'
import { useAlerts } from '@/composables/system/alerts'

export function useSubjectSelect<TItem>(
datatableConfig: any,
Expand All @@ -18,8 +19,10 @@ export function useSubjectSelect<TItem>(
const filterTouched: Ref<boolean> = ref(false)
const items: Ref<Array<TItem>> = ref([])
const selected: Ref<Array<TItem>> = ref([])
const loading = ref(false)

const { resetFilter, submitFilter } = useFilterHelpers()
const { showErrorsDefault } = useAlerts()

const { columnsVisible, pagination, updateSortBy, columnsAll, columnsHidden } = createDatatableColumnsConfig(
datatableConfig,
Expand Down Expand Up @@ -51,8 +54,15 @@ export function useSubjectSelect<TItem>(
}

const onFetchNextPage = async () => {
loading.value = true
pagination.page++
items.value.push(...(await fetch(pagination, filter)))
try {
items.value.push(...(await fetch(pagination, filter)))
} catch (e) {
showErrorsDefault(e)
} finally {
loading.value = false
}
}

const resetState = () => {
Expand All @@ -61,10 +71,17 @@ export function useSubjectSelect<TItem>(
}

const getList = async () => {
items.value = await fetch(pagination, filter)
loading.value = true
try {
items.value = await fetch(pagination, filter)
} catch (e) {
showErrorsDefault(e)
} finally {
loading.value = false
}
}

const onRowClick = (event: PointerEvent) => {
const onRowClick = (event: Event) => {
const eventTarget = event.target as HTMLElement | null
if (!eventTarget) return
const parent = eventTarget.parentElement
Expand All @@ -84,6 +101,7 @@ export function useSubjectSelect<TItem>(
columnsHidden,
columnsAll,
filterTouched,
loading,
onRowClick,
onFetchNextPage,
customToggleSelect,
Expand Down
4 changes: 3 additions & 1 deletion src/playground/subjectSelectView/PollSelectTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ADatatableOrdering from '@/components/ADatatableOrdering.vue'
import ADatatableConfigButton from '@/components/ADatatableConfigButton.vue'
import { toRef } from 'vue'
import ADatetime from '@/components/ADatetime.vue'
import ATableCopyIdButton from '@/components/buttons/table/ATableCopyIdButton.vue'
import { usePollSelectStore } from '@/playground/subjectSelectView/pollSelectStore'
import PollSelectFilter from '@/playground/subjectSelectView/PollSelectFilter.vue'
import { generateSelectStrategy } from '@/components/subjectSelect/selectStrategies'
Expand Down Expand Up @@ -44,6 +43,7 @@ const {
sortByChange,
getList,
onRowClick,
loading,
} = useSubjectSelect<PollDemo>(
[
{ key: 'id' },
Expand Down Expand Up @@ -76,6 +76,7 @@ const onConfirm = (items: Array<PollDemo>) => {
:selected-items="selected"
:submit-filter="submitFilter"
:reset-filter="resetFilter"
:loading="loading"
@on-open="onOpen"
@on-fetch-next-page="onFetchNextPage"
@on-page-change="getList"
Expand Down Expand Up @@ -110,6 +111,7 @@ const onConfirm = (items: Array<PollDemo>) => {
:headers="columnsVisible"
:items="items"
:items-length="items.length"
:loading="loading"
item-value="id"
return-object
:select-strategy="generateSelectStrategy(minCount, maxCount) as any"
Expand Down

0 comments on commit 3491bfe

Please sign in to comment.