Skip to content

Commit

Permalink
refactor: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Apr 26, 2024
1 parent 1385aa4 commit be2f2fb
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 45 deletions.
76 changes: 76 additions & 0 deletions packages/ui/src/components/MagnetarFiltersCodeRepresentation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<script setup lang="ts">
import { arrStr } from '@magnetarjs/utils'
import { isArray } from 'is-what'
import { computed } from 'vue'
import { FilterState, FiltersState, MUIFilter, OPaths, OrderByState } from '../types'
import { filterStateToClauses, orderByStateToClauses } from '../utils/tableHelpers'
const props = defineProps<{
labels: {
'magnetar table no active filters': string
}
hasSomeFilterOrOrderby: boolean
filters?: MUIFilter<any, any>[]
filtersState: FiltersState
orderByState: OrderByState
}>()
const emit = defineEmits<{
(
e: 'setFilter',
payload: {
filterIndex: number
payload: null | FilterState
}
): void
(
e: 'setOrderBy',
payload: {
fieldPath: OPaths<any>
direction: 'asc' | 'desc' | null
clearOtherOrderBy?: boolean
}
): void
}>()
const currentFilters = computed(() => filterStateToClauses(props.filtersState, props.filters ?? []))
const currentOrderBy = computed(() => orderByStateToClauses(props.orderByState))
</script>

<template>
<div class="magnetar-row magnetar-gap-sm">
<div v-if="!hasSomeFilterOrOrderby">{{ labels['magnetar table no active filters'] }}</div>
<div v-for="info in currentFilters" :key="info.filterIndex" class="magnetar-filter-code">
{{
isArray(info.result)
? info.result.map((where) => `.where(${arrStr(where)})`).join('')
: `.query(${JSON.stringify(info.result)})`
}}
<button @click="() => emit('setFilter', { filterIndex: info.filterIndex, payload: null })">
</button>
</div>
<div
v-for="_orderBy in currentOrderBy"
:key="JSON.stringify(_orderBy)"
class="magnetar-filter-code"
>
.orderBy({{ arrStr(_orderBy) }})
<button @click="() => emit('setOrderBy', { fieldPath: _orderBy[0], direction: null })">
</button>
</div>
</div>
</template>

<style lang="sass" src="./styles.sass" />
<style lang="sass" scoped>
.magnetar-filter-code
background: var(--c-primary-extra-light, whitesmoke)
border-radius: 0.25rem
padding: 0.25rem 0.5rem
display: flex
align-items: center
gap: 0.25rem
</style>
82 changes: 37 additions & 45 deletions packages/ui/src/components/MagnetarTable.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script setup lang="ts">
import type { CollectionInstance } from '@magnetarjs/types'
import { arrStr } from '@magnetarjs/utils'
import { useElementSize } from '@vueuse/core'
import { isAnyObject, isArray, isError, isPlainObject } from 'is-what'
import { isAnyObject, isError, isPlainObject } from 'is-what'
import { computed, nextTick, onMounted, ref, watch } from 'vue'
import {
FilterState,
Expand All @@ -19,18 +18,17 @@ import {
import {
calcCollection,
carbonCopyMap,
filterStateToClauses,
filtersAndColumnsToInitialState,
getRequiredOrderByBasedOnFilters,
mapUnshift,
orderByStateToClauses,
} from '../utils/tableHelpers'
import LoadingSpinner from './LoadingSpinner.vue'
import TableDataInfo from './TableDataInfo.vue'
import TableFilter from './TableFilter.vue'
import TablePagination from './TablePagination.vue'
import TableTh from './TableTh.vue'
import TableTr from './TableTr.vue'
import TextWithAnchorTags from './TextWithAnchorTags.vue'
import MagnetarFiltersCodeRepresentation from './MagnetarFiltersCodeRepresentation.vue'
const props = defineProps<{
collection: CollectionInstance<any>
Expand Down Expand Up @@ -113,9 +111,6 @@ watch(
{ deep: true }
)
const currentFilters = computed(() => filterStateToClauses(filtersState.value, props.filters ?? []))
const currentOrderBy = computed(() => orderByStateToClauses(orderByState.value))
/**
* This will always be true in case there was no initialState
*/
Expand Down Expand Up @@ -312,6 +307,20 @@ watch(pageIndex, async (newIndex) => {
}
})
// prettier-ignore
const labelsFiltersCodeRepresentation = computed(() => ({
'magnetar table no active filters': muiLabel('magnetar table no active filters'),
}))
// prettier-ignore
const labelsDataInfo = computed(() => ({
'magnetar table record counts': muiLabel('magnetar table record counts'),
'magnetar table info counts total': muiLabel('magnetar table info counts total'),
'magnetar table info counts filtered': muiLabel('magnetar table info counts filtered'),
'magnetar table info counts fetched': muiLabel('magnetar table info counts fetched'),
'magnetar table info counts showing': muiLabel('magnetar table info counts showing'),
}))
// prettier-ignore
const labelsPagination = computed(() => ({
'magnetar table fetch-more button end': muiLabel('magnetar table fetch-more button end'),
Expand Down Expand Up @@ -362,25 +371,19 @@ const debugMode = !!localStorage.getItem('DEBUG')
/>
</div>
<div v-if="showingFiltersCode" class="magnetar-row magnetar-gap-sm magnetar-active-filters">
<div v-if="!hasSomeFilterOrOrderby">{{ muiLabel('magnetar table no active filters') }}</div>
<div v-for="info in currentFilters" :key="info.filterIndex" class="magnetar-filter-code">
{{
isArray(info.result)
? info.result.map((where) => `.where(${arrStr(where)})`).join('')
: `.query(${JSON.stringify(info.result)})`
}}
<button @click="() => setFilter(info.filterIndex, null)">✕</button>
</div>
<div
v-for="_orderBy in currentOrderBy"
:key="JSON.stringify(_orderBy)"
class="magnetar-filter-code"
>
.orderBy({{ arrStr(_orderBy) }})
<button @click="() => setOrderBy(_orderBy[0], null)">✕</button>
</div>
</div>
<MagnetarFiltersCodeRepresentation
v-if="showingFiltersCode"
:labels="labelsFiltersCodeRepresentation"
:hasSomeFilterOrOrderby="hasSomeFilterOrOrderby"
:filters="filters"
:filtersState="filtersState"
:orderByState="orderByState"
@setFilter="({ filterIndex, payload }) => setFilter(filterIndex, payload)"
@setOrderBy="
({ fieldPath, direction, clearOtherOrderBy }) =>
setOrderBy(fieldPath, direction, clearOtherOrderBy)
"
/>
</section>
<TextWithAnchorTags
Expand All @@ -389,15 +392,13 @@ const debugMode = !!localStorage.getItem('DEBUG')
:text="fetchState.error"
/>
<section class="magnetar-column magnetar-gap-sm magnetar-items-end">
<h6>{{ muiLabel('magnetar table record counts') }}</h6>
<div class="magnetar-row magnetar-gap-md">
<LoadingSpinner v-if="fetchState === 'fetching'" />
{{ collection.count }} {{ muiLabel('magnetar table info counts total') }} /
{{ collectionInstance.count }} {{ muiLabel('magnetar table info counts filtered') }} /
{{ rows.length }} {{ muiLabel('magnetar table info counts showing') }}
</div>
</section>
<TableDataInfo
:labels="labelsDataInfo"
:fetchState="fetchState"
:collection="collection"
:collectionInstance="collectionInstance"
:rows="rows"
/>
<table ref="tableEl" :style="`min-height: ${minH}px; min-width: ${minW}px`">
<thead>
Expand Down Expand Up @@ -466,17 +467,8 @@ const debugMode = !!localStorage.getItem('DEBUG')
.magnetar-table
h6
margin: 0
.magnetar-table-info
min-height: 26px
.magnetar-fetch-state-error
color: var(--c-error, indianred)
.magnetar-filter-code
background: var(--c-primary-extra-light, whitesmoke)
border-radius: 0.25rem
padding: 0.25rem 0.5rem
display: flex
align-items: center
gap: 0.25rem
.magnetar-table-controls
display: flex
align-items: center
Expand Down
35 changes: 35 additions & 0 deletions packages/ui/src/components/TableDataInfo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script setup lang="ts">
import LoadingSpinner from './LoadingSpinner.vue'
import type { CollectionInstance } from '@magnetarjs/types'
const props = defineProps<{
labels: {
'magnetar table record counts': string
'magnetar table info counts total': string
'magnetar table info counts filtered': string
'magnetar table info counts fetched': string
'magnetar table info counts showing': string
}
fetchState: 'ok' | 'end' | 'fetching' | { error: string }
collection: CollectionInstance<any>
collectionInstance: CollectionInstance<any>
rows: any[]
}>()
</script>

<template>
<section class="magnetar-column magnetar-gap-sm magnetar-items-end">
<h6>{{ labels['magnetar table record counts'] }}</h6>
<div class="magnetar-row magnetar-gap-md">
<LoadingSpinner v-if="fetchState === 'fetching'" />
{{ `${collection.count} ${labels['magnetar table info counts total']} / ` }}
{{ `${collectionInstance.count} ${labels['magnetar table info counts filtered']} / ` }}
<template v-if="collectionInstance.data.size !== rows.length">{{
`${collectionInstance.data.size} ${labels['magnetar table info counts fetched']} / `
}}</template>
{{ `${rows.length} ${labels['magnetar table info counts showing']}` }}
</div>
</section>
</template>

<style lang="sass" src="./styles.sass" />
2 changes: 2 additions & 0 deletions packages/ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ export type MUILabel =
| 'magnetar table fetch-state error default'
| 'magnetar table info counts total'
| 'magnetar table info counts filtered'
| 'magnetar table info counts fetched'
| 'magnetar table info counts showing'
| 'magnetar table fetch-state reset'
| 'magnetar table filters'
Expand All @@ -513,6 +514,7 @@ export const muiLabelDic: Record<MUILabel, string> = {
'magnetar table fetch-state error default': 'An error occured, check the console',
'magnetar table info counts total': 'total',
'magnetar table info counts filtered': 'filtered',
'magnetar table info counts fetched': 'fetched',
'magnetar table info counts showing': 'showing',
'magnetar table fetch-state reset': 'Reset to Defaults',
'magnetar table filters': 'Filters',
Expand Down
1 change: 1 addition & 0 deletions packages/ui/testApp/TestTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const magnetarLabelDic = {
'magnetar table fetch-state error default': 'エラーが出ました',
'magnetar table info counts total': '件 全レコード数',
'magnetar table info counts filtered': '件 有効フィルター',
'magnetar table info counts fetched': '件 取得済み',
'magnetar table info counts showing': '件 表示中',
'magnetar table fetch-state reset': 'フィルターを初期値に戻す',
'magnetar table filters': 'フィルター',
Expand Down
1 change: 1 addition & 0 deletions packages/ui/testApp/TestTableManualFilters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const magnetarLabelDic = {
'magnetar table fetch-state error default': 'エラーが出ました',
'magnetar table info counts total': '件 全レコード数',
'magnetar table info counts filtered': '件 有効フィルター',
'magnetar table info counts fetched': '件 取得済み',
'magnetar table info counts showing': '件 表示中',
'magnetar table fetch-state reset': 'フィルターを初期値に戻す',
'magnetar table filters': 'フィルター',
Expand Down

0 comments on commit be2f2fb

Please sign in to comment.