Skip to content

Commit

Permalink
add spliter resizer to controlpanel and mapcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jantero93 committed Jun 9, 2024
1 parent fcff759 commit 59bbb28
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 12 deletions.
6 changes: 6 additions & 0 deletions react-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions react-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"react-leaflet": "^4.2.1",
"react-redux": "^9.1.2",
"react-router-dom": "^6.23.0",
"split.js": "^1.6.5",
"typeface-roboto": "^1.1.13"
},
"devDependencies": {
Expand Down
12 changes: 9 additions & 3 deletions react-client/src/store/slices/uiMapSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { ReverseGeocodingRes } from "@/services/GeocodingService";
import { mapObjectPropertiesUndefined } from "@/utilities/commonHelpers";
import { RootState } from "@/store/store";

type UiSelectedMapLocation = Partial<ReverseGeocodingRes>;
type UiSelectedMapLocation = Partial<ReverseGeocodingRes> & {
shouldInvalidateSize: boolean;
};

const initialState: UiSelectedMapLocation = {};
const initialState: UiSelectedMapLocation = { shouldInvalidateSize: true };

export const uiMapSlice = createSlice({
name: "ui-map",
Expand Down Expand Up @@ -34,10 +36,14 @@ export const uiMapSlice = createSlice({
const undefinedState = mapObjectPropertiesUndefined(state);
Object.assign(state, undefinedState);
},
invalidateMapSize: (state) => {
state.shouldInvalidateSize = !state.shouldInvalidateSize;
},
},
});

export const { setLocation, clearLocation } = uiMapSlice.actions;
export const { setLocation, clearLocation, invalidateMapSize } =
uiMapSlice.actions;

// Define the typed selector function
export const selectValidMapLocation = (state: RootState) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ControlPanel = () => {
);

return (
<PaddedStack spacing={1}>
<PaddedStack spacing={2}>
<SelectControlPanelDropdown />
{ComponentMap[selectedControlViewComponent]}
</PaddedStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const AddCompany = () => {

const mapLocation = useAppSelector(selectValidMapLocation);
const controllerComponent = useAppSelector(selectedControllerComponent);
const isLoggedIn = useAppSelector((s) => s.auth.isLoggedIn);

// Clear location selection when component is changed
useEffect(() => {
Expand All @@ -52,6 +53,10 @@ const AddCompany = () => {
};
}, [dispatch, controllerComponent]);

if (!isLoggedIn) {
return <Typography>Log in to select place for adding company</Typography>;
}

if (mapLocation === null) {
return <Typography>Select position from map</Typography>;
}
Expand Down
33 changes: 28 additions & 5 deletions react-client/src/views/mapView/MapPage.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import { useEffect, useRef } from "react";
import Split from "split.js";
import { styled } from "@mui/material";
import { useAppDispatch } from "@/hooks/useStoreHooks";
import ControlPanel from "@/views/mapView/ControlPanelItems/ControlPanel";
import LeafletMap from "@/views/mapView/mapComponents/LeafletMap";
import "@/views/mapView/gutter.css";
import { invalidateMapSize } from "@/store/slices/uiMapSlice";

const PageSection = styled("section")({
flex: 1,
display: "flex",
flexDirection: "column",
height: "100%",
overflow: "hidden",
});

const ViewContainer = styled("div")({
display: "flex",
flexDirection: "row",
width: "100%",
height: "100%",
flex: 1,
flexGrow: 1,
height: "calc(100vh - 130px)", // Adjust this based on your footer height
});

const MapPage = () => {
const dispatch = useAppDispatch();
const leftRef = useRef<HTMLDivElement>(null);
const rightRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (leftRef.current && rightRef.current) {
Split([leftRef.current, rightRef.current], {
sizes: [50, 50],
minSize: [200, 400],
gutterSize: 5,
cursor: "col-resize",
onDrag: () => dispatch(invalidateMapSize()),
onDragEnd: () => dispatch(invalidateMapSize()),
});
}
}, [dispatch]);

return (
<ViewContainer>
<PageSection>
<PageSection id="split-col-left" ref={leftRef}>
<ControlPanel />
</PageSection>
<PageSection>
<PageSection id="split-col-right" ref={rightRef}>
<LeafletMap />
</PageSection>
</ViewContainer>
Expand Down
14 changes: 14 additions & 0 deletions react-client/src/views/mapView/gutter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.gutter {
background-color: #505050;
background-repeat: no-repeat;
background-position: 70%;
}

.gutter.gutter-vertical {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFAQMAAABo7865AAAABlBMVEVHcEzMzMzyAv2sAAAAAXRSTlMAQObYZgAAABBJREFUeF5jOAMEEAIEEFwAn3kMwcB6I2AAAAAASUVORK5CYII=");
cursor: row-resize;
}
.gutter.gutter-horizontal {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==");
cursor: col-resize;
}
22 changes: 19 additions & 3 deletions react-client/src/views/mapView/mapComponents/LeafletMap.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useEffect } from "react";
import {
MapContainer,
Marker,
Popup,
TileLayer,
useMap,
useMapEvents,
} from "react-leaflet";
import L from "leaflet";
Expand All @@ -28,6 +30,9 @@ L.Icon.Default.mergeOptions({
const LeafletMap = () => {
const dispatch = useAppDispatch();
const controllerComponent = useAppSelector(selectedControllerComponent);
const shouldInvalidateMapSize = useAppSelector(
(s) => s.uiMap.shouldInvalidateSize
);

const handleMapClick = async (lat: number, lng: number) => {
if (controllerComponent === "AddCompany") {
Expand All @@ -51,15 +56,26 @@ const LeafletMap = () => {
return null;
};

const ResizeHandler = () => {
const map = useMap();

useEffect(() => {
shouldInvalidateMapSize && map.invalidateSize();
}, [map]);

return null;
};

return (
<MapContainer
center={[51.505, -0.09]}
zoom={13}
center={[61.4898, 23.7734]}
zoom={14}
style={{ height: "100%", width: "100%" }}
>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<MapEventHandlers />
<Marker position={[51.505, -0.09]}>
<ResizeHandler />
<Marker position={[61.4898, 23.7734]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
Expand Down

1 comment on commit 59bbb28

@Jantero93
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.