Commit 6191b422 authored by Chunchi Che's avatar Chunchi Che

add fetchCheckCardMeta and checkCardModalCase

parent 97944531
......@@ -25,6 +25,7 @@ import {
setCardModalInteractiviesImpl,
setCardListModalIsOpenImpl,
setCardListModalInfoImpl,
checkCardModalCase,
} from "./modalSlice";
import {
MonsterState,
......@@ -124,6 +125,7 @@ const duelSlice = createSlice({
monsterCase(builder);
magicCase(builder);
cemeteryCase(builder);
checkCardModalCase(builder);
},
});
......
import { PayloadAction, CaseReducer } from "@reduxjs/toolkit";
import {
PayloadAction,
CaseReducer,
createAsyncThunk,
ActionReducerMapBuilder,
} from "@reduxjs/toolkit";
import { fetchCard } from "../../api/cards";
import { RootState } from "../../store";
import { DuelState } from "./mod";
......@@ -28,9 +34,9 @@ export interface ModalState {
tags: {
tagName: string;
options: {
code: number;
name?: string;
desc?: string;
imgUrl?: string;
response: number;
}[];
}[];
......@@ -108,6 +114,64 @@ export const setCheckCardModalMinMaxImpl: CaseReducer<
state.modalState.checkCardModal.selectMax = action.payload.max;
};
// 增加卡牌选择选项
export const fetchCheckCardMeta = createAsyncThunk(
"duel/fetchCheckCardMeta",
async (param: {
tagName: string;
option: { code: number; response: number };
}) => {
const meta = await fetchCard(param.option.code);
const response = {
tagName: param.tagName,
meta: {
code: meta.id,
name: meta.text.name,
desc: meta.text.desc,
},
};
return response;
}
);
export const checkCardModalCase = (
builder: ActionReducerMapBuilder<DuelState>
) => {
builder.addCase(fetchCheckCardMeta.pending, (state, action) => {
const tagName = action.meta.arg.tagName;
const code = action.meta.arg.option.code;
const response = action.meta.arg.option.response;
for (const tag of state.modalState.checkCardModal.tags) {
if (tag.tagName === tagName) {
tag.options.push({ code, response });
return;
}
}
state.modalState.checkCardModal.tags.push({
tagName,
options: [{ code, response }],
});
});
builder.addCase(fetchCheckCardMeta.fulfilled, (state, action) => {
const tagName = action.payload.tagName;
const meta = action.payload.meta;
for (const tag of state.modalState.checkCardModal.tags) {
if (tag.tagName === tagName) {
for (const option of tag.options) {
if (option.code == meta.code) {
option.name = meta.name;
option.desc = meta.desc;
}
}
}
}
});
};
export const selectCardModalIsOpen = (state: RootState) =>
state.duel.modalState.cardModal.isOpen;
export const selectCardModalName = (state: RootState) =>
......
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