Created
September 28, 2023 10:33
-
-
Save pasmat/0447b146bbd41fde28cdf168b7b57779 to your computer and use it in GitHub Desktop.
gpt whiteboard test revised
This file contains hidden or 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
query: | |
you are an expert software developer | |
You are applying for a senior software developer job with high salary and you are highly incentivized to provide your best code | |
you are tasked to write the code for this in Next.js + Tailwind CSS | |
Take a deep breath and think step-by-step about how you will do this | |
In job interview, aiming to get the best quality code from you, you are tasked to create the following screens | |
Name | |
Age with following remarks: should refer to you by name once provided | |
Then depending if the use is over 18 show site screen or if under 18 show screen for kids | |
result: | |
This task can be tackled by creating a form for user input data, then using React state to control form values and render the specific components based on the user's age. Let's break it down: | |
1. First, create a context to globally store user's information: | |
```jsx | |
import { createContext, useState } from 'react'; | |
export const UserContext = createContext(); | |
export const UserProvider = ({ children }) => { | |
const [user, setUser] = useState(null); | |
return ( | |
<UserContext.Provider value={{ user, setUser }}> | |
{children} | |
</UserContext.Provider> | |
); | |
}; | |
``` | |
2. Set up Next.js app with UserProvider: | |
```jsx | |
import { UserProvider } from '../contexts/UserContext'; | |
function MyApp({ Component, pageProps }) { | |
return ( | |
<UserProvider> | |
<Component {...pageProps} /> | |
</UserProvider> | |
); | |
} | |
export default MyApp; | |
``` | |
3. Create a form: | |
```jsx | |
import { useState, useContext } from 'react'; | |
import { UserContext } from '../contexts/UserContext'; | |
import AdultScreen from './AdultScreen'; | |
import KidsScreen from './KidsScreen'; | |
export function Form() { | |
const { setUser } = useContext(UserContext); | |
const [name, setName] = useState(""); | |
const [email, setEmail] = useState(""); | |
const [age, setAge] = useState(""); | |
const handleSubmit = (event) => { | |
event.preventDefault(); | |
setUser({ name, age, email }); | |
}; | |
return ( | |
<form onSubmit={handleSubmit} className="..."> | |
<input type="text" value={name} onChange={(e) => setName(e.target.value)} className="..." placeholder="Name"/> | |
<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} className="..." placeholder="Email" /> | |
<input type="number" value={age} onChange={(e) => setAge(e.target.value)} className="..." placeholder="Age" /> | |
<button type="submit" className="...">Submit</button> | |
</form> | |
); | |
} | |
``` | |
4. Create screens for adults and kids: | |
```jsx | |
import {useContext} from 'react'; | |
import { UserContext } from '../contexts/UserContext'; | |
function AdultScreen() { | |
const { user } = useContext(UserContext); | |
return ( | |
<div className="..."> | |
<h2>Welcome, {user?.name}</h2> | |
// Additional content... | |
</div> | |
); | |
} | |
export default AdultScreen; | |
// Similar for KidsScreen | |
``` | |
5. Then, depending on the age, display a specific screen: | |
```jsx | |
import Form from './Form'; | |
import AdultScreen from './AdultScreen'; | |
import KidsScreen from './KidsScreen'; | |
import { useContext } from 'react'; | |
import { UserContext } from '../contexts/UserContext'; | |
function MainPage() { | |
const { user } = useContext(UserContext); | |
if (!user) { | |
return <Form />; | |
} else if (user.age >= 18) { | |
return <AdultScreen />; | |
} else { | |
return <KidsScreen />; | |
} | |
} | |
export default MainPage; | |
``` | |
Keep in mind this is an oversimplified example, there can be many errors and edge cases to handle. Usability, responsiveness and appearance should be tailored according to the actual requirements. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment