├── public ├── _redirects ├── robots.txt ├── Habit.png ├── favicon.ico ├── profile.jpg ├── favicon-32x32.png ├── favicon-64x64.png ├── manifest.json └── index.html ├── tailwind.config.js ├── src ├── components │ ├── Navbar │ │ ├── Navbar.css │ │ └── Navbar.jsx │ ├── Footer.jsx │ ├── HabitAnalytics.jsx │ └── Habit.jsx ├── index.js ├── App.css ├── pages │ ├── Home.css │ ├── Home.jsx │ └── Profile.jsx ├── App.js ├── index.css └── context │ └── ThemeContext.js ├── .gitignore ├── package.json ├── LICENSE └── README.md /public/_redirects: -------------------------------------------------------------------------------- 1 | /* /index.html 200 -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/Habit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohanmistry231/Habit-Tracker-Frontend/master/public/Habit.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohanmistry231/Habit-Tracker-Frontend/master/public/favicon.ico -------------------------------------------------------------------------------- /public/profile.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohanmistry231/Habit-Tracker-Frontend/master/public/profile.jpg -------------------------------------------------------------------------------- /public/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohanmistry231/Habit-Tracker-Frontend/master/public/favicon-32x32.png -------------------------------------------------------------------------------- /public/favicon-64x64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohanmistry231/Habit-Tracker-Frontend/master/public/favicon-64x64.png -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | "./src/**/*.{js,jsx,ts,tsx}", 5 | ], 6 | theme: { 7 | extend: {}, 8 | }, 9 | plugins: [], 10 | } -------------------------------------------------------------------------------- /src/components/Navbar/Navbar.css: -------------------------------------------------------------------------------- 1 | @keyframes slideOpacityBloom { 2 | 0% { 3 | transform: translateX(-30%); 4 | opacity: 0; 5 | } 6 | 100% { 7 | transform: translateX(0); 8 | opacity: 1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | 6 | const root = ReactDOM.createRoot(document.getElementById('root')); 7 | root.render( 8 | 9 | 10 | 11 | ); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Own Habit Tracker", 3 | "description": "A Personal Habit Tracking Website", 4 | "start_url": ".", 5 | "display": "standalone", 6 | "theme_color": "#000000", 7 | "background_color": "#ffffff", 8 | "icons": [ 9 | { 10 | "src": "Habit.png", 11 | "sizes": "192x192", 12 | "type": "image/png" 13 | }, 14 | { 15 | "src": "Habit.png", 16 | "sizes": "512x512", 17 | "type": "image/png" 18 | }, 19 | { 20 | "src": "favicon-64x64.png", 21 | "sizes": "64x64", 22 | "type": "image/png" 23 | }, 24 | { 25 | "src": "favicon-32x32.png", 26 | "sizes": "32x32", 27 | "type": "image/png" 28 | } 29 | ] 30 | } -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pages/Home.css: -------------------------------------------------------------------------------- 1 | @keyframes slide-small { 2 | from { 3 | transform: translateX(100%); 4 | } 5 | to { 6 | transform: translateX(-100%); 7 | } 8 | } 9 | 10 | @keyframes slide-large { 11 | from { 12 | transform: translateX(420%); /* Start from halfway */ 13 | } 14 | to { 15 | transform: translateX(-100%); /* End at halfway */ 16 | } 17 | } 18 | 19 | .animate-slide-text { 20 | animation-timing-function: linear; 21 | animation-iteration-count: infinite; 22 | } 23 | 24 | @media (max-width: 1023px) { 25 | /* Small and medium screens */ 26 | .animate-slide-text { 27 | animation-name: slide-small; 28 | } 29 | } 30 | 31 | @media (min-width: 1024px) { 32 | /* Larger screens */ 33 | .animate-slide-text { 34 | animation-name: slide-large; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "habit-tracker", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.17.0", 7 | "@testing-library/react": "^13.4.0", 8 | "@testing-library/user-event": "^13.5.0", 9 | "react": "^18.3.1", 10 | "react-dom": "^18.3.1", 11 | "react-icons": "^5.3.0", 12 | "react-modal": "^3.16.1", 13 | "react-router-dom": "^7.0.1", 14 | "react-scripts": "5.0.1", 15 | "tailwindcss": "^3.4.15", 16 | "web-vitals": "^2.1.4" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | // src/App.js 2 | import React from "react"; 3 | import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; 4 | import Navbar from "./components/Navbar/Navbar"; 5 | import Footer from "./components/Footer"; 6 | import { ThemeProvider, useTheme } from "./context/ThemeContext"; 7 | import Home from "./pages/Home"; 8 | import Habit from "./components/Habit"; 9 | import Profile from "./pages/Profile"; 10 | 11 | function App() { 12 | const { isDarkMode } = useTheme(); 13 | 14 | return ( 15 | 16 |
21 | 22 |
23 | 24 | } /> 25 | } /> 26 | } /> 27 | 28 |
29 |
31 |
32 | ); 33 | } 34 | 35 | const MainApp = () => ( 36 | 37 | 38 | 39 | ); 40 | 41 | export default MainApp; 42 | -------------------------------------------------------------------------------- /src/components/Footer.jsx: -------------------------------------------------------------------------------- 1 | // src/components/Footer.js 2 | import React from "react"; 3 | import { useTheme } from "../context/ThemeContext"; // Adjust the path if necessary 4 | 5 | const Footer = () => { 6 | const { theme } = useTheme(); 7 | const isDarkMode = theme === "dark"; // Check if dark mode is active 8 | 9 | return ( 10 | 33 | ); 34 | }; 35 | 36 | export default Footer; 37 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | body { 6 | margin: 0; 7 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 8 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 9 | sans-serif; 10 | -webkit-font-smoothing: antialiased; 11 | -moz-osx-font-smoothing: grayscale; 12 | transition: background-color 0.3s ease; /* Smooth transition for background color */ 13 | } 14 | 15 | code { 16 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 17 | monospace; 18 | } 19 | 20 | /* General scrollbar styling for WebKit browsers (Chrome, Safari, etc.) */ 21 | ::-webkit-scrollbar { 22 | width: 6px; /* Thin scrollbar track */ 23 | height: 6px; /* For horizontal scrollbar */ 24 | } 25 | 26 | ::-webkit-scrollbar-track { 27 | background-color: var(--scrollbar-track-color); 28 | } 29 | 30 | ::-webkit-scrollbar-thumb { 31 | background-color: var(--scrollbar-thumb-color); 32 | border-radius: 10px; 33 | width: 12px; /* Larger thumb for easier visibility */ 34 | } 35 | 36 | ::-webkit-scrollbar-thumb:hover { 37 | background-color: var(--scrollbar-thumb-hover-color); 38 | } 39 | 40 | /* Firefox styling */ 41 | * { 42 | scrollbar-width: thin; /* Makes the scrollbar track thin */ 43 | scrollbar-color: var(--scrollbar-thumb-color) var(--scrollbar-track-color); /* Thumb color and track color */ 44 | } 45 | 46 | /* Hover effect for Firefox, using the pseudo-class */ 47 | *:hover { 48 | scrollbar-color: var(--scrollbar-thumb-hover-color) var(--scrollbar-track-color); 49 | } 50 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Own Habit Tracker 27 | 28 | 29 | 30 |
31 | 41 | 42 | -------------------------------------------------------------------------------- /src/context/ThemeContext.js: -------------------------------------------------------------------------------- 1 | import { createContext, useState, useContext, useEffect } from "react"; 2 | 3 | // Updated Colors based on Udemy palette 4 | const themes = { 5 | dark: { 6 | background: "#111827", 7 | color: "#e5e7eb", 8 | scrollbarTrack: "#1f2937", // Darker gray for track 9 | scrollbarThumb: "#4b5563", // Medium gray for thumb 10 | scrollbarThumbHover: "#6b7280", // Slightly lighter gray for hover 11 | }, 12 | light: { 13 | background: "#ffffff", 14 | color: "#111827", 15 | scrollbarTrack: "#e5e7eb", // Light gray for track 16 | scrollbarThumb: "#111827", // Dark color for thumb 17 | scrollbarThumbHover: "#374151", // Slightly darker gray for hover 18 | }, 19 | }; 20 | 21 | const ThemeContext = createContext(); 22 | 23 | export const ThemeProvider = ({ children }) => { 24 | const [theme, setTheme] = useState(localStorage.getItem("theme") || "light"); 25 | 26 | const toggleTheme = () => { 27 | const newTheme = theme === "light" ? "dark" : "light"; 28 | setTheme(newTheme); 29 | localStorage.setItem("theme", newTheme); 30 | }; 31 | 32 | useEffect(() => { 33 | const currentTheme = themes[theme]; 34 | document.body.style.backgroundColor = currentTheme.background; 35 | document.body.style.color = currentTheme.color; 36 | 37 | // Apply scrollbar colors based on the theme 38 | document.documentElement.style.setProperty( 39 | "--scrollbar-track-color", 40 | currentTheme.scrollbarTrack 41 | ); 42 | document.documentElement.style.setProperty( 43 | "--scrollbar-thumb-color", 44 | currentTheme.scrollbarThumb 45 | ); 46 | document.documentElement.style.setProperty( 47 | "--scrollbar-thumb-hover-color", 48 | currentTheme.scrollbarThumbHover 49 | ); 50 | }, [theme]); 51 | 52 | return ( 53 | 54 | {children} 55 | 56 | ); 57 | }; 58 | 59 | export const useTheme = () => useContext(ThemeContext); 60 | -------------------------------------------------------------------------------- /src/pages/Home.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import Modal from "react-modal"; // Modal package for adding habits 3 | import { useTheme } from "../context/ThemeContext"; // Import useTheme from ThemeContext 4 | import HabitAnalytics from "../components/HabitAnalytics"; 5 | import "./Home.css"; 6 | 7 | Modal.setAppElement("#root"); // Set root element for accessibility 8 | 9 | const Home = () => { 10 | const [habits, setHabits] = useState([]); // Habit list 11 | const [loading, setLoading] = useState(true); 12 | const { theme } = useTheme(); // Access theme from ThemeContext 13 | const isDarkMode = theme === "dark"; 14 | 15 | // Fetch habits on component mount 16 | useEffect(() => { 17 | const fetchHabits = async () => { 18 | try { 19 | const response = await fetch( 20 | "https://habit-tracker-backend-0woy.onrender.com/habits" 21 | ); 22 | if (!response.ok) { 23 | throw new Error(`HTTP error! Status: ${response.status}`); 24 | } 25 | const data = await response.json(); 26 | setHabits(data); 27 | } catch (error) { 28 | console.error("Error fetching habits:", error.message); 29 | } finally { 30 | setLoading(false); 31 | } 32 | }; 33 | 34 | fetchHabits(); 35 | }, []); 36 | 37 | return ( 38 |
43 |
48 |
52 | ✨ Own Your Journey: Track Honestly, Grow Consistently. ✨ 53 |
54 |
55 | {loading ? ( 56 |
57 |
58 |
59 | ) : ( 60 | 61 | )} 62 |
63 | ); 64 | }; 65 | 66 | export default Home; 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 13 | 14 | The page will reload when you make changes.\ 15 | You may also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 35 | 36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. 39 | 40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /src/components/HabitAnalytics.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import { useTheme } from "../context/ThemeContext"; // Importing your custom ThemeContext for dark mode 3 | import { useNavigate } from "react-router-dom"; 4 | import { FaFireAlt, FaCheckCircle, FaClipboardList } from "react-icons/fa"; // Icons for better UI 5 | 6 | const HabitAnalytics = ({ habits }) => { 7 | const [analytics, setAnalytics] = useState({ 8 | highestStreak: 0, 9 | totalCompletedHabits: 0, 10 | totalHabits: habits.length, 11 | }); 12 | const { theme } = useTheme(); // Accessing the theme from ThemeContext 13 | const navigate = useNavigate(); 14 | 15 | useEffect(() => { 16 | if (habits.length > 0) { 17 | const highestStreak = Math.max(...habits.map((habit) => habit.streak)); 18 | const totalCompletedHabits = habits.filter( 19 | (habit) => habit.is_completed 20 | ).length; 21 | 22 | setAnalytics({ 23 | highestStreak, 24 | totalCompletedHabits, 25 | totalHabits: habits.length, 26 | }); 27 | } 28 | }, [habits]); 29 | 30 | return ( 31 |
36 |

Habit Analytics

37 |
38 | {/* Highest Streak Card */} 39 |
46 |
47 | 48 |

Highest Streak

49 |
50 |

51 | {analytics.highestStreak} 52 |

53 |
54 | 55 | {/* Total Completed Habits Card */} 56 |
63 |
64 | 65 |

Total Completed Habits

66 |
67 |

68 | {analytics.totalCompletedHabits} 69 |

70 |
71 | 72 | {/* Total Habits Card */} 73 |
80 |
81 | 82 |

Total Habits

83 |
84 |

85 | {analytics.totalHabits} 86 |

87 |
88 |
89 | 90 | {/* Button to View All Habits */} 91 |
92 | 102 |
103 |
104 | ); 105 | }; 106 | 107 | export default HabitAnalytics; 108 | -------------------------------------------------------------------------------- /src/pages/Profile.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import { useTheme } from "../context/ThemeContext"; // For dark mode context 3 | import { useNavigate } from "react-router-dom"; 4 | import axios from "axios"; // Make sure to install axios: npm install axios 5 | 6 | const Profile = ({ user }) => { 7 | const { theme } = useTheme(); // Accessing theme for dark mode 8 | const [habits, setHabits] = useState([]); 9 | const [loading, setLoading] = useState(true); 10 | const [analytics, setAnalytics] = useState({ 11 | highestStreak: 0, 12 | totalCompletedHabits: 0, 13 | totalHabits: 0, 14 | }); 15 | const navigate = useNavigate(); 16 | 17 | useEffect(() => { 18 | // Fetch habits from the backend when the component mounts 19 | const fetchHabits = async () => { 20 | try { 21 | const response = await axios.get( 22 | "https://habit-tracker-backend-0woy.onrender.com/habits" 23 | ); // Replace with your actual endpoint 24 | const habitsData = response.data; 25 | setHabits(habitsData); 26 | } catch (error) { 27 | console.error("Error fetching habits:", error); 28 | } finally { 29 | setLoading(false); 30 | } 31 | }; 32 | 33 | fetchHabits(); 34 | }, []); 35 | 36 | useEffect(() => { 37 | if (habits.length > 0) { 38 | const highestStreak = Math.max(...habits.map((habit) => habit.streak)); 39 | const totalCompletedHabits = habits.filter( 40 | (habit) => habit.is_completed 41 | ).length; 42 | 43 | setAnalytics({ 44 | highestStreak, 45 | totalCompletedHabits, 46 | totalHabits: habits.length, 47 | }); 48 | } 49 | }, [habits]); 50 | 51 | return ( 52 |
57 | {/* Profile Header */} 58 |
59 | Profile Avatar 64 |
65 |

Rohan Mistry

66 |

67 | rohanmistry231@gmail.com 68 |

69 |
70 |
71 | 72 | {loading ? ( 73 |
74 |
75 |
76 | ) : ( 77 | <> 78 | {/* Habit Analytics */} 79 |
80 | {/* Highest Streak Card */} 81 |
88 |

Highest Streak

89 |

90 | {analytics.highestStreak} 91 |

92 |
93 | 94 | {/* Total Completed Habits Card */} 95 |
102 |

103 | Total Completed Habits 104 |

105 |

106 | {analytics.totalCompletedHabits} 107 |

108 |
109 | 110 | {/* Total Habits Card */} 111 |
118 |

Total Habits

119 |

120 | {analytics.totalHabits} 121 |

122 |
123 |
124 | 125 | {/* Action Buttons */} 126 |
127 | 137 |
138 | 139 | )} 140 |
141 | ); 142 | }; 143 | 144 | export default Profile; 145 | -------------------------------------------------------------------------------- /src/components/Navbar/Navbar.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, useRef } from "react"; 2 | import { Link, useLocation } from "react-router-dom"; 3 | import { useTheme } from "../../context/ThemeContext"; 4 | import "./Navbar.css"; 5 | 6 | const Navbar = () => { 7 | const { theme, toggleTheme } = useTheme(); 8 | const [isOpen, setIsOpen] = useState(false); 9 | const location = useLocation(); // Tracks current path 10 | const menuRef = useRef(null); // Ref for the mobile menu container 11 | const menuToggleRef = useRef(null); // Ref for the hamburger/cross button 12 | 13 | const openMenu = () => setIsOpen(true); // Open the menu 14 | const closeMenu = () => setIsOpen(false); // Close the menu 15 | const isDarkMode = theme === "dark"; 16 | 17 | const getLinkClass = (path) => { 18 | const isActive = location.pathname === path; 19 | return isActive 20 | ? "text-orange-500" // Active link style 21 | : `${ 22 | isDarkMode ? "text-gray-300" : "text-gray-800" 23 | } hover:text-orange-500`; 24 | }; 25 | 26 | // Close menu if click is outside the menu or the toggle button 27 | useEffect(() => { 28 | const handleClickOutside = (event) => { 29 | // Check if the click is outside the menu and the toggle button 30 | if ( 31 | menuRef.current && 32 | !menuRef.current.contains(event.target) && 33 | menuToggleRef.current && 34 | !menuToggleRef.current.contains(event.target) 35 | ) { 36 | closeMenu(); // Close the menu if clicked outside 37 | } 38 | }; 39 | 40 | // Add event listener for clicks outside the menu and toggle button 41 | document.addEventListener("mousedown", handleClickOutside); 42 | 43 | // Cleanup the event listener on component unmount 44 | return () => { 45 | document.removeEventListener("mousedown", handleClickOutside); 46 | }; 47 | }, []); 48 | 49 | return ( 50 | 170 | ); 171 | }; 172 | 173 | export default Navbar; 174 | -------------------------------------------------------------------------------- /src/components/Habit.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import Modal from "react-modal"; // Modal package for adding and updating habits 3 | import { FaSearch, FaUpload, FaEdit, FaTrash, FaTimes } from "react-icons/fa"; 4 | import { useTheme } from "../context/ThemeContext"; // Import useTheme from ThemeContext 5 | 6 | Modal.setAppElement("#root"); // Set root element for accessibility 7 | 8 | const Habit = () => { 9 | const [habits, setHabits] = useState([]); // Habit list 10 | const [searchTerm, setSearchTerm] = useState(""); // Search term 11 | const [isModalOpen, setIsModalOpen] = useState(false); // Modal state for adding/editing habit 12 | const [newHabit, setNewHabit] = useState({ name: "", description: "" }); // New or updated habit data 13 | const [editingHabitId, setEditingHabitId] = useState(null); // Track habit being edited 14 | const [sortOrder, setSortOrder] = useState(""); // Sorting state, default is descending 15 | const [loading, setLoading] = useState(true); 16 | const { theme } = useTheme(); // Access theme from ThemeContext 17 | const isDarkMode = theme === "dark"; 18 | 19 | // Fetch habits on component mount 20 | useEffect(() => { 21 | const fetchHabits = async () => { 22 | try { 23 | const response = await fetch( 24 | "https://habit-tracker-backend-0woy.onrender.com/habits" 25 | ); 26 | if (!response.ok) { 27 | throw new Error(`HTTP error! Status: ${response.status}`); 28 | } 29 | const data = await response.json(); 30 | setHabits(data); 31 | } catch (error) { 32 | console.error("Error fetching habits:", error.message); 33 | } finally { 34 | setLoading(false); 35 | } 36 | }; 37 | 38 | fetchHabits(); 39 | }, []); 40 | 41 | // Add or update a habit 42 | const saveHabit = () => { 43 | const url = editingHabitId 44 | ? `https://habit-tracker-backend-0woy.onrender.com/habits/${editingHabitId}` 45 | : "https://habit-tracker-backend-0woy.onrender.com/habits"; 46 | const method = editingHabitId ? "PATCH" : "POST"; 47 | 48 | fetch(url, { 49 | method, 50 | headers: { "Content-Type": "application/json" }, 51 | body: JSON.stringify(newHabit), 52 | }) 53 | .then((response) => response.json()) 54 | .then((data) => { 55 | if (editingHabitId) { 56 | // Update the habit in the list 57 | setHabits((prev) => 58 | prev.map((habit) => (habit._id === editingHabitId ? data : habit)) 59 | ); 60 | } else { 61 | // Add the new habit to the list 62 | setHabits((prev) => [...prev, data]); 63 | } 64 | setIsModalOpen(false); // Close modal 65 | setNewHabit({ name: "", description: "" }); // Reset form 66 | setEditingHabitId(null); // Clear editing state 67 | }) 68 | .catch((error) => console.error("Error saving habit:", error)); 69 | }; 70 | 71 | // Delete a habit 72 | const deleteHabit = (habitId) => { 73 | const confirmDelete = window.confirm( 74 | "Are you sure you want to delete this habit?" 75 | ); 76 | if (!confirmDelete) return; 77 | 78 | fetch(`https://habit-tracker-backend-0woy.onrender.com/habits/${habitId}`, { 79 | method: "DELETE", 80 | }) 81 | .then(() => { 82 | setHabits((prev) => prev.filter((habit) => habit._id !== habitId)); 83 | alert("Habit deleted successfully."); 84 | }) 85 | .catch((error) => console.error("Error deleting habit:", error)); 86 | }; 87 | 88 | const handleDeleteImage = (habitId) => { 89 | // Ask for confirmation before deleting the image 90 | const confirmDelete = window.confirm( 91 | "Are you sure you want to delete today's image?" 92 | ); 93 | if (!confirmDelete) return; 94 | 95 | // Proceed with deleting the image 96 | fetch( 97 | `https://habit-tracker-backend-0woy.onrender.com/habits/${habitId}/upload`, 98 | { 99 | method: "DELETE", 100 | } 101 | ) 102 | .then((response) => response.json()) 103 | .then((updatedHabit) => { 104 | // Update the habits state with the updated habit 105 | setHabits((prev) => 106 | prev.map((habit) => 107 | habit._id === updatedHabit._id ? updatedHabit : habit 108 | ) 109 | ); 110 | alert("Today's image has been deleted and streak updated."); 111 | }) 112 | .catch((error) => { 113 | console.error("Error deleting today's image:", error); 114 | alert("Failed to delete image and update streak."); 115 | }); 116 | }; 117 | 118 | const sortHabits = (order) => { 119 | setSortOrder(order); 120 | const sortedHabits = [...habits].sort((a, b) => { 121 | if (order === "asc") { 122 | return a.streak - b.streak; // Low to High 123 | } else { 124 | return b.streak - a.streak; // High to Low 125 | } 126 | }); 127 | setHabits(sortedHabits); 128 | }; 129 | 130 | // Handle daily upload for habit (unchanged) 131 | const handleDailyUpload = (habitId) => { 132 | const habit = habits.find((habit) => habit._id === habitId); 133 | const today = new Date().toDateString(); 134 | const uploads = habit?.uploads || []; 135 | const lastUpload = 136 | uploads.length > 0 137 | ? new Date(uploads[uploads.length - 1]?.date).toDateString() 138 | : null; 139 | 140 | if (lastUpload === today) { 141 | alert("Today's image is already uploaded."); 142 | return; 143 | } 144 | 145 | const fileInput = document.createElement("input"); 146 | fileInput.type = "file"; 147 | fileInput.accept = "image/*"; 148 | fileInput.onchange = (e) => { 149 | const selectedFile = e.target.files[0]; 150 | if (selectedFile) { 151 | const formData = new FormData(); 152 | formData.append("photo", selectedFile); 153 | 154 | fetch( 155 | `https://habit-tracker-backend-0woy.onrender.com/habits/${habitId}/upload`, 156 | { 157 | method: "POST", 158 | body: formData, 159 | } 160 | ) 161 | .then((response) => response.json()) 162 | .then((updatedHabit) => { 163 | setHabits((prev) => 164 | prev.map((habit) => 165 | habit._id === updatedHabit._id ? updatedHabit : habit 166 | ) 167 | ); 168 | alert("Streak updated!"); 169 | }) 170 | .catch((error) => 171 | console.error("Error uploading daily habit:", error) 172 | ); 173 | } 174 | }; 175 | fileInput.click(); 176 | }; 177 | 178 | // Filter habits based on search 179 | const filteredHabits = habits.filter((habit) => 180 | habit.name.toLowerCase().includes(searchTerm.toLowerCase()) 181 | ); 182 | 183 | return ( 184 |
189 |

🏆 Habits 🏆

190 |
191 | {/* Search Bar with Icon on Right */} 192 |
193 | setSearchTerm(e.target.value)} // Assuming you have a searchTerm state 204 | /> 205 | 210 |
211 | 212 | {/* Sort by Streak Dropdown */} 213 |
214 | {" "} 215 | {/* Added mt-4 to create gap on smaller screens */} 216 | 229 |
230 | 231 | {/* Add Habit Button */} 232 | 246 |
247 | 248 | {loading ? ( 249 |
250 |
251 |
252 | ) : ( 253 | <> 254 |
255 | {filteredHabits.map((habit) => ( 256 |
264 | {/* Title and Description */} 265 |
266 |

{habit.name}

267 |

268 | {habit.description} 269 |

270 |
271 | 272 | {/* Streak */} 273 |
274 | 281 | Streak: {habit.streak}🔥 282 | 283 |
284 | 285 | {/* Buttons */} 286 |
287 | 298 | 299 | 317 | 318 | 329 |
330 | 331 | {/* Delete Today's Upload Button */} 332 |
333 | 344 |
345 |
346 | ))} 347 |
348 | 349 | )} 350 | 351 | {/* Modal for Adding/Editing Habit */} 352 | {isModalOpen && ( 353 |
setIsModalOpen(false)} // Close the modal when clicking outside 356 | > 357 |
e.stopPropagation()} // Prevent closing the modal when clicking inside it 364 | > 365 | {/* Modal Title */} 366 |

371 | {editingHabitId ? "Edit Habit" : "Add Habit"} 372 |

373 | 374 | {/* Modal Form */} 375 |
376 | {/* Habit Name Field */} 377 |
378 | 386 | 396 | setNewHabit({ ...newHabit, name: e.target.value }) 397 | } 398 | /> 399 |
400 | 401 | {/* Habit Description Field */} 402 |
403 | 411 |