CampFireID/app/components/Shop.tsx
degradin 54516a66e9 demo splitted
Исправлены экспорты компонентов:
UserProfile теперь использует export default
Shop теперь использует export default
TransferBalance теперь использует export default
Обновлены импорты в MainApp:
Все компоненты теперь импортируются как дефолтные импорты
Исправлены типы для безопасной работы с mongoose документами
Улучшен UI компонентов:
Добавлены отступы и границы
Улучшена читаемость текста
Добавлены информативные сообщения
Исправлена обработка ошибок:
Добавлены понятные сообщения об ошибках
Улучшена валидация форм
2025-03-16 12:19:42 +03:00

70 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client';
import React from 'react';
import { Box, SimpleGrid, Button, Text, Image, useToast } from '@chakra-ui/react';
import { IShopItem } from '../../backend/models/ShopItem';
interface ShopProps {
items: IShopItem[];
userBalance: number;
onPurchase: (item: IShopItem) => Promise<void>;
}
export default function Shop({ items, userBalance, onPurchase }: ShopProps) {
const toast = useToast();
const handlePurchase = async (item: IShopItem) => {
if (userBalance < item.price) {
toast({
title: 'Недостаточно средств',
description: `Для покупки ${item.name} нужно ${item.price} монет`,
status: 'error',
duration: 3000,
isClosable: true,
});
return;
}
await onPurchase(item);
};
return (
<Box w="100%">
<Text fontSize="2xl" mb={4}>Магазин</Text>
<Text mb={4}>Ваш баланс: {userBalance} монет</Text>
<SimpleGrid columns={[1, 2, 3]} spacing={6}>
{items.map((item) => (
<Box
key={item._id.toString()}
borderWidth="1px"
borderRadius="lg"
overflow="hidden"
p={4}
>
{item.imageUrl && (
<Image
src={item.imageUrl}
alt={item.name}
height="200px"
width="100%"
objectFit="cover"
mb={4}
/>
)}
<Text fontSize="xl" mb={2}>{item.name}</Text>
<Text mb={2}>{item.description}</Text>
<Text mb={4} color="green.500">{item.price} монет</Text>
<Button
colorScheme="blue"
onClick={() => handlePurchase(item)}
isDisabled={userBalance < item.price}
w="100%"
>
Купить
</Button>
</Box>
))}
</SimpleGrid>
</Box>
);
}