import React, { HTMLProps, useEffect, useMemo, useState } from "react";
import { BranchOffice, Company, emptyAddress, emptyBranchOffice, emptyCompany, formatAddress } from "@/data";
import { sampleCompanies } from "@/provider/dummy";
import { Autocomplete } from "@material-ui/lab";
import { Grid, TextField, Typography } from "@material-ui/core";
import { formFieldProps } from "./helpers";
export type CompanyFormProps = {}
export type BranchOfficeProps = {
company: Company,
disabled?: boolean
}
export const CompanyItem = ({ company, ...props }: { company: Company } & HTMLProps) => (
{ company.name }
NIP: { company.nip }
)
export const OfficeItem = ({ office, ...props }: { office: BranchOffice } & HTMLProps) => (
{ office.address.city }
{ formatAddress(office.address) }
)
export const BranchForm: React.FC = ({ company, disabled = false }) => {
const [office, setOffice] = useState(emptyBranchOffice)
const canEdit = useMemo(() => !office.id && !disabled, [office.id, disabled]);
const fieldProps = formFieldProps(office.address, address => setOffice({ ...office, address }))
const handleCityChange = (event: any, value: BranchOffice | string | null) => {
if (typeof value === "string") {
setOffice({
...emptyBranchOffice,
address: {
...emptyAddress,
city: value,
}
});
} else if (typeof value === "object" && value !== null) {
setOffice(value);
} else {
setOffice(emptyBranchOffice);
}
}
const handleCityInput = (event: any, value: string) => {
const base = office.id ? emptyBranchOffice : office;
setOffice({
...base,
address: {
...base.address,
city: value,
}
})
}
useEffect(() => void (office.id && setOffice(emptyBranchOffice)), [company])
return (
typeof office == "string" ? office : office.address.city }
renderOption={ office => }
renderInput={ props => }
onChange={ handleCityChange }
onInputChange={ handleCityInput }
inputValue={ office.address.city }
value={ office.id ? office : null }
freeSolo
/>
)
}
export const CompanyForm: React.FunctionComponent = props => {
const [company, setCompany] = useState(emptyCompany);
const canEdit = useMemo(() => !company.id, [company.id]);
const fieldProps = formFieldProps(company, setCompany)
const handleCompanyChange = (event: any, value: Company | string | null) => {
if (typeof value === "string") {
setCompany({
...emptyCompany,
name: value,
});
} else if (typeof value === "object" && value !== null) {
setCompany(value);
} else {
setCompany(emptyCompany);
}
}
return (
<>
option.name }
renderOption={ company => }
renderInput={ props => }
onChange={ handleCompanyChange }
freeSolo
/>
OddziaĆ
>
)
}