All files / src/components FilterDrawer.vue

98.92% Statements 92/93
90.36% Branches 75/83
100% Functions 28/28
98.8% Lines 83/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                                              2x 2x 2x 2x 2x 2x 2x     52x                                                           34x   34x 34x   34x   36x 36x 6x     36x   36x 99x 31x 31x 68x 44x 25x   19x           34x 35x   35x         36x 34x 2x 1x       84x 84x 63x 36x   27x       2x       1x       2x 6x 2x         34x 41x   41x 113x   113x 39x 29x 10x   10x 3x   12x 7x   74x 35x 39x 26x   13x   22x 17x     13x       41x       2x 2x 2x 2x       3x 8x     3x 8x 3x 5x 2x 2x   3x     3x       1x 3x   1x   1x 3x 1x       1x                                                                                                            
<template>
  <Slideout
    :id="id"
    :title="title"
    position="right"
    @close-slideout="handleClose"
  >
    <div class="filter-drawer-content">
      <Accordion
        :multiple="true"
        :active-index="activeIndex"
        size="medium"
        @tab-open="handleTabOpen"
        @tab-close="handleTabClose"
      >
        <AccordionTab
          v-for="section in filterSections"
          :key="section.name"
          :header="section.label"
          :sub-text="selectedCountTexts[section.name]"
          :data-testid="`filter-accordion-${section.name}`"
        >
          <div class="filter-section-content">
            <Radio
              v-if="section.options?.length && section.type === 'single'"
              :key="`radio-${section.name}`"
              :model-value="localSelections[section.name] || ''"
              :options="section.options"
              :name="section.name"
              spacing="margin-bottom-xs"
              :required="false"
              @update:modelValue="updateSelection(section.name, $event)"
            />
            <Checkbox
              v-else-if="section.options?.length && section.type === 'multiple'"
              :key="`checkbox-${section.name}`"
              :model-value="localSelections[section.name] || []"
              :options="section.options"
              :name="section.name"
              spacing="margin-bottom-xs"
              :required="false"
              @update:modelValue="updateSelection(section.name, $event)"
            />
            <InputText
              v-else-if="section.type === 'text'"
              :key="`text-${section.name}`"
              :model-value="textInputValues[section.name] || ''"
              :name="section.name"
              :label="section.placeholder || 'Enter value'"
              @update:modelValue="updateTextInput(section.name, $event)"
            />
            <div
              v-else
              class="no-options-message"
            >
              No options available
            </div>
          </div>
        </AccordionTab>
      </Accordion>
    </div>
 
    <template #footer>
      <div class="filter-drawer-footer">
        <Button
          variant="secondary"
          :expanded="true"
          :disabled="isClearDisabled"
          data-testid="filter-clear-btn"
          @click="handleClear"
        >
          Clear All
        </Button>
        <Button
          variant="primary"
          :expanded="true"
          data-testid="filter-apply-btn"
          @click="handleApply"
        >
          Apply
        </Button>
      </div>
    </template>
  </Slideout>
</template>
 
<script setup>
import { Accordion, AccordionTab } from '@atomic-ui/accordion';
import Button from '@atomic-ui/button';
import Checkbox from '@atomic-ui/checkbox';
import InputText from '@atomic-ui/inputText';
import Radio from '@atomic-ui/radio';
import { Slideout } from '@atomic-ui/slideout';
import {
  computed, reactive, ref, watch,
} from 'vue';
import { useStore } from 'vuex';
 
const props = defineProps({
  id: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    default: 'Filters',
  },
  filterSections: {
    type: Array,
    required: true,
    validator: (sections) => sections.every((s) => {
      const hasValidType = !s.type
          || s.type === 'single'
          || s.type === 'multiple'
          || s.type === 'text';
      const hasValidOptions = s.type === 'text' ? !s.options : Array.isArray(s.options);
      return s.name && s.label && hasValidType && hasValidOptions;
    }),
  },
  modelValue: {
    type: Object,
    default: () => ({}),
  },
});
 
const emit = defineEmits(['update:modelValue', 'apply', 'clear']);
const store = useStore();
 
const localSelections = reactive({});
const activeIndex = ref([]);
 
const textInputValues = reactive({});
 
