Skip to content

Commit

Permalink
Adds two logic for schedule modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Forostianyi committed Jan 2, 2021
1 parent 846b6c9 commit a2846d9
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 86 deletions.
Binary file modified cypress/fixtures/grafik.xlsx
Binary file not shown.
30 changes: 30 additions & 0 deletions cypress/integration/e2e/table/schedule-modes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ShiftCode } from "../../../../src/common-models/shift-info.model";
import { WorkerType } from "../../../../src/common-models/worker-info.model";

const testedCell = {
workerType: WorkerType.NURSE,
workerIdx: 0,
shiftIdx: 6,
initialShiftCode: ShiftCode.U,
desiredShiftCode: ShiftCode.W,
};

context("Schedule modes spec", () => {
beforeEach(() => {
cy.loadSchedule();
});

it("Should not be able to change shift in readonly mode", () => {
cy.getWorkerShift(testedCell);
cy.get(`[data-cy=autocomplete-${testedCell.initialShiftCode}]`, { timeout: 100000 }).should(
"not.exist"
);
});

it("Should be able to change shit in edit mode", () => {
cy.get("[data-cy=edit-mode-button]").click();
cy.checkWorkerShift({ ...testedCell, desiredShiftCode: testedCell.initialShiftCode });
cy.changeWorkerShift({ ...testedCell, newShiftCode: testedCell.desiredShiftCode });
cy.checkWorkerShift({ ...testedCell });
});
});
23 changes: 12 additions & 11 deletions cypress/integration/e2e/table/undo-redo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ context("Undo/Redo test", () => {
});
});

