All files / services/internal stateService.ts

85.48% Statements 106/124
64.28% Branches 9/14
63.63% Functions 7/11
85.48% Lines 106/124

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 1251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x   1x 1x 1x     1x 1x 1x 1x 1x 1x 4x 4x 4x   4x 4x 4x             4x 4x     4x 4x             4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x  
import { Container, Service } from 'typedi';
 
import { IApiSegment } from '@constants/api';
import { LocalStorageKey } from '@constants/site';
import { IDropdownOption } from '@contracts/dropdownOption';
import { debounceLeading } from '@helpers/debounceHelper';
import { getLocalStorage } from './localStorageService';
import { anyObject } from '@helpers/typescriptHacks';
import { uuidv4 } from '@helpers/guidHelper';
import { getConfig } from './configService';
 
interface IState {
  isSideBarOpen: boolean;
  anonymousUserGuid: string;
  submissions: {
    [prop in keyof IApiSegment]: Array<IDropdownOption>;
  };
  form: {
    [prop in keyof IApiSegment]: null | unknown;
  };
}
 
@Service()
export class StateService {
  private _internalState: IState = {
    isSideBarOpen: true,
    anonymousUserGuid: '',
    submissions: {
      community: [],
      builder: [],
      planetBuild: [],
    },
    form: {
      community: null,
      builder: null,
      planetBuild: null,
    },
  };
 
  constructor() {
    const localInitialState = getLocalStorage().get<IState>(LocalStorageKey.main);
    if (localInitialState != null) {
      let anonymousUserGuid =
        (localInitialState.anonymousUserGuid?.length ?? 0) > 10
          ? localInitialState.anonymousUserGuid
          : uuidv4();
 
      if (!getConfig().isProd()) {
        anonymousUserGuid = 'DEV';
      }
 
      this._internalState = {
        ...localInitialState,
        anonymousUserGuid,
      };
    }
  }
 
  saveToLocalStorage = debounceLeading((newState: IState) => {
    getLocalStorage().set(LocalStorageKey.main, newState);
  }, 250);
 
  setIsSidebarOpen(isOpen: boolean): void {
    this._internalState = {
      ...this._internalState,
      isSideBarOpen: isOpen,
    };
    this.saveToLocalStorage(this._internalState);
  }
 
  getIsSidebarOpen(): boolean {
    return this._internalState?.isSideBarOpen ?? true;
  }
 
  getAnonymousUserGuid = (): string => {
    if (this._internalState?.anonymousUserGuid == null) {
      this._internalState.anonymousUserGuid = uuidv4();
      this.saveToLocalStorage(this._internalState);
    }
    return this._internalState.anonymousUserGuid;
  };
 
  addSubmission(segment: keyof IApiSegment, ddOption: IDropdownOption): void {
    this._internalState = {
      ...this._internalState,
      submissions: {
        ...this._internalState.submissions,
        [segment]: [...this._internalState.submissions[segment], ddOption],
      },
    };
    this.saveToLocalStorage(this._internalState);
  }
 
  getSubmissions(segment: keyof IApiSegment): Array<IDropdownOption> {
    return this._internalState?.submissions?.[segment] ?? [];
  }
 
  delSubmission(segment: keyof IApiSegment, id: string): void {
    this._internalState = {
      ...this._internalState,
      submissions: {
        ...this._internalState.submissions,
        [segment]: this._internalState.submissions[segment].filter((s) => s.value != id),
      },
    };
    this.saveToLocalStorage(this._internalState);
  }
 
  getForm<T>(segment: keyof IApiSegment): T {
    return (this._internalState?.form?.[segment] as T) ?? anyObject;
  }
  setForm<T>(segment: keyof IApiSegment, newValue: T): void {
    this._internalState = {
      ...this._internalState,
      form: {
        ...this._internalState.form,
        [segment]: newValue,
      },
    };
    this.saveToLocalStorage(this._internalState);
  }
}
 
export const getStateService = () => Container.get(StateService);