Skip to content

Commit 4984269

Browse files
committed
takaisin-nappula documentpagelle
1 parent 808a75f commit 4984269

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed

frontend/src/components/DocumentPage.tsx

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios'
22
import type { Headings, DocumentPageProps } from "../types"
3-
import { useState} from 'react'
3+
import { useEffect, useState } from 'react'
44
import TableOfContent from './TableOfContent'
55
import { useParams } from 'react-router-dom'
66
import {Helmet} from "react-helmet";
@@ -17,26 +17,23 @@ const DocumentPage = ({language, apipath} : DocumentPageProps) => {
1717
const [law, setLaw] = useState<string>('')
1818
const [headings, setHeadings] = useState<Headings[]>([])
1919
const [lan, setLan] = useState<string>(language)
20-
let path = `/api/${apipath}/id/${docyear}/${docnumber}/${lan}`
21-
let headerpath = `/api/${apipath}/structure/id/${docyear}/${docnumber}/${lan}/`
22-
23-
if (apipath !== "statute") {
24-
path = `/api/${apipath}/id/${docyear}/${docnumber}/${lan}/${doclevel}`
25-
}
20+
const [backButtonHovered, setBackButtonHovered] = useState<boolean>(false)
2621

2722
const topStyle: React.CSSProperties = {
2823
display: 'flex',
2924
position: 'fixed',
3025
top: '0px',
3126
left: '0px',
32-
justifyContent: 'center',
33-
alignContent: 'center',
27+
alignItems: 'center',
28+
justifyContent: 'space-between',
3429
width: '100%',
3530
height: '50px',
3631
backgroundColor: '#0C6FC0',
3732
padding: '0px',
3833
margin: '0px',
39-
border: '0px solid red'
34+
border: '0px solid red',
35+
paddingLeft: '20px',
36+
paddingRight: '20px'
4037
}
4138

4239
const contentStyle: React.CSSProperties = {
@@ -72,13 +69,40 @@ const DocumentPage = ({language, apipath} : DocumentPageProps) => {
7269
border: '0px solid pink'
7370
}
7471

75-
if (docnumber === "") {
76-
throw new Error("Unexpected error: Missing docnumber")
72+
const backButtonStyle: React.CSSProperties = {
73+
color: '#fefefe',
74+
textDecoration: 'none',
75+
border: 'none',
76+
padding: '6px 12px',
77+
borderRadius: '4px',
78+
cursor: 'pointer',
79+
fontSize: '14px',
80+
backgroundColor: backButtonHovered ? 'rgba(255, 255, 255, 0.2)' : 'transparent',
81+
transition: 'background-color 0.2s ease'
82+
}
83+
84+
const getBackUrl = () => {
85+
if (apipath === 'statute') {
86+
return '/lainsaadanto/'
87+
} else {
88+
return '/oikeuskaytanto/'
89+
}
7790
}
78-
if (docyear === "") {
79-
throw new Error("Unexpected error: Missing docyear")
91+
92+
const buildPaths = (currentLanguage: string) => {
93+
let docPath = `/api/${apipath}/id/${docyear}/${docnumber}/${currentLanguage}`
94+
if (apipath !== 'statute') {
95+
docPath = `${docPath}/${doclevel}`
96+
}
97+
let structurePath = `/api/${apipath}/structure/id/${docyear}/${docnumber}/${currentLanguage}`
98+
if (apipath !== 'statute') {
99+
structurePath = `${structurePath}/${doclevel}`
100+
}
101+
return { docPath, structurePath }
80102
}
81103

104+
const hasRequiredParams = docnumber !== "" && docyear !== ""
105+
82106
const getHtml = async (path: string) => {
83107

84108
try {
@@ -127,44 +151,30 @@ const DocumentPage = ({language, apipath} : DocumentPageProps) => {
127151
const currentValue = (event.target as HTMLInputElement).value
128152
setLan(currentValue)
129153
localStorage.setItem("language", currentValue)
130-
131-
if(apipath === "statute") {
132-
path = `/api/${apipath}/id/${docyear}/${docnumber}/${currentValue}`
133-
} else {
134-
path = `/api/${apipath}/id/${docyear}/${docnumber}/${currentValue}/${doclevel}`
135-
}
136-
137-
headerpath = `/api/${apipath}/structure/id/${docyear}/${docnumber}/${currentValue}/`
138-
updateHTML()
139-
140154
}
141155

142-
const updateHTML = () => {
143-
if(apipath === "statute") {
144-
getLawHtml(path)
145-
} else {
146-
getHtml(path)
147-
}
148-
getHeadings()
149-
}
150-
151-
const getHeadings = async () => {
152-
156+
const getHeadings = async (structurePath: string) => {
153157
try {
154-
const response = await axios.get(`${headerpath}${doclevel ? doclevel : ''}`)
158+
const response = await axios.get(structurePath)
155159
setHeadings(response.data)
156160
} catch(error) {
157161
console.error(error)
158162
}
159163
}
160164

161-
if (law === '') {
162-
updateHTML()
163-
}
165+
useEffect(() => {
166+
if (!hasRequiredParams) {
167+
return
168+
}
164169

165-
if(headings.length < 1 && law.length < 1) {
166-
getHeadings()
167-
}
170+
const { docPath, structurePath } = buildPaths(lan)
171+
if (apipath === "statute") {
172+
getLawHtml(docPath)
173+
} else {
174+
getHtml(docPath)
175+
}
176+
getHeadings(structurePath)
177+
}, [apipath, docnumber, docyear, doclevel, lan, hasRequiredParams])
168178

169179
return (
170180
<>
@@ -174,18 +184,33 @@ const DocumentPage = ({language, apipath} : DocumentPageProps) => {
174184
</title>
175185
</Helmet>
176186
<div id="topId" style={topStyle}>
177-
178-
<TopMenu language={lan} handleSelect={handleSelect} />
187+
<button
188+
onClick={() => window.location.href = getBackUrl()}
189+
style={backButtonStyle}
190+
onMouseEnter={() => setBackButtonHovered(true)}
191+
onMouseLeave={() => setBackButtonHovered(false)}
192+
>
193+
{language === 'fin' ? 'Takaisin' : 'Tillbaka'}
194+
</button>
195+
<div style={{flex: 1, display: 'flex', justifyContent: 'center'}}>
196+
<TopMenu language={lan} handleSelect={handleSelect} />
197+
</div>
179198
</div>
180199
<div id="contentDiv" style={contentStyle}>
181200
<div id="contentBlock" style={contentBlockStyle}>
182-
<div id="leftMargin" style={tocStyle}>
183-
184-
<TableOfContent headings={headings} />
185-
186-
</div>
187-
188-
<div id="documentbodydiv" style={docBodyStyle} dangerouslySetInnerHTML={{ __html: law}}></div>
201+
{hasRequiredParams ? (
202+
<>
203+
<div id="leftMargin" style={tocStyle}>
204+
<TableOfContent headings={headings} />
205+
</div>
206+
207+
<div id="documentbodydiv" style={docBodyStyle} dangerouslySetInnerHTML={{ __html: law}}></div>
208+
</>
209+
) : (
210+
<div style={{ padding: '20px', color: '#666' }}>
211+
{language === 'fin' ? 'Asiakirjaa ei voitu ladata.' : 'Dokumentet kunde inte laddas.'}
212+
</div>
213+
)}
189214
</div>
190215
</div>
191216
</>

0 commit comments

Comments
 (0)