Skip to content

Commit

Permalink
Merge pull request #127 from ti-broish/violation-page
Browse files Browse the repository at this point in the history
Page for submitted violation
  • Loading branch information
hkdobrev committed Apr 2, 2023
2 parents 9478659 + 5f41df6 commit aacd0a5
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/components/Election.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RightsAndObligations } from './RightsAndObligations'
import { ROUTES } from './routes.js'
import { Submit } from './Submit'
import { ViolationForm } from './ViolationForm'
import { ViolationSummary } from './ViolationSummary'

export const ElectionContext = React.createContext()

Expand All @@ -36,10 +37,11 @@ export default () => {
<Switch>
<Route path={ROUTES.submit} component={Submit} />
<Route path={ROUTES.protocolForm} component={ProtocolForm} />
<Route path={ROUTES.violationForm} component={ViolationForm} />
<Route path={ROUTES.myProtocols} exact component={MyProtocols} />
<Route path={ROUTES.myViolations} exact component={MyViolations} />
<Route path={ROUTES.protocol} component={ProtocolSummary} />
<Route path={ROUTES.violationForm} component={ViolationForm} />
<Route path={ROUTES.violation} component={ViolationSummary} />
<Route
path={ROUTES.rightsAndObligations}
component={RightsAndObligations}
Expand Down
4 changes: 3 additions & 1 deletion src/components/MyViolations.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const MyViolations = () => {
<img src={violation.pictures[0]?.url} alt="" height="200px" />
</RouterLink>
)}
<LinkButton to={`/violations/${violation.id}`}>
<LinkButton
to={ROUTES.violation.replace(':violationId', violation.id)}
>
Вижте сигнала
</LinkButton>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/components/ProtocolSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ export const ProtocolSummary = () => {
<ProtocolSummaryStyle>
<div>
<h1 style={{ wordBreak: 'break-word' }}>Протокол {id}</h1>
{section && <p>Секция: {section.id}</p>}
{section && (
<p>
Секция: <Link to={`/${section.id}`}>{section.id}</Link>
</p>
)}
<p>Статус: {statusLocalized}</p>
<p>Получен на: {new Date(createdAt).toLocaleString('bg-BG')}</p>
</div>
Expand Down
17 changes: 13 additions & 4 deletions src/components/ViolationFeeds.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useEffect, useState } from 'react'

import axios from 'axios'
import { useParams } from 'react-router-dom'
import { useParams, useHistory } from 'react-router-dom'

import { Link } from 'react-router-dom'
import { ElectionContext } from './Election'
Expand All @@ -26,6 +26,7 @@ const Violation = styled.div`
box-shadow: 0 0 10px #ddd;
border-bottom: 3px solid #bbb;
color: #333;
cursor: pointer;
h4,
p {
Expand Down Expand Up @@ -69,6 +70,8 @@ const defaultRegionName = 'Последни сигнали'

export default (props) => {
const { meta, parties, dataURL } = useContext(ElectionContext)
const history = useHistory()
const navigateTo = (path) => history.push(path)

const [violationData, setViolationData] = useState({
items: null,
Expand Down Expand Up @@ -136,7 +139,7 @@ export default (props) => {

return (
<Fade key={violation.id} clear>
<Violation>
<Violation onClick={() => navigateTo(`/violation/${violation.id}`)}>
<h4>
{violation.town.name}
{violation.town.municipality
Expand All @@ -150,7 +153,10 @@ export default (props) => {
{!electionRegion ? (
''
) : (
<Link to={`/${electionRegion.code}`}>
<Link
to={`/${electionRegion.code}`}
onClick={(e) => e.stopPropagation()}
>
МИР {electionRegion.code}. {electionRegion.name}
</Link>
)}
Expand All @@ -160,7 +166,10 @@ export default (props) => {
) : (
<>
Секция{' '}
<Link to={`/${violation.section.id}`}>
<Link
to={`/${violation.section.id}`}
onClick={(e) => e.stopPropagation()}
>
{violation.section.id}
</Link>
</>
Expand Down
132 changes: 132 additions & 0 deletions src/components/ViolationSummary.js
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%;
}
`

0 comments on commit aacd0a5

Please sign in to comment.