All files / src/lib/store/metadata metadata.ts

0% Statements 0/44
100% Branches 0/0
0% Functions 0/15
0% Lines 0/43

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                                                                                                                                                                                                                                               
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
 
export interface Metadata {
  key: string;
  label: string;
  type: string;
  name: string;
}
 
interface MetadataState {
  categories: Metadata[];
  labels: Metadata[];
  units: Metadata[];
  levels: Metadata[];
  cuisines: Metadata[];
  servingUnits: Metadata[];
  dietaryFlags: Metadata[];
  allergens: Metadata[];
  equipment: Metadata[];
  costLevels: Metadata[];
  isLoading: boolean;
  isLoaded: boolean;
  error: string | null;
}
 
const initialState: MetadataState = {
  categories: [],
  labels: [],
  units: [],
  levels: [],
  cuisines: [],
  servingUnits: [],
  dietaryFlags: [],
  allergens: [],
  equipment: [],
  costLevels: [],
  isLoading: false,
  isLoaded: false,
  error: null,
};
 
const metadataSlice = createSlice({
  name: 'metadata',
  initialState,
  reducers: {
    setMetadataLoading(state, action: PayloadAction<boolean>) {
      state.isLoading = action.payload;
    },
    setMetadataLoaded(state, action: PayloadAction<boolean>) {
      state.isLoaded = action.payload;
    },
    setMetadataError(state, action: PayloadAction<string | null>) {
      state.error = action.payload;
    },
    setMetadata(state, action: PayloadAction<Metadata[]>) {
      const metadata = action.payload;
 
      // Separate metadata by type
      state.categories = metadata.filter((m) =>
        ['category', 'CATEGORY'].includes(m.type),
      );
      state.labels = metadata.filter((m) =>
        ['label', 'LABEL'].includes(m.type),
      );
      state.units = metadata.filter((m) => ['unit', 'UNIT'].includes(m.type));
      state.levels = metadata.filter((m) =>
        ['level', 'DIFFICULTY_LEVEL'].includes(m.type),
      );
      state.cuisines = metadata.filter((m) =>
        ['cuisine', 'CUISINE'].includes(m.type),
      );
      state.servingUnits = metadata.filter((m) =>
        ['serving_unit', 'SERVING_UNIT'].includes(m.type),
      );
      state.dietaryFlags = metadata.filter((m) =>
        ['diet', 'DIET'].includes(m.type),
      );
      state.allergens = metadata.filter((m) =>
        ['allergen', 'ALLERGEN'].includes(m.type),
      );
      state.equipment = metadata.filter((m) =>
        ['equipment', 'EQUIPMENT'].includes(m.type),
      );
      state.costLevels = metadata.filter((m) =>
        ['cost_level', 'COST_LEVEL'].includes(m.type),
      );
 
      state.isLoaded = true;
      state.isLoading = false;
      state.error = null;
    },
    clearMetadata(state) {
      state.categories = [];
      state.labels = [];
      state.units = [];
      state.levels = [];
      state.cuisines = [];
      state.servingUnits = [];
      state.dietaryFlags = [];
      state.allergens = [];
      state.equipment = [];
      state.costLevels = [];
      state.isLoaded = false;
      state.isLoading = false;
      state.error = null;
    },
  },
});
 
export const {
  setMetadataLoading,
  setMetadataLoaded,
  setMetadataError,
  setMetadata,
  clearMetadata,
} = metadataSlice.actions;
 
export const metadataReducer = metadataSlice.reducer;