fix(dashboard): scope owner and service views by resolved shop
This commit is contained in:
@@ -20,10 +20,13 @@ import {
|
|||||||
} from "@/components/ui/select"
|
} from "@/components/ui/select"
|
||||||
import { Textarea } from "@/components/ui/textarea"
|
import { Textarea } from "@/components/ui/textarea"
|
||||||
import { getGameById, listGames } from "@/lib/api"
|
import { getGameById, listGames } from "@/lib/api"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
import { GameIcon } from "@/lib/game-icons"
|
import { GameIcon } from "@/lib/game-icons"
|
||||||
import type { PlayerService } from "@/lib/types"
|
import type { PlayerService } from "@/lib/types"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
|
import { usePlayerStore } from "@/store/players"
|
||||||
import { useServiceStore } from "@/store/services"
|
import { useServiceStore } from "@/store/services"
|
||||||
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
const serviceSchema = z.object({
|
const serviceSchema = z.object({
|
||||||
gameId: z.string().min(1, "请选择游戏"),
|
gameId: z.string().min(1, "请选择游戏"),
|
||||||
@@ -40,10 +43,28 @@ export default function NewServicePage() {
|
|||||||
const searchParams = useSearchParams()
|
const searchParams = useSearchParams()
|
||||||
const serviceId = searchParams.get("serviceId")
|
const serviceId = searchParams.get("serviceId")
|
||||||
const userId = useAuthStore((state) => state.user?.id)
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const currentRole = useAuthStore((state) => state.currentRole)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const players = usePlayerStore((state) => state.players)
|
||||||
const services = useServiceStore((state) => state.services)
|
const services = useServiceStore((state) => state.services)
|
||||||
const createService = useServiceStore((state) => state.createService)
|
const createService = useServiceStore((state) => state.createService)
|
||||||
const updateService = useServiceStore((state) => state.updateService)
|
const updateService = useServiceStore((state) => state.updateService)
|
||||||
const editingService = services.find((service) => service.id === serviceId)
|
const ownerShop = resolveOwnerShop(userId, shops)
|
||||||
|
const scopedPlayerIds =
|
||||||
|
currentRole === "player"
|
||||||
|
? userId
|
||||||
|
? [userId]
|
||||||
|
: []
|
||||||
|
: currentRole === "owner"
|
||||||
|
? ownerShop
|
||||||
|
? players.filter((player) => player.shopId === ownerShop.id).map((player) => player.id)
|
||||||
|
: []
|
||||||
|
: []
|
||||||
|
const scopedPlayerIdSet = new Set(scopedPlayerIds)
|
||||||
|
const editingService = services.find(
|
||||||
|
(service) => service.id === serviceId && scopedPlayerIdSet.has(service.playerId),
|
||||||
|
)
|
||||||
|
const targetPlayerId = editingService?.playerId ?? scopedPlayerIds[0]
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
@@ -78,12 +99,20 @@ export default function NewServicePage() {
|
|||||||
const selectedUnit = useWatch({ control, name: "unit" })
|
const selectedUnit = useWatch({ control, name: "unit" })
|
||||||
const games = listGames()
|
const games = listGames()
|
||||||
|
|
||||||
|
if (serviceId && !editingService) {
|
||||||
|
return <div className="text-sm text-muted-foreground">服务不存在或当前身份不可编辑</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!targetPlayerId) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前身份下没有可管理的服务范围</div>
|
||||||
|
}
|
||||||
|
|
||||||
const onSubmit = async (data: z.infer<typeof serviceSchema>) => {
|
const onSubmit = async (data: z.infer<typeof serviceSchema>) => {
|
||||||
const game = getGameById(data.gameId)
|
const game = getGameById(data.gameId)
|
||||||
if (!game) return
|
if (!game) return
|
||||||
|
|
||||||
const payload: Omit<PlayerService, "id"> = {
|
const payload: Omit<PlayerService, "id"> = {
|
||||||
playerId: editingService?.playerId ?? userId ?? "u5",
|
playerId: targetPlayerId,
|
||||||
gameId: game.id,
|
gameId: game.id,
|
||||||
gameName: game.name,
|
gameName: game.name,
|
||||||
title: data.title,
|
title: data.title,
|
||||||
|
|||||||
@@ -13,11 +13,38 @@ import {
|
|||||||
TableHeader,
|
TableHeader,
|
||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table"
|
} from "@/components/ui/table"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
|
import { usePlayerStore } from "@/store/players"
|
||||||
import { useServiceStore } from "@/store/services"
|
import { useServiceStore } from "@/store/services"
|
||||||
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
export default function ServicesPage() {
|
export default function ServicesPage() {
|
||||||
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const currentRole = useAuthStore((state) => state.currentRole)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const players = usePlayerStore((state) => state.players)
|
||||||
const services = useServiceStore((state) => state.services)
|
const services = useServiceStore((state) => state.services)
|
||||||
const deleteService = useServiceStore((state) => state.deleteService)
|
const deleteService = useServiceStore((state) => state.deleteService)
|
||||||
|
const ownerShop = resolveOwnerShop(userId, shops)
|
||||||
|
|
||||||
|
const scopedPlayerIds =
|
||||||
|
currentRole === "player"
|
||||||
|
? userId
|
||||||
|
? [userId]
|
||||||
|
: []
|
||||||
|
: currentRole === "owner"
|
||||||
|
? ownerShop
|
||||||
|
? players.filter((player) => player.shopId === ownerShop.id).map((player) => player.id)
|
||||||
|
: []
|
||||||
|
: []
|
||||||
|
|
||||||
|
const scopedPlayerIdSet = new Set(scopedPlayerIds)
|
||||||
|
const scopedServices = services.filter((service) => scopedPlayerIdSet.has(service.playerId))
|
||||||
|
|
||||||
|
if (currentRole !== "player" && currentRole !== "owner") {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前身份不可管理服务</div>
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
@@ -48,7 +75,7 @@ export default function ServicesPage() {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{services.map((service) => (
|
{scopedServices.map((service) => (
|
||||||
<TableRow key={service.id}>
|
<TableRow key={service.id}>
|
||||||
<TableCell className="font-medium">{service.title}</TableCell>
|
<TableCell className="font-medium">{service.title}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
@@ -72,7 +99,14 @@ export default function ServicesPage() {
|
|||||||
<Edit className="h-4 w-4" />
|
<Edit className="h-4 w-4" />
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="ghost" size="icon" onClick={() => deleteService(service.id)}>
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={() => {
|
||||||
|
if (!scopedPlayerIdSet.has(service.playerId)) return
|
||||||
|
deleteService(service.id)
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Trash2 className="h-4 w-4 text-destructive" />
|
<Trash2 className="h-4 w-4 text-destructive" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import {
|
|||||||
TableHeader,
|
TableHeader,
|
||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table"
|
} from "@/components/ui/table"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { usePlayerStore } from "@/store/players"
|
import { usePlayerStore } from "@/store/players"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
@@ -39,27 +41,32 @@ const statusVariants: Record<string, "default" | "secondary" | "outline"> = {
|
|||||||
|
|
||||||
export default function EmployeesPage() {
|
export default function EmployeesPage() {
|
||||||
const [search, setSearch] = useState("")
|
const [search, setSearch] = useState("")
|
||||||
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
const players = usePlayerStore((state) => state.players)
|
const players = usePlayerStore((state) => state.players)
|
||||||
const assignToShop = usePlayerStore((state) => state.assignToShop)
|
const assignToShop = usePlayerStore((state) => state.assignToShop)
|
||||||
const removeFromShop = usePlayerStore((state) => state.removeFromShop)
|
const removeFromShop = usePlayerStore((state) => state.removeFromShop)
|
||||||
const shop = useShopStore((state) => state.shops[0])
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
const updateShop = useShopStore((state) => state.updateShop)
|
const updateShop = useShopStore((state) => state.updateShop)
|
||||||
|
|
||||||
const shopPlayers = useMemo(
|
const shopPlayers = useMemo(() => {
|
||||||
() =>
|
if (!shop) return []
|
||||||
players.filter(
|
return players.filter(
|
||||||
(player) =>
|
(player) =>
|
||||||
player.shopId === shop.id &&
|
player.shopId === shop.id &&
|
||||||
player.user.nickname.toLowerCase().includes(search.trim().toLowerCase()),
|
player.user.nickname.toLowerCase().includes(search.trim().toLowerCase()),
|
||||||
),
|
)
|
||||||
[players, shop.id, search],
|
}, [players, shop, search])
|
||||||
)
|
|
||||||
|
|
||||||
const inviteCandidate = useMemo(
|
const inviteCandidate = useMemo(
|
||||||
() => players.find((player) => player.shopId !== shop.id),
|
() => (shop ? players.find((player) => player.shopId !== shop.id) : undefined),
|
||||||
[players, shop.id],
|
[players, shop],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import {
|
|||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table"
|
} from "@/components/ui/table"
|
||||||
import { listTransactions } from "@/lib/api"
|
import { listTransactions } from "@/lib/api"
|
||||||
|
import { isActiveOrder, isCompletedOrder } from "@/lib/domain/order-filters"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useOrderStore } from "@/store/orders"
|
import { useOrderStore } from "@/store/orders"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
@@ -20,9 +22,14 @@ export default function ShopIncomePage() {
|
|||||||
const userId = useAuthStore((state) => state.user?.id)
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
const shops = useShopStore((state) => state.shops)
|
const shops = useShopStore((state) => state.shops)
|
||||||
const orders = useOrderStore((state) => state.orders)
|
const orders = useOrderStore((state) => state.orders)
|
||||||
const shop = shops.find((item) => item.owner.id === userId) ?? shops[0]
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
const shopOrders = orders.filter((order) => order.shopId === shop?.id)
|
const shopOrders = orders.filter((order) => order.shopId === shop?.id)
|
||||||
const completedOrders = shopOrders.filter((o) => o.status === "completed")
|
const completedOrders = shopOrders.filter((o) => isCompletedOrder(o.status))
|
||||||
const totalIncome = completedOrders.reduce((acc, order) => acc + order.totalPrice, 0)
|
const totalIncome = completedOrders.reduce((acc, order) => acc + order.totalPrice, 0)
|
||||||
|
|
||||||
const currentMonth = new Date().getMonth()
|
const currentMonth = new Date().getMonth()
|
||||||
@@ -31,7 +38,10 @@ export default function ShopIncomePage() {
|
|||||||
.reduce((acc, order) => acc + order.totalPrice, 0)
|
.reduce((acc, order) => acc + order.totalPrice, 0)
|
||||||
|
|
||||||
const pendingSettlement = shopOrders
|
const pendingSettlement = shopOrders
|
||||||
.filter((o) => ["in_progress", "pending_close", "pending_review"].includes(o.status))
|
.filter(
|
||||||
|
(o) =>
|
||||||
|
isActiveOrder(o.status) && o.status !== "pending_payment" && o.status !== "pending_accept",
|
||||||
|
)
|
||||||
.reduce((acc, order) => acc + order.totalPrice, 0)
|
.reduce((acc, order) => acc + order.totalPrice, 0)
|
||||||
|
|
||||||
const shopOrderIds = new Set(shopOrders.map((order) => order.id))
|
const shopOrderIds = new Set(shopOrders.map((order) => order.id))
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import {
|
|||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table"
|
} from "@/components/ui/table"
|
||||||
import { statusLabels } from "@/lib/constants"
|
import { statusLabels } from "@/lib/constants"
|
||||||
|
import { isActiveOrder, isCompletedOrder, isDisputedOrder } from "@/lib/domain/order-filters"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useOrderStore } from "@/store/orders"
|
import { useOrderStore } from "@/store/orders"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
@@ -22,21 +24,18 @@ export default function ShopOrdersPage() {
|
|||||||
const userId = useAuthStore((state) => state.user?.id)
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
const shops = useShopStore((state) => state.shops)
|
const shops = useShopStore((state) => state.shops)
|
||||||
const orders = useOrderStore((state) => state.orders)
|
const orders = useOrderStore((state) => state.orders)
|
||||||
const shop = shops.find((item) => item.owner.id === userId) ?? shops[0]
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
const shopOrders = orders.filter((order) => order.shopId === shop?.id)
|
const shopOrders = orders.filter((order) => order.shopId === shop?.id)
|
||||||
|
|
||||||
const totalOrders = shopOrders.length
|
const totalOrders = shopOrders.length
|
||||||
const activeOrders = shopOrders.filter((o) =>
|
const activeOrders = shopOrders.filter((o) => isActiveOrder(o.status)).length
|
||||||
[
|
const completedOrders = shopOrders.filter((o) => isCompletedOrder(o.status)).length
|
||||||
"pending_payment",
|
const disputedOrders = shopOrders.filter((o) => isDisputedOrder(o.status)).length
|
||||||
"pending_accept",
|
|
||||||
"in_progress",
|
|
||||||
"pending_close",
|
|
||||||
"pending_review",
|
|
||||||
].includes(o.status),
|
|
||||||
).length
|
|
||||||
const completedOrders = shopOrders.filter((o) => o.status === "completed").length
|
|
||||||
const disputedOrders = shopOrders.filter((o) => o.status === "disputed").length
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { DollarSign, Edit, ExternalLink, ListOrdered, Star, Users } from "lucide-react"
|
import { DollarSign, Edit, ExternalLink, ListOrdered, Star, Users } from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
import { useEffect, useState } from "react"
|
import { useState } from "react"
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { Badge } from "@/components/ui/badge"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
@@ -10,21 +10,48 @@ import { Input } from "@/components/ui/input"
|
|||||||
import { Label } from "@/components/ui/label"
|
import { Label } from "@/components/ui/label"
|
||||||
import { Separator } from "@/components/ui/separator"
|
import { Separator } from "@/components/ui/separator"
|
||||||
import { Textarea } from "@/components/ui/textarea"
|
import { Textarea } from "@/components/ui/textarea"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
|
import type { Shop } from "@/lib/types"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
export default function ShopManagementPage() {
|
export default function ShopManagementPage() {
|
||||||
const shop = useShopStore((state) => state.shops[0])
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
const updateShop = useShopStore((state) => state.updateShop)
|
const updateShop = useShopStore((state) => state.updateShop)
|
||||||
const updateAnnouncement = useShopStore((state) => state.updateAnnouncement)
|
const updateAnnouncement = useShopStore((state) => state.updateAnnouncement)
|
||||||
const addAnnouncement = useShopStore((state) => state.addAnnouncement)
|
const addAnnouncement = useShopStore((state) => state.addAnnouncement)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ShopManagementContent
|
||||||
|
key={shop.id}
|
||||||
|
shop={shop}
|
||||||
|
updateShop={updateShop}
|
||||||
|
updateAnnouncement={updateAnnouncement}
|
||||||
|
addAnnouncement={addAnnouncement}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ShopManagementContent({
|
||||||
|
shop,
|
||||||
|
updateShop,
|
||||||
|
updateAnnouncement,
|
||||||
|
addAnnouncement,
|
||||||
|
}: {
|
||||||
|
shop: Shop
|
||||||
|
updateShop: (shopId: string, patch: Partial<Omit<Shop, "id" | "owner">>) => void
|
||||||
|
updateAnnouncement: (shopId: string, index: number, announcement: string) => void
|
||||||
|
addAnnouncement: (shopId: string, announcement: string) => void
|
||||||
|
}) {
|
||||||
const [name, setName] = useState(shop.name)
|
const [name, setName] = useState(shop.name)
|
||||||
const [description, setDescription] = useState(shop.description)
|
const [description, setDescription] = useState(shop.description)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setName(shop.name)
|
|
||||||
setDescription(shop.description)
|
|
||||||
}, [shop])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
|
|||||||
@@ -14,13 +14,31 @@ import {
|
|||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select"
|
} from "@/components/ui/select"
|
||||||
import { Switch } from "@/components/ui/switch"
|
import { Switch } from "@/components/ui/switch"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
import type { Shop } from "@/lib/types"
|
import type { Shop } from "@/lib/types"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
export default function ShopRulesPage() {
|
export default function ShopRulesPage() {
|
||||||
const shop = useShopStore((state) => state.shops[0])
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
const updateShop = useShopStore((state) => state.updateShop)
|
const updateShop = useShopStore((state) => state.updateShop)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
return <ShopRulesForm key={shop.id} shop={shop} updateShop={updateShop} />
|
||||||
|
}
|
||||||
|
|
||||||
|
function ShopRulesForm({
|
||||||
|
shop,
|
||||||
|
updateShop,
|
||||||
|
}: {
|
||||||
|
shop: Shop
|
||||||
|
updateShop: (shopId: string, patch: Partial<Omit<Shop, "id" | "owner">>) => void
|
||||||
|
}) {
|
||||||
const [allowMultiShop, setAllowMultiShop] = useState(shop.allowMultiShop)
|
const [allowMultiShop, setAllowMultiShop] = useState(shop.allowMultiShop)
|
||||||
const [allowIndependentOrders, setAllowIndependentOrders] = useState(shop.allowIndependentOrders)
|
const [allowIndependentOrders, setAllowIndependentOrders] = useState(shop.allowIndependentOrders)
|
||||||
const [dispatchMode, setDispatchMode] = useState<Shop["dispatchMode"]>(shop.dispatchMode)
|
const [dispatchMode, setDispatchMode] = useState<Shop["dispatchMode"]>(shop.dispatchMode)
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import { type DragEvent, useEffect, useState } from "react"
|
|||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
import { Switch } from "@/components/ui/switch"
|
import { Switch } from "@/components/ui/switch"
|
||||||
import type { ShopSection } from "@/lib/types"
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
|
import type { Shop, ShopSection } from "@/lib/types"
|
||||||
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useShopStore } from "@/store/shops"
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
const sectionLabels: Record<ShopSection["type"], string> = {
|
const sectionLabels: Record<ShopSection["type"], string> = {
|
||||||
@@ -28,8 +30,31 @@ const sectionDescriptions: Record<ShopSection["type"], string> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function ShopTemplatesPage() {
|
export default function ShopTemplatesPage() {
|
||||||
const shop = useShopStore((state) => state.shops[0])
|
const userId = useAuthStore((state) => state.user?.id)
|
||||||
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const shop = resolveOwnerShop(userId, shops)
|
||||||
const updateTemplateSections = useShopStore((state) => state.updateTemplateSections)
|
const updateTemplateSections = useShopStore((state) => state.updateTemplateSections)
|
||||||
|
|
||||||
|
if (!shop) {
|
||||||
|
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ShopTemplatesEditor
|
||||||
|
key={shop.id}
|
||||||
|
shop={shop}
|
||||||
|
updateTemplateSections={updateTemplateSections}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ShopTemplatesEditor({
|
||||||
|
shop,
|
||||||
|
updateTemplateSections,
|
||||||
|
}: {
|
||||||
|
shop: Shop
|
||||||
|
updateTemplateSections: (shopId: string, sections: ShopSection[]) => void
|
||||||
|
}) {
|
||||||
const [sections, setSections] = useState<ShopSection[]>(
|
const [sections, setSections] = useState<ShopSection[]>(
|
||||||
[...shop.templateConfig.sections].sort((a, b) => a.order - b.order),
|
[...shop.templateConfig.sections].sort((a, b) => a.order - b.order),
|
||||||
)
|
)
|
||||||
|
|||||||
+40
-29
@@ -2,17 +2,25 @@
|
|||||||
|
|
||||||
import { Clock, MessageSquare, RefreshCw } from "lucide-react"
|
import { Clock, MessageSquare, RefreshCw } from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
import { useEffect, useState } from "react"
|
import { useState } from "react"
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { Badge } from "@/components/ui/badge"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
||||||
import { statusLabels } from "@/lib/constants"
|
import { statusLabels } from "@/lib/constants"
|
||||||
|
import {
|
||||||
|
isActiveOrder,
|
||||||
|
isCompletedOrder,
|
||||||
|
isDisputedOrder,
|
||||||
|
isPendingDispatch,
|
||||||
|
} from "@/lib/domain/order-filters"
|
||||||
|
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||||
import type { OrderStatus, UserRole } from "@/lib/types"
|
import type { OrderStatus, UserRole } from "@/lib/types"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
import { useChatStore } from "@/store/chat"
|
import { useChatStore } from "@/store/chat"
|
||||||
import { useOrderStore } from "@/store/orders"
|
import { useOrderStore } from "@/store/orders"
|
||||||
|
import { useShopStore } from "@/store/shops"
|
||||||
|
|
||||||
const statusColors: Record<OrderStatus, string> = {
|
const statusColors: Record<OrderStatus, string> = {
|
||||||
pending_payment: "bg-yellow-100 text-yellow-800",
|
pending_payment: "bg-yellow-100 text-yellow-800",
|
||||||
@@ -51,47 +59,46 @@ const ownerTabs = [
|
|||||||
|
|
||||||
export default function OrderListPage() {
|
export default function OrderListPage() {
|
||||||
const { currentRole, user } = useAuthStore()
|
const { currentRole, user } = useAuthStore()
|
||||||
const userId = user?.id ?? "u1"
|
const shops = useShopStore((state) => state.shops)
|
||||||
|
const ownerShop = resolveOwnerShop(user?.id, shops)
|
||||||
|
|
||||||
return <OrderListContent key={currentRole} currentRole={currentRole} userId={userId} />
|
return (
|
||||||
|
<OrderListContent
|
||||||
|
key={currentRole}
|
||||||
|
currentRole={currentRole}
|
||||||
|
userId={user?.id}
|
||||||
|
ownerShopId={ownerShop?.id}
|
||||||
|
/>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function OrderListContent({ currentRole, userId }: { currentRole: UserRole; userId: string }) {
|
function OrderListContent({
|
||||||
|
currentRole,
|
||||||
|
userId,
|
||||||
|
ownerShopId,
|
||||||
|
}: {
|
||||||
|
currentRole: UserRole
|
||||||
|
userId?: string
|
||||||
|
ownerShopId?: string
|
||||||
|
}) {
|
||||||
const [tab, setTab] = useState<TabFilter | "pending">("all")
|
const [tab, setTab] = useState<TabFilter | "pending">("all")
|
||||||
const orders = useOrderStore((state) => state.orders)
|
const orders = useOrderStore((state) => state.orders)
|
||||||
const sessions = useChatStore((state) => state.sessions)
|
const sessions = useChatStore((state) => state.sessions)
|
||||||
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
|
|
||||||
const ownerShopId = "shop1"
|
|
||||||
|
|
||||||
const tabs =
|
const tabs =
|
||||||
currentRole === "consumer" ? consumerTabs : currentRole === "player" ? playerTabs : ownerTabs
|
currentRole === "consumer" ? consumerTabs : currentRole === "player" ? playerTabs : ownerTabs
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
orders.forEach((order) => {
|
|
||||||
if (order.status === "pending_payment" || order.status === "cancelled") return
|
|
||||||
ensureOrderSession(order)
|
|
||||||
})
|
|
||||||
}, [orders, ensureOrderSession])
|
|
||||||
|
|
||||||
const roleFiltered = orders.filter((order) => {
|
const roleFiltered = orders.filter((order) => {
|
||||||
if (currentRole === "consumer") return order.consumerId === userId
|
if (currentRole === "consumer") return userId ? order.consumerId === userId : false
|
||||||
if (currentRole === "player") return order.playerId === userId
|
if (currentRole === "player") return userId ? order.playerId === userId : false
|
||||||
return order.shopId === ownerShopId
|
return ownerShopId ? order.shopId === ownerShopId : false
|
||||||
})
|
})
|
||||||
|
|
||||||
const filtered = roleFiltered.filter((order) => {
|
const filtered = roleFiltered.filter((order) => {
|
||||||
if (tab === "pending") return order.status === "pending_accept"
|
if (tab === "pending") return isPendingDispatch(order.status)
|
||||||
if (tab === "active") {
|
if (tab === "active") return isActiveOrder(order.status)
|
||||||
return [
|
if (tab === "completed") return isCompletedOrder(order.status, { includeCancelled: true })
|
||||||
"pending_payment",
|
if (tab === "disputed") return isDisputedOrder(order.status)
|
||||||
"pending_accept",
|
|
||||||
"in_progress",
|
|
||||||
"pending_close",
|
|
||||||
"pending_review",
|
|
||||||
].includes(order.status)
|
|
||||||
}
|
|
||||||
if (tab === "completed") return order.status === "completed" || order.status === "cancelled"
|
|
||||||
if (tab === "disputed") return order.status === "disputed"
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -119,7 +126,11 @@ function OrderListContent({ currentRole, userId }: { currentRole: UserRole; user
|
|||||||
|
|
||||||
<TabsContent value={tab} className="mt-4 space-y-3">
|
<TabsContent value={tab} className="mt-4 space-y-3">
|
||||||
{filtered.length === 0 ? (
|
{filtered.length === 0 ? (
|
||||||
<div className="text-center py-12 text-muted-foreground">暂无订单</div>
|
<Card>
|
||||||
|
<CardContent className="py-8 text-center text-sm text-muted-foreground">
|
||||||
|
暂无订单
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
) : (
|
) : (
|
||||||
filtered.map((order) => (
|
filtered.map((order) => (
|
||||||
<Card key={order.id}>
|
<Card key={order.id}>
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { OrderStatus } from "@/lib/types"
|
||||||
|
|
||||||
|
const ACTIVE_STATUSES: ReadonlySet<OrderStatus> = new Set([
|
||||||
|
"pending_payment",
|
||||||
|
"pending_accept",
|
||||||
|
"in_progress",
|
||||||
|
"pending_close",
|
||||||
|
"pending_review",
|
||||||
|
])
|
||||||
|
|
||||||
|
const COMPLETED_STATUSES: ReadonlySet<OrderStatus> = new Set(["completed"])
|
||||||
|
const DISPUTED_STATUSES: ReadonlySet<OrderStatus> = new Set(["disputed"])
|
||||||
|
const PENDING_DISPATCH_STATUSES: ReadonlySet<OrderStatus> = new Set(["pending_accept"])
|
||||||
|
|
||||||
|
export function isActiveOrder(status: OrderStatus) {
|
||||||
|
return ACTIVE_STATUSES.has(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isCompletedOrder(status: OrderStatus, options?: { includeCancelled?: boolean }) {
|
||||||
|
if (options?.includeCancelled && status === "cancelled") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return COMPLETED_STATUSES.has(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isDisputedOrder(status: OrderStatus) {
|
||||||
|
return DISPUTED_STATUSES.has(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isPendingDispatch(status: OrderStatus) {
|
||||||
|
return PENDING_DISPATCH_STATUSES.has(status)
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import type { Shop } from "@/lib/types"
|
||||||
|
|
||||||
|
export function resolveOwnerShop(userId: string | undefined, shops: Shop[]): Shop | null {
|
||||||
|
if (!userId) return null
|
||||||
|
return shops.find((shop) => shop.owner.id === userId) ?? null
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user