Skip to content

Commit 5f7321e

Browse files
committed
feat: implement PERT calculator and data retrieval hook
1 parent 4a68d7d commit 5f7321e

File tree

7 files changed

+451
-7
lines changed

7 files changed

+451
-7
lines changed

lib/components/Pert/Pert.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import type { ReactNode } from "react";
2-
export default (): ReactNode => {
3-
return <div>Pert Chart</div>;
4-
};
1+
import { useEffect } from "react";
2+
import type { PertProps } from "../../types/global.type";
3+
import { usePertContext } from "../../context/pertContext.tsx";
4+
5+
export default function Pert({ tasks }: PertProps) {
6+
const { pertData: pertData, calculatePertResults } = usePertContext();
7+
8+
useEffect(() => {
9+
calculatePertResults(tasks);
10+
}, [tasks]);
11+
12+
useEffect(() => {
13+
console.log("data", pertData);
14+
}, [pertData]);
15+
return <>PertChart</>;
16+
}

lib/context/pertContext.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { createContext, ReactNode, useContext, useState } from "react";
2+
import { PertDataType, TaskType } from "../types/global.type.ts";
3+
import PertCalculator from "../utlis/Pert.ts";
4+
5+
const EMPTY_RESULT: PertDataType = {
6+
tasks: [],
7+
levels: {},
8+
links: [],
9+
criticalPaths: [],
10+
};
11+
12+
type PertContextType = {
13+
pertData: PertDataType;
14+
calculatePertResults: (data: TaskType[]) => void;
15+
};
16+
17+
const PertContext = createContext<PertContextType | null>(null);
18+
19+
export const PertProvider = ({ children }: { children: ReactNode }) => {
20+
const [pertData, setPertResults] = useState<PertDataType>(EMPTY_RESULT as PertDataType);
21+
22+
const calculatePertResults = (data: TaskType[]) => {
23+
const results: PertDataType = new PertCalculator(data).solve();
24+
setPertResults(results);
25+
};
26+
27+
return (
28+
<PertContext.Provider value={{ pertData, calculatePertResults }}>
29+
{children}
30+
</PertContext.Provider>
31+
);
32+
};
33+
34+
export const usePertData = () => {
35+
const context = useContext(PertContext);
36+
if (!context) {
37+
throw new Error("usePertData must be used within a PertProvider");
38+
}
39+
return context.pertData;
40+
};
41+
42+
export const usePertContext = () => {
43+
const context = useContext(PertContext);
44+
if (!context) {
45+
throw new Error("usePertContext must be used within a PertProvider");
46+
}
47+
return context;
48+
};

lib/helpers/generateKey.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const generateKey = () => Math.random().toString(36).substring(7);

lib/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export { default } from "./components/Pert/Pert";
1+
export { default as Pert } from "./components/Pert/Pert";
2+
export * from "./types/global.type";
3+
export { PertProvider, usePertData } from "./context/pertContext";

lib/types/global.type.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export type TaskType = {
2+
key: string;
3+
text: string;
4+
duration: number;
5+
dependsOn?: string[];
6+
};
7+
8+
export type TaskResultType = TaskType & {
9+
earlyStart: number;
10+
earlyFinish: number;
11+
lateStart: number;
12+
lateFinish: number;
13+
level: number;
14+
critical: boolean;
15+
freeFloat: number;
16+
totalFloat: number;
17+
index: number;
18+
};
19+
20+
export type LevelType = {
21+
[key: string]: string[];
22+
};
23+
24+
export type LinkType = {
25+
from: string;
26+
to: string;
27+
critical: boolean;
28+
};
29+
30+
type PathItemType = {
31+
text: string;
32+
key: string;
33+
};
34+
35+
export type CriticalPathType = PathItemType[];
36+
37+
export type PertProps = {
38+
tasks: TaskType[];
39+
};
40+
41+
export type PertDataType = {
42+
tasks: TaskResultType[];
43+
levels: LevelType;
44+
links: LinkType[];
45+
criticalPaths: CriticalPathType[];
46+
};

0 commit comments

Comments
 (0)