-
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.
DONE 2nd screen, Duplicates and Card
- Loading branch information
1 parent
a2055e8
commit 6a617bd
Showing
5 changed files
with
96 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
# Other | ||
TODO.txt |
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,37 @@ | ||
import { useState } from "react"; | ||
|
||
function Card({ | ||
value, | ||
index, | ||
input, | ||
setInput, | ||
setHasNoDuplicates, | ||
setFirstRender | ||
}) { | ||
|
||
const handleCloseButton = ev => { | ||
const cardElement = ev.target.nextSibling | ||
const selectedChar = cardElement.innerText; | ||
const indexSelectedChar = index | ||
|
||
console.log({ selectedChar, indexSelectedChar }) | ||
const removedDuplicates = Array.from(input) | ||
.filter((char, index) => | ||
char !== selectedChar || index === indexSelectedChar) | ||
.join(""); | ||
setInput(removedDuplicates) | ||
setHasNoDuplicates(new Set(removedDuplicates).size === removedDuplicates.length) | ||
setFirstRender(true) | ||
} | ||
|
||
return ( | ||
<div> | ||
<button className="card"> | ||
<span onClick={handleCloseButton}>❌</span> | ||
<h3>{value}</h3> | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
export default Card; |
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,49 @@ | ||
import { useLocation, useNavigate } from "react-router-dom"; | ||
import Card from "../components/Card"; | ||
import { useState } from "react" | ||
|
||
function Duplicates() { | ||
const navigate = useNavigate() | ||
const location = useLocation(); | ||
const [input, setInput] = useState(location.state.originalInput); | ||
const [hasNoDuplicates, setHasNoDuplicates] = useState(false) | ||
const [isFirstRender, setIsFirstRender] = useState(false) | ||
|
||
function Prompt() { | ||
return ( | ||
<div> | ||
{hasNoDuplicates | ||
? <p>NO DUPLICATES 😁</p> | ||
: <p>Still Duplicates 😭</p>} | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => navigate(-1)}> ⬅️ GO BACK</button> | ||
<h1>You're in the second page</h1> | ||
<p>Heres your ORIGINAL input {location.state.originalInput}</p> | ||
<p>Heres your CURRENT input {input}</p> | ||
|
||
{ | ||
isFirstRender | ||
? <Prompt /> | ||
: null | ||
} | ||
<div className="cards"> | ||
{Array.from(input).map((char, index) => <Card | ||
value={char} | ||
key={index} | ||
index={index} | ||
input={input} | ||
setInput={setInput} | ||
setHasNoDuplicates={setHasNoDuplicates} | ||
setFirstRender={setIsFirstRender} | ||
/>)} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Duplicates; |
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