import React, { ChangeEvent } from "react"; import "./App.css"; import Headerbox from "./components/Headerbox"; import { Person } from './interfaces/persons'; import { Frequency } from "./interfaces/frequency"; import { Type } from "./interfaces/type"; import { WBS } from "./interfaces/wbs"; import { WbsService } from "./service/wbsService"; import { Income } from "./interfaces/incomes"; interface AppState { personanzahl: number; persons: Person[]; wbs: WBS; wbsResult: boolean; } const wbsService = new WbsService(); class App extends React.Component<{}, AppState> { types: Array = []; freq: Array = []; constructor(props: {}) { super(props); this.state = { wbsResult: false, wbs: { wbsGranted: "Keinen", difference: 0, percent: 0, yearlyAmount: 0, yearlyAmountMax: 0 }, personanzahl: 1, persons: [{ index: 1, incomes: [] as Income[], totalincome: 0 }] }; wbsService.getTypes().then(types => { types.forEach(type => { this.types.push(type); }); }); wbsService.getFrequencies().then(freqs => { freqs.forEach(freq => { this.freq.push(freq); }); }); } render() { return (

WBS Rechner

{this.state.persons.map((p, pindex) =>
{this.state.persons[p.index - 1].incomes.map((i, inindex) =>
)}
) }

Zusammenfassung Haushalt

{this.state.persons.map(p =>
{p.index}
N/A
N/A
{p.totalincome}
)} {this.state.wbsResult ?
{this.state.wbs.yearlyAmount} €
{this.state.wbs.yearlyAmountMax} €
{this.state.wbs.difference} €
{this.state.wbs.percent} %
{this.state.wbs.wbsGranted}
:
}
); } updateInputValue = (evt: ChangeEvent) => { this.setState({ personanzahl: evt.target.valueAsNumber }); this.refreshPeople(evt.target.valueAsNumber); } updateIncomeValue = (evt: ChangeEvent) => { this.updateValueState(evt.target.id, evt.target.valueAsNumber); } updateIncomeSelectValue = (evt: ChangeEvent) => { this.updateValueState(evt.target.id, evt.target.value); } updateValueState(id: string, value: any) { var parts = id.split("_"); var field = parts[0]; var personid: number = parseInt(parts[1]); var incomeId: number = parseInt(parts[2]); this.setState(prevState => { const persons = [...prevState.persons]; const person = persons[personid - 1]; const income = person.incomes[incomeId]; if (field === "amount") { income.amount = value; person.totalincome = this.calculateTotalIncome(personid - 1); } else if (field === "advertisingCosts") { income.advertisingCosts = value; person.totalincome = this.calculateTotalIncome(personid - 1); } else if (field === "frequency") { income.frequency = value; person.totalincome = this.calculateTotalIncome(personid - 1); } else if (field === "type") { income.type = value; } return { persons }; }); } calculateWBS() { var persons: Array = this.state.persons; wbsService.calculateWbs(persons).then(wbsErg => { this.setState({ wbsResult: true }); this.setState({ wbs: wbsErg }); }); } calculateTotalIncome(personindex: number): number { let totalIncome = 0; for (const income of this.state.persons[personindex].incomes) { totalIncome += ((income.frequency * income.amount) - income.advertisingCosts); } return totalIncome; } 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) })); } deleteIncome(pindex: number, iindex: number) { console.log(pindex + " - " + iindex); this.setState(prevState => { const persons = [...prevState.persons]; const person = persons[pindex]; console.log(person.incomes); person.incomes.splice(iindex, 1) console.log(person.incomes); person.totalincome = this.calculateTotalIncome(pindex); return { persons }; }); console.log(this.state.persons); } addIncome(index: number) { const newincome = { index: this.getHighestIndex(this.state.persons[index - 1].incomes) + 1, amount: 0, frequency: 0, advertisingCosts: 0, type: 0 }; this.setState(prevState => { const persons = [...prevState.persons]; const person = persons[index - 1]; person.incomes = [...person.incomes, newincome]; return { persons }; }); } getHighestIndex(arr: any[]) { let highestIndex = 0; if (typeof arr === "undefined") { return highestIndex; } for (const item of arr) { if (item.index > highestIndex) { highestIndex = item.index; } } return highestIndex; } } export default App;