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

View File

@ -1,60 +1,22 @@
import { import { PayloadAction, createSlice } from '@reduxjs/toolkit';
EntityId,
PayloadAction,
Update,
createEntityAdapter,
createSlice,
} from '@reduxjs/toolkit';
import { RootState } from 'app/store/store'; 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'; import { api } from 'services/apiSlice';
export const boardsAdapter = createEntityAdapter<BoardDTO>({ type BoardsState = {
selectId: (board) => board.board_id, searchText: string;
sortComparer: (a, b) => dateComparator(b.updated_at, a.updated_at),
});
type AdditionalBoardsState = {
offset: number;
limit: number;
total: number;
isLoading: boolean;
selectedBoardId?: string; selectedBoardId?: string;
searchText?: string;
updateBoardModalOpen: boolean; updateBoardModalOpen: boolean;
}; };
export const initialBoardsState = export const initialBoardsState: BoardsState = {
boardsAdapter.getInitialState<AdditionalBoardsState>({ updateBoardModalOpen: false,
offset: 0, searchText: '',
limit: 50, };
total: 0,
isLoading: false,
updateBoardModalOpen: false,
});
export type BoardsState = typeof initialBoardsState;
const boardsSlice = createSlice({ const boardsSlice = createSlice({
name: 'boards', name: 'boards',
initialState: initialBoardsState, initialState: initialBoardsState,
reducers: { 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>) => { boardIdSelected: (state, action: PayloadAction<string | undefined>) => {
state.selectedBoardId = action.payload; state.selectedBoardId = action.payload;
}, },
@ -66,33 +28,6 @@ const boardsSlice = createSlice({
}, },
}, },
extraReducers: (builder) => { 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( builder.addMatcher(
api.endpoints.deleteBoard.matchFulfilled, api.endpoints.deleteBoard.matchFulfilled,
(state, action) => { (state, action) => {
@ -104,22 +39,8 @@ const boardsSlice = createSlice({
}, },
}); });
export const { export const { boardIdSelected, setBoardSearchText, setUpdateBoardModalOpen } =
selectAll: selectBoardsAll, boardsSlice.actions;
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 boardsSelector = (state: RootState) => state.boards; export const boardsSelector = (state: RootState) => state.boards;