Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 1 and 2 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
import Profile from "./components/Profile";
import { ProfileData } from "./common-types";
import "./styles.css";
import { ProfileContext } from "./ProfileContext"

const Users: ProfileData[] = [
{
Expand Down Expand Up @@ -66,26 +67,30 @@ const App = () => {
);
const [fullName, setFullName] = useState<string>();

useEffect(() => {
setFullName(`${profileData.firstName} ${profileData.lastName}`);
}, [profileData.firstName, profileData.lastName]);
const handleClick = () => {
setProfileData(
(prevProfileData) =>
Users.filter(
(user) => user.firstName !== prevProfileData.firstName
)[Math.floor(Math.random() * (Users.length - 1))]
);
setFullName(`${profileData.firstName} ${profileData.lastName}`);
};

return (
<main>
<h1>Welcome to {fullName}'s Page</h1>
<button
onClick={() =>
setProfileData(
(prevProfileData) =>
Users.filter(
(user) => user.firstName !== prevProfileData.firstName
)[Math.floor(Math.random() * (Users.length - 1))]
)
handleClick()
}
>
Randomize User
</button>
<Profile profile={profileData} />
<ProfileContext.Provider value = {profileData}>
<Profile />
</ProfileContext.Provider>

</main>
);
};
Expand Down
13 changes: 13 additions & 0 deletions src/ProfileContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { createContext, useContext } from "react";
import { ProfileData } from "./common-types";

export const ProfileContext = createContext<ProfileData | null>(null);

export const useProfile = () => {
const context = useContext(ProfileContext);
if (!context) {
throw new Error("useProfile must be used within a ProfileProvider");
}
return context;
};

6 changes: 4 additions & 2 deletions src/components/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ProfileData } from "../common-types";
import ProfileCard from "./ProfileCard";
import { useSecondsElapsed } from "../hooks/useSecondsElapsed";
import { useProfile } from "../ProfileContext";

const Profile = ({ profile }: { profile: ProfileData }) => {
const Profile = () => {
const profile = useProfile();
const secondsElapsed = useSecondsElapsed(profile.firstName);
return (
<section className="profile">
<div>Wow, check out this really cool person below</div>
<div>Time spent viewing profile: {secondsElapsed} seconds.</div>
<ProfileCard profile={profile} />
<ProfileCard />
</section>
);
};
Expand Down
8 changes: 5 additions & 3 deletions src/components/ProfileCard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ProfileData } from "../common-types";
import ProfileCardContent from "./ProfileCardContent";
import ProfileCardHeader from "./ProfileCardHeader";
import { useProfile } from "../ProfileContext";

const ProfileCard = ({ profile }: { profile: ProfileData }) => {
const ProfileCard = () => {
const profile = useProfile();
return (
<div className="profile-card">
<ProfileCardHeader profile={profile} />
<ProfileCardContent profile={profile} />
<ProfileCardHeader />
<ProfileCardContent />
</div>
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/ProfileCardContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ProfileData } from "../common-types";
import { useProfile } from "../ProfileContext";

const ProfileCardContent = ({ profile }: { profile: ProfileData }) => {
const ProfileCardContent = () => {
const profile = useProfile();
return <p>{profile.bio}</p>;
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/ProfileCardHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ProfileData } from "../common-types";
import { useProfileViews } from "../hooks/useProfileViews";
import { useProfile } from "../ProfileContext";

const ProfileCardHeader = ({ profile }: { profile: ProfileData }) => {
const ProfileCardHeader = () => {
const profile = useProfile();
const profileViews = useProfileViews(profile.firstName);
return (
<div className="profile-card-header">
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useSecondsElapsed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import { useEffect, useState } from "react";
* @param key The key to use for resetting the timer
* @returns The number of seconds that have elapsed since the component was mounted
*/

export const useSecondsElapsed = (key: string) => {
const [secondsElapsed, setSecondsElapsed] = useState(0);

useEffect(() => {
// Reset the timer when the key changes
setSecondsElapsed(0);
console.log("seconds " + secondsElapsed);

setInterval(() => {
setSecondsElapsed((prevTime) => prevTime + 1);
const intervalId = setInterval(() => {
setSecondsElapsed((prevTime) => prevTime + 1); // Ensure we use the latest state
}, 1000);
return () => {
clearInterval(intervalId);
};
}, [key]);

return secondsElapsed;
Expand Down