Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add record filter preset saving #1227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 34 additions & 15 deletions client/web/compose/src/components/Common/RecordListFilter.vue
Expand Up @@ -215,13 +215,23 @@
{{ $t('general:label.reset') }}
</b-button>

<b-button
ref="btnSave"
variant="primary"
@click="onSave"
>
{{ $t('general.label.save') }}
</b-button>
<div class="d-flex">
<b-button
v-if="allowFilterPresetSave"
variant="outline-primary"
class="mr-2"
@click="onSave(true, 'filter-preset')"
>
{{ $t('recordList.filter.addFilterToPreset') }}
</b-button>
<b-button
ref="btnSave"
variant="primary"
@click="onSave"
>
{{ $t('general.label.save') }}
</b-button>
</div>
</b-card-footer>
</b-card>

Expand Down Expand Up @@ -288,6 +298,11 @@ export default {
type: String,
default: '',
},

allowFilterPresetSave: {
type: Boolean,
default: false,
},
},

data () {
Expand Down Expand Up @@ -592,13 +607,8 @@ export default {
}
},

onSave (close = true) {
if (close) {
this.$refs.popover.$emit('close')
}

// Emit only value and not whole record with every filter
this.$emit('filter', this.componentFilter.map(({ groupCondition, filter = [], name }) => {
processFilter () {
return this.componentFilter.map(({ groupCondition, filter = [], name }) => {
filter = filter.map(({ record, ...f }) => {
if (record) {
f.value = record[f.name] || record.values[f.name]
Expand All @@ -615,7 +625,16 @@ export default {
})

return { groupCondition, filter, name }
}))
})
},

onSave (close = true, type = 'filter') {
if (close) {
this.$refs.popover.$emit('close')
}

// Emit only value and not whole record with every filter
this.$emit(type, this.processFilter())
},

updateFilterProperties (filter) {
Expand Down
135 changes: 111 additions & 24 deletions client/web/compose/src/components/PageBlocks/RecordListBase.vue
Expand Up @@ -78,22 +78,32 @@
@export="onExport"
/>

<b-dropdown
v-if="filterPresets.length"
size="lg"
variant="light"
right
:text="$t('recordList.filter.filters.label')"
<b-button
:id="`${uniqueID}-filter-preset`"
class="btn dropdown-toggle btn-light btn-lg"
>
<b-dropdown-item
v-for="(f, idx) in filterPresets"
:key="idx"
:disabled="activeFilters.includes(f.name)"
@click="updateFilter(f.filter, f.name)"
>
{{ f.name }}
</b-dropdown-item>
</b-dropdown>
{{ $t('recordList.filter.filters.label') }}
</b-button>
<b-popover
:target="`${uniqueID}-filter-preset`"
triggers="click blur"
placement="bottom"
>
<ul class="list-style-none">
<li
v-for="(f, idx) in filterPresets"
:key="idx"
>
<button
class="dropdown-item"
:disabled="activeFilters.includes(f.name)"
@click="updateFilter(f.filter, f.name)"
>
{{ f.name }}
</button>
</li>
</ul>
</b-popover>

<column-picker
v-if="!options.hideConfigureFieldsButton"
Expand Down Expand Up @@ -293,8 +303,10 @@
:namespace="namespace"
:module="recordListModule"
:record-list-filter="recordListFilter"
:allow-filter-preset-save="options.customFilterPresets"
class="d-print-none ml-1"
@filter="onFilter"
@filter-preset="onSaveFilterPreset"
@reset="activeFilters = []"
/>

Expand Down Expand Up @@ -609,6 +621,13 @@
open-on-select
@save="onInlineEdit()"
/>

<!-- Modal for naming custom filter -->
<custom-filter-preset
:visible="showCustomPresetFilterModal"
@save="setStorageRecordListFilterPreset"
@close="showCustomPresetFilterModal = false"
/>
</template>

<template
Expand Down Expand Up @@ -733,6 +752,7 @@ import draggable from 'vuedraggable'
import RecordListFilter from 'corteza-webapp-compose/src/components/Common/RecordListFilter'
import ColumnPicker from 'corteza-webapp-compose/src/components/Admin/Module/Records/ColumnPicker'
import BulkEditModal from 'corteza-webapp-compose/src/components/Public/Record/BulkEdit'
import CustomFilterPreset from 'corteza-webapp-compose/src/components/Public/Record/CustomFilterPreset'

const { CInputSearch } = components

Expand All @@ -752,6 +772,7 @@ export default {
ColumnPicker,
CInputSearch,
BulkEditModal,
CustomFilterPreset,
},

extends: base,
Expand Down Expand Up @@ -815,6 +836,9 @@ export default {
items: [],
showingDeletedRecords: false,
activeFilters: [],
customPresetFilters: [],
currentCustomPresetFilter: undefined,
showCustomPresetFilterModal: false,
}
},

Expand Down Expand Up @@ -982,7 +1006,10 @@ export default {
},

filterPresets () {
return this.options.filterPresets.filter(({ name, roles }) => name && this.isUserRoleMember(roles))
return [
...this.options.filterPresets.filter(({ name, roles }) => name && this.isUserRoleMember(roles)),
...this.customPresetFilters,
]
},

authUserRoles () {
Expand All @@ -1008,6 +1035,7 @@ export default {
handler () {
this.createEvents()
this.getStorageRecordListFilter()
this.getStorageRecordListFilterPreset()
this.prepRecordList()
this.refresh(true)
},
Expand Down Expand Up @@ -1064,12 +1092,24 @@ export default {
onFilter (filter = []) {
filter.forEach(f => {
if (this.activeFilters.includes(f.name)) {
this.filterPresets.find(p => p.name === f.name).filter.forEach((filterPreset) => {
if (!isEqual(f.filter, filterPreset.filter)) {
const filterIndex = this.activeFilters.indexOf(f.name)
this.activeFilters.splice(filterIndex, 1)
}
})
const filterPresets = this.filterPresets.find(p => p.name === f.name)

if (filterPresets) {
filterPresets.filter.forEach((filterPreset) => {
if (!isEqual(f.filter, filterPreset.filter)) {
const filterIndex = this.activeFilters.indexOf(f.name)
this.activeFilters.splice(filterIndex, 1)
}
})
}
}

if (f.filter.length === 1 && (!f.filter[0].value && !f.filter[0].name)) {
const filterIndex = this.activeFilters.indexOf(f.name)
this.activeFilters.splice(filterIndex, 1)
} else {
this.activeFilters.push(this.$t('recordList.customFilter'))
f.name = this.$t('recordList.customFilter')
}
})

Expand All @@ -1078,6 +1118,15 @@ export default {
this.refresh(true)
},

onSaveFilterPreset (filter = []) {
this.currentCustomPresetFilter = {
filter,
}

this.showCustomPresetFilterModal = true
this.refresh(true)
Fajfa marked this conversation as resolved.
Show resolved Hide resolved
},

onUpdateFields (fields = []) {
this.options.fields = [...fields]
this.$emit('save-fields', this.options.fields)
Expand Down Expand Up @@ -1597,6 +1646,7 @@ export default {
removeItem(`record-list-filters-${this.uniqueID}`)
} else {
this.recordListFilter = currentFilters
this.activeFilters = currentFilters.map(f => f.name)
}
} catch (e) {
// Land here if the filter is corrupted
Expand All @@ -1606,6 +1656,21 @@ export default {
}
},

getStorageRecordListFilterPreset () {
try {
// Get record list filters from localStorage
const currentFilterPresets = getItem(`record-list-preset-${this.uniqueID}`)

// Set the custom preset filters
this.customPresetFilters = currentFilterPresets
} catch (e) {
// Land here if the filter is corrupted
console.warn(this.$t('notification:record-list.corrupted-filter'))
// Remove filter from the local storage
removeItem(`record-list-filters-${this.uniqueID}`)
}
},

setStorageRecordListFilter () {
let currentListFilters = []

Expand All @@ -1615,7 +1680,23 @@ export default {
currentListFilters = this.recordListFilter
setItem(`record-list-filters-${this.uniqueID}`, currentListFilters)
} catch (e) {
console.warning(this.$t('notification:record-list.corrupted-filter'))
console.warn(this.$t('notification:record-list.corrupted-filter'))
}
},

setStorageRecordListFilterPreset ({ name }) {
this.showCustomPresetFilterModal = false

const currentListFilters = [...this.customPresetFilters]
currentListFilters.push({ ...this.currentCustomPresetFilter, name })

this.customPresetFilters = currentListFilters
this.updateFilter(this.currentCustomPresetFilter.filter, name)

try {
setItem(`record-list-preset-${this.uniqueID}`, currentListFilters)
} catch (e) {
console.warn(this.$t('notification:record-list.corrupted-filter'))
}
},

Expand Down Expand Up @@ -1714,7 +1795,7 @@ export default {
return !expressions.value
},

updateFilter (filter, name) {
updateFilter (filter = [], name) {
const lastFilterIdx = this.recordListFilter.length - 1
filter = filter.map((filter) => ({ ...filter, name }))

Expand Down Expand Up @@ -1800,4 +1881,10 @@ td:hover .inline-actions {
font-family: $font-regular !important;
}
}

.list-style-none {
list-style: none;
margin: 0;
padding: 0;
}
</style>
Expand Up @@ -347,6 +347,21 @@
{{ $t('recordList.filter.addFilter') }}
</b-button>
</b-form-group>

<b-row class="mb-3">
<b-col>
<b-form-group
:label="$t('recordList.record.setCustomFilterPresets')"
label-class="text-primary"
>
<c-input-checkbox
v-model="options.customFilterPresets"
switch
:labels="checkboxLabel"
/>
</b-form-group>
</b-col>
</b-row>
</b-col>
</b-row>
</div>
Expand Down