-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from cophilot/5-favorite-games
added gamebutton and favorite section
- Loading branch information
Showing
9 changed files
with
174 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.btn-template { | ||
border: 2px dashed var(--font-color); | ||
} |
77 changes: 77 additions & 0 deletions
77
src/components/FavoriteGameSection/FavoriteGameSection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useState } from 'react'; | ||
import './FavoriteGameSection.scss'; | ||
import { getSortedGameNames } from '../../allGames'; | ||
import LocalStorageService from '../../utils/LocalStorageService'; | ||
import GameButton from '../GameButton/GameButton'; | ||
|
||
/** | ||
* This is a FavoriteGameSection component | ||
* @author cophilot | ||
* @version 1.0.0 | ||
* @created 2024-8-7 | ||
*/ | ||
function FavoriteGameSection() { | ||
const [addingMode, setAddingMode] = useState(false); | ||
const [favoriteGames, setFavoriteGamesInternal] = useState<string[]>( | ||
LocalStorageService.getFavoriteGames() | ||
); | ||
const games = getSortedGameNames(); | ||
|
||
const setFavoriteGames = (favoriteGames: string[]) => { | ||
setFavoriteGamesInternal(favoriteGames); | ||
LocalStorageService.setFavoriteGames(favoriteGames); | ||
}; | ||
|
||
const templateButtonStyle = { border: '2px dashed var(--font-color)' }; | ||
|
||
const onGameClick = (game: string) => { | ||
if (addingMode) { | ||
if (favoriteGames.includes(game)) { | ||
setFavoriteGames(favoriteGames.filter((g) => g !== game)); | ||
} else { | ||
setFavoriteGames([...favoriteGames, game]); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="ver"> | ||
{addingMode | ||
? games.map((game) => ( | ||
<button | ||
key={game} | ||
className={ | ||
'btn wide ' + | ||
(favoriteGames.includes(game) ? 'selected' : '') | ||
} | ||
style={ | ||
favoriteGames.includes(game) | ||
? {} | ||
: templateButtonStyle | ||
} | ||
onClick={() => onGameClick(game)}> | ||
{game} | ||
</button> | ||
)) | ||
: favoriteGames.map((game) => ( | ||
<GameButton game={game} key={game} /> | ||
))} | ||
<i | ||
className={ | ||
'bi icon ' + getIconClassName(addingMode, favoriteGames) | ||
} | ||
onClick={() => setAddingMode(!addingMode)}></i> | ||
</div> | ||
); | ||
} | ||
export default FavoriteGameSection; | ||
|
||
function getIconClassName(addingMode: boolean, favoriteGames: string[]) { | ||
if (addingMode) { | ||
return 'bi-check-lg'; | ||
} | ||
if (favoriteGames.length === 0) { | ||
return 'bi-plus-circle'; | ||
} | ||
return 'bi-pencil-square'; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
import './GameButton.scss'; | ||
import StringUtils from '../../utils/StringUtils'; | ||
|
||
interface GameButtonProps { | ||
game: string; | ||
asLink?: boolean; | ||
link?: string; | ||
} | ||
|
||
/** | ||
* This is a GameButton component | ||
* @author cophilot | ||
* @version 1.0.0 | ||
* @created 2024-8-7 | ||
*/ | ||
function GameButton({ game, asLink = false, link = '' }: GameButtonProps) { | ||
const navigate = useNavigate(); | ||
|
||
if (asLink) { | ||
return ( | ||
<div className="btn selected" style={{ width: '250px' }}> | ||
<a | ||
className="" | ||
href={link} | ||
target="_blank" | ||
style={{ color: 'white' }}> | ||
{game} <i className="bi bi-arrow-up-right-square"></i> | ||
</a> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<button | ||
className="btn selected wide" | ||
onClick={() => { | ||
navigate(`/game/${StringUtils.gameNameToPath(game)}`); | ||
}}> | ||
{game}{' '} | ||
</button> | ||
); | ||
} | ||
export default GameButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,9 @@ a { | |
.imp { | ||
color: var(--primary-color); | ||
} | ||
|
||
.icon { | ||
font-size: 30px; | ||
margin: 10px; | ||
cursor: pointer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default class LocalStorageService { | ||
private static FAVORITE_GAMES_KEY = 'bsh-favorite-games'; | ||
|
||
static getFavoriteGames() { | ||
const favoriteGames = localStorage.getItem( | ||
LocalStorageService.FAVORITE_GAMES_KEY | ||
); | ||
if (favoriteGames === null) { | ||
return []; | ||
} | ||
return JSON.parse(favoriteGames); | ||
} | ||
|
||
static setFavoriteGames(favoriteGames: string[]) { | ||
localStorage.setItem( | ||
LocalStorageService.FAVORITE_GAMES_KEY, | ||
JSON.stringify(favoriteGames) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters