All files / src/store/features/entities/reducers constantsReducers.ts

68.96% Statements 20/29
51.61% Branches 16/31
42.85% Functions 3/7
68.96% Lines 20/29

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            83x           83x 83x       83x                             83x 4x 4x   4x                   4x                 83x                                             83x 1x   1x 1x                                 83x                   83x                           83x               83x       10x                               83x  
import {
  PayloadAction,
  CaseReducer,
  SliceCaseReducers,
} from "@reduxjs/toolkit";
import { EntitiesState } from "shared/lib/entities/entitiesTypes";
import {
  genEntitySymbol,
  applyReparentFolderToCollection,
  applyReparentEntityToCollection,
} from "shared/lib/entities/entitiesHelpers";
import { Constant } from "shared/lib/resources/types";
import { constantsAdapter } from "store/features/entities/adapters";
import {
  localConstantSelectTotal,
  localConstantSelectById,
} from "store/features/entities/helpers";
import { v4 as uuid } from "uuid";
 
type AddConstantPayload = {
  constantId?: string;
  name?: string;
  value?: number;
};
 
type PreparedAddConstantPayload = AddConstantPayload & {
  constantId: string;
};
 
const addConstant: CaseReducer<
  EntitiesState,
  PayloadAction<PreparedAddConstantPayload>
> = (state, action) => {
  const numConstants = localConstantSelectTotal(state);
  const name = action.payload.name ?? "";
 
  const newConstant: Constant = {
    id: action.payload.constantId,
    name,
    symbol: genEntitySymbol(
      state,
      name ? `const_${name}` : `const_constant_${numConstants + 1}`,
    ),
    value: action.payload.value ?? 0,
  };
 
  constantsAdapter.addOne(state.constants, newConstant);
};
 
const editConstant: CaseReducer<
  EntitiesState,
  PayloadAction<{
    constantId: string;
    changes: Partial<Constant>;
  }>
> = (state, action) => {
  const constant = localConstantSelectById(state, action.payload.constantId);
 
  if (!constant) {
    return;
  }
 
  const patch = {
    ...action.payload.changes,
    symbol: action.payload.changes.name
      ? genEntitySymbol(state, `const_${action.payload.changes.name ?? "0"}`)
      : constant.symbol,
  };
 
  constantsAdapter.updateOne(state.constants, {
    id: action.payload.constantId,
    changes: patch,
  });
};
 
const renameConstant: CaseReducer<
  EntitiesState,
  PayloadAction<{ constantId: string; name: string }>
> = (state, action) => {
  const constant = localConstantSelectById(state, action.payload.constantId);
 
  Eif (!constant || constant.name === action.payload.name) {
    return;
  }
 
  constantsAdapter.updateOne(state.constants, {
    id: action.payload.constantId,
    changes: {
      name: action.payload.name,
      symbol: genEntitySymbol(state, `const_${action.payload.name ?? "0"}`),
    },
  });
};
 
const removeConstant: CaseReducer<
  EntitiesState,
  PayloadAction<{
    constantId: string;
  }>
> = (state, action) => {
  constantsAdapter.removeOne(state.constants, action.payload.constantId);
};
 
const reparentConstantsFolder: CaseReducer<
  EntitiesState,
  PayloadAction<{
    fromPath: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentFolderToCollection(
    state.constants.entities,
    action.payload.fromPath,
    action.payload.toPath,
  );
};
 
const reparentConstant: CaseReducer<
  EntitiesState,
  PayloadAction<{
    constantId: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentEntityToCollection(
    state.constants.entities,
    action.payload.constantId,
    action.payload.toPath,
  );
};
 
const constantsReducers = {
  addConstant: {
    reducer: addConstant,
    prepare: (payload: AddConstantPayload = {}) => {
      return {
        payload: {
          ...payload,
          constantId: payload.constantId ?? uuid(),
        },
      };
    },
  },
 
  editConstant,
  renameConstant,
  removeConstant,
  reparentConstantsFolder,
  reparentConstant,
} satisfies SliceCaseReducers<EntitiesState>;
 
export default constantsReducers;