Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In availability and booking #122

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 83 additions & 30 deletions client/src/components/profile/ProfileRequestForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import {
TextField,
Typography,
makeStyles,
FormControl,
Select,
InputLabel,
MenuItem,
} from "@material-ui/core";
import {
AuthDispatchContext,
AuthStateContext,
} from "../../context/AuthContext";
import { createRequest } from "../../actions/request";
import { format, isBefore, isSameDay, parseISO } from "date-fns";
import Alert from "../Alert";
import { CLEAR_ERRORS } from "../../actions/types";

Expand Down Expand Up @@ -42,7 +47,7 @@ const useStyles = makeStyles(() => ({
},
}));

function ProfileRequestForm({ sitterId }) {
function ProfileRequestForm({ sitterId, sitterPrice, sitterAvailability }) {
const classes = useStyles();

// Get dispatch method and state from auth context
Expand Down Expand Up @@ -78,6 +83,9 @@ function ProfileRequestForm({ sitterId }) {
}
}, [alerts]);

const checkDate = (date) => {
return isSameDay(parseISO(date.start), parseISO(startDate));
};
//clear any errors initially
useEffect(() => {
dispatch({ type: CLEAR_ERRORS });
Expand All @@ -90,13 +98,13 @@ function ProfileRequestForm({ sitterId }) {
}, [errors]);

// Handle change in 'Drop Off' (start) date input
const handleStartDateChange = e => {
const handleStartDateChange = (e) => {
const formattedStartDate = moment(
new Date(`${e.target.value}T${startTime}`)
).format();

setStartDate(e.target.value);

const result = sitterAvailability.find(checkDate);
// If start date is later than end date, change end date to same day as start date.
if (e.target.value > endDate) {
const formattedEndDate = moment(
Expand All @@ -117,7 +125,7 @@ function ProfileRequestForm({ sitterId }) {
};

// Handle change in 'Pick Up' (end) date input
const handleEndDateChange = e => {
const handleEndDateChange = (e) => {
const formattedEndDate = moment(
new Date(`${e.target.value}T${endTime}`)
).format();
Expand Down Expand Up @@ -146,7 +154,7 @@ function ProfileRequestForm({ sitterId }) {
};

// Handle change in 'Drop off' (start) time input
const handleStartTimeChange = e => {
const handleStartTimeChange = (e) => {
const formattedStartDate = moment(
new Date(`${startDate}T${e.target.value}`)
).format();
Expand All @@ -160,7 +168,7 @@ function ProfileRequestForm({ sitterId }) {
};

// Handle change in 'Pick Up' (start) time input.
const handleEndTimeChange = e => {
const handleEndTimeChange = (e) => {
const formattedEndDate = moment(
new Date(`${endDate}T${e.target.value}`)
).format();
Expand All @@ -173,19 +181,20 @@ function ProfileRequestForm({ sitterId }) {
});
};

const handleSubmit = async e => {
const handleSubmit = async (e) => {
setRequestSending(true);
e.preventDefault();
console.log(requestFormData);
createRequest(dispatch, requestFormData);
};

return (
<>
<Typography variant="h4" className={classes.hourlyRateHeading}>
$14/hr
<Typography variant='h4' className={classes.hourlyRateHeading}>
${sitterPrice}/hr
</Typography>
<Rating
name="simple-controlled"
name='simple-controlled'
value={3}
readOnly
className={classes.ratingsAlignment}
Expand All @@ -195,24 +204,46 @@ function ProfileRequestForm({ sitterId }) {
<Typography className={classes.labelStyles}>DROP OFF</Typography>
</Box>
<Box className={classes.inputGap}>
<FormControl variant='outlined'>
<InputLabel htmlFor='start-date'>Date</InputLabel>
<Select
value={startDate}
onChange={(e) => handleStartDateChange(e)}
label='Date'
>
{sitterAvailability.map(
(date) =>
!isBefore(parseISO(date.end), new Date()) && (
<MenuItem
key={date._id}
value={format(parseISO(date.start), "yyyy-MM-dd")}
>
{" "}
{format(parseISO(date.start), "MMMM dd, yyyy")}
</MenuItem>
)
)}
</Select>
</FormControl>
{/*
<TextField
InputProps={{
inputProps: { min: todayFormatted },
}}
type="date"
type='date'
value={startDate}
onChange={e => handleStartDateChange(e)}
variant="outlined"
></TextField>
onChange={(e) => handleStartDateChange(e)}
variant='outlined'
></TextField>*/}
<TextField
inputProps={{
step: 300, // 5 min
}}
type="time"
type='time'
value={startTime}
variant="outlined"
variant='outlined'
className={classes.textField + " " + classes.timeInputWidth}
onChange={e => {
onChange={(e) => {
handleStartTimeChange(e);
}}
/>
Expand All @@ -223,39 +254,61 @@ function ProfileRequestForm({ sitterId }) {
<Typography className={classes.labelStyles}>PICK UP</Typography>
</Box>
<Box className={classes.endDateAlign}>
<FormControl variant='outlined'>
<InputLabel htmlFor='end-date'>Date</InputLabel>
<Select
value={endDate}
onChange={(e) => handleEndDateChange(e)}
label='Date'
>
{sitterAvailability.map(
(date) =>
!isBefore(parseISO(date.end), new Date()) && (
<MenuItem
key={date._id}
value={format(parseISO(date.start), "yyyy-MM-dd")}
>
{" "}
{format(parseISO(date.start), "MMMM dd, yyyy")}
</MenuItem>
)
)}
</Select>
</FormControl>
{/*
<TextField
InputProps={{
inputProps: { min: startDate },
}}
type="date"
variant="outlined"
type='date'
variant='outlined'
value={endDate}
onChange={e => handleEndDateChange(e)}
></TextField>
onChange={(e) => handleEndDateChange(e)}
></TextField>*/}
<TextField
inputProps={{
step: 300, // 5 min
}}
type="time"
type='time'
value={endTime}
className={classes.textField + " " + classes.timeInputWidth}
variant="outlined"
onChange={e => {
variant='outlined'
onChange={(e) => {
handleEndTimeChange(e);
}}
/>
</Box>
</Box>
<Button
color="primary"
variant="contained"
type="submit"
size="large"
color='primary'
variant='contained'
type='submit'
size='large'
className={classes.buttonStyles}
onClick={e => handleSubmit(e)}
onClick={(e) => handleSubmit(e)}
>
{requestSending ? (
<CircularProgress color="white" size={20} />
<CircularProgress color='white' size={20} />
) : (
`SEND REQUEST`
)}
Expand Down
6 changes: 5 additions & 1 deletion client/src/pages/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ function Profile({ match }) {
classes.requestBreakpoints + " " + classes.centerContent
}
>
<ProfileRequestForm sitterId={match.params.user_id} />
<ProfileRequestForm
sitterId={match.params.user_id}
sitterPrice={profileDetails.price}
sitterAvailability={profileDetails.availability}
/>
</Paper>
)}
</>
Expand Down