Skip to content

Commit

Permalink
Make passing quizId to widget work (and other minor updates), fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuserik committed Oct 9, 2018
1 parent a773ae0 commit e2ac7f2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ <h1>Sample Quiz</h1>
quiz = new openPlatformQuiz.Widget({
elemId: 'quizElem',
openPlatformToken: '08396c0d40f002d48809f946f96d314c1e01bcf0',
quizId: 'ffdfa65b-4b14-4bec-a4bb-5f2938eca897',
quizId: '123f9078-045f-4d89-b870-4124b5bf3b98',
onDone: function(result) { console.log("result", result); }
});

admin = new openPlatformQuiz.Admin({
elemId: 'adminElem',
openPlatformToken: '08396c0d40f002d48809f946f96d314c1e01bcf0',
quizId: 'ffdfa65b-4b14-4bec-a4bb-5f2938eca897',
quizId: '123f9078-045f-4d89-b870-4124b5bf3b98',
});
}

Expand Down
1 change: 1 addition & 0 deletions src/components/EditQuiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function renderDescriptionSettings({classes, settings, updateSetting}) {
<Typography variant="headline" gutterBottom>
Quiz beskrivelse
</Typography>
<Typography>Quiz id: {settings.get('_id')}</Typography>
<FormControl fullWidth className={classes.margin}>
<InputLabel htmlFor="title">Titel</InputLabel>
<Input
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import QuizWidget from './components/Widget';
import QuizAdmin from './components/Admin';
import store from './redux/store';
import {init} from './redux/actions';
import {} from './redux/autosave';
import {Provider} from 'react-redux';
import openplatform from './redux/openplatform';
Expand All @@ -17,11 +18,9 @@ window.addEventListener('load', () => {
}
});

async function mount(Cls, {elemId, onDone, openPlatformToken}) {
if (onDone) {
store.dispatch({type: 'ONDONE_CALLBACK', fn: onDone});
}
async function mount(Cls, {elemId, onDone, openPlatformToken, quizId}) {
openPlatformToken && (await openplatform.connect(openPlatformToken));
store.dispatch(init({quizId, onDone}));
ReactDOM.render(
<Provider store={store}>
<Cls />
Expand Down
22 changes: 18 additions & 4 deletions src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ export const deleteQuiz = quizId => async (dispatch, getState) => {
await searchQuizzes()(dispatch, getState);
};

export const init = () => async (dispatch, getState) => {
export const init = ({onDone, quizId}) => async (dispatch, getState) => {
dispatch({type: 'LOADING_STARTED'});

user = await getUser();
if (!user) {
dispatch({type: 'LOADING_DONE'});
return;
return dispatch({type: 'LOADING_DONE'});
}
if (onDone) {
dispatch({type: 'ONDONE_CALLBACK', fn: onDone});
}

[[quizType], [quizImageType], [quizStatisticsType]] = await Promise.all([
Expand Down Expand Up @@ -141,6 +143,18 @@ export const init = () => async (dispatch, getState) => {
}
});

if (quizId) {
try {
const quiz = await storage.get({_id: quizId});
dispatch(setQuiz(quiz));
return dispatch({type: 'LOADING_DONE'});
} catch (e) {
if (e.statusCode === 404) {
// TODO handle 404
}
console.log(e);
}
}
await searchQuizzes()(dispatch, getState);
dispatch({type: 'LOADING_DONE'});
return dispatch({type: 'LOADING_DONE'});
};
2 changes: 0 additions & 2 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {createStore, applyMiddleware, compose} from 'redux';
import {root} from './reducers';
import thunk from 'redux-thunk';
import * as actions from './actions';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(root, composeEnhancers(applyMiddleware(thunk)));
store.dispatch(actions.init());
export default store;

0 comments on commit e2ac7f2

Please sign in to comment.