44 | );
45 | }
46 |
47 | export default App;
48 |
--------------------------------------------------------------------------------
/src/hooks/useDataManager.js:
--------------------------------------------------------------------------------
1 | import { useEffect, useState } from "react";
2 | import fakeData from "../data.json"; // Just temporary dummy data
3 |
4 | /**
5 | * Custom hook to manage our app data
6 | */
7 | const useDataManager = () => {
8 | const [taskList, setTaskList] = useState([]); // The array of all fetched tasks - type: array of Task
9 | const [isLoading, setIsLoading] = useState(false); // Indicator, if data is currently loading
10 | const [loadingError, setLoadingError] = useState(null); // contains the error from data loading - type Error | null
11 |
12 | // Initial loading the data
13 | useEffect(() => {
14 | setIsLoading(true);
15 |
16 | try {
17 | // useEffect doesn't allow async so we wrap the async tasks
18 | // into a function and call the fuction later
19 | const loadDataFromApi = async () => {
20 | // Here will the data fetch magic happen
21 | setTaskList(fakeData);
22 | }
23 |
24 | loadDataFromApi(); // Execute the async function
25 | } catch (err) {
26 | // Handle the error case
27 | setTaskList([]);
28 | setLoadingError(err);
29 | } finally {
30 | // Either successfull or with error, now we are done loading data :)
31 | setIsLoading(false);
32 | }
33 |
34 | }, []);
35 |
36 | return {
37 | taskList,
38 | isLoading,
39 | loadingError
40 | }
41 | }
42 |
43 | export default useDataManager;
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/components/Header/Header.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect } from 'react';
2 | import useConfigManager from '../../hooks/useConfigManager';
3 | import './styles.css';
4 |
5 | const Header = () => {
6 | const { currentConfig, isDarkMode, toggleMode } = useConfigManager(); // Custom hook to manage app config
7 |
8 | /**
9 | * Event handler for the theme button
10 | * calls the toggleMode mthod from our
11 | * custom hook 'useConfigManger'
12 | */
13 | const onBtnModeClicked = () => {
14 | toggleMode(); // Toggles the mode and stores the new config
15 | }
16 |
17 | // Debug only
18 | useEffect(() => {
19 | if (!currentConfig) return;
20 |
21 | console.log(':: current mode is', isDarkMode(), currentConfig);
22 | }, [isDarkMode, currentConfig]);
23 |
24 | return (
25 |
26 |
Productivity To Do List
27 |
28 |
31 |
32 |
33 | );
34 | };
35 |
36 | export default Header;
37 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/hooks/useConfigManager.js:
--------------------------------------------------------------------------------
1 | import { useEffect, useState } from "react"
2 |
3 | const LOCAL_STORAGE_KEY = "tt-todo";
4 |
5 | /*
6 | type Config = {
7 | theme: "default" | "dark"
8 | }
9 | */
10 |
11 | /**
12 | * Custom hook to manage the application configuration
13 | */
14 | const useConfigManager = () => {
15 | const [currentConfig, setCurrentConfig] = useState(null) // type: Config | null
16 |
17 | /**
18 | * Sets the UI mode to default (light) or dark
19 | * @param {string} newMode "default" | "dark"
20 | */
21 | const setMode = (newMode) => { // type: "default" | "dark"
22 | if (!(newMode in ["default", "dark"])) return; // param makes no sense...
23 | setCurrentConfig({ ...currentConfig, theme: newMode });
24 | }
25 |
26 | /**
27 | * Toggles the UI mode between dark mode and default (light) mode
28 | */
29 | const toggleMode = () => {
30 | const newMode = currentConfig.theme === "dark" ? "default" : "dark";
31 | setCurrentConfig({ ...currentConfig, theme: newMode });
32 | }
33 |
34 | // Tries to load a former saved config from the browser's localStorage
35 | // and uses it or stores a new default config and uses this one
36 | useEffect(() => {
37 | let savedConfig;
38 | const savedConfigTxt = localStorage.getItem(LOCAL_STORAGE_KEY);
39 |
40 | // Try getting former stored config
41 | if (!savedConfigTxt) {
42 | // nope, not found, so we save a default config
43 | savedConfig = { theme: "dark" }
44 | localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(savedConfig));
45 | }
46 | else {
47 | savedConfig = JSON.parse(savedConfigTxt);
48 | }
49 |
50 | // This setting triggers the text useEffect below
51 | setCurrentConfig(savedConfig);
52 | }, []);
53 |
54 | // Called every time the config changes
55 | useEffect(() => {
56 | if (!currentConfig) return; // Nothing to do
57 |
58 | if (currentConfig.theme === "dark") {
59 | // Add the css-class to the body tag, if we are in dark mode
60 | document.body.classList.add("dark");
61 | }
62 | else {
63 | // Remove the dark mode css-class
64 | document.body.classList.remove("dark");
65 | }
66 |
67 | // Store the new config
68 | localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(currentConfig));
69 | }, [currentConfig])
70 |
71 | return {
72 | currentConfig,
73 | setMode,
74 | toggleMode,
75 | isDarkMode: () => currentConfig.theme === "dark"
76 | }
77 | }
78 |
79 | export default useConfigManager;
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------