Skip to content

Commit

Permalink
Merge branch 'Forelesning3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Otro1 committed Jan 24, 2024
2 parents 4a1f29a + cb41b9b commit 08575b4
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 161 deletions.
18 changes: 0 additions & 18 deletions .eslintrc.cjs

This file was deleted.

21 changes: 21 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ jobs:
path: ./dist
- uses: actions/deploy-pages@v4
id: deployment
publish:
runs-on: ubuntu-latest
permissions:
id-token: write
pages: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
cache: "npm"
- run: npm ci
- run: npm run build
- uses: actions/upload-pages-artifact@v3
with:
path: ./dist
- uses: actions/deploy-pages@v4
id: deployment
30 changes: 0 additions & 30 deletions README.md

This file was deleted.

47 changes: 19 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
{
"name": "forelesning2",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"husky": "^8.0.3",
"ol": "^8.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.0.8"
}
"devDependencies": {
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"husky": "^8.0.3",
"prettier": "^3.2.4",
"typescript": "^5.3.3",
"vite": "^5.0.12"
},
"dependencies": {
"ol": "^8.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"scripts": {
"dev": "vite",
"build": "npm run check && vite build",
"check": "prettier --check . && tsc --noEmit",
"prepare": "husky install"
}
}
1 change: 0 additions & 1 deletion public/vite.svg

This file was deleted.

42 changes: 0 additions & 42 deletions src/App.css

This file was deleted.

14 changes: 0 additions & 14 deletions src/App.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/assets/react.svg

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/application/application.css

This file was deleted.

Empty file removed src/index.css
Empty file.
3 changes: 2 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { MapApplication } from "./components/application/mapApplication";
import { MapApplication } from "./modules/application/mapApplication";

const root = ReactDOM.createRoot(document.getElementById("root")!);

Expand Down
20 changes: 20 additions & 0 deletions src/modules/application/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
* {
margin: unset;
}

#root {
display: grid;
grid-template-rows: auto auto 1fr;
height: 100dvh;
}

nav {
display: flex;
gap: 1rem;
}

.kommune-overlay {
background-color: white;
translate: -50% calc(-50% - 30px);
padding: 0.5em;
}
52 changes: 52 additions & 0 deletions src/modules/application/mapApplication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { MutableRefObject, useEffect, useRef, useState } from "react";
import { Map, View } from "ol";
import TileLayer from "ol/layer/Tile";
import { OSM } from "ol/source";
import { useGeographic } from "ol/proj";

import "./application.css";
import "ol/ol.css";
import { KommuneLayerCheckbox } from "../kommune/kommuneLayerCheckbox";
import { Layer } from "ol/layer";

// eslint-disable-next-line react-hooks/rules-of-hooks
useGeographic();

const map = new Map({
view: new View({ center: [10, 59], zoom: 8 }),
});

export function MapApplication() {
function handleFocusUser(e: React.MouseEvent) {
e.preventDefault();
navigator.geolocation.getCurrentPosition((pos) => {
const { latitude, longitude } = pos.coords;
map.getView().animate({
center: [longitude, latitude],
zoom: 10,
});
});
}

const [layers, setLayers] = useState<Layer[]>([
new TileLayer({ source: new OSM() }),
]);

const mapRef = useRef() as MutableRefObject<HTMLDivElement>;
useEffect(() => map.setTarget(mapRef.current), []);
useEffect(() => map.setLayers(layers), [layers]);
return (
<>
<header>
<h1>Kommune kart</h1>
</header>
<nav>
<a href={"#"} onClick={handleFocusUser}>
Focus on me
</a>
<KommuneLayerCheckbox map={map} setLayers={setLayers} />
</nav>
<div ref={mapRef}></div>
</>
);
}
104 changes: 104 additions & 0 deletions src/modules/kommune/kommuneLayerCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
Dispatch,
MutableRefObject,
SetStateAction,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { Layer } from "ol/layer";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import { GeoJSON } from "ol/format";
import { Feature, Map, MapBrowserEvent, Overlay } from "ol";
import { Polygon } from "ol/geom";

type KommuneProperties = {
kommunenummer: string;
navn: {
sprak: string;
navn: string;
}[];
};

type KommuneFeature = Feature<Polygon> & {
getProperties(): KommuneProperties;
};

const kommuneSource = new VectorSource<KommuneFeature>({
url: "/kommuner.json",
format: new GeoJSON(),
});
const kommuneLayer = new VectorLayer({
source: kommuneSource,
});

export function KommuneLayerCheckbox({
map,
setLayers,
}: {
map: Map;
setLayers: Dispatch<SetStateAction<Layer[]>>;
}) {
const [checked, setChecked] = useState(false);
const overlay = useMemo(() => new Overlay({}), []);
const overlayRef = useRef() as MutableRefObject<HTMLDivElement>;
useEffect(() => {
overlay.setElement(overlayRef.current);
map.addOverlay(overlay);
return () => {
map.removeOverlay(overlay);
};
}, []);
const [selectedKommune, setSelectedKommune] = useState<
KommuneFeature | undefined
>();
function handleClick(e: MapBrowserEvent<MouseEvent>) {
const clickedKommune = kommuneSource.getFeaturesAtCoordinate(
e.coordinate
) as KommuneFeature[];
if (clickedKommune.length === 1) {
setSelectedKommune(clickedKommune[0]);
overlay.setPosition(e.coordinate);
} else {
setSelectedKommune(undefined);
overlay.setPosition(undefined);
}
}

useEffect(() => {
if (checked) {
setLayers((old) => [...old, kommuneLayer]);
map.on("click", handleClick);
}
return () => {
map.un("click", handleClick);
setLayers((old) => old.filter((l) => l !== kommuneLayer));
};
}, [checked]);

return (
<div>
<label>
<input
type={"checkbox"}
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>
{checked ? "Hide" : "Show"} kommune layer
</label>
<div ref={overlayRef} className={"kommune-overlay"}>
{selectedKommune && (
<>
{
(
selectedKommune.getProperties() as KommuneProperties
).navn.find((n) => n.sprak === "nor")!.navn
}
</>
)}
</div>
</div>
);
}
1 change: 0 additions & 1 deletion src/vite-env.d.ts

This file was deleted.

Loading

0 comments on commit 08575b4

Please sign in to comment.