function initializeSelections(baseValue = {}) {
  Object.keys(localSelections).forEach((key) => {
    delete localSelections[key];
  });
 
  Object.assign(localSelections, baseValue);
 
  props.filterSections.forEach((section) => {
    if (section.type === 'text') {
      textInputValues[section.name] = baseValue[section.name] || '';
      localSelections[section.name] = baseValue[section.name] || '';
    } else if (!(section.name in localSelections)) {
      if (section.type === 'single') {
        localSelections[section.name] = section.name === 'signedIn' ? 'both' : '';
      } else {
        localSelections[section.name] = [];
      }
    }
  });
}
 
watch(
  () => props.modelValue,
  (newValue) => {
    initializeSelections(newValue);
  },
  { immediate: true, deep: true },
);
 
const slideoutState = computed(() => store.state.slideoutStore?.[props.id]);
watch(slideoutState, (isOpen) => {
  if (isOpen) {
    initializeSelections(props.modelValue);
  }
});
 
const isClearDisabled = computed(() => Object.entries(localSelections).every(([key, value]) => {
  if (key === 'signedIn' && value === 'both') return true;
  if (Array.isArray(value)) {
    return value.length === 0;
  }
  return !value || value === '';
}));
 
function updateSelection(sectionName, selectedValues) {
  localSelections[sectionName] = selectedValues;
}
 
function updateTextInput(sectionName, value) {
  textInputValues[sectionName] = value;
}
 
function syncTextInputsToLocal() {
  props.filterSections.forEach((section) => {
    if (section.type === 'text') {
      localSelections[section.name] = textInputValues[section.name] || '';
    }
  });
}
 
const selectedCountTexts = computed(() => {
  const texts = {};
 
  props.filterSections.forEach((section) => {
    const selected = localSelections[section.name];
 
    if (section.type === 'single') {
      if (section.name === 'signedIn' && selected === 'both') {
        texts[section.name] = '';
      } else Iif (section.name === 'adobeVisitorId' && selected === 'no') {
        texts[section.name] = '';
      } else if (!selected || selected === '') {
        texts[section.name] = '';
      } else {
        const option = section.options.find((opt) => opt.value === selected);
        texts[section.name] = option?.displayValue || option?.label || selected;
      }
    } else if (section.type === 'text') {
      texts[section.name] = '';
    } else if (!selected || selected.length === 0) {
      texts[section.name] = '';
    } else {
      const selectedLabels = selected
        .map((value) => {
          const option = section.options.find((opt) => opt.value === value);
          return option?.displayValue || option?.label || value;
        })
        .join(', ');
      texts[section.name] = selectedLabels;
    }
  });
 
  return texts;
});
 
function handleApply() {
  syncTextInputsToLocal();
  emit('update:modelValue', { ...localSelections });
  emit('apply', { ...localSelections });
  store.commit('slideoutStore/CLOSE', props.id);
}
 
function handleClear() {
  Object.keys(localSelections).forEach((key) => {
    delete localSelections[key];
  });
 
  props.filterSections.forEach((section) => {
    if (section.type === 'single') {
      localSelections[section.name] = section.name === 'signedIn' ? 'both' : '';
    } else if (section.type === 'text') {
      localSelections[section.name] = '';
      textInputValues[section.name] = '';
    } else {
      localSelections[section.name] = [];
    }
  });
  emit('clear');
}
 
function handleClose() {
  Object.keys(localSelections).forEach((key) => {
    delete localSelections[key];
  });
  Object.assign(localSelections, props.modelValue);
 
  props.filterSections.forEach((section) => {
    if (section.type === 'text') {
      textInputValues[section.name] = props.modelValue[section.name] || '';
    }
  });
 
  store.commit('slideoutStore/CLOSE', props.id);
}
 
function handleTabOpen() {}
 
function handleTabClose() {}
</script>
 
<style lang="scss" scoped>
.filter-drawer-content {
  padding-bottom: 20px;
}
 
.filter-section-content {
  padding: 12px 16px;
 
  :deep(.input-text) {
    width: 100%;
    margin-bottom: 0;
  }
}
 
.no-options-message {
  padding: 16px;
  text-align: center;
  color: $gray-1-color;
  font-size: 14px;
}
 
.filter-drawer-footer {
  display: flex;
  gap: 12px;
  padding: 16px;
  background: $white;
  border-top: $gray-3-border;
 
  :deep(button) {
    flex: 1;
  }
}
 
:deep(.accordion-header-sub-text) {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  margin-top: 4px;
  color: $gray-1-color;
  font-size: 12px;
}
 
:deep(.slideout-footer) {
  padding: 0;
}
</style>