All files / src/stores mcad.store.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 10/10
100% Lines 17/17

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                            5x       10x     25x 29x 29x 29x       29x 11x     29x 1x     29x 27x 27x       2x       2x             2x     29x                        
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { CurrentMcadState, InitialMcadState } from '@3cr/types-ts';
import { inflateInitialMcadObjectState } from '@/functions/modelHelper';
import { parseDataOverlay } from '@/functions/parsers/parseDataOverlay';
import { parseMcadEvent } from '@/functions/parsers/parseMcadEvent';
import {
  DataOverlayMcad,
  DataOverlayMcadRaw,
  McadObjectInteraction,
  McadObjectInteractionRaw,
} from '@3cr/viewer-types-ts';
 
function toMcad(data: DataOverlayMcadRaw): DataOverlayMcad {
  return parseDataOverlay(data) as DataOverlayMcad;
}
 
function toMcadEvent(data: McadObjectInteractionRaw): McadObjectInteraction {
  return parseMcadEvent(data);
}
 
export const useMcadStore = defineStore('mcad', () => {
  const state = ref<InitialMcadState>(inflateInitialMcadObjectState());
  const event = ref<McadObjectInteractionRaw | null>(null);
  const sortBy = ref<{ key: string; order: 'asc' | 'desc' }[]>([
    { key: 'title', order: 'asc' },
  ]);
 
  const mcads = computed(() => {
    return state.value.Models.Models.map(toMcad);
  });
 
  const childKeys = computed(() => {
    return mcads.value.map((x) => x.Children).flat();
  });
 
  const mcadEvent = computed(() => {
    const value = event.value;
    return value ? toMcadEvent(value) : null;
  });
 
  function setInitialMcadState(message: string): void {
    state.value = JSON.parse(message) as InitialMcadState;
  }
 
  function setMcadState(message: string): void {
    state.value = {
      Version: '1.1.0',
      Models: JSON.parse(message) as CurrentMcadState,
    };
  }
 
  function setMcadEvent(message: string): void {
    event.value = JSON.parse(message) as McadObjectInteractionRaw;
  }
 
  return {
    state,
    event,
    mcads,
    mcadEvent,
    setInitialMcadState,
    setMcadState,
    setMcadEvent,
    childKeys,
    sortBy,
  };
});