Skip to content
Snippets Groups Projects
Verified Commit 9c96d8f7 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Pass state to avoid controlled component.

Work on #12.
parent 6ed7b87a
No related branches found
No related tags found
2 merge requests!69Possibility to insert a reservation and new docs.,!64Pass state to avoid controlled component.
Pipeline #13046 passed
...@@ -3,7 +3,7 @@ export type Location = { ...@@ -3,7 +3,7 @@ export type Location = {
}; };
export type InsertReservationProps = { export type InsertReservationProps = {
senior: number; senior: number | undefined;
insertedBy: number; insertedBy: number;
tripStages: string[]; tripStages: string[];
driverShift: string; driverShift: string;
......
...@@ -8,13 +8,14 @@ import { Control, Controller } from 'react-hook-form'; ...@@ -8,13 +8,14 @@ import { Control, Controller } from 'react-hook-form';
import { InsertReservationProps } from '../InsertReservationProps'; import { InsertReservationProps } from '../InsertReservationProps';
type SeniorSearchProps = { type SeniorSearchProps = {
control: Control<InsertReservationProps>; senior: ResponseProps | null;
setSenior: React.Dispatch<React.SetStateAction<ResponseProps | null>>;
}; };
export const SeniorSearch: FC<SeniorSearchProps> = ({ export const SeniorSearch: FC<SeniorSearchProps> = ({
control, senior,
setSenior,
}: SeniorSearchProps) => { }: SeniorSearchProps) => {
const [value, setValue] = useState<ResponseProps | null>(null);
const [seniors, setSeniors] = useState<ResponseProps[]>([]); const [seniors, setSeniors] = useState<ResponseProps[]>([]);
// FIX: Input closing when the data is changed. // FIX: Input closing when the data is changed.
...@@ -22,7 +23,7 @@ export const SeniorSearch: FC<SeniorSearchProps> = ({ ...@@ -22,7 +23,7 @@ export const SeniorSearch: FC<SeniorSearchProps> = ({
// attaching the value manually or to isolate this component. // attaching the value manually or to isolate this component.
const handleChange = (newValue: string): void => { const handleChange = (newValue: string): void => {
const MIN_SEARCH_LENGTH = 4; const MIN_SEARCH_LENGTH = 4;
if (newValue.length >= MIN_SEARCH_LENGTH && value == null) { if (newValue.length >= MIN_SEARCH_LENGTH && senior == null) {
getSeniorList(newValue).then((list) => { getSeniorList(newValue).then((list) => {
if (list.length !== 0) { if (list.length !== 0) {
setSeniors([...seniors, ...list]); setSeniors([...seniors, ...list]);
...@@ -34,37 +35,30 @@ export const SeniorSearch: FC<SeniorSearchProps> = ({ ...@@ -34,37 +35,30 @@ export const SeniorSearch: FC<SeniorSearchProps> = ({
}; };
return ( return (
<Controller <Autocomplete
control={control} value={senior}
name="senior" id="senior-searcher"
as={(field) => ( onInputChange={(_, newValue) => handleChange(newValue)}
<Autocomplete onChange={(_, val) => {
value={value} setSenior(val);
id="senior-searcher" }}
onInputChange={(_, newValue) => handleChange(newValue)} options={seniors}
onChange={(_, val) => { getOptionSelected={(option, val) => option.id === val.id}
field.onChange(val?.id); getOptionLabel={(option) => option.user.lastName}
setValue(val); renderOption={(option) => (
<>
{option.user.firstName} {option.user.lastName} {option.user.username}{' '}
{option.memberCardNumber}
</>
)}
renderInput={(params) => (
<TextField
{...params}
label="Search a senior"
variant="outlined"
InputProps={{
...params.InputProps,
}} }}
options={seniors}
getOptionSelected={(option, val) => option.id === val.id}
getOptionLabel={(option) => option.user.lastName}
renderOption={(option) => (
<>
{option.user.firstName} {option.user.lastName}{' '}
{option.user.username} {option.memberCardNumber}
</>
)}
renderInput={(params) => (
<TextField
{...params}
label="Search a senior"
variant="outlined"
InputProps={{
...params.InputProps,
}}
/>
)}
/> />
)} )}
/> />
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
Button, Button,
} from '@material-ui/core'; } from '@material-ui/core';
import { ResponseProps } from 'api/ResponseProps';
import { getRoleObject } from 'api/getRoleObject'; import { getRoleObject } from 'api/getRoleObject';
import { setReservation } from 'api/setReservation'; import { setReservation } from 'api/setReservation';
import { SubmitHandler, useForm } from 'react-hook-form'; import { SubmitHandler, useForm } from 'react-hook-form';
...@@ -25,10 +26,12 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({ ...@@ -25,10 +26,12 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({
handleClose, handleClose,
isOpen, isOpen,
}: ReservationDialogProps) => { }: ReservationDialogProps) => {
const { control, handleSubmit, register } = useForm<InsertReservationProps>({ const { control, handleSubmit } = useForm<InsertReservationProps>({
mode: 'onSubmit', mode: 'onSubmit',
}); });
const [senior, setSenior] = useState<ResponseProps | null>(null);
const onSubmit: SubmitHandler<InsertReservationProps> = ( const onSubmit: SubmitHandler<InsertReservationProps> = (
reservation: InsertReservationProps, reservation: InsertReservationProps,
) => { ) => {
...@@ -37,6 +40,7 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({ ...@@ -37,6 +40,7 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({
filledReservation.insertedBy = role.id; filledReservation.insertedBy = role.id;
}); });
filledReservation.driverShift = ''; filledReservation.driverShift = '';
filledReservation.senior = senior?.id;
console.log(filledReservation); console.log(filledReservation);
setReservation(filledReservation); setReservation(filledReservation);
handleClose(); handleClose();
...@@ -56,7 +60,7 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({ ...@@ -56,7 +60,7 @@ export const ReservationDialog: FC<ReservationDialogProps> = ({
Write here below the details of your next reservation Write here below the details of your next reservation
</DialogContentText> </DialogContentText>
<SeniorSearch control={control} /> <SeniorSearch senior={senior} setSenior={setSenior} />
<InputField <InputField
name="number" name="number"
label="Phone number" label="Phone number"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment