Skip to content

Commit

Permalink
Merge branch 'develop' into TASK-423-sticky-month-header
Browse files Browse the repository at this point in the history
  • Loading branch information
pectom authored Mar 24, 2021
2 parents eee1120 + 9c08749 commit 0280111
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 48 deletions.
3 changes: 3 additions & 0 deletions cypress/fixtures/march-2021-worker-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { ShiftCode } from "../../src/common-models/shift-info.model";
import { MonthDataArray } from "../../src/helpers/shifts.helper";
import { ContractType } from "../../src/common-models/worker-info.model";

const monthWorkerData = {
expectedRequiredWorkHours: {
Expand Down Expand Up @@ -1023,6 +1024,7 @@ const createWorkerInfoObject = (workerName: string): WorkerTestDataInstance => {
return {
workerName,
workerNorm: monthWorkerData.time[workerName],
workerContract: ContractType.EMPLOYMENT_CONTRACT,
workerReqiuredHours: monthWorkerData.expectedRequiredWorkHours[workerName],
workerActualHours: monthWorkerData.expectedActualHours[workerName],
actualWorkerShifts: currentMonthShifts,
Expand All @@ -1036,6 +1038,7 @@ const createWorkerInfoObject = (workerName: string): WorkerTestDataInstance => {
export interface WorkerTestDataInstance {
workerName: string;
workerNorm: number;
workerContract: ContractType;
workerReqiuredHours: number;
workerActualHours: number;
actualWorkerShifts: ShiftCode[];
Expand Down
3 changes: 2 additions & 1 deletion cypress/integration/unit/helpers/shift.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ describe("ShiftHelper", () => {
[ShiftCode.L4]: 0,
[ShiftCode.K]: 0,
[ShiftCode.OP]: 0,
[ShiftCode.OK]: 0,
[ShiftCode.O8]: 0,
[ShiftCode.O12]: 0,
[ShiftCode.NZ]: 0,
};

Expand Down
3 changes: 3 additions & 0 deletions cypress/integration/unit/helpers/worker-hours-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { MonthDataArray, ShiftHelper } from "../../../../src/helpers/shifts.helper";
import { WorkerHourInfo } from "../../../../src/helpers/worker-hours-info.model";
import { workerTestData, WorkerTestDataInstance } from "../../../fixtures/march-2021-worker-data";
import { ContractType } from "../../../../src/common-models/worker-info.model";

describe("Worker hours info", () => {
workerTestData.forEach((workerInstance) => {
Expand Down Expand Up @@ -72,6 +73,7 @@ describe("Worker hours info", () => {
shifts,
primaryShifts as MonthDataArray<ShiftCode>,
1,
ContractType.EMPLOYMENT_CONTRACT,
testedMonthParams.monthNumber,
testedMonthParams.year,
dates,
Expand All @@ -97,6 +99,7 @@ function calculateWorkerHoursFromWorkerInstance(
actualWorkerShifts ?? workerInstance.actualWorkerShifts,
baseWorkerShifts as MonthDataArray<ShiftCode>,
workerInstance.workerNorm,
workerInstance.workerContract,
workerInstance.month,
workerInstance.year,
workerInstance.dates,
Expand Down
21 changes: 17 additions & 4 deletions src/common-models/shift-info.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Shift {
to: number;
color: string;
isWorkingShift?: boolean;
normSubtraction?: number;
}

export enum ShiftCode {
Expand All @@ -36,8 +37,10 @@ export enum ShiftCode {
K = "K",
NZ = "NZ",
OP = "OP",
OK = "OK",
O8 = "O8",
O12 = "O12",
}

export const SHIFTS: { [code in ShiftCode]: Shift } = {
RP: {
code: "RP",
Expand Down Expand Up @@ -158,13 +161,23 @@ export const SHIFTS: { [code in ShiftCode]: Shift } = {
color: "fc03e7",
isWorkingShift: false,
},
OK: {
code: "OK",
name: "urlop okolicznościowy",
O8: {
code: "O8",
name: "urlop okolicznościowy 8h",
from: 0,
to: 24,
color: "56f5f5",
isWorkingShift: false,
normSubtraction: 8,
},
O12: {
code: "O12",
name: "urlop okolicznościowy 12h",
from: 0,
to: 24,
color: "C3A000",
isWorkingShift: false,
normSubtraction: 12,
},
NZ: {
code: "NZ",
Expand Down
11 changes: 9 additions & 2 deletions src/components/namestable/use-worker-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ export function useWorkerInfo(workerName: string): UseWorkerInfoReturn {
const workerContractType = useSelector((state: ApplicationStateModel) =>
getWorkerInfo<ContractType>(state, "contractType")
);

const workerGroup = useSelector((state: ApplicationStateModel) =>
getWorkerInfo<WorkerGroup>(state, "workerGroup")
);

const workerShifts = useSelector(
(state: ApplicationStateModel) =>
state.actualState.persistentSchedule.present.shifts[workerName]
);

useEffect(() => {
const newWorkerInfo = new WorkerInfo(
workerName,
workerContractType,
workerTime,
workerType,
workerShifts
workerShifts,
workerGroup
);
setWorkerInfo(newWorkerInfo);
}, [workerName, workerContractType, workerTime, workerType, workerShifts]);
}, [workerName, workerContractType, workerTime, workerType, workerShifts, workerGroup]);
return {
workerInfo,
setWorkerInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BaseCellOptions } from "../../schedule-parts/base-cell/base-cell.models
import { BaseRowComponent } from "../../schedule-parts/base-row.component";
import { PivotCell } from "../../schedule-parts/hooks/use-cell-selection";
import { ShiftRowOptions } from "../../schedule-parts/shift-row.component";
import { SelectionMatrix, useSelectionMatrix } from "./use-selection-matrix";
import { areDimesionsEqual, SelectionMatrix, useSelectionMatrix } from "./use-selection-matrix";

export enum DirectionKey {
ArrowRight = "ArrowRight",
Expand Down Expand Up @@ -78,8 +78,9 @@ function BaseSectionComponentF({
}
}

const dataArray = data.map((d) => d.rowData());
const { selectionMatrix, setSelectionMatrix, resetSelectionMatrix } = useSelectionMatrix(
data.map((d) => d.rowData())
dataArray
);

const resetSelection = useCallback((): void => {
Expand Down Expand Up @@ -112,29 +113,30 @@ function BaseSectionComponentF({

return (
<>
{data.map((dataRow, rowInd) => (
<RowComponent
selection={[...selectionMatrix[rowInd]]}
key={`${dataRow.rowKey}_${rowInd}`}
rowIndex={rowInd}
dataRow={dataRow}
cellComponent={cellComponent}
pointerPosition={pointerPosition.row === rowInd ? pointerPosition.cell : -1}
onKeyDown={movePointer}
onClick={(cellInd): void => handleCellClick(rowInd, cellInd)}
onDrag={(pivot, cellInd): void => onDrag(pivot, rowInd, cellInd)}
onDragEnd={(rowIndex, cellIndex): void =>
setPointerPosition({ row: rowIndex, cell: cellIndex })
}
sectionKey={sectionKey}
onSave={onSave}
onBlur={resetSelection}
isEditable={dataRow.isEditable}
errorSelector={(cellIndex, scheduleErrors): ScheduleError[] =>
errorSelector?.(dataRow.rowKey, cellIndex, scheduleErrors) ?? []
}
/>
))}
{areDimesionsEqual(selectionMatrix, dataArray) &&
data.map((dataRow, rowInd) => (
<RowComponent
selection={[...selectionMatrix[rowInd]]}
key={`${dataRow.rowKey}_${rowInd}`}
rowIndex={rowInd}
dataRow={dataRow}
cellComponent={cellComponent}
pointerPosition={pointerPosition.row === rowInd ? pointerPosition.cell : -1}
onKeyDown={movePointer}
onClick={(cellInd): void => handleCellClick(rowInd, cellInd)}
onDrag={(pivot, cellInd): void => onDrag(pivot, rowInd, cellInd)}
onDragEnd={(rowIndex, cellIndex): void =>
setPointerPosition({ row: rowIndex, cell: cellIndex })
}
sectionKey={sectionKey}
onSave={onSave}
onBlur={resetSelection}
isEditable={dataRow.isEditable}
errorSelector={(cellIndex, scheduleErrors): ScheduleError[] =>
errorSelector?.(dataRow.rowKey, cellIndex, scheduleErrors) ?? []
}
/>
))}
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { useState } from "react";
import { useEffect, useState } from "react";

export type SelectionMatrix = boolean[][];

export function areDimesionsEqual(matrix1: unknown[][], matrix2: unknown[][]): boolean {
return matrix1.length === matrix2.length && matrix1[0].length === matrix2[0].length;
}

interface UseSelectionMatrixReturn {
setSelectionMatrix: (
source: SelectionMatrix,
Expand All @@ -20,6 +24,12 @@ interface UseSelectionMatrixReturn {
export function useSelectionMatrix(matrix: unknown[][]): UseSelectionMatrixReturn {
const [selectionMatrix, setSelectionMatrix] = useState<SelectionMatrix>(getFalsyCopy(matrix));

useEffect(() => {
if (!areDimesionsEqual(matrix, selectionMatrix)) {
setSelectionMatrix(getFalsyCopy(matrix));
}
}, [selectionMatrix, matrix, setSelectionMatrix]);

function getFalsyCopy(source: unknown[][]): SelectionMatrix {
return [...Array(source.length)].map((_) => Array(source[0].length).fill(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ScheduleStateModel,
} from "../../../../state/models/application-state.model";
import { ScheduleMode } from "./schedule-state.model";
import { ContractType } from "../../../../common-models/worker-info.model";

export function useWorkerHoursInfo(workerName: string): WorkerHourInfoSummary {
const isEditMode = useSelector(
Expand All @@ -28,6 +29,16 @@ export function useWorkerHoursInfo(workerName: string): WorkerHourInfoSummary {
(state: ApplicationStateModel) => state.actualState[scheduleKey].present.shifts
)[workerName];

const contractType = useSelector(
(state: ApplicationStateModel) =>
state.actualState[scheduleKey].present.employee_info.contractType
);

const workerContractType =
contractType && contractType[workerName]
? contractType[workerName]
: ContractType.EMPLOYMENT_CONTRACT;

const primaryWorkerShifts = useSelector(
(state: ApplicationStateModel) => state.actualState.primaryRevision.shifts
)[workerName];
Expand All @@ -49,14 +60,15 @@ export function useWorkerHoursInfo(workerName: string): WorkerHourInfoSummary {

useEffect(() => {
if (primaryRevisionMonth === month) {
if (!isAllValuesDefined([workerTime, workerShifts])) {
if (!isAllValuesDefined([workerTime, workerShifts, workerContractType])) {
return setWorkHoursInfo(new WorkerHourInfo(0, 0, 0));
}
setWorkHoursInfo(
WorkerHourInfo.fromWorkerInfo(
workerShifts,
primaryWorkerShifts as MonthDataArray<ShiftCode>, // TODO: modify MonthDataModel to contain only MonthDataArray
workerTime,
workerContractType,
month,
year,
dates,
Expand All @@ -69,6 +81,7 @@ export function useWorkerHoursInfo(workerName: string): WorkerHourInfoSummary {
workerShifts,
primaryWorkerShifts,
workerTime,
workerContractType,
month,
year,
dates,
Expand Down
Loading

0 comments on commit 0280111

Please sign in to comment.