Commit a2b4da13 authored by Chunchi Che's avatar Chunchi Che

udpate handSlice

parent 334da341
Pipeline #18115 passed with stages
in 3 minutes and 2 seconds
import { PayloadAction, CaseReducer } from "@reduxjs/toolkit"; import {
PayloadAction,
CaseReducer,
createAsyncThunk,
ActionReducerMapBuilder,
} from "@reduxjs/toolkit";
import { DuelState } from "./mod"; import { DuelState } from "./mod";
import { RootState } from "../../store"; import { RootState } from "../../store";
import { CardMeta } from "../../api/cards"; import { CardMeta, fetchCard } from "../../api/cards";
export interface Hands { export interface Hands {
cards: CardMeta[]; cards: CardMeta[];
...@@ -37,6 +42,29 @@ export const opAddHandsImpl: CaseReducer<DuelState, PayloadAction<number[]>> = ( ...@@ -37,6 +42,29 @@ export const opAddHandsImpl: CaseReducer<DuelState, PayloadAction<number[]>> = (
} }
}; };
export const fetchMeHandsMeta = createAsyncThunk(
"duel/fetchMeHandsMeta",
async (Ids: number[]) => {
return await Promise.all(
Ids.map(async (id) => {
return await fetchCard(id);
})
);
}
);
export const meHandsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchMeHandsMeta.fulfilled, (state, action) => {
// TODO: 合法性校验
const cards = action.payload;
if (state.meHands) {
state.meHands.cards = cards;
} else {
state.meHands = { cards };
}
});
};
export const selectMeHands = (state: RootState) => export const selectMeHands = (state: RootState) =>
state.duel.meHands || { cards: [] }; state.duel.meHands || { cards: [] };
export const selectOpHands = (state: RootState) => export const selectOpHands = (state: RootState) =>
......
...@@ -5,7 +5,12 @@ ...@@ -5,7 +5,12 @@
import { createSlice } from "@reduxjs/toolkit"; import { createSlice } from "@reduxjs/toolkit";
import { InitInfo, meInfoInitImpl, opInfoInitImpl } from "./initInfoSlice"; import { InitInfo, meInfoInitImpl, opInfoInitImpl } from "./initInfoSlice";
import { Hands, meAddHandsImpl, opAddHandsImpl } from "./handsSlice"; import {
Hands,
meAddHandsImpl,
opAddHandsImpl,
meHandsCase,
} from "./handsSlice";
import { RootState } from "../../store"; import { RootState } from "../../store";
export interface DuelState { export interface DuelState {
...@@ -26,6 +31,9 @@ const duelSlice = createSlice({ ...@@ -26,6 +31,9 @@ const duelSlice = createSlice({
meAddHands: meAddHandsImpl, meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl, opAddHands: opAddHandsImpl,
}, },
extraReducers(builder) {
meHandsCase(builder);
},
}); });
export const { meInfoInit, opInfoInit, meAddHands, opAddHands } = export const { meInfoInit, opInfoInit, meAddHands, opAddHands } =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment