Skip to content

Commit

Permalink
TASK-304 Another iteration over drawer for managing workers (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwebeck authored and prenc committed Mar 22, 2021
1 parent f6b1469 commit 7d8d721
Show file tree
Hide file tree
Showing 17 changed files with 822 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const useStyles = makeStyles({
height: ScssVars.drawerHeaderHeight,
},
fullHeight: {
height: "100%",
height: "80%",
overflowY: "auto",
},
exitButton: {
Expand Down
97 changes: 97 additions & 0 deletions src/components/namestable/use-worker-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* 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 * as _ from "lodash";
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { ShiftCode } from "../../common-models/shift-info.model";
import { ContractType, WorkersInfoModel, WorkerType } from "../../common-models/worker-info.model";
import { ApplicationStateModel } from "../../state/models/application-state.model";
import { WorkerInfoExtendedInterface } from "./worker-edit";

interface UseWorkerInfoReturn {
workerInfo: WorkerInfo;
setWorkerInfo: (workerInfo: WorkerInfo) => void;
}
export function useWorkerInfo(workerName: string): UseWorkerInfoReturn {
const [workerInfo, setWorkerInfo] = useState<WorkerInfo>(new WorkerInfo());
const getWorkerInfo = <T>(
state: ApplicationStateModel,
key: keyof WorkersInfoModel
): T | undefined =>
state.actualState.persistentSchedule.present.employee_info[key]?.[workerName] as T | undefined;
const workerTime = useSelector((state: ApplicationStateModel) =>
getWorkerInfo<number>(state, "time")
);
const workerType = useSelector((state: ApplicationStateModel) =>
getWorkerInfo<WorkerType>(state, "type")
);
const workerContractType = useSelector((state: ApplicationStateModel) =>
getWorkerInfo<ContractType>(state, "contractType")
);
const workerShifts = useSelector(
(state: ApplicationStateModel) =>
state.actualState.persistentSchedule.present.shifts[workerName]
);
useEffect(() => {
const newWorkerInfo = new WorkerInfo(
workerName,
workerContractType,
workerTime,
workerType,
workerShifts
);
setWorkerInfo(newWorkerInfo);
}, [workerName, workerContractType, workerTime, workerType, workerShifts]);
return {
workerInfo,
setWorkerInfo,
};
}

export class WorkerInfo {
public previousWorkerName: string;
constructor(
public workerName: string = "",
public contractType?: ContractType,
public workerTime: number = 1,
public workerType?: WorkerType,
public workerShifts: ShiftCode[] = []
) {
this.previousWorkerName = workerName;
}

public withNewName(newName: string): WorkerInfo {
const copy = _.cloneDeep(this);
copy.workerName = newName;
return copy;
}

public withNewWorkerTime(newWorkerTime: number): WorkerInfo {
const copy = _.cloneDeep(this);
copy.workerTime = newWorkerTime;
return copy;
}

public withNewContractType(newContractType: ContractType): WorkerInfo {
const copy = _.cloneDeep(this);
copy.contractType = newContractType;
return copy;
}

public withNewWorkerType(newWorkerType: WorkerType): WorkerInfo {
const copy = _.cloneDeep(this);
copy.workerType = newWorkerType;
return copy;
}

asWorkerInfoExtendedInterface(): WorkerInfoExtendedInterface {
return {
prevName: this.previousWorkerName,
workerName: this.workerName,
workerType: this.workerType,
contractType: this.contractType,
time: this.workerTime,
};
}
}
Loading

0 comments on commit 7d8d721

Please sign in to comment.