Skip to content

Commit ebbd61d

Browse files
committed
Feat: added data sync from props
1 parent c5c1ca4 commit ebbd61d

File tree

23 files changed

+282
-62
lines changed

23 files changed

+282
-62
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
## Installation
2-
3-
Run `bun i @mezh-hq/react-seat-toolkit` to incorporate into your project <br/>
4-
51
## Getting started
62

73
- Run `bun install` to install all dependencies

README.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ React UI library to design and render seat layouts. The library is still under a
2929
</p>
3030

3131

32-
33-
3432
## Features
3533

3634
- **JSON based**: Define your seat layout using JSON data and retrieve it back as JSON after customization ✓
@@ -82,6 +80,71 @@ React UI library to design and render seat layouts. The library is still under a
8280

8381
- **Override styles**: Override the default styles to match your application's theme 🛠️
8482

83+
## Installation
84+
85+
Run `bun i @mezh-hq/react-seat-toolkit` to incorporate into your project <br/>
86+
87+
## Usage
88+
89+
### User mode
90+
91+
```jsx
92+
import React from 'react';
93+
import SeatToolkit from '@mezh-hq/react-seat-toolkit';
94+
95+
const App = () => {
96+
const data = {
97+
seats: [
98+
{
99+
id: '1',
100+
x: 100,
101+
y: 100,
102+
label: 'A1',
103+
color: 'blue',
104+
},
105+
],
106+
};
107+
return (
108+
<SeatToolkit
109+
mode="user"
110+
data={data}
111+
events={{
112+
onSeatClick: (seat) => {
113+
console.log(seat);
114+
},
115+
onSectionClick: (section) => {
116+
console.log(section);
117+
},
118+
}}
119+
/>
120+
);
121+
};
122+
123+
export default App;
124+
```
125+
126+
### Designer mode
127+
128+
```jsx
129+
import React from 'react';
130+
import SeatToolkit from '@mezh-hq/react-seat-toolkit';
131+
132+
const App = () => {
133+
return (
134+
<SeatToolkit
135+
mode="designer"
136+
events={{
137+
onExport: (data) => {
138+
console.log(data);
139+
},
140+
}}
141+
/>
142+
);
143+
};
144+
145+
export default App;
146+
```
147+
85148
## Contributing
86149

