forked from jonasschmedtmann/ultimate-react-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes 17-the-wild-oasis starter file fix jonasschmedtmann#8
- Loading branch information
Showing
2 changed files
with
19 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/**/node_modules |
83 changes: 17 additions & 66 deletions
83
17-the-wild-oasis/starter/features/settings/UpdateSettingsForm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,25 @@ | ||
import Spinner from 'ui/Spinner'; | ||
import { useSettings } from 'features/settings/useSettings'; | ||
import { useUpdateSetting } from 'features/settings/useUpdateSetting'; | ||
|
||
import Form from 'ui/Form'; | ||
import FormRow from 'ui/FormRow'; | ||
import Input from 'ui/Input'; | ||
|
||
function UpdateSettingsForm() { | ||
const { | ||
settings: { | ||
minBookingLength, | ||
maxBookingLength, | ||
maxGuestsPerBooking, | ||
breakfastPrice, | ||
} = {}, | ||
isLoading, | ||
} = useSettings(); | ||
const { mutate: updateSetting, isLoading: isUpdating } = useUpdateSetting(); | ||
|
||
// return <Spinner />; | ||
if (isLoading) return <Spinner />; | ||
|
||
function handleBlur(e, field) { | ||
const { value } = e.target; | ||
|
||
if (!value) return; | ||
updateSetting({ [field]: value }); | ||
} | ||
|
||
// This time we are using UNCONTROLLED fields, so we will NOT store state | ||
return ( | ||
<Form> | ||
<FormRow label='Minimum nights/booking'> | ||
<Input | ||
type='number' | ||
defaultValue={minBookingLength} | ||
onBlur={(e) => handleBlur(e, 'minBookingLength')} | ||
disabled={isUpdating} | ||
id='min-nights' | ||
/> | ||
</FormRow> | ||
<FormRow label='Maximum nights/booking'> | ||
<Input | ||
type='number' | ||
defaultValue={maxBookingLength} | ||
onBlur={(e) => handleBlur(e, 'maxBookingLength')} | ||
disabled={isUpdating} | ||
id='max-nights' | ||
/> | ||
</FormRow> | ||
<FormRow label='Maximum guests/booking'> | ||
<Input | ||
type='number' | ||
defaultValue={maxGuestsPerBooking} | ||
onBlur={(e) => handleBlur(e, 'maxGuestsPerBooking')} | ||
disabled={isUpdating} | ||
id='max-guests' | ||
/> | ||
</FormRow> | ||
<FormRow label='Breakfast price'> | ||
<Input | ||
type='number' | ||
defaultValue={breakfastPrice} | ||
onBlur={(e) => handleBlur(e, 'breakfastPrice')} | ||
disabled={isUpdating} | ||
id='breakfast-price' | ||
/> | ||
</FormRow> | ||
</Form> | ||
); | ||
// This time we are using UNCONTROLLED fields, so we will NOT store state | ||
return ( | ||
<Form> | ||
<FormRow label="Minimum nights/booking"> | ||
<Input type="number" id="min-nights" /> | ||
</FormRow> | ||
<FormRow label="Maximum nights/booking"> | ||
<Input type="number" id="max-nights" /> | ||
</FormRow> | ||
<FormRow label="Maximum guests/booking"> | ||
<Input type="number" id="max-guests" /> | ||
</FormRow> | ||
<FormRow label="Breakfast price"> | ||
<Input type="number" id="breakfast-price" /> | ||
</FormRow> | ||
</Form> | ||
); | ||
} | ||
|
||
export default UpdateSettingsForm; |