|
|
@@ -0,0 +1,98 @@
|
|
|
+import React, { ChangeEvent } from "react";
|
|
|
+import "./App.css";
|
|
|
+import Headerbox from "./components/Headerbox";
|
|
|
+
|
|
|
+import { Income } from './interfaces/incomes';
|
|
|
+import { Person } from './interfaces/persons';
|
|
|
+
|
|
|
+
|
|
|
+interface AppState {
|
|
|
+ personanzahl : number;
|
|
|
+ persons: Person[];
|
|
|
+}
|
|
|
+
|
|
|
+class App extends React.Component<{}, AppState> {
|
|
|
+ constructor(props: {}) {
|
|
|
+ super(props);
|
|
|
+ this.state = {
|
|
|
+ personanzahl: 1,
|
|
|
+ persons: [{index: 1, incomes: [], totalincome: 0}]
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ return (
|
|
|
+ <div className="App">
|
|
|
+ <h1>WBS Rechner</h1>
|
|
|
+ <Headerbox title="Haushaltsmitglieder">
|
|
|
+ <div className="label-container">
|
|
|
+ <label htmlFor="personenanzahl">Anzahl Personen im Haushalt</label>
|
|
|
+ <input
|
|
|
+ value={this.state.personanzahl}
|
|
|
+ id="personenanzahl"
|
|
|
+ type="number"
|
|
|
+ min="1"
|
|
|
+ max="20"
|
|
|
+ onChange={this.updateInputValue}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ </Headerbox>
|
|
|
+ {this.state.persons.map( p =>
|
|
|
+ <Headerbox title={'Daten der ' + p.index + '. Person'}>
|
|
|
+ <div></div>
|
|
|
+ </Headerbox>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ updateInputValue = (evt : ChangeEvent<HTMLInputElement>) => {
|
|
|
+ this.setState({ personanzahl: evt.target.valueAsNumber });
|
|
|
+ this.refreshPeople(evt.target.valueAsNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ refreshPeople(personenanzahl: number ) {
|
|
|
+ if (personenanzahl > this.state.persons.length) {
|
|
|
+ let anzahl = personenanzahl - this.state.persons.length;
|
|
|
+ for (let i = 1; i <= anzahl; i++) {
|
|
|
+ this.addPerson();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (personenanzahl < this.state.persons.length) {
|
|
|
+ let anzahl = this.state.persons.length - personenanzahl;
|
|
|
+ for (let i = 1; i <= anzahl; i++) {
|
|
|
+ this.deletePerson(this.state.persons.length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ addPerson() {
|
|
|
+ const newPerson: Person = {
|
|
|
+ index: this.getHighestIndex(this.state.persons) + 1,
|
|
|
+ incomes: [],
|
|
|
+ totalincome: 0
|
|
|
+ };
|
|
|
+ this.setState(prevState => ({
|
|
|
+ persons: [...prevState.persons, newPerson]
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ deletePerson(index: number) {
|
|
|
+ this.setState(prevState => ({
|
|
|
+ persons: prevState.persons.filter(person => person.index !== index)
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ getHighestIndex(arr: Person[]) {
|
|
|
+ let highestIndex = 0;
|
|
|
+ for (const item of arr) {
|
|
|
+ if (item.index > highestIndex) {
|
|
|
+ highestIndex = item.index;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return highestIndex;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default App;
|