Skip to content
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
23 changes: 23 additions & 0 deletions frontend/expensesui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
16 changes: 16 additions & 0 deletions frontend/expensesui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# User Interface For Expenses API

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts To Run The UI

In the project directory, you can run:

### docker-compose up

or

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11,474 changes: 11,474 additions & 0 deletions frontend/expensesui/package-lock.json

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions frontend/expensesui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "provectus_internship",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.13",
"@types/react": "^17.0.20",
"@types/react-dom": "^17.0.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added frontend/expensesui/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions frontend/expensesui/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added frontend/expensesui/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/expensesui/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions frontend/expensesui/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions frontend/expensesui/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
51 changes: 51 additions & 0 deletions frontend/expensesui/src/components/AddExpense/AddExpense.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.add-expense{
position: absolute;
top: 30%;
left: 35%;
background-color: white;
border-color: black;
border-style: solid;
border-width: 1px;
width: 400px;
-webkit-backdrop-filter: blur(10px);
backdrop-filter: blur(10px);
background-color: rgba(255, 255, 255, 0.5);
font-family: Verdana,sans-serif;
}

label{
margin-left: 20px;
display: inline-block;
margin-top: 2%;
float: left;
clear: left;
text-align: left;
}
input{
margin-left: 30%;
width: 60%;
display: inline-block;
}

input[type=text] {
margin-top: 1%;
width: 90%;
padding: 12px 20px;
margin-left: 5%;
box-sizing: border-box;
}

.title{
margin-top: 2%;
margin-left: 40%;
}

.button{
margin-bottom: 2%;
width: 30%;
margin-left: 35%;
}

.submit{
margin-top: 8%;
}
39 changes: 39 additions & 0 deletions frontend/expensesui/src/components/AddExpense/AddExpense.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import './AddExpense.css';
import { AddExpenseProps } from './types';
import React from 'react';
import { addExpense } from 'src/lib/api/addExpense';

export default function AddExpense({ openModal, closeModal, add }: AddExpenseProps) {
if (!openModal) return null;

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const newExpense = {
description: ((e.target as HTMLFormElement)[0] as HTMLInputElement).value,
amount: +((e.target as HTMLFormElement)[1] as HTMLInputElement).value,
category: ((e.target as HTMLFormElement)[2] as HTMLInputElement).value,
date: ((e.target as HTMLFormElement)[3] as HTMLInputElement).value
}
addExpense(newExpense).then((data) => add({ _id: data._id, ...newExpense }))
}


return <div className="add-expense">
<div className="title"> Enter data </div>
<form onSubmit={handleSubmit}>
<label htmlFor="description">Description</label>
<input type="text" id="description" name="description"/>
<br/>
<label htmlFor="amount">Amount</label>
<input type="text" id="amount" name="amount"/>
<br/>
<label htmlFor="category">Category</label>
<input type="text" id="category" name="category"/>
<br/>
<label htmlFor="date">Date</label>
<input type="text" id="date" name="date" />
<input type="submit" value="Submit" className="button submit" />
<input type="button" value="Cancel" className="button" onClick={closeModal}/>
</form>
</div>
}
7 changes: 7 additions & 0 deletions frontend/expensesui/src/components/AddExpense/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Expense } from "../Table/types";

export interface AddExpenseProps{
openModal: boolean;
closeModal: () => void;
add: (expense: Expense) => void;
}
8 changes: 8 additions & 0 deletions frontend/expensesui/src/components/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.toolbar{
display: flex;
}

.add-expenses{
margin-left: 1%;
width: 20%;
}
56 changes: 56 additions & 0 deletions frontend/expensesui/src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { Expense, Category } from './Table/types';
import Table from './Table/Table';
import getExpenses from 'src/lib/api/getExpenses';
import getCategories from 'src/lib/api/getCategories';
import { mapCategoryIdTitle } from './utils';
import AddExpense from 'src/components/AddExpense/AddExpense';
import Search from './Search/Search';
import './App.css';

export default function App() {
const [expenses, setExpenses] = React.useState<Expense[] | null>(null);
const [categories, setCategories] = React.useState<Category[] | null>(null);
const [openModal, setOpenModal] = React.useState<boolean>(false);
const [searchResults, setSearchResults] = React.useState<Expense[]>([]);

React.useEffect(() => {
const getData = async () => {
const respExpenses = await getExpenses();
const respCategories = await getCategories();
setExpenses(mapCategoryIdTitle(respExpenses, respCategories));
setCategories(respCategories);
}

getData();
}, []);

const updateExpense = (index: number, expense: Expense) =>
setExpenses((prev) => prev ? [
...prev.slice(0, index),
expense,
...prev.slice(index + 1, prev.length)
] : []);

const deleteExpense = (index: number) =>
setExpenses((prev) => prev ? [
...prev.slice(0, index),
...prev.slice(index + 1, prev.length)
] : [])


const addExpense = (expense: Expense) =>
setExpenses((prev) => prev && categories ? mapCategoryIdTitle([
expense,
...prev
], categories): [])

return <div>
<div className="toolbar">
<Search setSearchResults={(expenses) => setSearchResults(expenses)}/>
<input type="button" value="Add expense" onClick={() => setOpenModal(true)} className="add-expenses"/>
<AddExpense openModal={openModal} closeModal={() => setOpenModal(false)} add={addExpense}/>
</div>
{!!expenses && <Table data={searchResults.length !== 0? searchResults : expenses} updateData={updateExpense} deleteData={deleteExpense} />}
</div>;
};
18 changes: 18 additions & 0 deletions frontend/expensesui/src/components/Common/EditableField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EditableFieldProps } from "./types"
import React from 'react';

export default function EditableField({ value, editing, inputRef }: EditableFieldProps) {
const [valueState, setValueState] = React.useState<(string | number)>(value);

React.useEffect(() => {
if (valueState !== value) {
setValueState(valueState);
}
}, [value, valueState])

return <>
{editing && <input type="text" value={valueState} ref={inputRef}
onChange={(e) => setValueState(e.target.value)} />}
{ !editing && value }
</>
}
7 changes: 7 additions & 0 deletions frontend/expensesui/src/components/Common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export interface EditableFieldProps{
value: string | number;
editing: boolean;
inputRef?: React.RefObject<HTMLInputElement>;
}
3 changes: 3 additions & 0 deletions frontend/expensesui/src/components/Search/Search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.searchbar{
margin-top: 0!important;
}
26 changes: 26 additions & 0 deletions frontend/expensesui/src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import './Search.css';
import findExpense from 'src/lib/api/findExpense';
import { SearchProps } from './types';
import React from 'react';
import {
_Error
} from 'src/lib/api/types';
import { Expense } from '../Table/types';

export default function Search({ setSearchResults } : SearchProps) {
const isError = (data: Expense | _Error): data is _Error => {
return (data as _Error).error !== undefined;
}

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.value === '') {
setSearchResults([]);
return;
}

findExpense(e.target.value)
.then((data) => setSearchResults(isError(data)? [] : [data]))
}

return <input type="text" placeholder="Search by ID here" className="searchbar" onChange={handleChange}/>
}
5 changes: 5 additions & 0 deletions frontend/expensesui/src/components/Search/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Expense } from "../Table/types";

export interface SearchProps{
setSearchResults: (expenses: Expense[]) => void;
}
Loading