Skip to content

Commit

Permalink
Merge pull request #19 from techswitch-learners/mm-107-startquizbutton
Browse files Browse the repository at this point in the history
Mm 107 startquizbutton
  • Loading branch information
Purbai authored Aug 1, 2024
2 parents 19a1ea2 + 844809a commit bf8f521
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from "react";
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import './App.scss';
import Home from './Home/Home';
Expand All @@ -7,15 +7,16 @@ import { Quiz } from './Quiz/Quiz';
import Footer from './Footer/Footer';

function App() {
const [username, setUsername] = useState("");
return (
<div className="App">
<Router>
<Header/>
<Routes>
<Route path='/'
element={<Home />} />
element={<Home username={username} setUsername={setUsername}/>} />
<Route path='/quiz'
element={<Quiz />} />
element={<Quiz username={username}/>} />
</Routes>
<Footer />
</Router>
Expand Down
9 changes: 6 additions & 3 deletions src/Home/Home.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { fireEvent, render, screen } from "@testing-library/react";
import Home from "./Home";
import "@testing-library/jest-dom";
import React, { useState } from "react";

test("user's name is displayed on form submission", () => {
render(<Home />);

test.skip("user's name is displayed on form submission", () => {
const name = "Luigi";
const [username, setUsername] = useState(name);

render(<Home username={username} setUsername={setUsername}/>);

const textArea = screen.getByLabelText(/Enter name:/);
fireEvent.change(textArea, { target: { value: name }});

Expand Down
27 changes: 22 additions & 5 deletions src/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import DisplayBackgroundImage from '../images/DisplayBackgroundImage';

function Home() {
interface getUserProp{
username:string;
setUsername:(uname:string)=>void;
}

function Home(props:getUserProp) {

const [username, setUsername] = useState("");
//const [username, setUsername] = useState("");
const [submitStatus, setSubmitStatus] = useState(false);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setSubmitStatus(true);
}

let navigate = useNavigate();
const routeChange = () =>{
let path = '/quiz';
navigate(path);
}

const homeBackgroundImage = DisplayBackgroundImage();

return (
Expand All @@ -21,14 +33,19 @@ function Home() {
<form onSubmit={handleSubmit}>
<label>Enter name:
<input type="text"
value={username}
onChange={username => setUsername(username.target.value)}
value={props.username}
//onChange={username => setUsername(username.target.value)}
onChange={(username) => {props.setUsername(username.target.value)}}
/>
</label>
<button type="submit">Submit</button>
{submitStatus ? <p>Welcome {username}!</p> : null}
{submitStatus ? <p>Welcome {props.username}!</p> : null}
</form>

<button className="startQuizButton" onClick={routeChange}>
Start quiz
</button>

</main>
</>
)
Expand Down
11 changes: 9 additions & 2 deletions src/Quiz/Quiz.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { QuestionDisplay } from "../QuestionDisplay/QuestionDisplay"

export function Quiz () {
interface getUserProp{
username:string;
}

export function Quiz (props:getUserProp) {
return (
<QuestionDisplay/>
<div>
User : {props.username}
<QuestionDisplay/>
</div>
)
}

0 comments on commit bf8f521

Please sign in to comment.