Skip to content

Commit cc5f862

Browse files
committed
enhancement of the documentation, added comments
1 parent c6d3db3 commit cc5f862

File tree

7 files changed

+103
-19
lines changed

7 files changed

+103
-19
lines changed

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
# Introduction
22

3-
An example about how works the modal.
4-
![](./assets/global_architecture.png)
3+
This project is a web application based on react flow (A highly customizable React component for building node-based editors and interactive diagrams).
4+
5+
This project is using Next.js, React.js, Typescript, Tremor and Tailwind.
6+
7+
Despite it is not recommended for clean code, I added few comments in the code to improve the comprenhension for beginners.
58

69
## Getting Started
710

8-
First, run the development server:
11+
First, install the dependencies with
12+
13+
```bash
14+
npm install
15+
# or
16+
yarn install
17+
# or
18+
pnpm install
19+
```
920

1021
```bash
1122
npm run dev
1223
# or
1324
yarn dev
1425
# or
15-
pnpm dev
26+
pnpm run dev
1627
```
1728

1829
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
1930

20-
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
31+
## Utilisation
32+
33+
In the home page, you can add your own _JSON_ file or start with an exemple and modify it after. (Excel isn't available for the moment).
34+
You can also download the file in two formats : _JSON_ or _Excel (xlsx)_.
35+
You can see at top right a boutton to see all keyboard shortcuts.
36+
37+
---
2138

22-
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
39+
You can add different types of nodes by clicking on "add node".
40+
If you click on the black circle on each group/node bottom you can create edges between them.
2341

24-
## Learn More
42+
## Learn more
43+
44+
An example about how works the modal.
45+
![](./assets/global_architecture.png)
2546

2647
To learn more about Next.js, take a look at the following resources:
2748

@@ -30,8 +51,12 @@ To learn more about Next.js, take a look at the following resources:
3051

3152
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
3253

33-
## Deploy on Vercel
54+
### React Flow
3455

35-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
56+
If you're interested in learning more about React Flow, here are some resources to get you started:
3657

37-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
58+
- [React Flow Documentation](https://reactflow.dev/docs/getting-started/introduction) - the official documentation for React Flow.
59+
- [React Flow Tutorial](https://www.smashingmagazine.com/2020/09/node-based-editor-react-flow/) - a tutorial on building a node-based editor with React Flow.
60+
- [React Flow Examples](https://github.com/wbkd/react-flow/tree/main/examples) - a collection of examples demonstrating different use cases for React Flow.
61+
- [React Flow Playground](https://reactflow.dev/examples/playground/) - an interactive playground where you can experiment with React Flow.
62+
- [React Flow GitHub Repository](https://github.com/wbkd/react-flow) - the GitHub repository for React Flow, where you can find the source code and contribute to the project.

src/app/api/flow/route.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { NextApiRequest, NextApiResponse } from "next";
2-
import { redirect } from "next/navigation";
31
import controlJsonFile from "@/utils/controlJsonFile";
42
import { NextResponse } from "next/server";
53

src/app/components/ReactFlowContainer.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ExcelConvertedJson } from "@/app/types/interface";
2222

2323
import "reactflow/dist/style.css";
2424
import { ToastContainer } from "react-toastify";
25-
import { Button, Select, SelectItem } from "@tremor/react";
25+
import { Button, Select, SelectItem, Title } from "@tremor/react";
2626

2727
type NodeType = "Group" | "input" | "output" | "default" | "resizeRotate";
2828

@@ -64,8 +64,12 @@ const ReactFlowContainer = () => {
6464
}
6565
}, [nodes]);
6666

67+
/**
68+
* The above code snippet is a TypeScript React component that uses the useEffect hook to update the
69+
* nodes and edges state variables based on changes in the jsonData and jsonFormattedData variables.
70+
* It also defines functions for opening and closing a modal, and for handling node and edge changes.
71+
*/
6772
useEffect(() => {
68-
console.log("geg");
6973
if (jsonData.nodes.length === 0) return;
7074
const formattedData = getReactFlowFromJson(jsonData);
7175
if (!formattedData) return;
@@ -166,7 +170,7 @@ const ReactFlowContainer = () => {
166170
<MiniMap />
167171
{/*// @ts-ignore*/}
168172
<Background variant="lines" gap={12} size={1} />
169-
<Panel position="top-left">
173+
<Panel position="top-left" className="flex items-center justify-center">
170174
<Select
171175
placeholder="Add node"
172176
onValueChange={(value) => onAdd(value as NodeType)}
@@ -176,6 +180,11 @@ const ReactFlowContainer = () => {
176180
<SelectItem value="output">Output Node</SelectItem>
177181
<SelectItem value="Group">Group</SelectItem>
178182
</Select>
183+
{nodes.length === 0 && (
184+
<Title color="red" className="w-[300px] pl-3">
185+
Please add data
186+
</Title>
187+
)}
179188
</Panel>
180189
</ReactFlow>
181190
</div>

src/app/components/download/DownloadFlow.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { DownloadFlowProps } from "@/app/types/interface";
66
import * as XLSX from "xlsx";
77
import { Node, Edge } from "reactflow";
88

9+
/**
10+
* The `downloadExcel` function takes an array of nodes and edges, converts them into worksheets, and
11+
* then saves them as an Excel file.
12+
* @param {Node[]} nodes - The `nodes` parameter is an array of objects representing nodes in a graph.
13+
* Each node object has the following properties:
14+
* @param {Edge[]} edges - The `edges` parameter is an array of objects representing the connections
15+
* between nodes. Each object in the array should have properties such as `source`, `target`, and any
16+
* other relevant information about the edge.
17+
*/
918
const downloadExcel = (nodes: Node[], edges: Edge[]) => {
1019
const flattenedNodes = nodes.map((node) => {
1120
return {
@@ -31,6 +40,12 @@ const downloadExcel = (nodes: Node[], edges: Edge[]) => {
3140
XLSX.writeFile(workbook, "data.xlsx");
3241
};
3342

43+
/**
44+
* The `DownloadFlow` function is a React component that handles the download of data in either JSON or
45+
* Excel format.
46+
* @param {"JSON" | "Excel"} fileType - The `fileType` parameter is a string that can have two possible
47+
* values: "JSON" or "Excel". It is used to determine the type of file to be downloaded.
48+
*/
3449
const DownloadFlow: React.FC<DownloadFlowProps> = ({ nodes, edges }) => {
3550
const handleDownload = (fileType: "JSON" | "Excel") => {
3651
if (fileType === "Excel") {

src/app/components/uploadFile/UploadJson.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ export const UploadJson: React.FC<UploadJsonFileProps> = ({
1414
}) => {
1515
const [isValidJsonUpload, setIsValidJsonUpload] = useState<boolean>(false);
1616
const [unverifiedJson, setUnverifiedJson] = useState<{}>({});
17+
1718
const fileInputRef = useRef<HTMLInputElement>(null);
19+
20+
/**
21+
* The function handles the change event when a JSON file is selected, reads the file, parses the
22+
* JSON data, and sets the parsed JSON as state.
23+
* @param {any} event - The event parameter is an object that represents the event that triggered the
24+
* file change. It contains information about the selected file, such as its name, size, and type.
25+
*/
1826
const handleJsonFileChange = (event: any) => {
1927
const jsonFile = event.target.files[0];
2028

@@ -38,6 +46,10 @@ export const UploadJson: React.FC<UploadJsonFileProps> = ({
3846
}
3947
};
4048

49+
/**
50+
* The function handles the upload of a JSON file, verifies its structure, sets the formatted data,
51+
* and displays success or error messages.
52+
*/
4153
const handleJsonFileUpload = () => {
4254
try {
4355
controlJsonFile(unverifiedJson);

src/utils/controlJsonFile.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const optionalEdgesFields = [
4848
"updatable",
4949
];
5050

51+
/**
52+
* The function `controlJsonFile` checks if a given JSON file has the required keys and valid structure
53+
* for nodes and edges.
54+
* @param {any} json - The `json` parameter is an object that represents a JSON file. It is expected to
55+
* have a `nodes` property and an `edges` property, both of which should be arrays. Each element in the
56+
* `nodes` and `edges` arrays should be an object with at least three properties.
57+
*/
5158
const controlJsonFile = (json: any): void => {
5259
if (!json.nodes || !json.edges) {
5360
throw new Error("Your file should have a nodes and edges key");

src/utils/jsonToFlow.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Take a JSON file resulted from a Excel conversion
2+
13
import { Edge, Node } from "reactflow";
24
import {
35
ColorType,
@@ -7,11 +9,27 @@ import {
79
} from "@/app/types/interface";
810
import { toast } from "react-toastify";
911

12+
/*
13+
* * This code below are not used in the application because layouting is to complex to handle,
14+
* * the best way to do that are either use JSON file or Excel file with specifiq and required fields such as
15+
* * nodes positions and sizes.
16+
*/
17+
1018
//TODO : Create a function to adapt the group position depending of the group number and the number of nodes in the group
1119
//TODO : Create a function to adapt the node position in the group depending of the group number and the number of nodes in the group
1220

21+
/**
22+
* The function `getReactFlowFromJson` takes in JSON data and returns formatted nodes and edges for
23+
* ReactFlow, or undefined if there is an error.
24+
* @param {ExcelConvertedJson} jsonData - The `jsonData` parameter is of type `ExcelConvertedJson`. It
25+
* is an object that contains the data extracted from an Excel file and converted to JSON format. The
26+
* `jsonData` object has two properties: `nodes` and `edges`.
27+
* @returns an object with two properties: "formattedNodes" and "formattedEdges". The value of
28+
* "formattedNodes" is an array of Node objects, and the value of "formattedEdges" is an array of Edge
29+
* objects.
30+
*/
1331
export const getReactFlowFromJson = (
14-
jsonData: ExcelConvertedJson,
32+
jsonData: ExcelConvertedJson
1533
):
1634
| {
1735
formattedNodes: Node[];
@@ -35,7 +53,7 @@ export const getReactFlowFromJson = (
3553

3654
export const createNodesData = (
3755
jsonDataNode: ExcelConvertedJsonNode[],
38-
edgesData: Edge[],
56+
edgesData: Edge[]
3957
) => {
4058
const formattedNodes: Node[] = [];
4159
const numberNodeByGroup = getNumberNodeByGroup(jsonDataNode);
@@ -184,7 +202,7 @@ interface SizeByGroup {
184202

185203
export const setSizeByGroup = (
186204
numberNodeByGroup: NumberNodeByGroup,
187-
edgesData: Edge[],
205+
edgesData: Edge[]
188206
): SizeByGroup => {
189207
const numberOfNodesPerRow = 3;
190208

@@ -238,7 +256,7 @@ interface GroupTree {
238256

239257
const getGroupPosition = (
240258
sizeAndNodesDataByGroup: SizeByGroup,
241-
edgeData: Edge[],
259+
edgeData: Edge[]
242260
) => {
243261
//
244262
};

0 commit comments

Comments
 (0)