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

Home page #20

Merged
merged 11 commits into from
Oct 9, 2023
63 changes: 63 additions & 0 deletions src/client/components/home-page/create-habit-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useState } from "react";
import { COLOR_THEME, FONT_THEME } from "../../themes";
import styled from "styled-components";
import { NavigateFunction, useNavigate } from "react-router-dom";
import { Method, fetchServer, verifyAuth } from "../../scripts/fetch-server";
import HabitWidget from "./habit-widget";
// import "./home-page.css"




const StyledButton = styled.button`
background-color: ${COLOR_THEME.BUTTON};
font-family: ${FONT_THEME.BUTTON_FONT};
display: flex;
justify-content: center;
width: 1335px;
margin: 10px 0px 10px 0px;

`
interface CreateHabitButtonProps {
setUpdate: React.Dispatch<React.SetStateAction<number>>;

}

const createHabit = async (navigate: NavigateFunction, setUpdate: React.Dispatch<React.SetStateAction<number>>) => {
console.log("create habit");

const habitName = prompt("Enter habit name:");
if (habitName === null) return;

const response = verifyAuth(navigate, await fetchServer(Method.POST, "/createhabit", {name: habitName}));
console.log(response);

// refresh
setUpdate((update) => update + 1);
};


function CreateHabitButton({setUpdate}: CreateHabitButtonProps){

const navigate = useNavigate();

const handleCreateHabit = async () => {
// Call the callback function to create a habit
await createHabit(navigate, setUpdate);
};


return( <>

<StyledButton className=" btn btn-primary border-0" onClick={handleCreateHabit}>
+ Create New Habit
</StyledButton>

</>

);


}

export default CreateHabitButton;
52 changes: 52 additions & 0 deletions src/client/components/home-page/habit-list-widget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useState } from "react";
import { COLOR_THEME, FONT_THEME } from "../../themes";
import { NavigateFunction } from "react-router-dom";
import styled from "styled-components";
import CreateHabitButton from "./create-habit-button";
import HabitWidget from "./habit-widget";
import "./home-page.css"
import { UserHabit } from "../../../../models";

const BigWidgetStyle = styled.div`
background-color: ${COLOR_THEME.SECTION};
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
padding: 20px 10px 20px 10px;
margin-right: 40px;
margin-left: 40px;



`
interface HabitListWidgetProps {
setUpdate: React.Dispatch<React.SetStateAction<number>>;
habits: UserHabit[];
}


function HabitListWidget({setUpdate,habits}: HabitListWidgetProps ){

return( <>
<BigWidgetStyle className = "rounded">

{habits.map((habit) => (<>
<div><HabitWidget habit = {habit}/></div>
</>))}
<div><CreateHabitButton setUpdate={setUpdate} /></div>

</BigWidgetStyle>



\


</>
);


}

export default HabitListWidget;
63 changes: 63 additions & 0 deletions src/client/components/home-page/habit-widget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useEffect, useState } from "react";
import { COLOR_THEME, FONT_THEME } from "../../themes";
import { NavigateFunction, useNavigate } from "react-router-dom";
import styled from "styled-components";
import { UserHabit } from "../../../../models";
import CreateHabitButton from "./create-habit-button";
import { Link } from "react-router-dom";
import { Method, fetchServer } from "../../scripts/fetch-server";
// import "./home-page.css"


const StyledButton = styled.button`
background-color: ${COLOR_THEME.BUTTON};
font-family: ${FONT_THEME.BUTTON_FONT};
display: flex;
justify-content: center;
width: 1335px;
margin: 10px 0px 10px 0px;


`


interface HabitWidgetProps {
habit: UserHabit;
}

// function handleClick({habit}: HabitWidgetProps ) {
// <p><Link to={"/habit/"+habit.habitID}>{habit.name}</Link></p>

// console.log('Link clicked!');
// }


function HabitWidget({habit}: HabitWidgetProps){

const navigate = useNavigate(); // Get the navigate function from React Router

const handleClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
event.preventDefault(); // Prevent the default button click behavior
navigate("/habit/" + habit.habitID); // Programmatically navigate to the desired URL
};


return(
<StyledButton className=" btn btn-primary border-0" onClick={handleClick}>

<p>
{habit.name}
{/* Streak: {habit.currentStreak}
Number of Logged Days: {habit.numLoggedDays}
Percent Success Week: {habit.percentSuccessWeek}
Percent Success Lifetime: {habit.percentSuccessLifetime} */}
</p>

</StyledButton>

);


}

export default HabitWidget;
16 changes: 16 additions & 0 deletions src/client/components/home-page/home-page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


.flex-container {
display: flex;
background-color: DodgerBlue;
flex-direction: column;
}

.flex-container > div {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}


37 changes: 23 additions & 14 deletions src/client/components/home-page/home-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,10 @@ import { getDateToday } from "../../scripts/date";
import { UserHabit, UserInfo } from "../../../../models";
import { Loading } from "../css-components/loading";
import PercentIcon from "../shared-components/percent-icon";
import HabitListWidget from "./habit-list-widget"
import HabitWidget from "./habit-widget";

const createHabit = async (navigate: NavigateFunction, setUpdate: React.Dispatch<React.SetStateAction<number>>) => {
console.log("create habit");

const habitName = prompt("Enter habit name:");
if (habitName === null) return;

const response = verifyAuth(navigate, await fetchServer(Method.POST, "/createhabit", {name: habitName}));
console.log(response);

// refresh
setUpdate((update) => update + 1);
};

interface HomePageProps {
setUsername: (username: string) => void;
Expand All @@ -31,6 +22,7 @@ const HomePage: FC<HomePageProps> = ({ setUsername }) => {

console.log("Home Page");


const {year, month, day} = getDateToday();
const params = {
userID: undefined,
Expand Down Expand Up @@ -69,7 +61,7 @@ const HomePage: FC<HomePageProps> = ({ setUsername }) => {
<p>Percent Success Lifetime: {userInfo.percentSuccessLifetime}</p>
<p>Number of Logged Days: {userInfo.numLoggedDays}</p>
<p>Habits: {userInfo.habits.toString()}</p>

{/*
{
userInfo.habits.map((habit) => (<>
<p><Link to={"/habit/"+habit.habitID}>{habit.name} {habit.habitID}</Link></p>
Expand All @@ -80,8 +72,25 @@ const HomePage: FC<HomePageProps> = ({ setUsername }) => {
<li>Percent Success Lifetime: {habit.percentSuccessLifetime}</li>
</ul>
</>))
}
<button onClick ={() => createHabit(navigate, setUpdate)}>Create Habit</button>
} */}

<HabitListWidget setUpdate={setUpdate} habits={userInfo.habits}/>


{/* {

userInfo.habits.map((habit) => (<>
<HabitWidget userHabit={habit} />
<HabitListWidget setUpdate={setUpdate} userHabit={habit}/>

</>))
} */}







</>
};
Expand Down