-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entities-shared): add useTableState (#1858)
* feat(entities-shared): add useTableState * fix(entities-shared): fix typo
- Loading branch information
Showing
4 changed files
with
63 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/entities/entities-shared/src/composables/useTableState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { toValue, ref, computed } from 'vue' | ||
import type { Ref } from 'vue' | ||
import type { TableStateParams } from '../types' | ||
|
||
// This composable is used to access the table data state and provide shared logic | ||
// for hiding toolbars, etc. | ||
export default function useTableState(query?: Ref<string> | (() => string)) { | ||
let previousQuery = '' | ||
|
||
const hasRecords = ref(false) | ||
const tableState = ref<TableStateParams | null>(null) | ||
|
||
const hideTableToolbar = computed(() => { | ||
let showTableToolbar | ||
|
||
if (hasRecords.value) { | ||
showTableToolbar = true | ||
} else if (!tableState.value) { | ||
// Initial state, hide the toolbar | ||
showTableToolbar = false | ||
} else { | ||
showTableToolbar = tableState.value.hasData || (query && toValue(query)) | ||
} | ||
|
||
return !showTableToolbar | ||
}) | ||
|
||
const handleStateChange = (stateParams: TableStateParams): void => { | ||
if (stateParams.hasData) { | ||
// In our scenario, as long as the table contains any data at any time, | ||
// it indicates that there is at least a corresponding entity record in the backend. | ||
hasRecords.value = true | ||
} else if (stateParams.state === 'success' && !stateParams.hasData && (!query || !previousQuery)) { | ||
// If the table is in a success state but has no data and no query, it means there are no records | ||
// Why do we record the previous query: | ||
// When we clear the query, the table `state` event will be emitted in the following order: | ||
// - Immediately: { state: 'success', hasData: <from-cache> }, query: '' | ||
// - After revalidation: { state: 'success', hasData: <from-backend> }, query: '' <- This is the one we want to capture | ||
// So we'll have to record the previous query to reset `hasRecords` correctly after revalidation | ||
// - Immediately: { state: 'success', hasData: <from-cache> }, previousQuery: 'foo', query: '' <- just check previous query | ||
// - After revalidation: { state: 'success', hasData: <from-backend> }, previousQuery: '', query: '' | ||
hasRecords.value = false | ||
} | ||
|
||
if (query) { | ||
previousQuery = toValue(query) | ||
} | ||
|
||
tableState.value = { ...stateParams } | ||
} | ||
|
||
return { | ||
tableState, | ||
hasRecords, | ||
hideTableToolbar, | ||
handleStateChange, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters