├── .gitignore ├── README.md ├── README.old.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.tsx ├── components │ ├── AppHeader.tsx │ ├── Categories.tsx │ ├── Login.tsx │ ├── Logout.tsx │ ├── PrivateRoute.tsx │ ├── Records.tsx │ └── SignUp.tsx ├── index.tsx ├── react-app-env.d.ts ├── store │ ├── actions │ │ ├── categoryActions.ts │ │ ├── recordActions.ts │ │ └── userActions.ts │ ├── index.ts │ └── reducers │ │ ├── categoryReducer.ts │ │ ├── recordReducer.ts │ │ └── userReducer.ts ├── types │ ├── category.ts │ ├── general.ts │ ├── record.ts │ └── user.ts └── utils │ ├── api.ts │ ├── showError.ts │ └── showSuccess.ts └── tsconfig.json /.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 | -------------------------------------------------------------------------------- /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 the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will 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 | -------------------------------------------------------------------------------- /README.old.md: -------------------------------------------------------------------------------- 1 | React Dersleri kanalında yayınlanan Expense Tracker uygulaması eğitim serisi için hazırlanan boş proje. 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expense_tracker_fe", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@ant-design/icons": "^4.6.2", 7 | "@testing-library/jest-dom": "^5.14.1", 8 | "@testing-library/react": "^11.2.7", 9 | "@testing-library/user-event": "^12.8.3", 10 | "@types/jest": "^26.0.23", 11 | "@types/node": "^12.20.15", 12 | "@types/react": "^17.0.13", 13 | "@types/react-dom": "^17.0.8", 14 | "@types/react-router-dom": "^5.1.7", 15 | "antd": "^4.16.6", 16 | "axios": "^0.21.1", 17 | "react": "^17.0.2", 18 | "react-color": "^2.19.3", 19 | "react-dom": "^17.0.2", 20 | "react-redux": "^7.2.4", 21 | "react-router-dom": "^5.2.0", 22 | "react-scripts": "4.0.3", 23 | "redux": "^4.1.0", 24 | "redux-thunk": "^2.3.0", 25 | "typescript": "^4.3.5", 26 | "web-vitals": "^1.1.2" 27 | }, 28 | "scripts": { 29 | "start": "react-scripts start", 30 | "build": "react-scripts build", 31 | "test": "react-scripts test", 32 | "eject": "react-scripts eject" 33 | }, 34 | "eslintConfig": { 35 | "extends": [ 36 | "react-app", 37 | "react-app/jest" 38 | ] 39 | }, 40 | "browserslist": { 41 | "production": [ 42 | ">0.2%", 43 | "not dead", 44 | "not op_mini all" 45 | ], 46 | "development": [ 47 | "last 1 chrome version", 48 | "last 1 firefox version", 49 | "last 1 safari version" 50 | ] 51 | }, 52 | "devDependencies": { 53 | "@types/react-color": "^3.0.5" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/reactdersleri/expense_tracker_fe/2d16e92ca41a0422551b37165c9efecad718e977/public/favicon.ico -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/reactdersleri/expense_tracker_fe/2d16e92ca41a0422551b37165c9efecad718e977/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/reactdersleri/expense_tracker_fe/2d16e92ca41a0422551b37165c9efecad718e977/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import { Route } from "react-router-dom"; 2 | import { Layout } from "antd"; 3 | import SignUp from "./components/SignUp"; 4 | import Login from "./components/Login"; 5 | import PrivateRoute from "./components/PrivateRoute"; 6 | import Categories from "./components/Categories"; 7 | import Records from "./components/Records"; 8 | import AppHeader from "./components/AppHeader"; 9 | import Logout from "./components/Logout"; 10 | 11 | const { Content, Footer } = Layout; 12 | 13 | function App() { 14 | return ( 15 | 16 | 17 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | ); 32 | } 33 | 34 | export default App; 35 | -------------------------------------------------------------------------------- /src/components/AppHeader.tsx: -------------------------------------------------------------------------------- 1 | import { Menu } from "antd"; 2 | import { Header } from "antd/lib/layout/layout"; 3 | import React, { useEffect } from "react"; 4 | import { useDispatch, useSelector } from "react-redux"; 5 | import { Link, useLocation } from "react-router-dom"; 6 | import { AppState } from "../store"; 7 | import { isLoggedIn } from "../store/actions/userActions"; 8 | 9 | function AppHeader() { 10 | const { data, loading, error } = useSelector((state: AppState) => state.user); 11 | 12 | const dispatch = useDispatch(); 13 | 14 | useEffect(() => { 15 | dispatch(isLoggedIn()); 16 | }, []); 17 | 18 | const { pathname } = useLocation(); 19 | 20 | return ( 21 |
22 |
23 | 24 | {data.username ? ( 25 | 26 | 27 | Harcama Kayıtları 28 | 29 | 30 | Kategori 31 | 32 | 33 | Çıkış 34 | 35 | 36 | ) : loading ? null : ( 37 | 38 | Giriş 39 | 40 | )} 41 | 42 |
43 | ); 44 | } 45 | 46 | export default AppHeader; 47 | -------------------------------------------------------------------------------- /src/components/Categories.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | Button, 3 | Form, 4 | Input, 5 | Modal, 6 | Select, 7 | Space, 8 | Spin, 9 | Table, 10 | Tag, 11 | } from "antd"; 12 | import React, { useState } from "react"; 13 | import { useEffect } from "react"; 14 | import { useDispatch, useSelector } from "react-redux"; 15 | import { AppState } from "../store"; 16 | import { 17 | addCategory, 18 | deleteCategory, 19 | getCategories, 20 | updateCategory, 21 | } from "../store/actions/categoryActions"; 22 | import { Category, CategoryForm } from "../types/category"; 23 | import { SketchPicker } from "react-color"; 24 | import { DeleteOutlined, EditOutlined } from "@ant-design/icons"; 25 | import { Mode } from "../types/general"; 26 | 27 | const emptyForm: CategoryForm = { 28 | name: "", 29 | type: "expense", 30 | color: "black", 31 | }; 32 | 33 | function Categories() { 34 | const { data, loading, error } = useSelector( 35 | (state: AppState) => state.categories 36 | ); 37 | 38 | const [isModalVisible, setIsModalVisible] = useState(false); 39 | const [mode, setMode] = useState("new"); 40 | const [form, setForm] = useState(emptyForm); 41 | const [updateId, setUpdateId] = useState(null); 42 | const [deleteId, setDeleteId] = useState(null); 43 | 44 | const showModal = (mode: Mode) => { 45 | setIsModalVisible(true); 46 | setMode(mode); 47 | }; 48 | 49 | const handleOk = () => { 50 | // Mode degerine gore create or update action creator fonksiyonu cagir 51 | if (mode === "new") dispatch(addCategory(form)); 52 | else if (mode === "edit" && typeof updateId === "number") 53 | dispatch(updateCategory(form, updateId)); 54 | else if (mode === "delete" && typeof deleteId === "number") 55 | dispatch(deleteCategory(deleteId)); 56 | setIsModalVisible(false); 57 | setMode("new"); 58 | setForm(emptyForm); 59 | setUpdateId(null); 60 | }; 61 | 62 | const handleCancel = () => { 63 | setIsModalVisible(false); 64 | setMode("new"); 65 | setForm(emptyForm); 66 | setUpdateId(null); 67 | setDeleteId(null); 68 | }; 69 | 70 | const columns = [ 71 | { 72 | title: "Name", 73 | dataIndex: "name", 74 | key: "name", 75 | }, 76 | { 77 | title: "Type", 78 | dataIndex: "type", 79 | key: "id", 80 | render: (text: string, category: Category) => { 81 | return {text.toUpperCase()}; 82 | }, 83 | }, 84 | { 85 | title: "Action", 86 | dataIndex: "id", 87 | key: "action", 88 | render: (text: string, category: Category) => ( 89 | 90 | { 93 | showModal("edit"); 94 | setForm(category); 95 | setUpdateId(category.id); 96 | }} 97 | /> 98 | { 101 | showModal("delete"); 102 | setDeleteId(category.id); 103 | }} 104 | /> 105 | 106 | ), 107 | }, 108 | ]; 109 | 110 | const dispatch = useDispatch(); 111 | 112 | useEffect(() => { 113 | dispatch(getCategories()); 114 | }, []); 115 | 116 | return ( 117 | 118 |
119 |
126 | 129 |
130 | 131 | 144 | {mode === "edit" || mode === "new" ? ( 145 |
146 | 147 | setForm({ ...form, name: e.target.value })} 151 | /> 152 | 153 | 154 | 162 | 163 | 164 | setForm({ ...form, color: color.hex })} 167 | /> 168 | 169 |
170 | ) : mode === "delete" ? ( 171 | <>Are you sure? 172 | ) : null} 173 |
174 |
175 | 181 | 182 | ); 183 | } 184 | 185 | export default Categories; 186 | -------------------------------------------------------------------------------- /src/components/Login.tsx: -------------------------------------------------------------------------------- 1 | import { Form, Input, Result, Button } from "antd"; 2 | import { useEffect } from "react"; 3 | import { useDispatch, useSelector } from "react-redux"; 4 | import { useHistory, useLocation } from "react-router-dom"; 5 | import { AppState } from "../store"; 6 | import { login } from "../store/actions/userActions"; 7 | import { LoginForm } from "../types/user"; 8 | import showError from "../utils/showError"; 9 | import showSuccess from "../utils/showSuccess"; 10 | 11 | function Login() { 12 | const history = useHistory(); 13 | const location = useLocation<{ newSignUp?: boolean }>(); 14 | const dispatch = useDispatch(); 15 | 16 | const { data, loading, error } = useSelector((state: AppState) => state.user); 17 | 18 | const onFinish = (values: LoginForm) => { 19 | dispatch(login(values)); 20 | }; 21 | 22 | useEffect(() => { 23 | error && showError(error); 24 | }, [error]); 25 | 26 | useEffect(() => { 27 | data.username && showSuccess("You have successfully logged in!"); 28 | }, [data.username]); 29 | 30 | useEffect(() => { 31 | const token = localStorage.getItem("token"); 32 | if (token) { 33 | history.push("/"); 34 | } 35 | }, [data]); 36 | 37 | return ( 38 | 46 |

Please login

47 | {location.state?.newSignUp && ( 48 | 53 | )} 54 | 59 | 60 | 61 | 62 | 67 | 68 | 69 | 70 | 71 | 74 | 75 | 76 | ); 77 | } 78 | 79 | export default Login; 80 | -------------------------------------------------------------------------------- /src/components/Logout.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from "react"; 2 | import { useDispatch, useSelector } from "react-redux"; 3 | import { Redirect } from "react-router"; 4 | import { AppState } from "../store"; 5 | import { logout } from "../store/actions/userActions"; 6 | 7 | function Logout() { 8 | const { data } = useSelector((state: AppState) => state.user); 9 | 10 | const dispatch = useDispatch(); 11 | 12 | useEffect(() => { 13 | dispatch(logout()); 14 | }, []); 15 | 16 | if (!data.username) return ; 17 | 18 | return
Logging out...
; 19 | } 20 | 21 | export default Logout; 22 | -------------------------------------------------------------------------------- /src/components/PrivateRoute.tsx: -------------------------------------------------------------------------------- 1 | import { Redirect, Route, RouteProps } from "react-router-dom"; 2 | 3 | interface PrivateRouteProps extends RouteProps { 4 | component: React.FC; 5 | } 6 | 7 | function PrivateRoute({ component: Component, ...theRest }: PrivateRouteProps) { 8 | return ( 9 | { 12 | const token = localStorage.getItem("token"); 13 | 14 | if (token) { 15 | return ; 16 | } 17 | return ; 18 | }} 19 | /> 20 | ); 21 | } 22 | 23 | export default PrivateRoute; 24 | -------------------------------------------------------------------------------- /src/components/Records.tsx: -------------------------------------------------------------------------------- 1 | import { DeleteOutlined, EditOutlined } from "@ant-design/icons"; 2 | import { Button, Form, Input, Modal, Select, Space, Table, Tag } from "antd"; 3 | import React, { useState } from "react"; 4 | import { useEffect } from "react"; 5 | import { useDispatch, useSelector } from "react-redux"; 6 | import { AppState } from "../store"; 7 | import { getCategories } from "../store/actions/categoryActions"; 8 | import { 9 | addRecord, 10 | deleteRecord, 11 | getRecords, 12 | updateRecord, 13 | } from "../store/actions/recordActions"; 14 | import { Category } from "../types/category"; 15 | import { Mode } from "../types/general"; 16 | import { Record, RecordForm } from "../types/record"; 17 | 18 | const emptyForm: RecordForm = { 19 | title: "", 20 | amount: 0, 21 | category_id: 0, 22 | }; 23 | 24 | function Records() { 25 | const { data, loading, error } = useSelector( 26 | (state: AppState) => state.records 27 | ); 28 | 29 | const { data: categories } = useSelector( 30 | (state: AppState) => state.categories 31 | ); 32 | 33 | const [isModalVisible, setIsModalVisible] = useState(false); 34 | const [mode, setMode] = useState("new"); 35 | const [form, setForm] = useState(emptyForm); 36 | const [updateId, setUpdateId] = useState(null); 37 | const [deleteId, setDeleteId] = useState(null); 38 | 39 | const showModal = (mode: Mode) => { 40 | setIsModalVisible(true); 41 | setMode(mode); 42 | }; 43 | 44 | const handleOk = () => { 45 | if (mode === "new") dispatch(addRecord(form)); 46 | else if (mode === "edit" && typeof updateId === "number") 47 | dispatch(updateRecord(form, updateId)); 48 | else if (mode === "delete" && typeof deleteId === "number") 49 | dispatch(deleteRecord(deleteId)); 50 | setIsModalVisible(false); 51 | setMode("new"); 52 | setForm(emptyForm); 53 | setUpdateId(null); 54 | }; 55 | 56 | const handleCancel = () => { 57 | setIsModalVisible(false); 58 | setMode("new"); 59 | setForm(emptyForm); 60 | setUpdateId(null); 61 | setDeleteId(null); 62 | }; 63 | 64 | const columns = [ 65 | { 66 | title: "Title", 67 | dataIndex: "title", 68 | key: "title", 69 | }, 70 | { 71 | title: "Amount", 72 | dataIndex: "amount", 73 | key: "amount", 74 | render: (amount: Record["amount"], record: Record) => { 75 | return ( 76 | <> 77 | {Intl.NumberFormat("tr-TR", { 78 | style: "currency", 79 | currency: "TRY", 80 | }).format(amount)} 81 | 82 | ); 83 | }, 84 | }, 85 | { 86 | title: "Category", 87 | dataIndex: "category", 88 | key: "category", 89 | render: (category: Category, record: Record) => { 90 | return {category.name.toUpperCase()}; 91 | }, 92 | }, 93 | { 94 | title: "Last Update", 95 | dataIndex: "updatedAt", 96 | key: "updatedAt", 97 | render: (updatedAt: string, record: Record) => { 98 | const updatedAtObj = new Date(updatedAt); 99 | return ( 100 | <> 101 | {updatedAtObj.toLocaleDateString()}{" "} 102 | {updatedAtObj.toLocaleTimeString("tr-TR", { 103 | hour: "2-digit", 104 | minute: "2-digit", 105 | })} 106 | 107 | ); 108 | }, 109 | }, 110 | { 111 | title: "Action", 112 | dataIndex: "id", 113 | key: "action", 114 | render: (text: string, record: Record) => { 115 | const { title, amount } = record; 116 | const category_id = record.category.id; 117 | return ( 118 | 119 | { 122 | showModal("edit"); 123 | setForm({ title, amount, category_id }); 124 | setUpdateId(record.id); 125 | }} 126 | /> 127 | { 130 | showModal("delete"); 131 | setDeleteId(record.id); 132 | }} 133 | /> 134 | 135 | ); 136 | }, 137 | }, 138 | ]; 139 | 140 | const dispatch = useDispatch(); 141 | 142 | useEffect(() => { 143 | dispatch(getRecords()); 144 | !categories.length && dispatch(getCategories()); 145 | }, []); 146 | 147 | const isFormValid = !( 148 | !form.title || 149 | form.amount === 0 || 150 | form.category_id === 0 151 | ); 152 | 153 | return ( 154 | 155 |
156 |
163 | 166 |
167 | 180 | {mode === "edit" || mode === "new" ? ( 181 |
182 | 183 | setForm({ ...form, title: e.target.value })} 187 | /> 188 | 189 | 190 | 195 | setForm({ ...form, amount: Number(e.target.value) }) 196 | } 197 | /> 198 | 199 | 200 | 216 | 217 | 218 | ) : mode === "delete" ? ( 219 | <>Are you sure? 220 | ) : null} 221 |
222 |
223 |
229 | 230 | ); 231 | } 232 | 233 | export default Records; 234 | -------------------------------------------------------------------------------- /src/components/SignUp.tsx: -------------------------------------------------------------------------------- 1 | import { Form, Input, Button } from "antd"; 2 | import { useHistory } from "react-router-dom"; 3 | 4 | import api from "../utils/api"; 5 | import showError from "../utils/showError"; 6 | 7 | function SignUp() { 8 | const layout = { 9 | labelCol: { span: 8 }, 10 | wrapperCol: { span: 16 }, 11 | }; 12 | 13 | const validateMessages = { 14 | required: "${label} is required!", 15 | types: { 16 | email: "${label} is not a valid email!", 17 | number: "${label} is not a valid number!", 18 | }, 19 | number: { 20 | range: "${label} must be between ${min} and ${max}", 21 | }, 22 | }; 23 | 24 | const history = useHistory(); 25 | 26 | const onFinish = async (values: any) => { 27 | try { 28 | await api().post("/users/register", values); 29 | history.push("/login", { newSignUp: true }); 30 | } catch (error) { 31 | console.log({ error }); 32 | showError((error as any).response.data.errorMessage); 33 | } 34 | }; 35 | 36 | return ( 37 | 43 |

44 | Register for an account 45 |

46 | 47 | 48 | 49 | 56 | 57 | 58 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 72 | 73 | 74 | ); 75 | } 76 | 77 | export default SignUp; 78 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import ReactDOM from "react-dom"; 2 | import { Provider } from "react-redux"; 3 | import { applyMiddleware, createStore } from "redux"; 4 | import thunk from "redux-thunk"; 5 | import "antd/dist/antd.css"; 6 | import { BrowserRouter as Router } from "react-router-dom"; 7 | 8 | import App from "./App"; 9 | import rootReducer from "./store"; 10 | 11 | const store = createStore(rootReducer, applyMiddleware(thunk)); 12 | 13 | ReactDOM.render( 14 | 15 | 16 | 17 | 18 | , 19 | document.getElementById("root") 20 | ); 21 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/store/actions/categoryActions.ts: -------------------------------------------------------------------------------- 1 | import { Category, CategoryDispatch, CategoryForm } from "../../types/category"; 2 | import api from "../../utils/api"; 3 | 4 | export const getCategories = () => async (dispatch: CategoryDispatch) => { 5 | dispatch({ type: "GET_CATEGORIES_START" }); 6 | try { 7 | const response = await api().get("/categories"); 8 | dispatch({ type: "GET_CATEGORIES_SUCCESS", payload: response.data }); 9 | } catch { 10 | dispatch({ type: "GET_CATEGORIES_ERROR" }); 11 | } 12 | }; 13 | 14 | export const addCategory = 15 | (form: CategoryForm) => async (dispatch: CategoryDispatch) => { 16 | dispatch({ type: "ADD_CATEGORY_START" }); 17 | try { 18 | const response = await api().post("/categories", form); 19 | dispatch({ type: "ADD_CATEGORY_SUCCESS", payload: response.data }); 20 | } catch { 21 | dispatch({ type: "ADD_CATEGORY_ERROR" }); 22 | } 23 | }; 24 | 25 | export const updateCategory = 26 | (form: Partial, categoryId: number) => 27 | async (dispatch: CategoryDispatch) => { 28 | dispatch({ type: "UPDATE_CATEGORY_START" }); 29 | try { 30 | const response = await api().put( 31 | "/categories/" + categoryId, 32 | form 33 | ); 34 | dispatch({ type: "UPDATE_CATEGORY_SUCCESS", payload: response.data }); 35 | } catch { 36 | dispatch({ type: "UPDATE_CATEGORY_ERROR" }); 37 | } 38 | }; 39 | 40 | export const deleteCategory = 41 | (categoryId: number) => async (dispatch: CategoryDispatch) => { 42 | dispatch({ type: "DELETE_CATEGORY_START" }); 43 | try { 44 | await api().delete("/categories/" + categoryId); 45 | dispatch({ type: "DELETE_CATEGORY_SUCCESS", payload: categoryId }); 46 | } catch { 47 | dispatch({ type: "DELETE_CATEGORY_ERROR" }); 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /src/store/actions/recordActions.ts: -------------------------------------------------------------------------------- 1 | import { Record, RecordDispatch, RecordForm } from "../../types/record"; 2 | import api from "../../utils/api"; 3 | 4 | export const getRecords = () => async (dispatch: RecordDispatch) => { 5 | dispatch({ type: "GET_RECORDS_START" }); 6 | try { 7 | const response = await api().get("/records"); 8 | response.data.sort((a, b) => b.id - a.id); 9 | dispatch({ type: "GET_RECORDS_SUCCESS", payload: response.data }); 10 | } catch { 11 | dispatch({ type: "GET_RECORDS_ERROR" }); 12 | } 13 | }; 14 | 15 | export const addRecord = 16 | (form: RecordForm) => async (dispatch: RecordDispatch) => { 17 | dispatch({ type: "ADD_RECORD_START" }); 18 | try { 19 | const response = await api().post("/records", form); 20 | dispatch({ type: "ADD_RECORD_SUCCESS", payload: response.data }); 21 | } catch { 22 | dispatch({ type: "ADD_RECORD_ERROR" }); 23 | } 24 | }; 25 | 26 | export const updateRecord = 27 | (form: RecordForm, id: Record["id"]) => async (dispatch: RecordDispatch) => { 28 | dispatch({ type: "UPDATE_RECORD_START" }); 29 | try { 30 | const response = await api().put("/records/" + id, form); 31 | dispatch({ type: "UPDATE_RECORD_SUCCESS", payload: response.data }); 32 | } catch { 33 | dispatch({ type: "UPDATE_RECORD_ERROR" }); 34 | } 35 | }; 36 | 37 | export const deleteRecord = 38 | (id: Record["id"]) => async (dispatch: RecordDispatch) => { 39 | dispatch({ type: "DELETE_RECORD_START" }); 40 | try { 41 | await api().delete("/records/" + id); 42 | dispatch({ type: "DELETE_RECORD_SUCCESS", payload: id }); 43 | } catch { 44 | dispatch({ type: "DELETE_RECORD_ERROR" }); 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /src/store/actions/userActions.ts: -------------------------------------------------------------------------------- 1 | import { LoginForm, User, UserDispatch } from "../../types/user"; 2 | import api from "../../utils/api"; 3 | 4 | export const login = (creds: LoginForm) => async (dispatch: UserDispatch) => { 5 | dispatch({ type: "LOGIN_START" }); 6 | try { 7 | const response = await api().post("/users/login", creds); 8 | dispatch({ type: "LOGIN_SUCCESS", payload: response.data }); 9 | localStorage.setItem("token", response.data.token); 10 | } catch { 11 | dispatch({ type: "LOGIN_ERROR" }); 12 | } 13 | }; 14 | 15 | export const isLoggedIn = () => async (dispatch: UserDispatch) => { 16 | dispatch({ type: "IS_LOGGED_IN_START" }); 17 | try { 18 | const response = await api().post("/users/is_logged_in"); 19 | dispatch({ type: "IS_LOGGED_IN_SUCCESS", payload: response.data }); 20 | } catch { 21 | dispatch({ type: "IS_LOGGED_IN_ERROR" }); 22 | } 23 | }; 24 | 25 | export const logout = () => (dispatch: UserDispatch) => { 26 | localStorage.removeItem("token"); 27 | dispatch({ type: "LOGOUT" }); 28 | }; 29 | -------------------------------------------------------------------------------- /src/store/index.ts: -------------------------------------------------------------------------------- 1 | import { combineReducers } from "redux"; 2 | import { CategoryState } from "../types/category"; 3 | import { RecordState } from "../types/record"; 4 | import { UserState } from "../types/user"; 5 | import categoryReducer from "./reducers/categoryReducer"; 6 | import recordReducer from "./reducers/recordReducer"; 7 | import userReducer from "./reducers/userReducer"; 8 | 9 | export interface AppState { 10 | user: UserState; 11 | categories: CategoryState; 12 | records: RecordState; 13 | } 14 | 15 | const rootReducer = combineReducers({ 16 | user: userReducer, 17 | categories: categoryReducer, 18 | records: recordReducer, 19 | }); 20 | 21 | export default rootReducer; 22 | -------------------------------------------------------------------------------- /src/store/reducers/categoryReducer.ts: -------------------------------------------------------------------------------- 1 | import { CategoryAction, CategoryState } from "../../types/category"; 2 | 3 | const defaultState: CategoryState = { 4 | data: [], 5 | loading: false, 6 | error: "", 7 | }; 8 | 9 | const categoryReducer = ( 10 | state: CategoryState = defaultState, 11 | action: CategoryAction 12 | ) => { 13 | switch (action.type) { 14 | case "GET_CATEGORIES_START": 15 | return { ...state, loading: true, error: "" }; 16 | case "GET_CATEGORIES_SUCCESS": 17 | return { ...state, loading: false, data: action.payload }; 18 | case "GET_CATEGORIES_ERROR": 19 | return { ...state, loading: false, error: "Error fetching categories" }; 20 | case "ADD_CATEGORY_START": 21 | return { ...state, loading: true, error: "" }; 22 | case "ADD_CATEGORY_SUCCESS": 23 | return { 24 | ...state, 25 | loading: false, 26 | data: [action.payload, ...state.data], 27 | }; 28 | case "ADD_CATEGORY_ERROR": 29 | return { ...state, loading: false, error: "Error adding categories" }; 30 | case "UPDATE_CATEGORY_START": 31 | return { ...state, loading: true, error: "" }; 32 | case "UPDATE_CATEGORY_SUCCESS": 33 | return { 34 | ...state, 35 | loading: false, 36 | data: state.data.map((category) => 37 | category.id === action.payload.id ? action.payload : category 38 | ), 39 | }; 40 | case "UPDATE_CATEGORY_ERROR": 41 | return { ...state, loading: false, error: "Error updating category" }; 42 | case "DELETE_CATEGORY_START": 43 | return { ...state, loading: true, error: "" }; 44 | case "DELETE_CATEGORY_SUCCESS": 45 | return { 46 | ...state, 47 | loading: false, 48 | data: state.data.filter((category) => category.id !== action.payload), 49 | }; 50 | case "DELETE_CATEGORY_ERROR": 51 | return { ...state, loading: false, error: "Error deleting category" }; 52 | default: 53 | return state; 54 | } 55 | }; 56 | 57 | export default categoryReducer; 58 | -------------------------------------------------------------------------------- /src/store/reducers/recordReducer.ts: -------------------------------------------------------------------------------- 1 | import { RecordAction, RecordState } from "../../types/record"; 2 | 3 | const defaultState: RecordState = { 4 | data: [], 5 | loading: false, 6 | error: "", 7 | }; 8 | 9 | const recordReducer = ( 10 | state: RecordState = defaultState, 11 | action: RecordAction 12 | ): RecordState => { 13 | switch (action.type) { 14 | case "GET_RECORDS_START": 15 | return { ...state, loading: true, error: "" }; 16 | case "GET_RECORDS_SUCCESS": 17 | return { ...state, loading: false, data: action.payload }; 18 | case "GET_RECORDS_ERROR": 19 | return { ...state, loading: false, error: "Error fetching records" }; 20 | case "ADD_RECORD_START": 21 | return { ...state, loading: true, error: "" }; 22 | case "ADD_RECORD_SUCCESS": 23 | return { 24 | ...state, 25 | loading: false, 26 | data: [action.payload, ...state.data], 27 | }; 28 | case "ADD_RECORD_ERROR": 29 | return { ...state, loading: false, error: "Error adding record" }; 30 | case "UPDATE_RECORD_START": 31 | return { ...state, loading: true, error: "" }; 32 | case "UPDATE_RECORD_SUCCESS": 33 | return { 34 | ...state, 35 | loading: false, 36 | data: state.data.map((record) => 37 | record.id === action.payload.id ? action.payload : record 38 | ), 39 | }; 40 | case "UPDATE_RECORD_ERROR": 41 | return { ...state, loading: false, error: "Error updating record" }; 42 | case "DELETE_RECORD_START": 43 | return { ...state, loading: true, error: "" }; 44 | case "DELETE_RECORD_SUCCESS": 45 | return { 46 | ...state, 47 | loading: false, 48 | data: state.data.filter((record) => record.id !== action.payload), 49 | }; 50 | case "DELETE_RECORD_ERROR": 51 | return { ...state, loading: false, error: "Error deleting record" }; 52 | default: 53 | return state; 54 | } 55 | }; 56 | 57 | export default recordReducer; 58 | -------------------------------------------------------------------------------- /src/store/reducers/userReducer.ts: -------------------------------------------------------------------------------- 1 | import { User, UserAction, UserState } from "../../types/user"; 2 | 3 | const defaultState: UserState = { 4 | data: {} as User, 5 | loading: false, 6 | error: "", 7 | }; 8 | 9 | const userReducer = (state: UserState = defaultState, action: UserAction) => { 10 | switch (action.type) { 11 | case "LOGIN_START": 12 | case "IS_LOGGED_IN_START": 13 | return { ...state, loading: true, error: "" }; 14 | case "LOGIN_SUCCESS": 15 | case "IS_LOGGED_IN_SUCCESS": 16 | return { ...state, loading: false, data: action.payload }; 17 | case "LOGIN_ERROR": 18 | return { ...state, loading: false, error: "Login failed." }; 19 | case "IS_LOGGED_IN_ERROR": 20 | return { ...state, loading: false, error: "Token missing or invalid." }; 21 | case "LOGOUT": 22 | return { ...state, data: {} as User }; 23 | default: 24 | return state; 25 | } 26 | }; 27 | 28 | export default userReducer; 29 | -------------------------------------------------------------------------------- /src/types/category.ts: -------------------------------------------------------------------------------- 1 | import { ThunkDispatch } from "redux-thunk"; 2 | 3 | export interface CategoryState { 4 | data: Category[]; 5 | loading: boolean; 6 | error: string; 7 | } 8 | 9 | export interface Category { 10 | id: number; 11 | name: string; 12 | type: "expense" | "income"; 13 | color: string; 14 | } 15 | 16 | export interface CategoryForm { 17 | name: string; 18 | type: "income" | "expense"; 19 | color?: string; 20 | } 21 | 22 | interface GET_START { 23 | type: "GET_CATEGORIES_START"; 24 | } 25 | 26 | interface GET_SUCCESS { 27 | type: "GET_CATEGORIES_SUCCESS"; 28 | payload: Category[]; 29 | } 30 | 31 | interface GET_ERROR { 32 | type: "GET_CATEGORIES_ERROR"; 33 | } 34 | 35 | interface ADD_START { 36 | type: "ADD_CATEGORY_START"; 37 | } 38 | 39 | interface ADD_SUCCESS { 40 | type: "ADD_CATEGORY_SUCCESS"; 41 | payload: Category; 42 | } 43 | 44 | interface ADD_ERROR { 45 | type: "ADD_CATEGORY_ERROR"; 46 | } 47 | 48 | interface UPDATE_START { 49 | type: "UPDATE_CATEGORY_START"; 50 | } 51 | 52 | interface UPDATE_SUCCESS { 53 | type: "UPDATE_CATEGORY_SUCCESS"; 54 | payload: Category; 55 | } 56 | 57 | interface UPDATE_ERROR { 58 | type: "UPDATE_CATEGORY_ERROR"; 59 | } 60 | 61 | interface DELETE_START { 62 | type: "DELETE_CATEGORY_START"; 63 | } 64 | 65 | interface DELETE_SUCCESS { 66 | type: "DELETE_CATEGORY_SUCCESS"; 67 | payload: number; 68 | } 69 | 70 | interface DELETE_ERROR { 71 | type: "DELETE_CATEGORY_ERROR"; 72 | } 73 | 74 | export type CategoryAction = 75 | | GET_START 76 | | GET_SUCCESS 77 | | GET_ERROR 78 | | ADD_START 79 | | ADD_SUCCESS 80 | | ADD_ERROR 81 | | UPDATE_START 82 | | UPDATE_SUCCESS 83 | | UPDATE_ERROR 84 | | DELETE_START 85 | | DELETE_SUCCESS 86 | | DELETE_ERROR; 87 | export type CategoryDispatch = ThunkDispatch< 88 | CategoryState, 89 | void, 90 | CategoryAction 91 | >; 92 | -------------------------------------------------------------------------------- /src/types/general.ts: -------------------------------------------------------------------------------- 1 | export type Mode = "new" | "edit" | "delete"; 2 | -------------------------------------------------------------------------------- /src/types/record.ts: -------------------------------------------------------------------------------- 1 | import { ThunkDispatch } from "redux-thunk"; 2 | import { Category } from "./category"; 3 | 4 | export interface RecordState { 5 | data: Record[]; 6 | loading: boolean; 7 | error: string; 8 | } 9 | 10 | export interface Record { 11 | id: number; 12 | title: string; 13 | amount: number; 14 | createdAt: string; 15 | updatedAt: string; 16 | category: Category; 17 | } 18 | 19 | export interface RecordForm { 20 | title: string; 21 | amount: number; 22 | category_id: number; 23 | } 24 | 25 | interface GET_START { 26 | type: "GET_RECORDS_START"; 27 | } 28 | 29 | interface GET_SUCCESS { 30 | type: "GET_RECORDS_SUCCESS"; 31 | payload: Record[]; 32 | } 33 | 34 | interface GET_ERROR { 35 | type: "GET_RECORDS_ERROR"; 36 | } 37 | 38 | interface ADD_START { 39 | type: "ADD_RECORD_START"; 40 | } 41 | 42 | interface ADD_SUCCESS { 43 | type: "ADD_RECORD_SUCCESS"; 44 | payload: Record; 45 | } 46 | 47 | interface ADD_ERROR { 48 | type: "ADD_RECORD_ERROR"; 49 | } 50 | 51 | interface UPDATE_START { 52 | type: "UPDATE_RECORD_START"; 53 | } 54 | 55 | interface UPDATE_SUCCESS { 56 | type: "UPDATE_RECORD_SUCCESS"; 57 | payload: Record; 58 | } 59 | 60 | interface UPDATE_ERROR { 61 | type: "UPDATE_RECORD_ERROR"; 62 | } 63 | 64 | interface DELETE_START { 65 | type: "DELETE_RECORD_START"; 66 | } 67 | 68 | interface DELETE_SUCCESS { 69 | type: "DELETE_RECORD_SUCCESS"; 70 | payload: Record["id"]; 71 | } 72 | 73 | interface DELETE_ERROR { 74 | type: "DELETE_RECORD_ERROR"; 75 | } 76 | 77 | export type RecordAction = 78 | | GET_START 79 | | GET_SUCCESS 80 | | GET_ERROR 81 | | ADD_START 82 | | ADD_SUCCESS 83 | | ADD_ERROR 84 | | UPDATE_START 85 | | UPDATE_SUCCESS 86 | | UPDATE_ERROR 87 | | DELETE_START 88 | | DELETE_SUCCESS 89 | | DELETE_ERROR; 90 | export type RecordDispatch = ThunkDispatch; 91 | -------------------------------------------------------------------------------- /src/types/user.ts: -------------------------------------------------------------------------------- 1 | import { ThunkDispatch } from "redux-thunk"; 2 | 3 | export interface User { 4 | message: string; 5 | username: string; 6 | email: string; 7 | full_name: string; 8 | token: string; 9 | } 10 | 11 | export interface LoginForm { 12 | username: string; 13 | password: string; 14 | } 15 | 16 | export interface UserState { 17 | data: User; 18 | loading: boolean; 19 | error: string; 20 | } 21 | 22 | interface LOGIN_START { 23 | type: "LOGIN_START"; 24 | } 25 | 26 | interface LOGIN_SUCCESS { 27 | type: "LOGIN_SUCCESS"; 28 | payload: User; 29 | } 30 | 31 | interface LOGIN_ERROR { 32 | type: "LOGIN_ERROR"; 33 | } 34 | 35 | interface IS_LOGGED_IN_START { 36 | type: "IS_LOGGED_IN_START"; 37 | } 38 | 39 | interface IS_LOGGED_IN_SUCCESS { 40 | type: "IS_LOGGED_IN_SUCCESS"; 41 | payload: User; 42 | } 43 | 44 | interface IS_LOGGED_IN_ERROR { 45 | type: "IS_LOGGED_IN_ERROR"; 46 | } 47 | 48 | interface LOGOUT { 49 | type: "LOGOUT"; 50 | } 51 | 52 | export type UserAction = 53 | | LOGIN_START 54 | | LOGIN_SUCCESS 55 | | LOGIN_ERROR 56 | | IS_LOGGED_IN_START 57 | | IS_LOGGED_IN_SUCCESS 58 | | IS_LOGGED_IN_ERROR 59 | | LOGOUT; 60 | 61 | export type UserDispatch = ThunkDispatch; 62 | -------------------------------------------------------------------------------- /src/utils/api.ts: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | export default () => { 4 | const token = localStorage.getItem("token"); 5 | 6 | return axios.create({ 7 | baseURL: "https://expensetracker-be.herokuapp.com", 8 | headers: { 9 | Authorization: token, 10 | }, 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /src/utils/showError.ts: -------------------------------------------------------------------------------- 1 | import { message } from "antd"; 2 | 3 | const showError = (errorMessage: string) => { 4 | message.error(errorMessage); 5 | }; 6 | 7 | export default showError; 8 | -------------------------------------------------------------------------------- /src/utils/showSuccess.ts: -------------------------------------------------------------------------------- 1 | import { message } from "antd"; 2 | 3 | const showSuccess = (successMessage: string) => { 4 | message.success(successMessage); 5 | }; 6 | 7 | export default showSuccess; 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | --------------------------------------------------------------------------------