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

65.22% Statements 30/46
33.33% Branches 4/12
41.67% Functions 5/12
65.22% Lines 30/46

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          46x 46x 46x   46x           46x 46x         46x 1x                       1x           46x 1x   1x                         46x 2x 2x       2x   2x 1x 1x   1x     1x             46x                                               46x 1x           46x                   46x                           46x               46x       2x                                                   46x  
import {
  PayloadAction,
  CaseReducer,
  SliceCaseReducers,
} from "@reduxjs/toolkit";
import l10n from "shared/lib/lang/l10n";
import { DMG_PALETTE } from "consts";
import uuid from "uuid";
import { EntitiesState } from "shared/lib/entities/entitiesTypes";
import {
  nextIndexedName,
  applyReparentFolderToCollection,
  applyReparentEntityToCollection,
} from "shared/lib/entities/entitiesHelpers";
import { Palette } from "shared/lib/resources/types";
import { palettesAdapter } from "store/features/entities/adapters";
import { localPaletteSelectTotal } from "store/features/entities/helpers";
 
const addPalette: CaseReducer<
  EntitiesState,
  PayloadAction<{ paletteId: string }>
> = (state, action) => {
  const newPalette: Palette = {
    id: action.payload.paletteId,
    name: `${l10n("TOOL_PALETTE_N", {
      number: localPaletteSelectTotal(state) + 1,
    })}`,
    colors: [
      DMG_PALETTE.colors[0],
      DMG_PALETTE.colors[1],
      DMG_PALETTE.colors[2],
      DMG_PALETTE.colors[3],
    ],
  };
  palettesAdapter.addOne(state.palettes, newPalette);
};
 
const editPalette: CaseReducer<
  EntitiesState,
  PayloadAction<{ paletteId: string; changes: Partial<Palette> }>
> = (state, action) => {
  const patch = { ...action.payload.changes };
 
  palettesAdapter.updateOne(state.palettes, {
    id: action.payload.paletteId,
    changes: patch,
  });
};
 
const editPaletteColor: CaseReducer<
  EntitiesState,
  PayloadAction<{
    paletteId: string;
    colorId: 0 | 1 | 2 | 3;
    color: string;
  }>
> = (state, action) => {
  const existingPalette = state.palettes.entities[action.payload.paletteId];
  Iif (!existingPalette) {
    return;
  }
 
  const [white, light, dark, black] = existingPalette.colors;
 
  if (action.payload.colorId === 0) {
    existingPalette.colors = [action.payload.color, light, dark, black];
  } else Iif (action.payload.colorId === 1) {
    existingPalette.colors = [white, action.payload.color, dark, black];
  } else Iif (action.payload.colorId === 2) {
    existingPalette.colors = [white, light, action.payload.color, black];
  } else {
    existingPalette.colors = [white, light, dark, action.payload.color];
  }
};
 
const duplicatePalette: CaseReducer<
  EntitiesState,
  PayloadAction<{ paletteId: string; newPaletteId: string }>
> = (state, action) => {
  const existingPalette = state.palettes.entities[action.payload.paletteId];
  Iif (!existingPalette) {
    return;
  }
 
  const allNames = state.palettes.ids
    .map((id) => state.palettes.entities[id]?.name)
    .filter((n) => !!n);
 
  const newName = nextIndexedName(existingPalette.name, allNames);
 
  const newPalette: Palette = {
    ...existingPalette,
    id: action.payload.newPaletteId,
    name: newName,
  };
 
  palettesAdapter.addOne(state.palettes, newPalette);
};
 
const removePalette: CaseReducer<
  EntitiesState,
  PayloadAction<{ paletteId: string }>
> = (state, action) => {
  palettesAdapter.removeOne(state.palettes, action.payload.paletteId);
};
 
const removePalettes: CaseReducer<
  EntitiesState,
  PayloadAction<{ paletteIds: string[] }>
> = (state, action) => {
  palettesAdapter.removeMany(state.palettes, action.payload.paletteIds);
};
 
const reparentPalettesFolder: CaseReducer<
  EntitiesState,
  PayloadAction<{
    fromPath: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentFolderToCollection(
    state.palettes.entities,
    action.payload.fromPath,
    action.payload.toPath,
  );
};
 
const reparentPalette: CaseReducer<
  EntitiesState,
  PayloadAction<{
    paletteId: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentEntityToCollection(
    state.palettes.entities,
    action.payload.paletteId,
    action.payload.toPath,
  );
};
 
const palettesReducers = {
  addPalette: {
    reducer: addPalette,
    prepare: () => {
      return {
        payload: {
          paletteId: uuid(),
        },
      };
    },
  },
  editPalette,
  editPaletteColor,
  duplicatePalette: {
    reducer: duplicatePalette,
    prepare: (payload: { paletteId: string }) => {
      return {
        payload: {
          ...payload,
          newPaletteId: uuid(),
        },
      };
    },
  },
  removePalette,
  removePalettes,
  reparentPalettesFolder,
  reparentPalette,
} satisfies SliceCaseReducers<EntitiesState>;
 
export default palettesReducers;