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