Skip to content

Commit

Permalink
wip: Refactor collections based components #917
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Oct 30, 2024
1 parent 112e923 commit 56a89e8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 190 deletions.
17 changes: 15 additions & 2 deletions core/client/components/collection/KGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!--
Content
-->
<div v-if="items.length > 0"
<div v-if="items && items.length > 0"
id="grid-content"
ref="contentRef"
class="col scroll"
Expand Down Expand Up @@ -62,7 +62,9 @@
</div>
</div>
<!-- Empty slot -->
<div v-else id="grid-content">
<div v-else-if="items && items.length === 0"
id="grid-empty"
>
<slot name="empty">
<div class="row justify-center">
<KStamp
Expand All @@ -75,6 +77,17 @@
</div>
</slot>
</div>
<!-- Initializing slot -->
<div v-else id="grid-initializing">
<slot name="initializing">
<div class="row justify-center">
<q-spinner
color="primary"
size="2rem"
/>
</div>
</slot>
</div>
<!--
Controls
-->
Expand Down
64 changes: 42 additions & 22 deletions core/client/components/collection/KTable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div v-if="items.length > 0">
<!-- Content -->
<div v-if="items && items.length > 0">
<q-table
:title="title"
:rows="items"
Expand Down Expand Up @@ -52,12 +53,32 @@
</template>
</q-table>
</div>
<div v-else>
<div slot="empty-section">
<!-- Empty slot -->
<div v-else-if="items && items.length === 0"
id="table-empty"
>
<slot name="empty">
<div class="row justify-center">
<KStamp icon="las la-exclamation-circle" icon-size="1.6rem" :text="$t('KTable.EMPTY_TABLE')" direction="horizontal" />
<KStamp
icon="las la-exclamation-circle"
icon-size="1.6rem"
:text="$t('KTable.EMPTY_TABLE')"
direction="horizontal"
class="q-pa-md"
/>
</div>
</div>
</slot>
</div>
<!-- Initializing slot -->
<div v-else id="grid-initializing">
<slot name="initializing">
<div class="row justify-center">
<q-spinner
color="primary"
size="2rem"
/>
</div>
</slot>
</div>
</template>

Expand Down Expand Up @@ -158,6 +179,20 @@ const { schema, compile } = useSchema()
const options = Object.assign({ filterQuery }, _.omit(toRefs(props), ['filterQuery']))
const { items, nbTotalItems, nbPages, currentPage, refreshCollection, resetCollection } = useCollection(options)
// Watch
watch(items, onCollectionRefreshed)
watch(schema, () => {
processSchema()
resetCollection()
})
watch(() => props.schema, async (value) => {
await compile(props.schema || `${props.service}.get`)
})
watch(nbTotalItems, () => {
// Update pagination for table
pagination.value.rowsNumber = nbTotalItems.value
})
// Functions
function processSchema () {
_.forOwn(schema.value.properties, (value, key) => {
Expand Down Expand Up @@ -227,29 +262,14 @@ function onCollectionRefreshed () {
emit('collection-refreshed', items.value)
}
// Lifecycle hooks
// Emit events so that embbeding components can be aware of it
watch(items, onCollectionRefreshed)
watch(schema, () => {
processSchema()
resetCollection()
})
watch(() => props.schema, async (value) => {
await compile(props.schema || `${props.service}.get`)
})
watch(nbTotalItems, () => {
// Update pagination for table
pagination.value.rowsNumber = nbTotalItems.value
})
// Hooks
onBeforeMount(async () => {
// This will launch collection refresh
await compile(props.schema || `${props.service}.get`)
refreshCollection()
// Whenever the user abilities are updated, update collection as well
Events.on('user-abilities-changed', refreshCollection)
})
onBeforeUnmount(() => {
Events.off('user-abilities-changed', refreshCollection)
})
Expand Down
18 changes: 16 additions & 2 deletions core/client/components/collection/KTimeLine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!--
Content
-->
<div v-if="items.length > 0"
<div v-if="items && items.length > 0"
id="timeline-content"
ref="contentRef"
class="col scroll"
Expand Down Expand Up @@ -97,7 +97,9 @@
</q-timeline>
</div>
<!-- Empty slot -->
<div v-else id="timeline-content">
<div v-else-if="items && items.length === 0"
id="timeline-content"
>
<slot name="empty">
<KStamp
icon="las la-exclamation-circle"
Expand All @@ -109,6 +111,18 @@
/>
</slot>
</div>
<!-- Initializing slot -->
<div v-else id="timeline-initializing">
<slot name="initializing">
<div class="row justify-center">
<q-spinner
color="primary"
size="4rem"
class="absolute-center"
/>
</div>
</slot>
</div>
<!--
Controls
-->
Expand Down
4 changes: 3 additions & 1 deletion core/client/composables/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useCollection (options) {
})

// Data
const items = ref([])
const items = ref(null)
const nbTotalItems = ref(0)
const currentPage = ref(1)
let itemListener = null
Expand Down Expand Up @@ -118,6 +118,8 @@ export function useCollection (options) {
}, options.refreshThrottle.value, { leading: false })

function resetCollection () {
// Do not reset the collection since it is initializing
if (_.isNil(items.value)) return
// Reset pagination and start again refreshing the collection
if (options.appendItems.value) setCollectionItems([])
currentPage.value = 1
Expand Down
1 change: 0 additions & 1 deletion core/client/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './mixin.base-context.js'
export * from './mixin.base-activity.js'
export * from './mixin.base-collection.js'
export * from './mixin.base-editor.js'
export * from './mixin.base-item.js'
export * from './mixin.base-field.js'
Expand Down
162 changes: 0 additions & 162 deletions core/client/mixins/mixin.base-collection.js

This file was deleted.

0 comments on commit 56a89e8

Please sign in to comment.