Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
🐛 fix ratings when get a slow connexion (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Feb 1, 2019
1 parent 1754375 commit af29e55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import values from 'lodash/values'
import compact from 'lodash/compact'

import TotalRatings from 'screens/organizer/components/totalRatings'
import TalkSelection from 'screens/organizer/components/talkSelection'
Expand All @@ -8,8 +10,11 @@ import './proposalInfo.css'

const ProposalInfo = ({ proposal, isMobile, deliberationActive }) => {
const {
id, rating, loves, hates, noopinion,
id, rating, loves, hates, noopinion, usersRatings,
} = proposal

const nbVotes = compact(values(usersRatings)).length

return (
<div className="proposal-item-info">
{(!isMobile && deliberationActive) && <TalkSelection proposalId={id} />}
Expand All @@ -18,6 +23,7 @@ const ProposalInfo = ({ proposal, isMobile, deliberationActive }) => {
loves={loves}
hates={hates}
noopinion={noopinion}
nbvotes={nbVotes}
/>
</div>
)
Expand Down
12 changes: 8 additions & 4 deletions src/store/reactions/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const rateProposal = async (action, store, { router }) => {
const { uid } = store.auth.get()
const eventId = router.getParam('eventId')
const proposalId = router.getParam('proposalId')

// add or remove the rating in database and store
const rated = !!rating.rating || !!rating.feeling
if (rated) {
Expand All @@ -27,11 +28,14 @@ export const rateProposal = async (action, store, { router }) => {
await deleteRating(eventId, proposalId, uid)
store.data.ratings.remove([uid])
}

// retrieve all ratings before updating proposal computed
const ratings = await getRatings(eventId, proposalId)
// compute average rating
const avgRating = getRatingsAverage(store)
const loves = getFeelingsCount('love')(store)
const hates = getFeelingsCount('hate')(store)
const noopinion = getFeelingsCount('noopinion')(store)
const avgRating = getRatingsAverage(ratings)
const loves = getFeelingsCount('love')(ratings)
const hates = getFeelingsCount('hate')(ratings)
const noopinion = getFeelingsCount('noopinion')(ratings)
const ratingUpdated = {
rating: avgRating, loves, hates, noopinion,
}
Expand Down
11 changes: 5 additions & 6 deletions src/store/reducers/data/ratings.selectors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable import/prefer-default-export */
import isEmpty from 'lodash/isEmpty'

export const getRatingsAverage = (store) => {
const ratings = store.data.ratings.getAsArray().filter(r => r.feeling !== 'noopinion')
if (isEmpty(ratings)) return null
return ratings.map(r => r.rating).reduce((p, c) => p + c, 0) / ratings.length
export const getRatingsAverage = (ratings) => {
const usersRatings = ratings.filter(r => r.feeling !== 'noopinion')
if (isEmpty(usersRatings)) return null
return usersRatings.map(r => r.rating).reduce((p, c) => p + c, 0) / usersRatings.length
}

export const getFeelingsCount = feeling => (store) => {
const ratings = store.data.ratings.getAsArray()
export const getFeelingsCount = feeling => (ratings) => {
if (isEmpty(ratings)) return null
return ratings.filter(r => r.feeling === feeling).length
}

0 comments on commit af29e55

Please sign in to comment.