Commit 54bba4d7 authored by Emil Henček's avatar Emil Henček
Browse files

Initial commit

parents
Pipeline #33335 failed with stages
in 0 seconds
REACT_APP_API_BASE_URL=https://my-json-server.typicode.com/1ohnny/test-api
\ No newline at end of file
# 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*
# Webnotion zadanie
Run this React app with commands:
### `yarn install`
### `yarn start`
{
"name": "webnotion",
"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-bootstrap": "^0.32.28",
"@types/react-dom": "^17.0.9",
"bootstrap": "5.1.3",
"moment": "^2.29.1",
"react": "^17.0.2",
"react-bootstrap": "^2.0.4",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"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"
]
}
}
<!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>Webnotion zadanie - Emil Henček</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>
{
"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"
}
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
import React, { useState, useEffect } from "react";
import "./App.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Listing from "./components/Listing";
import Detail from "./components/Detail";
import { Position, ContractType } from "./types";
const apiUrl = process.env.REACT_APP_API_BASE_URL;
const App = () => {
const [positions, setPositions] = useState<Position[]>([]);
const [contractTypes, setContractTypes] = useState<ContractType[]>([]);
// fetch static data
useEffect(() => {
fetch(`${apiUrl}/positions`)
.then((response) => response.json())
.then((data) => {
setPositions(data);
});
fetch(`${apiUrl}/contractTypes`)
.then((response) => response.json())
.then((data) => {
setContractTypes(data);
});
}, []);
const staticApiData = { positions, contractTypes };
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Listing staticApiData={staticApiData} />} />
<Route
path="/listing"
element={<Listing staticApiData={staticApiData} />}
/>
<Route
path="/detail"
element={<Detail staticApiData={staticApiData} />}
>
<Route
path="/detail/:employeeId"
element={<Detail staticApiData={staticApiData} />}
></Route>
</Route>
<Route
path="*"
element={
<main style={{ padding: "1rem" }}>
<p>404 Not found</p>
</main>
}
/>
</Routes>
</BrowserRouter>
);
};
export default App;
import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import moment from "moment";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import Table from "react-bootstrap/Table";
import Button from "react-bootstrap/Button";
import Spinner from "react-bootstrap/Spinner";
import { Contract, ContractType, Position } from "../../types";
interface DetailProps {
staticApiData: {
positions: Position[];
contractTypes: ContractType[];
};
}
const apiUrl = "https://my-json-server.typicode.com/1ohnny/test-api";
const Detail = (props: DetailProps) => {
const { positions, contractTypes } = props.staticApiData;
const { employeeId } = useParams();
const navigate = useNavigate();
const [currentEmployee, setCurrentEmployee] = useState<any>(null);
const [loading, setLoading] = useState<boolean>(false);
const [errorText, setErrorText] = useState<string>("");
useEffect(() => {
setLoading(true);
fetch(`${apiUrl}/employees/${employeeId}`)
.then((response) => {
if (response.status === 404) {
setErrorText("Zamestnanec sa nenašiel");
}
return response.json();
})
.then((data) => {
if (data) {
setCurrentEmployee(data);
}
setLoading(false);
})
.catch((error) => {
console.log("404: ", error);
setLoading(false);
});
}, [employeeId]);
const dateFormat = "D.M.YYYY";
const getContractType = (contractId: number): string => {
const name = contractTypes?.filter(
(contract) => contract.id === contractId
)[0].name;
if (name) {
return name;
} else return "?";
};
return (
<Container fluid="md">
<Row>
{!employeeId ? (
<Row>
<Col>
<h3>Chýbajúce ID zamestnanca</h3>
</Col>
</Row>
) : loading ? (
<Row>
<Col className="d-flex justify-content-center">
<Spinner animation="border" className="screenLoading">
<span className="visually-hidden">Loading...</span>
</Spinner>
</Col>
</Row>
) : errorText ? (
<Row>
<Col>
<h3>{errorText}</h3>
</Col>
</Row>
) : (
<Row>
<Col md={9}>
<h3>Detail zamestnanca {currentEmployee?.name}</h3>
</Col>
<Col md={3}>
<Button
variant="primary"
onClick={() => {
navigate(`/listing`);
}}
>
Späť na zoznam zamestnancov
</Button>
</Col>
<Col md={12}>
<strong>Identifikačné číslo: </strong>
{currentEmployee?.id}
</Col>
<Col md={12}>
<strong>Celé meno: </strong>
{currentEmployee?.name + " " + currentEmployee?.surname}
</Col>
<Col md={12}>
<strong>Pozícia: </strong>{" "}
{positions
? positions.filter(
(position) => position.id === currentEmployee?.positionId
)[0]?.name
: null}
</Col>
<Col>
<div className={"space"}></div>
</Col>
<Col md={12}>
<Table striped bordered hover>
<thead>
<tr>
<th>Typ zmluvy</th>
<th>Začiatok</th>
<th>Koniec</th>
</tr>
</thead>
<tbody>
{currentEmployee?.contracts?.length > 0 ? (
currentEmployee?.contracts?.map((contract: Contract) => {
return (
<tr key={contract.id}>
<td>{getContractType(contract.typeId)}</td>
<td>
{contract.from
? moment(contract.from).format(dateFormat)
: " - "}
</td>
<td>
{contract.to
? moment(contract.to).format(dateFormat)
: " - "}
</td>
</tr>
);
})
) : (
<tr>
<td colSpan={4}>Zamestnanec nemá žiadne kontrakty.</td>
</tr>
)}
</tbody>
</Table>
</Col>
</Row>
)}
</Row>
</Container>
);
};
export default Detail;
import React, { useState } from "react";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import { ContractType, Position, FilterFields } from "../../types";
interface FilterProps {
data: {
positions: Position[];
contractTypes: ContractType[];
};
activeFilter: FilterFields;
onChangeFilter: any;
}
const Filter = (props: FilterProps) => {
const handleSelectPosition = (e: any) => {
props.onChangeFilter("position", e.target.value);
};
const handleSelectContractType = (e: any) => {
props.onChangeFilter("contractType", e.target.value);
};
const handleSelectState = (e: any) => {
props.onChangeFilter("state", e.target.value);
};
const handleSearch = (e: any) => {
props.onChangeFilter("query", e.target.value);
};
const positions = props.data.positions.sort(
(a, b) => a.orderIdx - b.orderIdx
);
const contractTypes = props.data.contractTypes.sort((a, b) => {
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return textA < textB ? -1 : textA > textB ? 1 : 0;
});
const isFilterActive = (filter: FilterFields): boolean => {
if (Object.keys(filter).length === 0) {
return false;
}
return true;
};
return (
<Row>
<Col md={12}>
<Form.Control
type="text"
placeholder="Vyhľadať zamestnanca"
value={props.activeFilter.query || ""}
onChange={handleSearch}
/>
</Col>
<Col md={12}>
<div className={"space"}></div>
</Col>
<Col md={3}>
<Form.Select
onChange={handleSelectPosition}
value={props.activeFilter.position}
>
<option value={0}>Pozícia</option>
{positions.map((position) => {
return (
<option key={position.id} value={position.id}>
{position.name}
</option>
);
})}
</Form.Select>
</Col>
<Col md={3}>
<Form.Select onChange={handleSelectContractType}>
<option value={0}>Typ zmluvy</option>
{contractTypes.map((contractType) => {
return (
<option key={contractType.id} value={contractType.id}>
{contractType.name}
</option>
);
})}
</Form.Select>
</Col>
<Col md={3}>
<Form.Select onChange={handleSelectState}>
<option value="all">Všetci</option>
<option value="active">Aktívni</option>
<option value="notActive">Neaktívni</option>
</Form.Select>
</Col>
<Col md={3}>
{isFilterActive(props.activeFilter) ? (
<Button
variant="primary"
onClick={() => {
props.onChangeFilter("all", null);
}}
>
Zrušiť filter
</Button>
) : null}
</Col>
</Row>
);
};
export default Filter;
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import moment from "moment";
import Table from "react-bootstrap/Table";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import { Employee } from "../../types";
interface GridProps {
employees: any[];
}
const Grid = (props: GridProps) => {
const navigate = useNavigate();
const [sortColumn, setSortColumn] = useState<string>("name");
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc");
const toggleSortDirection = () => {
sortDirection === "asc"
? setSortDirection("desc")
: setSortDirection("asc");
};
const handleChangeSort = (colName: string) => {
if (sortColumn === colName) {
toggleSortDirection();
} else {
setSortColumn(colName);
}
};
const handleRowClick = (empId: number) => {
navigate(`/detail/${empId}`);
};
const employees = props.employees.sort((a, b) => {
let textA, textB;
if (sortColumn === "activeContractText") {