Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | 2x 2x 2x 2x 2x 9x 7x 32x 28x 30x 30x 30x 30x 30x 30x 2x 3x 2x 30x 5x 5x 3x 1x 1x 1x 1x 1x 1x 5x 1x 4x 5x 30x 28x 5x 5x 5x 5x 5x 3x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 4x 1x 4x 5x 26x 26x 26x 3x 3x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 2x 1x 1x 2x 1x 1x | /*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import Shift from 'domain/Shift';
import { AppState } from 'store/types';
import { rosterOperations, rosterSelectors } from 'store/roster';
import { spotSelectors } from 'store/spot';
import { connect } from 'react-redux';
import WeekPicker from 'ui/components/WeekPicker';
import moment from 'moment';
import {
Level, LevelItem, Button, EmptyState, EmptyStateVariant, Title, EmptyStateIcon, EmptyStateBody,
} from '@patternfly/react-core';
import EditShiftModal from '../shift/EditShiftModal';
import TypeaheadSelectInput from 'ui/components/TypeaheadSelectInput';
import { alert } from 'store/alert';
import RosterState from 'domain/RosterState';
import ShiftEvent, { ShiftPopupHeader, ShiftPopupBody } from '../shift/ShiftEvent';
import EmployeeAvailability from 'domain/EmployeeAvailability';
import { employeeSelectors } from 'store/employee';
import Employee from 'domain/Employee';
import { availabilityOperations } from 'store/availability';
import { shiftOperations } from 'store/shift';
import EditAvailabilityModal from './EditAvailabilityModal';
import AvailabilityEvent, { AvailabilityPopoverHeader, AvailabilityPopoverBody } from './AvailabilityEvent';
import Schedule, { StyleSupplier } from "ui/components/calendar/Schedule";
import { CubesIcon } from '@patternfly/react-icons';
import {
withRouter, RouteComponentProps,
} from 'react-router-dom';
import { withTranslation, WithTranslation, Trans } from 'react-i18next';
interface StateProps {
isSolving: boolean;
isLoading: boolean;
allEmployeeList: Employee[];
shownEmployeeList: Employee[];
employeeIdToShiftListMap: Map<number, Shift[]>;
employeeIdToAvailabilityListMap: Map<number, EmployeeAvailability[]>;
startDate: Date | null;
endDate: Date | null;
totalNumOfSpots: number;
rosterState: RosterState | null;
}
// Snapshot of the last value to show when loading
let lastEmployeeIdToShiftListMap: Map<number, Shift[]> = new Map<number, Shift[]>();
let lastEmployeeIdToAvailabilityListMap:
Map<number, EmployeeAvailability[]> = new Map<number, EmployeeAvailability[]>();
let lastShownEmployeeList: Employee[] = [];
const mapStateToProps = (state: AppState): StateProps => ({
isSolving: state.solverState.isSolving,
isLoading: rosterSelectors.isLoading(state),
allEmployeeList: employeeSelectors.getEmployeeList(state),
shownEmployeeList: lastShownEmployeeList = rosterSelectors.isLoading(state)
? lastShownEmployeeList : rosterSelectors.getEmployeeListInAvailabilityRoster(state),
employeeIdToShiftListMap: lastEmployeeIdToShiftListMap = rosterSelectors
.getEmployeeListInAvailabilityRoster(state)
.reduce((prev, curr) => prev.set(curr.id as number,
rosterSelectors.getShiftListForEmployee(state, curr)),
rosterSelectors.isLoading(state) ? lastEmployeeIdToShiftListMap : new Map<number, Shift[]>()),
employeeIdToAvailabilityListMap: lastEmployeeIdToAvailabilityListMap = rosterSelectors
.getEmployeeListInAvailabilityRoster(state)
.reduce((prev, curr) => prev.set(curr.id as number,
rosterSelectors.getAvailabilityListForEmployee(state, curr)),
rosterSelectors.isLoading(state) ? lastEmployeeIdToAvailabilityListMap
: new Map<number, EmployeeAvailability[]>()),
startDate: (state.availabilityRoster.availabilityRosterView)
? moment(state.availabilityRoster.availabilityRosterView.startDate).toDate() : null,
endDate: (state.availabilityRoster.availabilityRosterView)
? moment(state.availabilityRoster.availabilityRosterView.endDate).toDate() : null,
totalNumOfSpots: spotSelectors.getSpotList(state).length,
rosterState: state.rosterState.rosterState,
});
export interface DispatchProps {
addEmployeeAvailability: typeof availabilityOperations.addEmployeeAvailability;
removeEmployeeAvailability: typeof availabilityOperations.removeEmployeeAvailability;
updateEmployeeAvailability: typeof availabilityOperations.updateEmployeeAvailability;
getAvailabilityRosterFor: typeof rosterOperations.getAvailabilityRosterFor;
refreshAvailabilityRoster: typeof rosterOperations.refreshAvailabilityRoster;
getInitialAvailabilityRoster: typeof rosterOperations.getInitialAvailabilityRoster;
solveRoster: typeof rosterOperations.solveRoster;
publishRoster: typeof rosterOperations.publish;
terminateSolvingRosterEarly: typeof rosterOperations.terminateSolvingRosterEarly;
showInfoMessage: typeof alert.showInfoMessage;
addShift: typeof shiftOperations.addShift;
updateShift: typeof shiftOperations.updateShift;
removeShift: typeof shiftOperations.removeShift;
}
const mapDispatchToProps: DispatchProps = {
addEmployeeAvailability: availabilityOperations.addEmployeeAvailability,
removeEmployeeAvailability: availabilityOperations.removeEmployeeAvailability,
updateEmployeeAvailability: availabilityOperations.updateEmployeeAvailability,
getAvailabilityRosterFor: rosterOperations.getAvailabilityRosterFor,
refreshAvailabilityRoster: rosterOperations.refreshAvailabilityRoster,
getInitialAvailabilityRoster: rosterOperations.getInitialAvailabilityRoster,
solveRoster: rosterOperations.solveRoster,
publishRoster: rosterOperations.publish,
terminateSolvingRosterEarly: rosterOperations.terminateSolvingRosterEarly,
showInfoMessage: alert.showInfoMessage,
addShift: shiftOperations.addShift,
updateShift: shiftOperations.updateShift,
removeShift: shiftOperations.removeShift,
}
export type Props = RouteComponentProps & StateProps & DispatchProps & WithTranslation;
interface State {
selectedAvailability?: EmployeeAvailability;
isCreatingOrEditingAvailability: boolean;
isCreatingOrEditingShift: boolean;
selectedShift?: Shift;
}
export interface ShiftOrAvailability {
type: 'Shift'|'Availability';
start: Date;
end: Date;
reference: Shift|EmployeeAvailability;
}
export function isShift(shiftOrAvailability: Shift|EmployeeAvailability): shiftOrAvailability is Shift {
return 'spot' in shiftOrAvailability;
}
export function isAvailability(shiftOrAvailability: Shift|EmployeeAvailability):
shiftOrAvailability is EmployeeAvailability {
return !isShift(shiftOrAvailability);
}
export function isDay(start: Date, end: Date) {
return start.getHours() === 0 && start.getMinutes() === 0
&& end.getHours() === 0 && end.getMinutes() === 0
}
export function isAllDayAvailability(ea: EmployeeAvailability) {
return isDay(ea.startDateTime, ea.endDateTime);
}
export class AvailabilityRosterPage extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.onDateChange = this.onDateChange.bind(this);
this.onUpdateEmployeeList = this.onUpdateEmployeeList.bind(this);
this.getEventStyle = this.getEventStyle.bind(this);
this.getDayStyle = this.getDayStyle.bind(this);
this.state = {
isCreatingOrEditingShift: false,
isCreatingOrEditingAvailability: false,
};
}
onDateChange(startDate: Date, endDate: Date) {
this.props.getAvailabilityRosterFor({
fromDate: startDate,
toDate: endDate,
employeeList: this.props.shownEmployeeList,
});
}
onUpdateEmployeeList(employee: Employee|undefined) {
if (employee) {
this.props.getAvailabilityRosterFor({
fromDate: this.props.startDate as Date,
toDate: this.props.endDate as Date,
employeeList: [employee],
});
}
}
getEventStyle: StyleSupplier<ShiftOrAvailability> = (soa) => {
const style: React.CSSProperties = {};
if (isAvailability(soa.reference)) {
switch (soa.reference.state) {
case 'DESIRED': {
style.backgroundColor = 'green';
break;
}
case 'UNDESIRED': {
style.backgroundColor = 'yellow';
break;
}
case 'UNAVAILABLE': {
style.backgroundColor = 'red';
break;
}
}
}
if (this.props.rosterState !== null && moment(soa.start).isBefore(this.props.rosterState.firstDraftDate)) {
style.border = '1px solid';
} else {
style.border = '1px dashed';
}
return { style };
}
getDayStyle: (availabilities: EmployeeAvailability[]) => StyleSupplier<Date> = (availabilities) =>
(date) => {
let className = "";
const style: React.CSSProperties = {};
const dayAvailability = availabilities.find(ea =>
!moment(ea.startDateTime).isAfter(date) && moment(date).isBefore(ea.endDateTime))
if (dayAvailability !== undefined) {
switch (dayAvailability.state) {
case "DESIRED": {
className = "desired";
break;
}
case "UNDESIRED": {
className = "undesired";
break;
}
case "UNAVAILABLE": {
className = "unavailable";
break;
}
}
}
if (this.props.rosterState !== null && moment(date).isBefore(this.props.rosterState.firstDraftDate)) {
Eif (!className) {
style.backgroundColor = "var(--pf-global--BackgroundColor--300)";
}
className = className + " published-day";
}
else {
if (!className) {
style.backgroundColor = "var(--pf-global--BackgroundColor--100)"
}
className = className + " draft-day";
}
return { className: className.trim() , style };
}
render() {
const { t, tReady } = this.props;
Iif (!tReady) {
return (<></>);
}
if (this.props.shownEmployeeList.length <= 0) {
Iif (!this.props.isLoading && this.props.allEmployeeList.length > 0) {
this.props.getInitialAvailabilityRoster();
}
return (
<EmptyState variant={EmptyStateVariant.full}>
<EmptyStateIcon icon={CubesIcon} />
<Trans
t={t}
i18nKey="noEmployees"
components={[
<Title key={0} headingLevel="h5" size="lg" />,
<EmptyStateBody key={1} />,
<Button
key={2}
aria-label="Employees Page"
variant="primary"
onClick={() => this.props.history.push('/employees')}
/>
]}
/>
</EmptyState>
);
}
const startDate = this.props.startDate as Date;
const endDate = this.props.endDate as Date;
const shownEmployee = this.props.shownEmployeeList[0];
const events: ShiftOrAvailability[] = [];
Eif (this.props.employeeIdToAvailabilityListMap.get(shownEmployee.id as number) !== undefined) {
(this.props.employeeIdToAvailabilityListMap.get(shownEmployee.id as number) as EmployeeAvailability[])
.forEach((ea) => {
events.push({
type: 'Availability',
start: ea.startDateTime,
end: ea.endDateTime,
reference: ea,
})
});
}
Eif (this.props.employeeIdToShiftListMap.get(shownEmployee.id as number) !== undefined) {
(this.props.employeeIdToShiftListMap.get(shownEmployee.id as number) as Shift[]).forEach((shift) => {
events.push({
type: 'Shift',
start: shift.startDateTime,
end: shift.endDateTime,
reference: shift,
})
});
}
return (
<>
<Level
gutter="sm"
style={{
height: '60px',
padding: '5px 5px 5px 5px',
backgroundColor: 'var(--pf-global--BackgroundColor--100)',
}}
>
<LevelItem style={{ display: 'flex' }}>
<TypeaheadSelectInput
aria-label="Select Employee"
emptyText={t("selectEmployee")}
optionToStringMap={employee => employee.name}
options={this.props.allEmployeeList}
value={this.props.shownEmployeeList[0]}
onChange={this.onUpdateEmployeeList}
/>
<WeekPicker
aria-label="Select Week to View"
value={this.props.startDate as Date}
onChange={this.onDateChange}
/>
</LevelItem>
<LevelItem style={{ display: 'flex' }}>
<Button
style={{ margin: '5px' }}
aria-label="Publish"
onClick={this.props.publishRoster}
>
{t("publish")}
</Button>
{(!this.props.isSolving
&& (
<Button
style={{ margin: '5px' }}
aria-label="Solve"
onClick={this.props.solveRoster}
>
{t("schedule")}
</Button>
)) || (
<Button
style={{ margin: '5px' }}
aria-label="Terminate Early"
onClick={this.props.terminateSolvingRosterEarly}
>
{t("terminateEarly")}
</Button>
)
}
<Button
style={{ margin: '5px' }}
aria-label="Refresh"
onClick={() => {
this.props.refreshAvailabilityRoster();
this.props.showInfoMessage('availabilityRosterRefresh');
}
}
>
{t("refresh")}
</Button>
<Button
style={{ margin: '5px' }}
aria-label="Create Availability"
onClick={() => {
Eif (!this.state.isCreatingOrEditingShift) {
this.setState({
selectedAvailability: undefined,
isCreatingOrEditingAvailability: true,
})
}
}}
>
{t("createAvailability")}
</Button>
</LevelItem>
</Level>
<EditAvailabilityModal
availability={this.state.selectedAvailability}
isOpen={this.state.isCreatingOrEditingAvailability}
onSave={(availability) => {
if (this.state.selectedAvailability !== undefined) {
this.props.updateEmployeeAvailability(availability);
} else {
this.props.addEmployeeAvailability(availability);
}
this.setState({ selectedAvailability: undefined, isCreatingOrEditingAvailability: false });
}}
onDelete={(availability) => {
this.props.removeEmployeeAvailability(availability);
this.setState({ isCreatingOrEditingAvailability: false });
}}
onClose={() => this.setState({ selectedAvailability: undefined, isCreatingOrEditingAvailability: false })}
/>
<EditShiftModal
aria-label="Edit Shift"
isOpen={this.state.isCreatingOrEditingShift}
shift={this.state.selectedShift}
onDelete={(shift) => {
this.props.removeShift(shift);
this.setState({ selectedShift: undefined, isCreatingOrEditingShift: false });
}
}
onSave={(shift) => {
if (this.state.selectedShift !== undefined) {
this.props.updateShift(shift);
} else {
this.props.addShift(shift);
}
this.setState({ selectedShift: undefined, isCreatingOrEditingShift: false });
}}
onClose={() => this.setState({ selectedShift: undefined, isCreatingOrEditingShift: false })}
/>
<Schedule<ShiftOrAvailability>
showAllDayCell
key={shownEmployee.id}
startDate={startDate}
endDate={endDate}
events={events}
titleAccessor={soa => (isShift(soa.reference) ? soa.reference.spot.name : soa.reference.state)}
startAccessor={soa => soa.start}
endAccessor={soa => soa.end}
addEvent={
(start, end) => {
this.props.addEmployeeAvailability({
tenantId: shownEmployee.tenantId,
startDateTime: start,
endDateTime: end,
employee: shownEmployee,
state: 'UNAVAILABLE',
});
}
}
eventStyle={this.getEventStyle}
dayStyle={this.getDayStyle(
(this.props.employeeIdToAvailabilityListMap
.get(shownEmployee.id as number) as EmployeeAvailability[])
.filter(isAllDayAvailability))}
wrapperStyle={event => ({
className: (isAvailability(event.reference))
? (isAllDayAvailability(event.reference)
? 'availability-allday-wrapper' : 'availability-wrapper')
: undefined,
style: {
zIndex: (isShift(event.reference))? 1 : 0
}
})}
popoverHeader={
soa => ((isShift(soa.reference)) ? ShiftPopupHeader({
shift: soa.reference,
onEdit: () => {
if (!this.state.isCreatingOrEditingAvailability) {
this.setState({
selectedShift: soa.reference as Shift,
isCreatingOrEditingShift: true,
})
}
},
onDelete: () => {
this.props.updateShift({
...soa.reference as Shift,
employee: null,
})
},
}) : AvailabilityPopoverHeader({
availability: soa.reference,
onEdit: ea => this.setState({
isCreatingOrEditingAvailability: true,
selectedAvailability: ea,
}),
onDelete: ea => this.props.removeEmployeeAvailability(ea),
updateEmployeeAvailability: this.props.updateEmployeeAvailability,
removeEmployeeAvailability: this.props.removeEmployeeAvailability,
}))
}
popoverBody={
soa => ((isShift(soa.reference)) ? ShiftPopupBody(soa.reference) : AvailabilityPopoverBody)
}
eventComponent={props => (isShift(props.event.reference) ? ShiftEvent(
{
...props.event.reference,
title: props.event.reference.spot.name,
event: props.event.reference,
},
) : AvailabilityEvent({
availability: props.event.reference,
onEdit: ea => this.setState({
isCreatingOrEditingAvailability: true,
selectedAvailability: ea,
}),
onDelete: ea => this.props.removeEmployeeAvailability(ea),
updateEmployeeAvailability: this.props.updateEmployeeAvailability,
removeEmployeeAvailability: this.props.removeEmployeeAvailability,
}))
}
/>
</>
);
}
}
export default withTranslation("AvailabilityRosterPage")(
connect(mapStateToProps, mapDispatchToProps)(withRouter(AvailabilityRosterPage))); |