Elbette! Size Cyber Attacks sayfasına benzer bir sayfayı Material-UI ve React kullanarak nasıl oluşturabileceğinizi adım adım göstereceğim. Bu sayfa, oyunları listeleyen, filtreleyen ve detaylarını modal pencerede gösteren bir yapı olacaktır. Ayrıca, URL parametreleri kullanarak belirli oyunların açılmasını sağlayacağız.
Proje Yapısı
Öncelikle, projenizin dosya yapısını şu şekilde düzenleyebilirsiniz:
src/
├── components/
│ ├── GameCard.js
│ ├── GameFilter.js
│ └── GameModal.js
├── config/
│ └── gamesConfig.js
├── pages/
│ └── Games.js
├── App.js
└── index.js
1. gamesConfig.js
Oyunlarınızın verilerini bu dosyada tutabilirsiniz. Her oyun için bir nesne oluşturun:
// src/config/gamesConfig.js
const gamesConfig = [
{
id: 1,
title: 'Cyber Attack 1',
category: 'Action',
description: 'İlk cyber saldırı oyunu.',
image: 'https://via.placeholder.com/300',
// Diğer gerekli alanlar
},
{
id: 2,
title: 'Cyber Attack 2',
category: 'Adventure',
description: 'İkinci cyber saldırı oyunu.',
image: 'https://via.placeholder.com/300',
},
// Daha fazla oyun ekleyebilirsiniz
];
export default gamesConfig;
2. GameCard.js
Oyun kartlarını göstermek için bu bileşeni kullanacağız.
// src/components/GameCard.js
import React from 'react';
import { Card, CardMedia, CardContent, Typography, CardActions, Button } from '@mui/material';
const GameCard = ({ game, onPlay }) => {
return (
<Card>
<CardMedia
component="img"
height="140"
image={game.image}
alt={game.title}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
{game.title}
</Typography>
<Typography variant="body2" color="text.secondary">
{game.category}
</Typography>
</CardContent>
<CardActions>
<Button size="small" onClick={onPlay}>Play</Button>
</CardActions>
</Card>
);
};
export default GameCard;
3. GameFilter.js
Filtreleme ve arama işlemleri için bu bileşeni kullanacağız.
// src/components/GameFilter.js
import React from 'react';
import { Box, TextField, FormControl, InputLabel, Select, MenuItem } from '@mui/material';
const GameFilter = ({ categories, selectedCategory, onCategoryChange, onSearch }) => {
return (
<Box sx={{ display: 'flex', gap: 2, mb: 4 }}>
<FormControl sx={{ minWidth: 200 }}>
<InputLabel>Category</InputLabel>
<Select
value={selectedCategory}
label="Category"
onChange={(e) => onCategoryChange(e.target.value)}
>
{categories.map((category) => (
<MenuItem value={category} key={category}>{category}</MenuItem>
))}
</Select>
</FormControl>
<TextField
label="Search"
variant="outlined"
onChange={(e) => onSearch(e.target.value)}
sx={{ flexGrow: 1 }}
/>
</Box>
);
};
export default GameFilter;
4. GameModal.js
Seçili oyun hakkında detaylı bilgi göstermek için modal penceresi kullanacağız.
// src/components/GameModal.js
import React from 'react';
import { Modal, Box, Typography, Button } from '@mui/material';
const style = {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 400,
bgcolor: 'background.paper',
boxShadow: 24,
p: 4,
};
const GameModal = ({ game, open, onClose }) => {
if (!game) return null;
return (
<Modal
open={open}
onClose={onClose}
aria-labelledby="game-modal-title"
aria-describedby="game-modal-description"
>
<Box sx={style}>
<Typography id="game-modal-title" variant="h6" component="h2">
{game.title}
</Typography>
<Typography id="game-modal-description" sx={{ mt: 2 }}>
{game.description}
</Typography>
<Button onClick={onClose} sx={{ mt: 2 }}>Close</Button>
</Box>
</Modal>
);
};
export default GameModal;
5. Games.js
Kullanıcının oyunları filtreleyip seçtiği oyunun detayını görüntülemesini sağlayan ana sayfa bileşeni.
// src/pages/Games.js
import React, { memo, useCallback, useMemo, useState, useEffect } from 'react';
import { Container, Grid, Typography, Box } from '@mui/material';
import GameCard from '../components/GameCard';
import GameFilter from '../components/GameFilter';
import GameModal from '../components/GameModal';
import gamesConfig from '../config/gamesConfig'; // AllGames import edildi
import { useLocation, useNavigate } from 'react-router-dom';
const Games = () => {
const [selectedCategory, setSelectedCategory] = useState('All');
const [searchQuery, setSearchQuery] = useState('');
const [selectedGame, setSelectedGame] = useState(null);
const navigate = useNavigate();
const location = useLocation();
const handleCategoryChange = useCallback((category) => setSelectedCategory(category), []);
const handleSearchChange = useCallback((query) => setSearchQuery(query), []);
const handlePlay = useCallback((game) => {
if (game) {
navigate(`?game=${game.id}`);
} else {
navigate(location.pathname);
}
setSelectedGame(game);
}, [navigate, location.pathname]);
// URL parametresini oku ve oyunu seç
useEffect(() => {
const params = new URLSearchParams(location.search);
const gameId = params.get('game');
if (gameId) {
const game = gamesConfig.find(g => g.id === parseInt(gameId));
setSelectedGame(game || null);
} else {
setSelectedGame(null);
}
}, [location.search]);
// Kategori seçenekleri
const categories = ['All', 'Action', 'Adventure', 'Puzzle', 'Strategy', 'Sports', 'Simulation', 'Survival', 'Rhythm', 'Battle Royal', 'Racing'];
// AllGames listesinden gelen oyun verilerini filtreleme
const filteredGames = useMemo(() => gamesConfig.filter((game) => {
const matchesCategory = selectedCategory === 'All' || game.category === selectedCategory;
const matchesSearch = game.title.toLowerCase().includes(searchQuery.toLowerCase());
return matchesCategory && matchesSearch;
}), [selectedCategory, searchQuery]);
return (
<Box sx={{ py: 4 }}>
<Container maxWidth="lg">
<Typography variant="h3" sx={{ mb: 4 }}>
Games Library
</Typography>
{/* Filtreleme bileşeni */}
<GameFilter
categories={categories}
selectedCategory={selectedCategory}
onCategoryChange={handleCategoryChange}
onSearch={handleSearchChange}
/>
{/* Oyun Kartları */}
<Grid container spacing={4}>
{filteredGames.map((game) => (
<Grid item xs={12} sm={6} md={4} key={game.id}>
<GameCard game={game} onPlay={() => handlePlay(game)} />
</Grid>
))}
</Grid>
{/* Oyun Modal */}
<GameModal
game={selectedGame}
open={Boolean(selectedGame)}
onClose={() => handlePlay(null)}
/>
</Container>
</Box>
);
};
export default memo(Games);
6. App.js ve index.js
React Router'ı kullanarak sayfa yönlendirmesini ayarlayacağız.
// src/App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Games from './pages/Games';
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Games />} />
{/* Diğer rotalar eklenebilir */}
</Routes>
</Router>
);
};
export default App;
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import CssBaseline from '@mui/material/CssBaseline';
ReactDOM.render(
<React.StrictMode>
<CssBaseline />
<App />
</React.StrictMode>,
document.getElementById('root')
);
7. Paketlerin Kurulumu
Projenizde gerekli olan paketleri yüklediğinizden emin olun. Özellikle @mui/material, @emotion/react, @emotion/styled ve react-router-dom gereklidir.
npm install @mui/material @emotion/react @emotion/styled react-router-dom
veya
yarn add @mui/material @emotion/react @emotion/styled react-router-dom
8. Açıklamalar ve Özellikler
-
Filtreleme ve Arama: GameFilter bileşeni, kullanıcının oyunları kategoriye göre filtrelemesine ve başlıklarına göre arama yapmasına olanak tanır.
-
Oyun Kartları: GameCard bileşeni, her bir oyunu kart şeklinde gösterir. "Play" butonuna tıklandığında ilgili oyunun detayları modalde açılır ve URL'ye ?game=ID parametresi eklenir.
-
Modal Penceresi: GameModal bileşeni, seçilen oyunun detaylarını gösterir. Modal kapandığında URL parametresi kaldırılır.
-
URL Parametreleri: React Router kullanılarak, oyun seçildiğinde URL'ye ?game=ID parametresi eklenir. Sayfa yenilendiğinde veya direkt olarak bu URL'e gidildiğinde ilgili oyun otomatik olarak seçili olur ve modal açılır.
9. Sonuç
Bu yapı, istediğiniz gibi oyunları listeleyen, filtreleyen ve detaylarını modal pencerede gösteren bir sayfa oluşturmanıza yardımcı olacaktır. Ayrıca, URL parametreleri kullanarak belirli oyunların doğrudan açılmasını sağlar. Bu sayede kullanıcılar, belirli bir oyun bağlantısını paylaşabilir veya doğrudan o oyunu görüntüleyebilirler.
Projenizi geliştirdikçe, oyun verilerini dinamik olarak API'den çekebilir, sayfalama ekleyebilir veya daha gelişmiş özellikler entegre edebilirsiniz. Material-UI'nin zengin bileşen kütüphanesini kullanarak tasarımınızı daha da iyileştirebilirsiniz.
Başarılar dilerim! Herhangi bir sorunuz olursa yardımcı olmaktan memnuniyet duyarım.