|
| 1 | +import _ from 'lodash' |
| 2 | +import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue' |
| 3 | +import { getCollectionService, getLatestTime, getOldestTime, listenToServiceEvents, unlistenToServiceEvents } from '../utils/index.js' |
| 4 | +import { useCurrentActivity } from './activity.js' |
| 5 | + |
| 6 | +export function useCollectionFilter (options = {}) { |
| 7 | + // Data |
| 8 | + const { CurrentActivityContext } = useCurrentActivity() |
| 9 | + const tagsFilter = CurrentActivityContext.state.tagsFilter |
| 10 | + const timeFilter = CurrentActivityContext.state.timeFilter |
| 11 | + const filterQuery = ref({}) |
| 12 | + let listeners |
| 13 | + |
| 14 | + // Computed |
| 15 | + const selectedTags = computed(() => { |
| 16 | + return _.get(tagsFilter, 'selection', []) |
| 17 | + }) |
| 18 | + const hasTagsSelection = computed(() => { |
| 19 | + return !_.isEmpty(selectedTags.value) |
| 20 | + }) |
| 21 | + const selectedTime = computed(() => { |
| 22 | + return _.cloneDeep(timeFilter) |
| 23 | + }) |
| 24 | + const hasTimeSelection = computed(() => { |
| 25 | + const start = _.get(timeFilter, 'start') |
| 26 | + const end = _.get(timeFilter, 'end') |
| 27 | + return start && end && start !== end |
| 28 | + }) |
| 29 | + |
| 30 | + // Watch |
| 31 | + watch(selectedTags, () => refreshFilterQuery()) |
| 32 | + watch(selectedTime, (newTimeRange, oldTimeRange) => { |
| 33 | + if (oldTimeRange && oldTimeRange.start !== null && oldTimeRange.end !== null) refreshFilterQuery() |
| 34 | + }, { deep: true }) |
| 35 | + |
| 36 | + // Functions |
| 37 | + function getService () { |
| 38 | + return getCollectionService(options.service.value, options.context ? options.context.value : null) |
| 39 | + } |
| 40 | + function getBaseQuery () { |
| 41 | + return _.get(options, 'baseQuery.value', {}) |
| 42 | + } |
| 43 | + function getTimeField () { |
| 44 | + return _.get(options, 'timeField.value', 'createdAt') |
| 45 | + } |
| 46 | + function getTagFields () { |
| 47 | + return _.get(options, 'tagFields', []) |
| 48 | + } |
| 49 | + async function refreshOptions () { |
| 50 | + const timeField = getTimeField() |
| 51 | + // update time filter |
| 52 | + const min = await getOldestTime(getService(), timeField, getBaseQuery()) |
| 53 | + if (min) _.set(timeFilter, 'min', min) |
| 54 | + const max = await getLatestTime(getService(), timeField, getBaseQuery()) |
| 55 | + if (max) _.set(timeFilter, 'max', max) |
| 56 | + // update tags filter |
| 57 | + const tagFields = getTagFields() |
| 58 | + let tagsOptions = [] |
| 59 | + _.forEach(tagFields, (values, property) => { |
| 60 | + const propertyOptions = _.map(values, (state, key) => { |
| 61 | + return _.merge({ scope: property, name: key }, state) |
| 62 | + }) |
| 63 | + if (_.size(propertyOptions) > 1) tagsOptions = _.concat(tagsOptions, propertyOptions) |
| 64 | + }) |
| 65 | + _.set(tagsFilter, 'options', tagsOptions) |
| 66 | + } |
| 67 | + function refreshFilterQuery () { |
| 68 | + const query = {} |
| 69 | + // filter against the selected tags |
| 70 | + if (hasTagsSelection.value) { |
| 71 | + const tagFields = getTagFields() |
| 72 | + _.forEach(tagFields, (values, property) => { |
| 73 | + const tagsProperty = _.map(_.filter(selectedTags.value, { scope: property }), tag => { return tag.name }) |
| 74 | + if (!_.isEmpty(tagsProperty)) { |
| 75 | + _.merge(query, { [property]: { $in: tagsProperty } }) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | + // filter against the selected time range |
| 80 | + if (hasTimeSelection.value) { |
| 81 | + _.merge(query, { |
| 82 | + [getTimeField()]: { |
| 83 | + $gte: selectedTime.value.start, |
| 84 | + $lte: selectedTime.value.end |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + filterQuery.value = query |
| 89 | + } |
| 90 | + |
| 91 | + // Hooks |
| 92 | + onMounted(async () => { |
| 93 | + await refreshOptions() |
| 94 | + listeners = listenToServiceEvents(getService(), { |
| 95 | + created: () => refreshOptions('created'), |
| 96 | + updated: () => refreshOptions('updated'), |
| 97 | + patched: () => refreshOptions('patched'), |
| 98 | + removed: () => refreshOptions('removed') |
| 99 | + }) |
| 100 | + }) |
| 101 | + onBeforeUnmount(() => { |
| 102 | + unlistenToServiceEvents(listeners) |
| 103 | + }) |
| 104 | + |
| 105 | + // Expose |
| 106 | + return { |
| 107 | + filterQuery, |
| 108 | + hasTagsSelection, |
| 109 | + hasTimeSelection |
| 110 | + } |
| 111 | +} |
0 commit comments