1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import { createEffect, createMemo, createState } from 'solid-js';
7 import { For } from 'solid-js/dom';
8 import { Form, Person, TaxReturn } from 'ustaxlib/core';
10 import FormView from './FormView';
12 const S = require('./TaxReturnView.css');
14 export interface Props {
17 onFormChange?: (formName: string) => void;
20 function hashifyFormName(name: string): string {
21 return '#' + name.replace(/[^\w]+/g, '_');
24 export default function TaxReturnView(props: Props) {
25 const [ state, setState ] = createState({ form: props.tr.forms[0] });
27 const changeForm = e => {
28 setState({ form: props.tr.forms[e.target.value] });
29 if (props.onFormChange)
30 props.onFormChange(hashifyFormName(e.target.selectedOptions[0].textContent));
33 const formIndexToName = createMemo(() => {
34 const formNameCounts = {};
35 let forms = props.tr.forms.map((form, i) => {
37 const person = form.person();
38 if (person !== undefined) {
39 const personName = person === Person.joint ? 'Joint' : person.name;
40 name += ` (${personName})`;
42 if (name in formNameCounts) {
43 formNameCounts[name] += 1;
44 name += ` (${formNameCounts[name]})`;
46 formNameCounts[name] = 1;
50 forms.sort((a, b) => {
62 for (let f of formIndexToName()) {
63 if (hashifyFormName(f[1] as string) === props.showForm) {
64 setState({ form: props.tr.forms[f[0]] });
70 const formSelector = (
71 <select onchange={changeForm}>
72 <For each={formIndexToName()}>
73 {tuple => (<option value={tuple[0]} selected={state.form === props.tr.forms[tuple[0]]}>{tuple[1]}</option>)}
80 <div class={S.header}>
81 <h1>ustaxlib Federal {props.tr.year}</h1>
85 <FormView tr={props.tr} form={state.form as Form<any>} />