Commit ac2b7630 authored by timel's avatar timel

Merge branch 'fix/max-in-build-card' into 'main'

fix: max in build deck

See merge request !285
parents 5c9a4632 ba2015e9
...@@ -26,8 +26,9 @@ import { LoaderFunction } from "react-router-dom"; ...@@ -26,8 +26,9 @@ import { LoaderFunction } from "react-router-dom";
import { proxy, useSnapshot } from "valtio"; import { proxy, useSnapshot } from "valtio";
import { subscribeKey } from "valtio/utils"; import { subscribeKey } from "valtio/utils";
import { type CardMeta, searchCards, forbidden } from "@/api"; import { type CardMeta, forbidden, searchCards } from "@/api";
import { isToken } from "@/common"; import { isToken } from "@/common";
import { useConfig } from "@/config";
import { FtsConditions } from "@/middleware/sqlite/fts"; import { FtsConditions } from "@/middleware/sqlite/fts";
import { deckStore, type IDeck, initStore } from "@/stores"; import { deckStore, type IDeck, initStore } from "@/stores";
import { import {
...@@ -50,7 +51,6 @@ import { ...@@ -50,7 +51,6 @@ import {
iDeckToEditingDeck, iDeckToEditingDeck,
type Type, type Type,
} from "./utils"; } from "./utils";
import { useConfig } from "@/config";
const { assetsPath } = useConfig(); const { assetsPath } = useConfig();
...@@ -407,7 +407,7 @@ const DeckZone: React.FC<{ ...@@ -407,7 +407,7 @@ const DeckZone: React.FC<{
// 当拖拽物在这个拖放区域放下时触发,这个item就是拖拽物的item(拖拽物携带的数据) // 当拖拽物在这个拖放区域放下时触发,这个item就是拖拽物的item(拖拽物携带的数据)
drop: ({ value, source }: { value: CardMeta; source: Type | "search" }) => { drop: ({ value, source }: { value: CardMeta; source: Type | "search" }) => {
if (type === source) return; if (type === source) return;
const { result, reason } = editDeckStore.canAdd(value, type); const { result, reason } = editDeckStore.canAdd(value, type, source);
if (result) { if (result) {
editDeckStore.add(type, value); editDeckStore.add(type, value);
if (source !== "search") { if (source !== "search") {
...@@ -419,7 +419,9 @@ const DeckZone: React.FC<{ ...@@ -419,7 +419,9 @@ const DeckZone: React.FC<{
}, },
hover: ({ value, source }) => { hover: ({ value, source }) => {
setAllowToDrop( setAllowToDrop(
type !== source ? editDeckStore.canAdd(value, type).result : true, type !== source
? editDeckStore.canAdd(value, type, source).result
: true,
); );
}, },
collect: (monitor) => ({ collect: (monitor) => ({
......
...@@ -48,7 +48,11 @@ export const editDeckStore = proxy({ ...@@ -48,7 +48,11 @@ export const editDeckStore = proxy({
]; ];
}, },
/** 一张卡能不能放入某个区 */ /** 一张卡能不能放入某个区 */
canAdd(card: CardMeta, type: Type): { result: boolean; reason: string } { canAdd(
card: CardMeta,
type: Type,
source: Type | "search",
): { result: boolean; reason: string } {
const deckType = editDeckStore[type]; const deckType = editDeckStore[type];
const cardType = card.data.type ?? 0; const cardType = card.data.type ?? 0;
...@@ -74,13 +78,14 @@ export const editDeckStore = proxy({ ...@@ -74,13 +78,14 @@ export const editDeckStore = proxy({
reason = "卡片种类不符合"; reason = "卡片种类不符合";
} }
const maxSameCard = forbidden.get(card.id) ?? 3; // TODO: 禁卡表 const max = forbidden.get(card.id) ?? 3; // TODO: 禁卡表
const sameCardCount = editDeckStore const numOfSameCards =
.getAll() editDeckStore.getAll().filter((c) => c.id === card.id).length -
.filter((c) => c.id === card.id).length; (source !== "search" ? 1 : 0);
if (sameCardCount >= maxSameCard) {
if (numOfSameCards >= max) {
result = false; result = false;
reason = `超过同名卡 ${maxSameCard} 张的上限`; reason = `超过同名卡 ${max} 张的上限`;
} }
return { result, reason }; return { result, reason };
}, },
......
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