Last active
November 3, 2015 18:42
-
-
Save lesniakania/61bab24f4ea2a86fcf10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from 'events'; | |
import {ReduceStore} from 'flux/utils'; | |
import AppDispatcher from '../AppDispatcher'; | |
import ActionTypes from '../constants/ActionTypes'; | |
import Connection from '../lib/Connection'; | |
const CHANGE_EVENT = 'change'; | |
class SubmissionStore extends EventEmitter { | |
constructor() { | |
super(); | |
this.submission = null; | |
} | |
getSubmission(submissionId) { | |
return this.submission; | |
} | |
addChangeListener(listener) { | |
this.addListener(CHANGE_EVENT, listener); | |
} | |
removeChangeListener(listener) { | |
this.removeListener(CHANGE_EVENT, listener); | |
} | |
emitChange() { | |
this.emit(CHANGE_EVENT); | |
} | |
} | |
const submissionStore = new SubmissionStore(); | |
submissionStore.dispatchToken = AppDispatcher.register((payload) => { | |
let id; | |
switch (payload.action.actionType) { | |
case ActionTypes.REQUEST_SUBMISSION: | |
id = payload.action.id; | |
Connection.get(`/submissions/${id}`).then((response) => { | |
submissionStore.submission = response.data; | |
submissionStore.emitChange(); | |
}); | |
break; | |
case ActionTypes.PERFORM_RATING: | |
id = payload.action.id; | |
const rate = payload.action.rate; | |
Connection.post(`/submissions/${id}/rate`, { rate: rate }).then( | |
(response) => { | |
submissionStore.submission = response.data; | |
submissionStore.emitChange(); | |
}); | |
break; | |
default: | |
// Do nothing | |
} | |
}); | |
export default submissionStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment