1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Add round parameter to prize ratings

This commit is contained in:
Brett Sun 2016-01-04 19:40:54 +01:00
parent c4730dbae5
commit 0ce1633294
4 changed files with 65 additions and 30 deletions

View File

@ -15,10 +15,10 @@ class PrizeRatingActions {
);
}
fetchAverage(pieceId) {
fetchAverage(pieceId, round) {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.fetchAverage(pieceId)
.fetchAverage(pieceId, round)
.then((res) => {
this.actions.updatePrizeRatingAverage(res.data);
resolve(res);
@ -30,10 +30,10 @@ class PrizeRatingActions {
});
}
fetchOne(pieceId) {
fetchOne(pieceId, round) {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.fetchOne(pieceId)
.fetchOne(pieceId, round)
.then((res) => {
this.actions.updatePrizeRating(res.rating.rating);
resolve(res);
@ -44,10 +44,10 @@ class PrizeRatingActions {
});
}
createRating(pieceId, rating) {
createRating(pieceId, rating, round) {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.rate(pieceId, rating)
.rate(pieceId, rating, round)
.then((res) => {
this.actions.updatePrizeRating(res.rating.rating);
resolve(res);
@ -71,10 +71,6 @@ class PrizeRatingActions {
});
});
}
updateRating(rating) {
this.actions.updatePrizeRating(rating);
}
}
export default alt.createActions(PrizeRatingActions);
export default alt.createActions(PrizeRatingActions);

View File

@ -9,15 +9,16 @@ import StarRating from 'react-star-rating';
import ReactError from '../../../../../../mixins/react_error';
import { ResourceNotFoundError } from '../../../../../../models/errors';
import PieceActions from '../../../../../../actions/piece_actions';
import PieceStore from '../../../../../../stores/piece_store';
import PieceListStore from '../../../../../../stores/piece_list_store';
import PieceListActions from '../../../../../../actions/piece_list_actions';
import PrizeActions from '../../actions/prize_actions';
import PrizeStore from '../../stores/prize_store';
import PrizeRatingActions from '../../actions/prize_rating_actions';
import PrizeRatingStore from '../../stores/prize_rating_store';
import PieceActions from '../../../../../../actions/piece_actions';
import PieceStore from '../../../../../../stores/piece_store';
import PieceListStore from '../../../../../../stores/piece_list_store';
import PieceListActions from '../../../../../../actions/piece_list_actions';
import UserStore from '../../../../../../stores/user_store';
import UserActions from '../../../../../../actions/user_actions';
@ -234,21 +235,24 @@ let PrizePieceRatings = React.createClass({
getInitialState() {
return mergeOptions(
PieceListStore.getState(),
PrizeStore.getState(),
PrizeRatingStore.getInitialState()
);
},
componentDidMount() {
PrizeRatingStore.listen(this.onChange);
PieceListStore.listen(this.onChange);
PrizeStore.listen(this.onChange);
PrizeRatingStore.listen(this.onChange);
PrizeRatingActions.fetchOne(this.props.piece.id);
PrizeRatingActions.fetchAverage(this.props.piece.id);
PrizeActions.fetchPrize();
this.fetchPrizeRatings();
},
componentWillUnmount() {
PrizeRatingStore.unlisten(this.onChange);
PieceListStore.unlisten(this.onChange);
PrizeStore.unlisten(this.onChange);
PrizeRatingStore.unlisten(this.onChange);
},
// The StarRating component does not have a property that lets us set
@ -256,7 +260,12 @@ let PrizePieceRatings = React.createClass({
// every mouseover be overridden, we need to set it ourselves initially to deal
// with the problem.
onChange(state) {
if (state.prize && state.prize.active_round != this.state.prize.active_round) {
this.fetchPrizeRatings(state);
}
this.setState(state);
if (this.refs.rating) {
this.refs.rating.state.ratingCache = {
pos: this.refs.rating.state.pos,
@ -267,10 +276,15 @@ let PrizePieceRatings = React.createClass({
}
},
fetchPrizeRatings(state = this.state) {
PrizeRatingActions.fetchOne(this.props.piece.id, state.prize.active_round);
PrizeRatingActions.fetchAverage(this.props.piece.id, state.prize.active_round);
},
onRatingClick(event, args) {
event.preventDefault();
PrizeRatingActions
.createRating(this.props.piece.id, args.rating)
.createRating(this.props.piece.id, args.rating, this.state.prize.active_round)
.then(this.refreshPieceData);
},

View File

@ -4,16 +4,41 @@ import requests from '../../../../../utils/requests';
let PrizeRatingFetcher = {
fetchAverage(pieceId) {
return requests.get('rating_average', {'piece_id': pieceId});
fetchAverage(pieceId, round) {
const params = {
'piece_id': pieceId
};
if (typeof round === 'number') {
params.round = round;
}
return requests.get('rating_average', params);
},
fetchOne(pieceId) {
return requests.get('rating', {'piece_id': pieceId});
fetchOne(pieceId, round) {
const params = {
'piece_id': pieceId
};
if (typeof round === 'number') {
params.round = round;
}
return requests.get('rating', params);
},
rate(pieceId, rating) {
return requests.post('ratings', {body: {'piece_id': pieceId, 'note': rating}});
rate(pieceId, rating, round) {
const body = {
'piece_id': pieceId,
'note': rating
};
if (typeof round === 'number') {
body.round = round;
}
return requests.post('ratings', { body });
},
select(pieceId) {

View File

@ -6,7 +6,7 @@ import PrizeActions from '../actions/prize_actions';
class PrizeStore {
constructor() {
this.prize = [];
this.prize = {};
this.bindActions(PrizeActions);
}
@ -15,4 +15,4 @@ class PrizeStore {
}
}
export default alt.createStore(PrizeStore, 'PrizeStore');
export default alt.createStore(PrizeStore, 'PrizeStore');