aboutsummaryrefslogtreecommitdiff
path: root/src/components/Form/People/SelectClientForm.tsx
blob: ca984ca3a870ea2a43295280881f73cf472ec919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * OpenBills - Self hosted browser app to generate and keep track of simple invoices
 * Version - 0
 * Licensed under the MIT license - https://opensource.org/licenses/MIT
 *
 * Copyright (c) 2021 Vidhu Kant Sharma
*/

import React, { Dispatch, SetStateAction } from "react";
import { Person } from "./../../../interfaces";
import "./../Form.scss";

import ClientInfoDisplay from "../../Display/ClientInfoDisplay";

interface Props {
  savedPeople: Person[]
  selectedClient: Person
  setSelectedClient: Dispatch<SetStateAction<Person>>
}

const SelectClientForm: React.FC<Props> = (props) => {
  const enterValuePrompt = "start typing here";
  const registerPrompt = "add new";

  // TODO: make it use email if no address found, shorten the name too
  // in short, make formatter flexible
  const formatter = (i: Person): string =>
    // TODO: this shit is ugly
    `${i.Name} - ${i.BillAddress.AddressLine.slice(0, 20).concat(i.BillAddress.AddressLine.length < 20 ? "" : "")}`;

  // TODO: if no client found at least clear the display
  // do this in other components too
  // check the client name value and do stuff accordingly
  const setClientInfo = (e: string) =>
    (props.savedPeople === null || e === registerPrompt)
      ? alert("coming soon") // toggle registerPersonPrompt visibility
      : props.savedPeople.some((i) =>
        e === formatter(i) && props.setSelectedClient(i))
  
  return (
    <div className={"documentInfoChild"}>
      <label>
        Client Name:
          <select
            className={"selectInputBox"}
            value={props.selectedClient.Name} 
            onChange={
              (event) => {
                setClientInfo(event.target.value);
              }
          }>
            <option key={enterValuePrompt}>{enterValuePrompt}</option>

            {props.savedPeople === null || props.savedPeople.map(
              (i) => <option key={i.ID}>{formatter(i)}</option>
            )}

            <option key={registerPrompt}>{registerPrompt}</option>
          </select>
      </label>

      <ClientInfoDisplay client={props.selectedClient}/>
    </div>
  )
}

export default SelectClientForm;