From 9c54c434046e090a5090e16406d70efc622a627a Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Mon, 24 Oct 2016 16:11:29 +0000 Subject: [PATCH 1/7] Sort in Icelandic order --- thingmenn-frontend/package.json | 1 + thingmenn-frontend/src/components/mps/index.js | 10 ++-------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/thingmenn-frontend/package.json b/thingmenn-frontend/package.json index d2096a1..ebec27e 100644 --- a/thingmenn-frontend/package.json +++ b/thingmenn-frontend/package.json @@ -13,6 +13,7 @@ "react-ga": "^2.1.2", "react-highcharts": "^10.0.0", "react-router": "^2.8.1", + "sort-international": "^0.0.6", "whatwg-fetch": "^1.0.0" }, "scripts": { diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index ec61132..d10a1d5 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -1,4 +1,5 @@ import React from 'react'; +import sortInternational from 'sort-international' import mpService from '../../services/mp-service' import Mp from '../../widgets/mp' @@ -53,18 +54,11 @@ export default class Mps extends React.Component { this.setState({ sortByParty, searchInput }) } - sortItem(mp1, mp2) { - if (this.state.sortByParty) { - return mp1.party.localeCompare(mp2.party) - } - return mp1.name.localeCompare(mp2.name) - } - render() { const { mps, sortByParty } = this.state const items = mps.filter(this.searchFilter.bind(this)) - .sort(this.sortItem.bind(this)) + .sort(sortInternational(sortByParty ? 'party' : 'name')) return (
From 5ef811f20d061735e2d4fc905146757dd9a830cb Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Mon, 24 Oct 2016 18:19:36 +0000 Subject: [PATCH 2/7] Sort party correctly --- .../src/components/mps/index.js | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index d10a1d5..30aa39e 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -10,12 +10,35 @@ import './styles.css' let searchInput = '' +/** + * Partition MPs into lists, ordered by name or party AND name. + * @param {Array} mps + * @returns {{byName: Array., byParty: Array.}} + */ +function createMpPartition(mps) { + const compareFn = sortInternational('name') + const partition = mps.reduce((parties, mp) => { + parties[mp.party] = parties[mp.party] || [] + parties[mp.party].push(mp) + return parties + }, {}) + + const sortParty = party => partition[party].sort(compareFn) + const byParty = [].concat([], ...Object.keys(partition).sort(compareFn).map(sortParty)) + const byName = mps.sort(compareFn) + + return { byName, byParty } +} + export default class Mps extends React.Component { constructor(props) { super(props) this.state = { - mps: [], + mps: { + byName: [], + byParty: [], + }, searchInput: '', sortByParty: false, } @@ -28,7 +51,7 @@ export default class Mps extends React.Component { }) } - searchFilter(mp) { + searchFilter = (mp) => { const { searchInput } = this.state if (searchInput) { return (mp.name.toLowerCase().indexOf(searchInput.toLowerCase()) !== -1) @@ -36,11 +59,11 @@ export default class Mps extends React.Component { return mp } + componentWillMount() { mpService.getMps() - .then(mps => { - this.setState({ mps }) - }) + .then(createMpPartition) + .then(mps => this.setState({ mps })) this.setSorting(this.props) } @@ -56,16 +79,14 @@ export default class Mps extends React.Component { render() { const { mps, sortByParty } = this.state - - const items = mps.filter(this.searchFilter.bind(this)) - .sort(sortInternational(sortByParty ? 'party' : 'name')) + const allMPs = mps[sortByParty ? 'byParty' : 'byName'].filter(this.searchFilter) return (

Allir þingmenn

- {items.map(mp => ( + {allMPs.map(mp => ( ))} From 10ab80d8aea3e20051b74f99af6846e04d582198 Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Fri, 28 Oct 2016 00:03:43 +0000 Subject: [PATCH 3/7] Simplify party sort --- .../src/components/mps/index.js | 56 ++++++++----------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index 30aa39e..a54727f 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -1,5 +1,5 @@ import React from 'react'; -import sortInternational from 'sort-international' +import icelandic from 'sort-international' import mpService from '../../services/mp-service' import Mp from '../../widgets/mp' @@ -10,35 +10,12 @@ import './styles.css' let searchInput = '' -/** - * Partition MPs into lists, ordered by name or party AND name. - * @param {Array} mps - * @returns {{byName: Array., byParty: Array.}} - */ -function createMpPartition(mps) { - const compareFn = sortInternational('name') - const partition = mps.reduce((parties, mp) => { - parties[mp.party] = parties[mp.party] || [] - parties[mp.party].push(mp) - return parties - }, {}) - - const sortParty = party => partition[party].sort(compareFn) - const byParty = [].concat([], ...Object.keys(partition).sort(compareFn).map(sortParty)) - const byName = mps.sort(compareFn) - - return { byName, byParty } -} - export default class Mps extends React.Component { constructor(props) { super(props) this.state = { - mps: { - byName: [], - byParty: [], - }, + mps: [], searchInput: '', sortByParty: false, } @@ -59,11 +36,8 @@ export default class Mps extends React.Component { return mp } - componentWillMount() { - mpService.getMps() - .then(createMpPartition) - .then(mps => this.setState({ mps })) + this.getMps() this.setSorting(this.props) } @@ -77,16 +51,32 @@ export default class Mps extends React.Component { this.setState({ sortByParty, searchInput }) } - render() { + async getMps() { + const data = await mpService.getMps() + const mps = data.map(mp => ({ ...mp, partyKey: `${mp.party}${mp.name}` })) + this.setState({ mps }) + } + + get mpList() { const { mps, sortByParty } = this.state - const allMPs = mps[sortByParty ? 'byParty' : 'byName'].filter(this.searchFilter) + const list = mps + .filter(this.searchFilter) + .sort(icelandic(sortByParty ? 'partyKey' : 'name')) + return list + } + render() { + const { sortByParty } = this.state return (

Allir þingmenn

- + - {allMPs.map(mp => ( + {this.mpList.map(mp => ( ))} From 3eea65b0e7e948d6c2e8b96aef1cf6c7d2d3d230 Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Fri, 28 Oct 2016 00:05:59 +0000 Subject: [PATCH 4/7] Remove unnecessary constructor --- thingmenn-frontend/src/components/mps/index.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index a54727f..49d6d31 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -11,14 +11,10 @@ import './styles.css' let searchInput = '' export default class Mps extends React.Component { - constructor(props) { - super(props) - - this.state = { - mps: [], - searchInput: '', - sortByParty: false, - } + state = { + mps: [], + searchInput: '', + sortByParty: false, } handleSearchInput = (evt) => { From f85059da82ec7d690df1a27dee7ceea6005beb72 Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Fri, 28 Oct 2016 00:09:26 +0000 Subject: [PATCH 5/7] Remove global variable --- thingmenn-frontend/src/components/mps/index.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index 49d6d31..3eca627 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -8,8 +8,6 @@ import List from '../../widgets/list' import './styles.css' -let searchInput = '' - export default class Mps extends React.Component { state = { mps: [], @@ -18,10 +16,8 @@ export default class Mps extends React.Component { } handleSearchInput = (evt) => { - searchInput = evt.target.value - this.setState({ - searchInput - }) + const searchInput = evt.target.value + this.setState({ searchInput }) } searchFilter = (mp) => { @@ -44,7 +40,7 @@ export default class Mps extends React.Component { setSorting(props) { const { query } = props.location const sortByParty = query.rada === 'flokkar' - this.setState({ sortByParty, searchInput }) + this.setState({ sortByParty }) } async getMps() { @@ -62,7 +58,7 @@ export default class Mps extends React.Component { } render() { - const { sortByParty } = this.state + const { sortByParty, searchInput } = this.state return (

Allir þingmenn

From feeccd6966b359ecb56d03466487002276c02b37 Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Fri, 28 Oct 2016 00:17:15 +0000 Subject: [PATCH 6/7] Simplify filter method --- thingmenn-frontend/src/components/mps/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index 3eca627..e4cbae3 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -22,10 +22,7 @@ export default class Mps extends React.Component { searchFilter = (mp) => { const { searchInput } = this.state - if (searchInput) { - return (mp.name.toLowerCase().indexOf(searchInput.toLowerCase()) !== -1) - } - return mp + return mp.name.toLowerCase().includes(searchInput.toLowerCase()) } componentWillMount() { From 54c4725abb0299f14488c9f856d719cc62fb515b Mon Sep 17 00:00:00 2001 From: Ragnar Valgeirsson Date: Fri, 28 Oct 2016 00:19:11 +0000 Subject: [PATCH 7/7] AJAX should be performed in componentDidMount --- thingmenn-frontend/src/components/mps/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thingmenn-frontend/src/components/mps/index.js b/thingmenn-frontend/src/components/mps/index.js index e4cbae3..8f68dbb 100644 --- a/thingmenn-frontend/src/components/mps/index.js +++ b/thingmenn-frontend/src/components/mps/index.js @@ -25,7 +25,7 @@ export default class Mps extends React.Component { return mp.name.toLowerCase().includes(searchInput.toLowerCase()) } - componentWillMount() { + componentDidMount() { this.getMps() this.setSorting(this.props) }