tidy(ui): remove references to boardsAdapter

This commit is contained in:
psychedelicious 2023-06-21 20:10:28 +10:00
parent 26b75b85f7
commit 083a0fc4cf
2 changed files with 11 additions and 95 deletions

View File

@ -12,7 +12,6 @@ import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { defaultSelectorOptions } from 'app/store/util/defaultMemoizeOptions';
import {
boardsSelector,
selectBoardsAll,
setBoardSearchText,
} from 'features/gallery/store/boardSlice';
import { memo, useState } from 'react';
@ -24,12 +23,8 @@ import { CloseIcon } from '@chakra-ui/icons';
import { useListAllBoardsQuery } from 'services/apiSlice';
const selector = createSelector(
[selectBoardsAll, boardsSelector],
(boards, boardsState) => {
// const selectedBoard = boards.find(
// (board) => board.board_id === boardsState.selectedBoardId
// );
// return { selectedBoard, searchText: boardsState.searchText };
[boardsSelector],
(boardsState) => {
const { selectedBoardId, searchText } = boardsState;
return { selectedBoardId, searchText };
},

View File

@ -1,60 +1,22 @@
import {
EntityId,
PayloadAction,
Update,
createEntityAdapter,
createSlice,
} from '@reduxjs/toolkit';
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { RootState } from 'app/store/store';
import { BoardDTO } from 'services/api';
import { dateComparator } from 'common/util/dateComparator';
import {
boardCreated,
boardDeleted,
boardUpdated,
receivedBoards,
} from '../../../services/thunks/board';
import { api } from 'services/apiSlice';
export const boardsAdapter = createEntityAdapter<BoardDTO>({
selectId: (board) => board.board_id,
sortComparer: (a, b) => dateComparator(b.updated_at, a.updated_at),
});
type AdditionalBoardsState = {
offset: number;
limit: number;
total: number;
isLoading: boolean;
type BoardsState = {
searchText: string;
selectedBoardId?: string;
searchText?: string;
updateBoardModalOpen: boolean;
};
export const initialBoardsState =
boardsAdapter.getInitialState<AdditionalBoardsState>({
offset: 0,
limit: 50,
total: 0,
isLoading: false,
updateBoardModalOpen: false,
});
export type BoardsState = typeof initialBoardsState;
export const initialBoardsState: BoardsState = {
updateBoardModalOpen: false,
searchText: '',
};
const boardsSlice = createSlice({
name: 'boards',
initialState: initialBoardsState,
reducers: {
boardUpserted: (state, action: PayloadAction<BoardDTO>) => {
boardsAdapter.upsertOne(state, action.payload);
},
boardUpdatedOne: (state, action: PayloadAction<Update<BoardDTO>>) => {
boardsAdapter.updateOne(state, action.payload);
},
boardRemoved: (state, action: PayloadAction<string>) => {
boardsAdapter.removeOne(state, action.payload);
},
boardIdSelected: (state, action: PayloadAction<string | undefined>) => {
state.selectedBoardId = action.payload;
},
@ -66,33 +28,6 @@ const boardsSlice = createSlice({
},
},
extraReducers: (builder) => {
builder.addCase(receivedBoards.pending, (state) => {
state.isLoading = true;
});
builder.addCase(receivedBoards.rejected, (state) => {
state.isLoading = false;
});
builder.addCase(receivedBoards.fulfilled, (state, action) => {
state.isLoading = false;
const { items, offset, limit, total } = action.payload;
state.offset = offset;
state.limit = limit;
state.total = total;
boardsAdapter.upsertMany(state, items);
});
builder.addCase(boardCreated.fulfilled, (state, action) => {
const board = action.payload;
boardsAdapter.upsertOne(state, board);
});
builder.addCase(boardUpdated.fulfilled, (state, action) => {
const board = action.payload;
boardsAdapter.upsertOne(state, board);
});
builder.addCase(boardDeleted.pending, (state, action) => {
const boardId = action.meta.arg;
console.log({ boardId });
boardsAdapter.removeOne(state, boardId);
});
builder.addMatcher(
api.endpoints.deleteBoard.matchFulfilled,
(state, action) => {
@ -104,22 +39,8 @@ const boardsSlice = createSlice({
},
});
export const {
selectAll: selectBoardsAll,
selectById: selectBoardsById,
selectEntities: selectBoardsEntities,
selectIds: selectBoardsIds,
selectTotal: selectBoardsTotal,
} = boardsAdapter.getSelectors<RootState>((state) => state.boards);
export const {
boardUpserted,
boardUpdatedOne,
boardRemoved,
boardIdSelected,
setBoardSearchText,
setUpdateBoardModalOpen,
} = boardsSlice.actions;
export const { boardIdSelected, setBoardSearchText, setUpdateBoardModalOpen } =
boardsSlice.actions;
export const boardsSelector = (state: RootState) => state.boards;