testCases.forEach((testCase) => {
it(`Should change worker (type: ${testCase.testedShiftCell.workerType.toLowerCase()}shift and
use undo and redo shortcuts to set proper cell state`, () => {
cy.changeWorkerShift({ ...testCase.testedShiftCell, newShiftCode: testCase.firstShift });
cy.changeWorkerShift({ ...testCase.testedShiftCell, newShiftCode: testCase.secondShift });
// TODO: add shortcut integration depending on schedule mode
// testCases.forEach((testCase) => {
// it(`Should change worker (type: ${testCase.testedShiftCell.workerType.toLowerCase()}shift and
// use undo and redo shortcuts to set proper cell state`, () => {
// cy.changeWorkerShift({ ...testCase.testedShiftCell, newShiftCode: testCase.firstShift });
// cy.changeWorkerShift({ ...testCase.testedShiftCell, newShiftCode: testCase.secondShift });

cy.get("body").type("{ctrl}{z}");
cy.getWorkerShift(testCase.testedShiftCell).contains(testCase.firstShift);
// cy.get("body").type("{ctrl}{z}");
// cy.getWorkerShift(testCase.testedShiftCell).contains(testCase.firstShift);

cy.get("body").type("{ctrl}{shift}{z}");
cy.getWorkerShift(testCase.testedShiftCell).contains(testCase.secondShift);
});
});
// cy.get("body").type("{ctrl}{shift}{z}");
// cy.getWorkerShift(testCase.testedShiftCell).contains(testCase.secondShift);
// });
// });
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ context("Work hours info (summary table)", () => {
// sanity check in case schedule in the docs gets changed and these tests start failing because of it
before("Has expected initial values of workHourInfo in example schedule", () => {
cy.loadSchedule();
cy.get("[data-cy=edit-mode-button]").click();

cy.checkHoursInfo({
workerType: WorkerType.NURSE,
Expand Down
23 changes: 14 additions & 9 deletions src/api/local-storage-provider.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import PouchDB from "pouchdb-browser";
import { ScheduleDataModel } from "../common-models/schedule-data.model";
import { ScheduleDataActionType } from "../state/reducers/schedule-data.reducer";
import {
ScheduleActionCreators,
ScheduleDataActionType,
} from "../state/reducers/schedule-data.reducer";
import {
PersistanceStoreProvider,
RevisionFilter,
Expand Down Expand Up @@ -35,10 +38,11 @@ export class LocalStorageProvider extends PersistanceStoreProvider {
data: schedule,
revisionType: type,
});
dispatch({
type: ScheduleDataActionType.ADD_NEW,
payload: schedule,
});
const action = ScheduleActionCreators.createScheduleAction(
ScheduleDataActionType.ADD_NEW,
schedule
);
dispatch(action);
};
}

Expand All @@ -56,10 +60,11 @@ export class LocalStorageProvider extends PersistanceStoreProvider {
if (!result?.doc) {
return;
}
dispatch({
type: ScheduleDataActionType.ADD_NEW,
payload: result.doc.data,
});
const action = ScheduleActionCreators.createScheduleAction(
ScheduleDataActionType.UPDATE,
result.doc.data
);
dispatch(action);
};
}
}
30 changes: 16 additions & 14 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React, { useEffect, useState } from "react";
import { CustomGlobalHotKeys, HeaderComponent } from "./components/common-components";
import { SchedulePage } from "./components/schedule-page/schedule-page.component";
import { useDispatch } from "react-redux";
import { Route, Switch } from "react-router-dom";
import schedule from "./assets/devMode/schedule.js";
import { ScheduleDataActionType } from "./state/reducers/schedule-data.reducer";
import { ActionModel } from "./state/models/action.model";
import { ScheduleDataModel } from "./common-models/schedule-data.model";
import ManagementPage from "./components/workers-page/management-page.component";
import { ScheduleDataModel } from "./common-models/schedule-data.model.js";
import { HeaderComponent } from "./components/common-components";
import RouteButtonsComponent from "./components/common-components/route-buttons/route-buttons.component";
import { Route, Switch } from "react-router-dom";
import { NewMonthPlanComponent } from "./components/schedule-page/new-month-page.component";
import { SchedulePage } from "./components/schedule-page/schedule-page.component";
import ManagementPage from "./components/workers-page/management-page.component";
import {
ScheduleActionCreators,
ScheduleDataActionType,
} from "./state/reducers/schedule-data.reducer";

interface TabData {
label: string;
Expand All @@ -27,17 +29,17 @@ function App(): JSX.Element {

useEffect(() => {
if (process.env.REACT_APP_DEV_MODE === "true") {
scheduleDispatcher({
type: ScheduleDataActionType.ADD_NEW,
payload: schedule,
} as ActionModel<ScheduleDataModel>);
const action = ScheduleActionCreators.createScheduleAction(
ScheduleDataActionType.ADD_NEW,
schedule as ScheduleDataModel
);
scheduleDispatcher(action);
}
}, [scheduleDispatcher]);

return (
<React.Fragment>
<>
<div>
<CustomGlobalHotKeys />
<Switch>
<Route path="/next-month">
<HeaderComponent isNewMonthPage={true} />
Expand All @@ -49,7 +51,7 @@ function App(): JSX.Element {
</Route>
</Switch>
</div>
</React.Fragment>
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";
import { useDispatch } from "react-redux";
import { ActionCreators as UndoActionCreators } from "redux-undo";

// TODO: Add integration based on actual schedule mode
export function CustomGlobalHotKeys(): JSX.Element {
const dispatch = useDispatch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function EditPageToolbar({ closeEdit }: EditPageToolbarOptions): JSX.Elem
data-cy="save-schedule-button"
variant="outlined"
onClick={(): void => {
scheduleLogic && scheduleLogic.updateActualRevision();
scheduleLogic?.updateActualRevision();
}}
>
Zapisz
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React, { ChangeEvent, useEffect, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useScheduleConverter } from "./hooks/use-schedule-converter";
import { ScheduleError } from "../../../common-models/schedule-error.model";
import { ScheduleExportLogic } from "../../../logic/schedule-exporter/schedule-export.logic";
import { ActionModel } from "../../../state/models/action.model";
import { ApplicationStateModel } from "../../../state/models/application-state.model";
import { ScheduleDataModel } from "../../../common-models/schedule-data.model";
import { ScheduleError } from "../../../common-models/schedule-error.model";
import { ScheduleDataActionType } from "../../../state/reducers/schedule-data.reducer";
import {
ScheduleActionCreators,
ScheduleDataActionType,
} from "../../../state/reducers/schedule-data.reducer";
import { ScheduleErrorActionType } from "../../../state/reducers/schedule-errors.reducer";
import { ScheduleExportLogic } from "../../../logic/schedule-exporter/schedule-export.logic";
import {
ButtonData,
DropdownButtons,
} from "../../common-components/dropdown-buttons/dropdown-buttons.component";
import { useScheduleConverter } from "./hooks/use-schedule-converter";

export function ImportButtonsComponent(): JSX.Element {
const DEFAULT_FILENAME = "grafik.xlsx";
Expand All @@ -38,10 +40,11 @@ export function ImportButtonsComponent(): JSX.Element {

useEffect(() => {
if (scheduleModel) {
scheduleDipatcher({
type: ScheduleDataActionType.ADD_NEW,
payload: scheduleModel,
} as ActionModel<ScheduleDataModel>);
const action = ScheduleActionCreators.createScheduleAction(
ScheduleDataActionType.ADD_NEW,
scheduleModel
);
scheduleDipatcher(action);
} else if (errorOccurred) {
//todo display message
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export function BaseCellComponent({
collect: (monitor) => {
if (monitor.isOver()) {
if (!isBlocked) {
onDrag && onDrag(monitor.getItem() as PivotCell);
onDrag?.(monitor.getItem() as PivotCell);
}
}
},
drop: () => {
onDragEnd && onDragEnd();
onDragEnd?.();
},
});
const [, drag, preview] = useDrag({
Expand All @@ -92,7 +92,7 @@ export function BaseCellComponent({
cellIndex: index,
} as PivotCell,
end: (item, monitor) => {
if (!monitor.didDrop()) onDragEnd && onDragEnd();
if (!monitor.didDrop()) onDragEnd?.();
},
});
// Below lines disable default preview image that is inserted by browser on dragging
Expand All @@ -102,13 +102,13 @@ export function BaseCellComponent({

function _onKeyDown(e: React.KeyboardEvent<HTMLInputElement>): void {
if (e.key === CellManagementKeys.Enter) {
onValueChange && onValueChange(e.currentTarget.value);
onValueChange?.(e.currentTarget.value);
return;
}
onKeyDown && onKeyDown(e);
onKeyDown?.(e);
}
function _onValueChange(newValue: string): void {
onValueChange && onValueChange(newValue);
onValueChange?.(newValue);
}
function getId(): string {
if (verboseDate && monthNumber) {
Expand All @@ -133,7 +133,7 @@ export function BaseCellComponent({
className={classNames("mainCell", { selection: isSelected, blocked: isBlocked })}
id={getId()}
onBlur={(): void => {
onBlur && onBlur();
onBlur?.();
}}
>
<div className="content" ref={drag}>
Expand Down Expand Up @@ -164,7 +164,7 @@ export function BaseCellComponent({
data-cy="cell"
className="relative"
onClick={(): void => {
!isBlocked && onClick && onClick();
if (!isBlocked) onClick?.();
}}
>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function BaseRowComponentF({
const numberOfDays = verboseDates?.length;

function saveValue(newValue: string): void {
if (sectionKey) onSave && onSave(newValue);
if (sectionKey) onSave?.(newValue);
}
let data = dataRow.rowData(false);

Expand All @@ -70,14 +70,14 @@ export function BaseRowComponentF({
style={ShiftHelper.getShiftColor(cellData, verboseDates?.[cellIndex])}
isPointerOn={cellIndex === pointerPosition}
isBlocked={!isEditable}
onKeyDown={(event): void => onKeyDown && onKeyDown(cellIndex, event)}
onKeyDown={(event): void => onKeyDown?.(cellIndex, event)}
onValueChange={saveValue}
onClick={(): void => onClick && onClick(cellIndex)}
onBlur={(): void => onBlur && onBlur()}
onClick={(): void => onClick?.(cellIndex)}
onBlur={(): void => onBlur?.()}
monthNumber={currMonthNumber}
verboseDate={verboseDates?.[cellIndex]}
onDrag={(pivot): void => onDrag && onDrag(pivot, cellIndex)}
onDragEnd={(): void => onDragEnd && onDragEnd(index, cellIndex)}
onDrag={(pivot): void => onDrag?.(pivot, cellIndex)}
onDragEnd={(): void => onDragEnd?.(index, cellIndex)}
/>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ScheduleViewOnlyPageOptions {

export function ScheduleViewOnlyPage(props: ScheduleViewOnlyPageOptions): JSX.Element {
const scheduleModel = useSelector((state: ApplicationStateModel) => {
return state.scheduleData.present;
return state.actualRevision.present;
});
const { scheduleLogic, setNewSchedule, scheduleLocalState } = useScheduleState(
scheduleModel,
Expand Down
2 changes: 1 addition & 1 deletion src/logic/schedule-logic/data-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class DataRow implements DataRowModel {
return this.key;
}

public disableEdit() {
public disableEdit(): boolean {
this.isEditable = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/logic/schedule-logic/foundation-info.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class FoundationInfoLogic
new DataRow(FoundationSectionKey.NurseCount, this.getWorkersCount(WorkerType.NURSE), false),
];

public disableEdit() {
public disableEdit(): void {
this.rows.forEach((row) => row.disableEdit());
}

Expand Down
8 changes: 0 additions & 8 deletions src/logic/schedule-logic/metadata.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,4 @@ export class MetadataLogic extends BaseSectionLogic implements MetadataProvider
public get dates(): number[] {
return this.monthLogic.verboseDates.map((d) => d.date);
}

// public get dayNumbers(): number[] {
// return this.monthLogic.dates;
// }

// public get sectionData(): DataRow[] {
// return [new DataRow(MetaDataSectionKey.MonthDays, this.dayNumbers)];
// }
}
12 changes: 7 additions & 5 deletions src/logic/schedule-logic/schedule.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ShiftsInfoLogic } from "./shifts-info.logic";
import { ChildrenSectionKey, ExtraWorkersSectionKey } from "../section.model";
import { PersistanceStoreProvider, RevisionFilter } from "../../api/persistance-store.model";
import {
ScheduleActionCreators,
ScheduleActionModel,
ScheduleDataActionType,
} from "../../state/reducers/schedule-data.reducer";
Expand All @@ -34,7 +35,7 @@ export class ScheduleLogic implements ScheduleProvider {
this.update(scheduleModel);
}

public disableEdit() {
public disableEdit(): void {
Object.values(this.sections).forEach((section) => {
(section as BaseSectionLogic).disableEdit();
});
Expand Down Expand Up @@ -168,9 +169,10 @@ export class ScheduleLogic implements ScheduleProvider {

private updateGlobalState(): void {
const model = this.schedule.getDataModel();
this.dispatchScheduleUpdate({
type: ScheduleDataActionType.UPDATE,
payload: model,
});
const action = ScheduleActionCreators.createEditableSchedulAction(
ScheduleDataActionType.UPDATE,
model
);
this.dispatchScheduleUpdate(action);
}
}
2 changes: 1 addition & 1 deletion src/logic/schedule-logic/shifts-info.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ShiftsInfoLogic extends BaseSectionLogic implements ShiftsProvider
this._scheduleErrors = value;
}

private get shifts() {
private get shifts(): { [key: string]: DataRow } {
return ArrayHelper.arrayToObject(
this.sectionData,
(row) => row.rowKey,
Expand Down
Loading

0 comments on commit a2846d9

Please sign in to comment.