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

rating create and fetch

This commit is contained in:
diminator 2015-08-10 17:02:10 +02:00
parent a27415eb86
commit 36ca2004ff
5 changed files with 144 additions and 5 deletions

View File

@ -0,0 +1,66 @@
'use strict';
import alt from '../../../../alt';
import Q from 'q';
import PrizeRatingFetcher from '../fetchers/prize_rating_fetcher';
class PrizeRatingActions {
constructor() {
this.generateActions(
'updatePrizeRatings',
'updatePrizeRating'
);
}
fetch() {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.fetch()
.then((res) => {
this.actions.updatePrizeRatings(res.ratings);
resolve(res);
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
});
}
fetchOne(pieceId) {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.fetchOne(pieceId)
.then((res) => {
this.actions.updatePrizeRating(res.rating.rating);
resolve(res);
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
});
}
createRating(pieceId, rating) {
return Q.Promise((resolve, reject) => {
PrizeRatingFetcher
.rate(pieceId, rating)
.then((res) => {
this.actions.updatePrizeRating(res.rating.rating);
resolve(res);
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
});
}
updateRating(rating) {
this.actions.updatePrizeRating(rating);
}
}
export default alt.createActions(PrizeRatingActions);

View File

@ -2,9 +2,14 @@
import React from 'react'; import React from 'react';
import StarRating from 'react-star-rating';
import PieceActions from '../../../../../actions/piece_actions'; import PieceActions from '../../../../../actions/piece_actions';
import PieceStore from '../../../../../stores/piece_store'; import PieceStore from '../../../../../stores/piece_store';
import PrizeRatingActions from '../../actions/prize_rating_actions';
import PrizeRatingStore from '../../stores/prize_rating_store';
import Piece from '../../../../../components/ascribe_detail/piece'; import Piece from '../../../../../components/ascribe_detail/piece';
import AppConstants from '../../../../../constants/application_constants'; import AppConstants from '../../../../../constants/application_constants';
@ -14,8 +19,6 @@ import Property from '../../../../../components/ascribe_forms/property';
import InputTextAreaToggable from '../../../../../components/ascribe_forms/input_textarea_toggable'; import InputTextAreaToggable from '../../../../../components/ascribe_forms/input_textarea_toggable';
import CollapsibleParagraph from '../../../../../components/ascribe_collapsible/collapsible_paragraph'; import CollapsibleParagraph from '../../../../../components/ascribe_collapsible/collapsible_paragraph';
import StarRating from 'react-star-rating';
/** /**
* This is the component that implements resource/data specific functionality * This is the component that implements resource/data specific functionality
*/ */
@ -73,9 +76,33 @@ let PrizePieceDetails = React.createClass({
piece: React.PropTypes.object piece: React.PropTypes.object
}, },
onRatingClick(event, position, rating, caption, name) { getInitialState() {
console.log(rating); return PrizeRatingStore.getState();
}, },
onChange(state) {
this.setState(state);
},
componentDidMount() {
PrizeRatingStore.listen(this.onChange);
PrizeRatingActions.fetchOne(this.props.piece.id);
},
componentWillUnmount() {
// Every time we're leaving the piece detail page,
// just reset the piece that is saved in the piece store
// as it will otherwise display wrong/old data once the user loads
// the piece detail a second time
PrizeRatingActions.updateRating({});
PrizeRatingStore.unlisten(this.onChange);
},
onRatingClick(event, args) {
event.preventDefault();
PrizeRatingActions.createRating(this.props.piece.id, args.rating);
},
render() { render() {
if (this.props.piece.prize if (this.props.piece.prize
&& this.props.piece.prize.name && this.props.piece.prize.name
@ -90,6 +117,7 @@ let PrizePieceDetails = React.createClass({
caption="" caption=""
step={1} step={1}
size='lg' size='lg'
rating={this.state.currentRating}
onRatingClick={this.onRatingClick} onRatingClick={this.onRatingClick}
ratingAmount={5} /> ratingAmount={5} />
<Form ref='form'> <Form ref='form'>

View File

@ -14,7 +14,9 @@ function getApiUrls(subdomain) {
'jurys': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/', 'jurys': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/',
'jury': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/', 'jury': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/',
'jury_activate': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/activate/', 'jury_activate': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/activate/',
'jury_resend': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/resend/' 'jury_resend': AppPrizeConstants.prizeApiEndpoint + subdomain + '/jury/${email}/resend/',
'ratings': AppPrizeConstants.prizeApiEndpoint + subdomain + '/ratings/',
'rating': AppPrizeConstants.prizeApiEndpoint + subdomain + '/ratings/${piece_id}/'
}; };
} }

View File

@ -0,0 +1,20 @@
'use strict';
import requests from '../../../../utils/requests';
let PrizeRatingFetcher = {
fetch() {
return requests.get('rating');
},
fetchOne(pieceId) {
return requests.get('rating', {'piece_id': pieceId});
},
rate(pieceId, rating) {
return requests.post('ratings', {body: {'piece_id': pieceId, 'rating': rating}});
}
};
export default PrizeRatingFetcher;

View File

@ -0,0 +1,23 @@
'use strict';
import alt from '../../../../alt';
import PrizeRatingActions from '../actions/prize_rating_actions';
class PrizeRatingStore {
constructor() {
this.ratings = [];
this.currentRating = null;
this.bindActions(PrizeRatingActions);
}
onUpdatePrizeRatings( ratings ) {
this.ratings = ratings;
}
onUpdatePrizeRating( rating ) {
this.currentRating = parseInt(rating, 10);
}
}
export default alt.createStore(PrizeRatingStore, 'PrizeRatingStore');