Skip to content

Commit

Permalink
Merge pull request #24 from Iteam1337/feat/eppGroups
Browse files Browse the repository at this point in the history
When in EU mode- lets use the groups in the parliament instead
  • Loading branch information
irony authored May 29, 2024
2 parents 1234aeb + cc7292a commit ba84280
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 103 deletions.
2 changes: 1 addition & 1 deletion k8s/deployment-eu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
command:
- /bin/sh
- -c
- 'VITE_PARLIAMENT_SEATS=21 npm run build && npm run start'
- 'VITE_EU=true npm run build && npm run start'
ports:
- containerPort: 80
readinessProbe:
Expand Down
17 changes: 12 additions & 5 deletions src/components/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ body {
}
}


.App {
text-align: center;
}
Expand All @@ -64,7 +63,7 @@ body {
margin: auto;
}

.App-sliders {
.App-sliders span {
align-items: center;
color: #fff;
margin-left: auto;
Expand Down Expand Up @@ -150,7 +149,7 @@ body {
display: flex;
}

.App-sliders {
.App-sliders span {
background: #333;
flex: 1;
display: flex;
Expand Down Expand Up @@ -220,6 +219,14 @@ body {
position: relative;
}

.EU .wide {
display: none;
}

.EU .small {
display: block;
}

.party h1,
.party h2 {
color: #fff;
Expand Down Expand Up @@ -291,7 +298,7 @@ body {
display: block;
}

.App-sliders {
.App-sliders span {
padding: 0;
}
}
Expand Down Expand Up @@ -343,7 +350,7 @@ body {
}

@media only screen and (max-width: 850px) {
.App-sliders {
.App-sliders span {
flex-direction: row;
margin-left: 0;
margin-right: 0;
Expand Down
75 changes: 28 additions & 47 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,44 @@ import Seating from './Seating'
import Sliders from './Sliders'
import Polls from './Polls'
import Footer from './Footer'
const EU = import.meta.env.VITE_EU === 'true'

class App extends Component {
sumGroups(parties) {
const regering = parties
.filter((a) => a.affiliation === 'regering')
.sort((a, b) => b.seats - a.seats)
const opposition = parties
.filter((a) => a.affiliation === 'opposition')
.sort((a, b) => b.seats - a.seats)
const stod = parties
.filter((a) => a.affiliation === 'stod')
.sort((a, b) => b.seats - a.seats)

return [
{
name: 'regering',
parties: regering,
title: `Regering`,
seats: regering.reduce((t, party) => t + party.seats, 0),
percentage:
Math.round(
regering.reduce((t, party) => t + party.seatPercentage, 0) * 1000
) / 10,
},
sumGroups(parties, groups) {
const partiesByGroup = Object.entries(groups).reduce(
(acc, [key, group]) => [
...acc,
{
...group,
name: key,
parties: parties
.filter((a) => (EU ? a.eu === key : a.affiliation === key))
.sort((a, b) => b.seats - a.seats),
},
],
[]
)

{
name: 'stod',
parties: stod,
title: `Stödpartier`,
seats: stod.reduce((t, party) => t + party.seats, 0),
percentage:
Math.round(
stod.reduce((t, party) => t + party.seatPercentage, 0) * 1000
) / 10,
},
{
name: 'opposition',
parties: opposition,
title: `Opposition`,
percentage:
Math.round(
opposition.reduce((t, party) => t + party.seatPercentage, 0) * 1000
) / 10,
seats: opposition.reduce((t, party) => t + party.seats, 0),
},
]
return partiesByGroup.map((group) => ({
...group,
seats: group.parties.reduce((t, party) => t + party.seats, 0),
percentage:
Math.round(
group.parties.reduce((t, party) => t + party.seatPercentage, 0) * 1000
) / 10,
}))
}

render() {
const { parties, coalitions, groups, polls } = this.props
const legendGroups = this.sumGroups(parties)
const allParties = legendGroups.reduce((a, b) => a.concat(b.parties), [])
const legendGroups = this.sumGroups(parties, groups)
const allParties = legendGroups.reduce((a, b) => [...a, ...b.parties], [])
const totalPercentage = Math.round(
parties.reduce((t, party) => t + party.percentage, 0)
)

return (
<div className="App">
<div className={`App ${EU ? 'EU' : 'Riksdag'}`}>
<div className="App-header">
<header className="App-header-inner">
<img alt="Mandatkollen logotyp" src="/images/icon.png" />
Expand Down Expand Up @@ -139,7 +119,8 @@ class App extends Component {
<br />
<h2>Eller experimentera själv %</h2>
<Sliders
parties={parties}
groups={groups}
parties={allParties}
editCoalitions={coalitions.editCoalitions}
/>
{totalPercentage < 99.6 || totalPercentage > 100.4 ? (
Expand Down
1 change: 0 additions & 1 deletion src/components/Seating.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const obj = (parties) =>
)

const Seating = ({ parties, seatCount = true }) => {
console.log('seating', parties, seatCount)
return parliamentSVG(obj(parties), seatCount)
}

Expand Down
48 changes: 14 additions & 34 deletions src/components/Sliders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,21 @@ import pureact from 'pureact'
import Mixer from './Mixer.jsx'
import { dragOver, dropUpdate, dragEnter } from '../lib/draganddrop'

const Sliders = ({ parties, editCoalitions }) => (
const Sliders = ({ groups, parties, editCoalitions }) => (
<div className="App-sliders" id="sliders">
<section
ondragenter={dragEnter}
ondragover={dragOver}
ondrop={dropUpdate('regering')}
>
{parties
.filter((x) => x.affiliation === 'regering')
.map((party) => (
<Mixer party={party} editCoalitions={editCoalitions} />
))}
</section>
<section
ondragenter={dragEnter}
ondragover={dragOver}
ondrop={dropUpdate('stod')}
>
{parties
.filter((x) => x.affiliation === 'stod')
.map((party) => (
<Mixer party={party} editCoalitions={editCoalitions} />
))}
</section>
<section
ondragenter={dragEnter}
ondragover={dragOver}
ondrop={dropUpdate('opposition')}
>
{parties
.filter((x) => x.affiliation === 'opposition')
.map((party) => (
<Mixer party={party} editCoalitions={editCoalitions} />
))}
</section>
{Object.entries(groups).map(([group, { title }]) => (
<section
ondragenter={dragEnter}
ondragover={dragOver}
ondrop={dropUpdate(group)}
>
{parties
.filter((x) => x.affiliation === group || x.eu === group)
.map((party) => (
<Mixer party={party} editCoalitions={editCoalitions} />
))}
</section>
))}
</div>
)

Expand Down
2 changes: 0 additions & 2 deletions src/lib/draganddrop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ const dropUpdate = (affiliation) => (event) => {

const dragEnter = (group) => (event) => {
event.preventDefault()
console.log('dropenter', group)
groupEnter(group)
}

const dragLeave = (group) => (event) => {
event.preventDefault()
console.log('dropleave', group)
groupLeave(group)
}

Expand Down
30 changes: 20 additions & 10 deletions src/reducers/groups.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
const initialState = {
regering: { hover: false, count: 0 },
opposition: { hover: false, count: 0 },
stod: { hover: false, count: 0 },
}
const EU = import.meta.env.VITE_EU === 'true'
const initialState = EU
? {
epp: { title: 'EPP', hover: false, count: 0 },
social: { title: 'S&D', hover: false, count: 0 },
renew: { title: 'Renew', hover: false, count: 0 },
greens: { title: 'Greens/EFA', hover: false, count: 0 },
ecr: { title: 'ECR', hover: false, count: 0 },
left: { title: 'The Left', hover: false, count: 0 },
}
: {
regering: { title: 'Regering', hover: false, count: 0 },
stod: { title: 'Stödpartier', hover: false, count: 0 },
opposition: { title: 'Opposition', hover: false, count: 0 },
}

export default function (state = initialState, action) {
console.log('action', action)
const group = state[action.group]
switch (action.type) {
case 'GROUP_ENTER': {
const count = state[action.group].count + 1
const count = group.count + 1
const newState = {
...state,
[action.group]: { hover: count > 0, count },
[action.group]: { ...group, hover: count > 0, count },
}
return newState
}
case 'GROUP_LEAVE': {
const count = state[action.group].count - 1
const count = group.count - 1
return {
...state,
[action.group]: { hover: count > 0, count },
[action.group]: { ...group, hover: count > 0, count },
}
}
default:
Expand Down
18 changes: 15 additions & 3 deletions src/reducers/parties.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Parliament } from '../lib/parliament.mjs'

const SEATS = parseFloat(import.meta.env.VITE_PARLIAMENT_SEATS || 349)
const EU = import.meta.env.VITE_EU === 'true'
const SEATS = EU ? 21 : 349
const parties = [
{
id: 1,
name: 'Kristdemokraterna',
percentage: 5.34,
seats: 19,
affiliation: 'regering',
eu: 'epp',
colour: '#3163A6',
abbreviation: 'KD',
},
Expand All @@ -17,6 +18,7 @@ const parties = [
percentage: 19.1,
seats: 68,
affiliation: 'regering',
eu: 'ecr',
colour: '#2F80ED',
abbreviation: 'M',
},
Expand All @@ -26,6 +28,7 @@ const parties = [
percentage: 4.61,
seats: 16,
affiliation: 'regering',
eu: 'renew',
colour: '#56CCF2',
abbreviation: 'L',
},
Expand All @@ -35,6 +38,7 @@ const parties = [
percentage: 6.71,
seats: 24,
affiliation: 'opposition',
eu: 'renew',
colour: '#27AE60',
abbreviation: 'C',
},
Expand All @@ -44,6 +48,7 @@ const parties = [
percentage: 20.54,
seats: 73,
affiliation: 'stod',
eu: 'ecr',
colour: '#F2C94C',
abbreviation: 'SD',
},
Expand All @@ -53,6 +58,7 @@ const parties = [
percentage: 30.33,
seats: 107,
affiliation: 'opposition',
eu: 'social',
colour: '#E04B49',
abbreviation: 'S',
},
Expand All @@ -62,6 +68,7 @@ const parties = [
percentage: 5.08,
seats: 18,
affiliation: 'opposition',
eu: 'greens',
colour: '#219653',
abbreviation: 'MP',
},
Expand All @@ -71,6 +78,7 @@ const parties = [
percentage: 6.75,
seats: 24,
affiliation: 'opposition',
eu: 'left',
colour: '#B12827',
abbreviation: 'V',
},
Expand All @@ -94,7 +102,11 @@ export default function (state = initialState, action) {
const updatedParties = state
.map((party) =>
party.abbreviation === action.abbreviation
? { ...party, affiliation: action.affiliation }
? {
...party,
affiliation: action.affiliation,
eu: action.affiliation,
}
: party
)
.sort(parliament.sort)
Expand Down

0 comments on commit ba84280

Please sign in to comment.