87-
Please read [CONTRIBUTING.md](https://github.com/mezh-hq/react-seat-toolkit/blob/main/CONTRIBUTING.md) for details on setting up your development environment and the process for submitting pull requests to us.
150+
Please read [CONTRIBUTING.md](https://github.com/mezh-hq/react-seat-toolkit/blob/main/CONTRIBUTING.md) for details on setting up your development environment and the process of submitting pull requests to us.

src/components/core/animated-switcher.jsx renamed to src/components/core/animated-switcher.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import { motion } from "framer-motion";
22
import { twMerge } from "tailwind-merge";
33

4-
const AnimatedSwitcher = ({ customKey, component, alternateComponent, show, className, duration }) => {
4+
interface AnimatedSwitcherProps {
5+
customKey?: string;
6+
component: React.ReactNode;
7+
alternateComponent?: React.ReactNode;
8+
show: boolean;
9+
className?: string;
10+
duration?: number;
11+
}
12+
13+
const AnimatedSwitcher = ({
14+
customKey,
15+
component,
16+
alternateComponent,
17+
show,
18+
className,
19+
duration
20+
}: AnimatedSwitcherProps) => {
521
return (
622
<motion.div
723
key={customKey ?? (show ? "component" : "alternateComponent")}

src/components/footer.jsx renamed to src/components/footer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AnimatedSwitcher } from "./core";
33
import { tools } from "./toolbar/data";
44

55
const Footer = () => {
6-
const selectedTool = useSelector((state) => state.toolbar.selectedTool);
6+
const selectedTool = useSelector((state: any) => state.toolbar.selectedTool);
77
return (
88
<div className="w-full fixed bottom-0 h-8 flex justify-center items-center bg-black text-white">
99
<span className="text-sm">React Seat Toolkit </span>
@@ -12,6 +12,7 @@ const Footer = () => {
1212
customKey={selectedTool}
1313
className="absolute top-[0.4rem] left-5 text-xs"
1414
component={<span>{tools[selectedTool]?.description}</span>}
15+
alternateComponent={null}
1516
duration={0.2}
1617
/>
1718
</div>

src/components/index.jsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/components/index.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { STKMode } from "@/constants";
2+
import { useEvents, useInteractions } from "@/hooks";
3+
import type { ISTKProps } from "@/types";
4+
import { default as Controls } from "./controls";
5+
import { default as Footer } from "./footer";
6+
import { default as Operations } from "./operations";
7+
import { default as Toolbar } from "./toolbar";
8+
import { Cursor, default as Workspace } from "./workspace";
9+
10+
export * from "./core";
11+
12+
const Designer: React.FC<ISTKProps> = (props) => {
13+
useEvents();
14+
useInteractions();
15+
return (
16+
<>
17+
<div className="h-full flex flex-col">
18+
<Operations />
19+
<div className="h-full flex relative">
20+
<Toolbar />
21+
<Workspace {...props} />
22+
<Controls />
23+
</div>
24+
</div>
25+
<Footer />
26+
<Cursor />
27+
</>
28+
);
29+
};
30+
31+
const User: React.FC<ISTKProps> = (props) => {
32+
return (
33+
<div className="h-full flex flex-col relative">
34+
<Workspace {...props} />
35+
</div>
36+
);
37+
};
38+
39+
const Core = (props: ISTKProps) => {
40+
if (props.mode === STKMode.Designer) {
41+
return <Designer {...props} />;
42+
}
43+
return <User {...props} />;
44+
};
45+
46+
export default Core;

src/components/workspace/crosshairs.jsx renamed to src/components/workspace/crosshairs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const Crosshairs = ({ render }) => {
77
const [y, setY] = useState(0);
88
const [enabled, setEnabled] = useState(false);
99

10-
const move = (e) => {
10+
const move = (e: Event) => {
1111
const pointer = d3.pointer(e);
1212
setX(pointer[0]);
1313
setY(pointer[1]);

src/components/workspace/grid.jsx renamed to src/components/workspace/grid.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import GridLines from "react-gridlines";
1+
import { default as GridLines } from "react-gridlines";
22
import { useSelector } from "react-redux";
33
import { AnimatedSwitcher } from "../core";
44

55
const Grid = () => {
6-
const grid = useSelector((state) => state.editor.grid);
6+
const grid = useSelector((state: any) => state.editor.grid);
77
if (!grid) return null;
88
return (
99
<AnimatedSwitcher
1010
show={grid}
1111
component={<GridLines className="w-full h-full opacity-20" cellWidth={44} strokeWidth={2} />}
12-
alternateComponent={null}
1312
className="absolute top-0 left-0 pointer-events-none"
1413
/>
1514
);

src/components/workspace/index.jsx renamed to src/components/workspace/index.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { useCallback, useLayoutEffect } from "react";
22
import { useSelector } from "react-redux";
33
import { ids } from "@/constants";
44
import { store } from "@/store";
5-
import { initializeElements } from "@/store/reducers/editor";
5+
import { initializeElements, sync } from "@/store/reducers/editor";
6+
import type { ISTKProps } from "@/types";
67
import { Tool, tools } from "../toolbar/data";
78
import { default as Crosshairs } from "./crosshairs";
89
import { default as Element, ElementType } from "./elements";
@@ -11,21 +12,25 @@ import { default as Zoom } from "./zoom";
1112

1213
export { default as Cursor } from "./cursor";
1314

14-
export const Workspace = () => {
15-
const booths = useSelector((state) => state.editor.booths);
16-
const seats = useSelector((state) => state.editor.seats);
17-
const text = useSelector((state) => state.editor.text);
18-
const shapes = useSelector((state) => state.editor.shapes);
19-
const polylines = useSelector((state) => state.editor.polylines);
20-
const images = useSelector((state) => state.editor.images);
21-
const categories = useSelector((state) => state.editor.categories);
22-
const selectedElementIds = useSelector((state) => state.editor.selectedElementIds);
23-
const selectedPolylineId = useSelector((state) => state.editor.selectedPolylineId);
24-
const selectedTool = useSelector((state) => state.toolbar.selectedTool);
15+
export const Workspace: React.FC<ISTKProps> = (props) => {
16+
const booths = useSelector((state: any) => state.editor.booths);
17+
const seats = useSelector((state: any) => state.editor.seats);
18+
const text = useSelector((state: any) => state.editor.text);
19+
const shapes = useSelector((state: any) => state.editor.shapes);
20+
const polylines = useSelector((state: any) => state.editor.polylines);
21+
const images = useSelector((state: any) => state.editor.images);
22+
const categories = useSelector((state: any) => state.editor.categories);
23+
const selectedElementIds = useSelector((state: any) => state.editor.selectedElementIds);
24+
const selectedPolylineId = useSelector((state: any) => state.editor.selectedPolylineId);
25+
const selectedTool = useSelector((state: any) => state.toolbar.selectedTool);
2526

2627
useLayoutEffect(() => {
27-
store.dispatch(initializeElements());
28-
}, []);
28+
if (props.data) {
29+
store.dispatch(sync(props.data));
30+
} else {
31+
store.dispatch(initializeElements());
32+
}
33+
}, [props.data]);
2934

3035
const elementProps = useCallback(
3136
(elem) => ({

src/constants/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ export const seatStatusColors = {
4646
label: "#ffffff"
4747
}
4848
};
49+
50+
export enum STKMode {
51+
Designer = "designer",
52+
User = "user"
53+
}

0 commit comments

Comments
 (0)