-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from ti-broish/violation-page
Page for submitted violation
- Loading branch information
Showing
5 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import styled from 'styled-components' | ||
import api from '../utils/api' | ||
import { useParams } from 'react-router-dom' | ||
import { Link } from './components/Link' | ||
import { ROUTES } from './routes' | ||
|
||
export const ViolationSummary = () => { | ||
const { violationId } = useParams() | ||
const [violationDetails, setViolationDetails] = useState({}) | ||
const [error, setError] = useState(null) | ||
const [ownViolation, setOwnViolation] = useState(false) | ||
|
||
useEffect(async () => { | ||
if (!localStorage || !violationId) return | ||
let ignore = false | ||
const violationsFromStorage = | ||
JSON.parse(localStorage.getItem('violations')) || [] | ||
const violationFromStorage = violationsFromStorage.find( | ||
(violation) => violation.id === violationId | ||
) | ||
let violationUrl = `violations/${violationId}` | ||
if (violationFromStorage) { | ||
setOwnViolation(true) | ||
violationUrl = `${violationUrl}?secret=${encodeURIComponent( | ||
violationFromStorage.secret | ||
)}` | ||
} | ||
try { | ||
const data = await api.get(violationUrl) | ||
if (!ignore) { | ||
setViolationDetails(data) | ||
setError(null) | ||
} | ||
} catch (error) { | ||
if (error.response?.status === 403 || error.response?.status === 401) { | ||
setError('Нямате достъп до този сигнал') | ||
} else if (error.response?.status === 404) { | ||
setError('Не е намерен сигнал с този номер') | ||
} else { | ||
setError( | ||
'Изникна неочаквана грешка при зареждането на сигнала. Моля опитайте отново по-късно.' | ||
) | ||
} | ||
} | ||
|
||
return () => { | ||
ignore = true | ||
} | ||
}, [violationId]) | ||
|
||
if (error) { | ||
return <p>{error}</p> | ||
} | ||
|
||
const { | ||
id, | ||
statusLocalized, | ||
description, | ||
publishedText, | ||
pictures, | ||
section, | ||
createdAt, | ||
town, | ||
} = violationDetails | ||
|
||
return ( | ||
<> | ||
{ownViolation && ( | ||
<Link to={ROUTES.submit}> | ||
<small>⟵ обратно</small> | ||
</Link> | ||
)} | ||
<ViolationSummaryStyle> | ||
<div> | ||
<h1 style={{ wordBreak: 'break-word' }}>Сигнал {id}</h1> | ||
{section && ( | ||
<p> | ||
Секция: <Link to={`/${section.id}`}>{section.id}</Link> | ||
</p> | ||
)} | ||
{town && ( | ||
<> | ||
<p> | ||
{town.municipality ? ( | ||
<>Община: {town.municipality.name}</> | ||
) : ( | ||
<>Държава: {town.country.name}</> | ||
)} | ||
, Населено място: {town.name} | ||
</p> | ||
{(section?.electionRegion || | ||
town.municipality?.electionRegions?.length === 1) && ( | ||
<p> | ||
МИР:{' '} | ||
<Link | ||
to={`/${ | ||
section?.electionRegion?.code || | ||
town.municipality?.electionRegions[0].code | ||
}`} | ||
> | ||
{section?.electionRegion?.name || | ||
town.municipality?.electionRegions[0].name} | ||
</Link> | ||
</p> | ||
)} | ||
</> | ||
)} | ||
<p>Статус: {statusLocalized}</p> | ||
<p>Получен на: {new Date(createdAt).toLocaleString('bg-BG')}</p> | ||
<p> | ||
<b>Нарушение:</b> | ||
<br /> <br /> {description || publishedText} | ||
</p> | ||
</div> | ||
<div> | ||
{pictures?.map((picture, index) => ( | ||
<img src={picture.url} alt="" key={index} /> | ||
))} | ||
</div> | ||
</ViolationSummaryStyle> | ||
</> | ||
) | ||
} | ||
|
||
const ViolationSummaryStyle = styled.div` | ||
padding: 10px; | ||
img { | ||
max-width: 100%; | ||
} | ||
` |