UNPKG

trc-client-core

Version:
113 lines (105 loc) 3.67 kB
import Reflux from 'reflux'; import Immutable from 'immutable'; import ImmutableStoreMixin from 'reflux-immutable/ImmutableStoreMixin'; import History from 'trc-client-core/src/global/history'; import QandaActions from 'trc-client-core/src/qanda/QandaActions'; import QandaEditQuestionFormActions from 'trc-client-core/src/qanda/QandaEditQuestionFormActions'; import NavigationActions from 'trc-client-core/src/global/NavigationActions'; import NavigationStore from 'trc-client-core/src/global/NavigationStore'; import LoadingActions from 'trc-client-core/src/global/LoadingActions'; function questionById(id) { return function(qq){ return qq.get('id') === id; }; } var QandaStore = Reflux.createStore({ listenables: [ QandaActions, QandaEditQuestionFormActions, NavigationActions ], mixins: [ImmutableStoreMixin], localStorageKey: 'Qanda', init: function () { this.setState({ allQuestions: [], questions: [], question: {}, tags: [], askFormSuccess: null, askFormRequest: null, askForm: { questionContent: null, tag: null } }); QandaActions.fetchQuestions(); QandaActions.fetchTags(); }, onRouteChange: function (data) { this.setState(data); this.updateCurrentQuestion(); }, onFetchQuestionsCompleted: function(data) { this.setState({ questions: data.filter((item) => item.status === 'Publish'), allQuestions: data }); this.updateCurrentQuestion(); }, updateCurrentQuestion: function () { // console.log(this.state.toJS(), History); var id = this.state.getIn(['params','id']); if(this.state.get('allQuestions').size) { this.setState({ question: this.state.get('allQuestions').find(questionById(id)) || {} }); } }, onFetchTagsCompleted: function(data) { this.setState({ tags: data }); }, onSaveQuestion:function (question) { var allQuestions = this.state.get('allQuestions'); this.setState({ allQuestions: allQuestions.set(allQuestions.findIndex(questionById(question.id)), Immutable.Map(question)), question: question }); }, onSaveQuestionCompleted:function () { LoadingActions.flashMessage('success', 'Save Complete.'); }, onDeleteQuestionCompleted: function () { QandaActions.fetchQuestions(); }, onCreateQuestionCompleted:function () { LoadingActions.flashMessage('success', 'Question created.'); QandaActions.fetchQuestions(); History.pushState(null, '/product/qanda/edit'); }, // Ask Form onAskQuestion: function () { QandaActions.sendAskFormData(this.state.get('askForm').toJS()); }, onSendAskFormData: function () { this.setState({askFormRequest: true}); }, onSendAskFormDataFailed: function () { this.setState({askFormSuccess: false}); LoadingActions.flashMessage('failure long', 'You don\'t have the required participant data to ask a question.', 5000); }, onSendAskFormDataCompleted: function () { this.setState({askFormSuccess: true}); }, onUpdateAskForm: function (e, details) { this.setState({ askForm: this.state.get('askForm').set(details.key, details.value) }); }, getQuestionById(id) { return this.state.get('allQuestions').find(questionById(id)); } }); module.exports = QandaStore;