Skip to content

Commit

Permalink
exchanged modal
Browse files Browse the repository at this point in the history
  • Loading branch information
daryazherya committed Apr 20, 2024
1 parent c04e580 commit e8e3ac5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 56 deletions.
23 changes: 16 additions & 7 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import Modal from './components/Modal/Modal';
import { randomRightWord } from './helpers/randomRightWord/randomRightWord';


function App() {

function Game({ rightWord, generateNewWord }) {
const [state, setState] = useState({
rightWord: randomRightWord(),
words: [],
inputWord: '',
})
const [activeModal, setActiveModal] = useState(true);
const numberEmpty = 6 - state.words.length;
const win = state.words.includes(state.rightWord);
const win = state.words.includes(rightWord);
const loser = state.words.length === 6 && !win;
const keyboardLettersColors = createColorfulKeyboard(state.rightWord, state.words);
const keyboardLettersColors = createColorfulKeyboard(rightWord, state.words);


const onEnter = () => {
setState(prev => {
console.log({prev});
if (prev.inputWord.length === 5) {
prev.words.push(prev.inputWord)
return {
Expand Down Expand Up @@ -54,17 +53,18 @@ function App() {
status={win}
activeModal={activeModal}
setActiveModal={setActiveModal}
setState={setState}
randomRightWord={randomRightWord}
onClick={generateNewWord}
/>
<Modal text={'К сожалению, ты не угадал слово.'}
status={loser}
activeModal={activeModal}
setActiveModal={setActiveModal}
onClick={generateNewWord}
/>
<div className='wrapper'>
<Words
numberEmpty={numberEmpty}
rightWord={rightWord}
state={state}
/>
</div>
Expand All @@ -79,4 +79,13 @@ function App() {
)
}

function App() {
const [rightWord, setRightWord] = useState(randomRightWord());
function generateNewWord () {
setRightWord(randomRightWord());
}

return <Game key={rightWord} rightWord={rightWord} generateNewWord={generateNewWord} />
}

export default App;
87 changes: 51 additions & 36 deletions src/components/Keyboard/Keyboard.jsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,70 @@
import { useEffect } from 'react';
import { alphabet } from '../../helpers/variables';

const Keyboard = ({ keyboardLettersColors, onEnter, addLetter, deleteLetter, state}) => {
import { useEffect } from "react";
import {
alphabet,
firstRow,
secondRow,
thirdRow,
} from "../../helpers/variables";
import KeyboardRow from "../KeyboardRow/KeyboardRow";

const Keyboard = ({
keyboardLettersColors,
onEnter,
addLetter,
deleteLetter,
state,
}) => {
useEffect(() => {
function handler(e) {
if (e.key === 'Enter') {
if (e.key === "Enter") {
onEnter();
} else if (e.key === 'Backspace') {
} else if (e.key === "Backspace") {
deleteLetter();
} else {
} else if (alphabet.includes(e.key)) {
addLetter(e.key.toUpperCase());
}

}

window.addEventListener('keydown', handler)
window.addEventListener("keydown", handler);

return () => {
window.removeEventListener('keydown', handler)
}
}, [state, onEnter, addLetter, deleteLetter]);
window.removeEventListener("keydown", handler);
};
}, []);

return (
<div className="wrapper-keyboard">
<div className='keyboard'>
{alphabet.map((letter, i) => {
return <button
key={i}
style={{ backgroundColor: keyboardLettersColors[letter.toUpperCase()] }}
name={letter.toUpperCase()}
onClick={(e) => addLetter(e.target.name)}
>
{letter.toUpperCase()}
<div className="keyboard">
<div className="wrapper-keyboard-button">
<KeyboardRow
row={firstRow}
addLetter={addLetter}
keyboardLettersColors={keyboardLettersColors}
/>
</div>
<div className="wrapper-keyboard-button">
<KeyboardRow
row={secondRow}
addLetter={addLetter}
keyboardLettersColors={keyboardLettersColors}
/>
</div>
<div className="wrapper-keyboard-button">
<button className="keyboard-button" onClick={onEnter}>
Enter
</button>
<KeyboardRow
row={thirdRow}
addLetter={addLetter}
keyboardLettersColors={keyboardLettersColors}
/>
<button className="keyboard-button" onClick={deleteLetter}>
Delete
</button>
})}
<button
className='keyboard-button'
onClick={onEnter}
>
Enter
</button>
<button
className='keyboard-button'
onClick={deleteLetter}
>
Delete
</button>
</div>
</div>
</div>
);
}
};

export default Keyboard;
export default Keyboard;
14 changes: 14 additions & 0 deletions src/components/KeyboardRow/KeyboardRow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const KeyboardRow = ({row, addLetter, keyboardLettersColors}) => {
return row.map((letter, i) => {
return <button
key={i}
style={{ backgroundColor: keyboardLettersColors[letter.toUpperCase()]}}
name={letter.toUpperCase()}
onClick={(e) => addLetter(e.target.name)}
>
{letter.toUpperCase()}
</button>
})
}

export default KeyboardRow;
14 changes: 7 additions & 7 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Modal = ({ text, status, activeModal, setActiveModal,setState, randomRightWord }) => {
const Modal = ({ text, status, activeModal, setActiveModal, onClick }) => {
if(!status) {
return null
}

return (
status && <div className={activeModal ? 'modal-active' : 'modal-close'}
<div className={activeModal ? 'modal-active' : 'modal-close'}
onClick={() => {
setActiveModal(!status)
setState({
rightWord: randomRightWord(),
words: [],
inputWord: '',
})
onClick()
}}
>
<div className="modal_content">
Expand Down
4 changes: 2 additions & 2 deletions src/components/Words/Words.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Letters from "./Letters";
import EmptyWord from "./EmptyWord";


const Words = ({state, numberEmpty}) => {
const Words = ({rightWord, numberEmpty, state}) => {


return <div className="words-style">
{state.words.map((word, i) => <Letters word={word} rightWord={state.rightWord} key={i}/>)}
{state.words.map((word, i) => <Letters word={word} rightWord={rightWord} key={i}/>)}
{numberEmpty > 0 && <EmptyWord inputWord={state.inputWord}/>}
{numberEmpty > 0 && Array(numberEmpty - 1).fill().map((_) => <EmptyWord />)}
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const alphabet = [
"m",
];

export const firstRow = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"];
export const secondRow = ["a", "s", "d", "f", "g", "h", "j", "k", "l", "z"];
export const thirdRow = ["x", "c", "v", "b", "n", "m"];

export const words = [
"HELLO",
"WORLD",
Expand Down
10 changes: 6 additions & 4 deletions src/styles/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
}

.keyboard {
display: grid;
margin: 25px;
grid-template-columns: repeat(10, 4fr);
grid-gap: 7px;
margin: 50px;
width: 500px;
}

.wrapper-keyboard-button {
display: flex;
justify-content: center;
}

.words-style {
Expand Down

0 comments on commit e8e3ac5

Please sign in to comment.