caremyway-client/src/components/User/Provider/ProviderShift.jsx

275 lines
7.6 KiB
React
Raw Normal View History

2018-04-23 17:07:33 +00:00
import { utc, duration, ISO_8601 } from "moment";
import React, { Component, Fragment } from "react";
2018-04-23 17:07:33 +00:00
import { connect } from "react-redux";
import { Redirect } from "react-router-dom";
import {
Button,
Container,
Header,
Label,
List,
Loader,
2018-04-23 18:19:14 +00:00
Message,
TextArea,
Form
2018-04-23 17:07:33 +00:00
} from "semantic-ui-react";
2018-04-23 18:19:14 +00:00
import {
setClearPShiftState,
setFormShiftChart
} from "../../../actions/pShift/reducer.actions";
2018-04-23 17:07:33 +00:00
import {
getPShiftRequest,
updatePShiftRequest
} from "../../../actions/pShift/saga.actions";
import Error from "../../Shared/Error";
import {
getEmployerFromPrice,
getWorkTypeFromPrice,
getPriceFromPrice
} from "./ProviderShiftsShared";
import ShiftLabel from "../../Shared/ShiftLabel";
2018-04-23 17:07:33 +00:00
class ProviderShift extends Component {
handleChangePShiftApproval = (uuid, approved) => {
return () => {
this.props.dispatch(
updatePShiftRequest({
uuid,
provider_approved: approved,
2018-04-23 17:07:33 +00:00
action: "",
chart: null,
single: true
})
);
};
};
2018-04-23 18:19:14 +00:00
handleCheckInPShift = () => {
const { pShiftRequestSuccess } = this.props;
this.props.dispatch(
updatePShiftRequest({
uuid: pShiftRequestSuccess.uuid,
provider_approved: true,
2018-04-23 18:19:14 +00:00
action: "checkin",
chart: null,
single: true
})
);
};
handleChangeChart = event => {
this.props.dispatch(setFormShiftChart(event.target.value));
};
handleCheckOutPShift = () => {
const { pShiftRequestSuccess, chart } = this.props;
this.props.dispatch(
updatePShiftRequest({
uuid: pShiftRequestSuccess.uuid,
provider_approved: true,
2018-04-23 18:19:14 +00:00
action: "checkout",
chart,
single: true
})
);
};
2018-04-23 17:07:33 +00:00
render() {
const {
isSendingPShiftRequest,
pShiftRequestError,
pShiftRequestSuccess,
2018-04-23 18:19:14 +00:00
selfUser,
chart
2018-04-23 17:07:33 +00:00
} = this.props;
if (!selfUser.provider) {
return <Redirect to="/" />;
}
return (
<ProviderShiftFormView
isSendingPShiftRequest={isSendingPShiftRequest}
pShiftRequestError={pShiftRequestError}
shift={pShiftRequestSuccess}
2018-04-23 18:19:14 +00:00
chart={chart}
2018-04-23 17:07:33 +00:00
user={selfUser}
handleChangePShiftApproval={this.handleChangePShiftApproval}
2018-04-23 18:19:14 +00:00
handleChangeChart={this.handleChangeChart}
handleCheckInPShift={this.handleCheckInPShift}
handleCheckOutPShift={this.handleCheckOutPShift}
2018-04-23 17:07:33 +00:00
/>
);
}
}
const ProviderShiftFormView = ({
isSendingPShiftRequest,
pShiftRequestError,
shift,
2018-04-23 18:19:14 +00:00
chart,
2018-04-23 17:07:33 +00:00
user,
2018-04-23 18:19:14 +00:00
handleChangeChart,
handleChangePShiftApproval,
handleCheckInPShift,
handleCheckOutPShift
2018-04-23 17:07:33 +00:00
}) => {
2018-04-23 20:58:14 +00:00
const OLD_PEOPLE_TIME_FORMAT = "dddd, MMMM Do YYYY, h:mm a";
2018-04-23 17:07:33 +00:00
const employer = getEmployerFromPrice(shift.price, user) || {};
const client = employer.client || {};
const workType = getWorkTypeFromPrice(shift.price, user) || {};
const price = getPriceFromPrice(shift.price, user) || {};
2018-04-23 18:19:14 +00:00
const checkedIn = !!shift.actual_start && utc(shift.actual_start, ISO_8601);
const checkedOut = !!shift.actual_end && utc(shift.actual_end, ISO_8601);
2018-04-23 17:07:33 +00:00
const min = duration(
utc(shift.set_end, ISO_8601) - utc(shift.set_start, ISO_8601),
"milliseconds"
).as("minutes");
let displayDuration = duration(min, "minutes").humanize();
if (min % 60) {
displayDuration = duration(Math.floor(min / 60), "hours").humanize();
displayDuration += ` and ${duration(min % 60, "minutes").humanize()}`;
}
const approved = !!shift.provider_approved;
const rejected = !shift.provider_approved && shift.provider_approved !== null;
// const pending = shift.provider_approved === null;
2018-04-23 17:07:33 +00:00
return (
<Container>
<Header>Shift Details</Header>
<Loader active={isSendingPShiftRequest} />
<ShiftLabel
provider_approved={shift.provider_approved}
client_approved_start={shift.approved_start}
client_approved_end={shift.approved_end}
/>
2018-04-23 17:07:33 +00:00
<Label
circular
empty
style={{
backgroundColor: workType.color,
borderColor: workType.color
}}
/>{" "}
{workType.label}
<List bulleted>
<List.Item>
{"Scheduled for " +
2018-04-23 17:07:33 +00:00
utc(shift.set_start, ISO_8601)
.local(false)
2018-04-23 20:58:14 +00:00
.format(OLD_PEOPLE_TIME_FORMAT)}
2018-04-23 17:07:33 +00:00
</List.Item>
<List.Item>{displayDuration}</List.Item>
<List.Item>{`Rate $${price.amount}/hour`}</List.Item>
<List.Item>
<strong>Employer/Client:</strong>{" "}
{`${client.first_name} ${client.last_name}`.trim() || "No Name!"}{" "}
<a href={"mailto:" + client.email}>{client.email}</a>
</List.Item>
</List>
{shift.description && (
<Message>
<Message.Header>Description</Message.Header>
<p>{shift.description}</p>
</Message>
)}
2018-04-23 18:19:14 +00:00
{!checkedIn && (
<Button.Group>
{!approved && (
<Button
color="green"
onClick={handleChangePShiftApproval(shift.uuid, true)}
>
Approve
</Button>
)}
{!rejected && (
<Button
color="red"
onClick={handleChangePShiftApproval(shift.uuid, false)}
>
Reject
</Button>
)}
</Button.Group>
)}
{shift.approved_start && (
<Fragment>
<strong>Client</strong>
<List bulleted>
<List.Item>
{`Approved time start ${utc(shift.approved_start)
.local(false)
.format(OLD_PEOPLE_TIME_FORMAT)}`}
</List.Item>
<List.Item>{`Approved time end ${utc(shift.approved_end)
.local(false)
.format(OLD_PEOPLE_TIME_FORMAT)}`}</List.Item>
</List>
</Fragment>
)}
<strong>Provider</strong>
2018-04-23 18:19:14 +00:00
<List bulleted>
{!checkedIn && <List.Item>Pending Check In</List.Item>}{" "}
{checkedIn && (
<List.Item>
<strong>Checked In At:</strong>{" "}
2018-04-23 20:58:14 +00:00
{checkedIn.local(false).format(OLD_PEOPLE_TIME_FORMAT)}
2018-04-23 18:19:14 +00:00
</List.Item>
)}
{checkedOut && (
<List.Item>
<strong>Checked Out At:</strong>{" "}
2018-04-23 20:58:14 +00:00
{checkedOut.local(false).format(OLD_PEOPLE_TIME_FORMAT)}
2018-04-23 18:19:14 +00:00
</List.Item>
2018-04-23 17:07:33 +00:00
)}
2018-04-23 18:19:14 +00:00
</List>
{shift.chart && (
<Message>
<Message.Header>Chart</Message.Header>
{shift.chart}
</Message>
)}
{!checkedIn && <Button onClick={handleCheckInPShift}>Check In</Button>}
{checkedIn &&
!checkedOut && (
<Form onSubmit={handleCheckOutPShift}>
<label>Chart</label>
<TextArea
value={chart}
onChange={handleChangeChart}
placeholder="Chart"
/>
<Button>Check Out</Button>
</Form>
2018-04-23 17:07:33 +00:00
)}
2018-04-23 18:19:14 +00:00
<Error error={pShiftRequestError} />
2018-04-23 17:07:33 +00:00
</Container>
);
};
function mapStateToProps(state) {
return { ...state.pShift, selfUser: state.user.selfUser };
}
class ProviderShiftWrapper extends Component {
constructor(props) {
super(props);
this.props.dispatch(setClearPShiftState());
this.props.dispatch(
getPShiftRequest({ uuid: this.props.match.params.shiftUUID })
);
}
render() {
const { pShiftRequestSuccess, pShiftRequestError } = this.props;
if (pShiftRequestSuccess.uuid) {
return <ProviderShift {...this.props} />;
} else if (!pShiftRequestError) {
return <Loader active />;
} else {
return <Error error={pShiftRequestError} />;
}
}
}
export default connect(mapStateToProps)(ProviderShiftWrapper);