Skip to content

Commit

Permalink
Merge pull request #177 from DBCDK/164-delete-quiz
Browse files Browse the repository at this point in the history
delete quiz, fixes #164
  • Loading branch information
rasmuserik committed Oct 6, 2018
2 parents 3bf6f4b + dfc102b commit 857f3a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/components/QuizList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import style from './style';
import {withStyles} from '@material-ui/core/styles';

import {searchResults} from '../redux/selectors';
import {addQuiz, setQuiz} from '../redux/actions';
import {addQuiz, setQuiz, deleteQuiz} from '../redux/actions';

export class QuizList extends Component {
render() {
const {classes, searchResults, newQuiz, setQuiz} = this.props;
const {classes, searchResults, newQuiz, setQuiz, deleteQuiz} = this.props;
return (
<Grid container spacing={16}>
<Grid item xs={12}>
Expand Down Expand Up @@ -72,7 +72,10 @@ export class QuizList extends Component {
<IconButton aria-label="Search" onClick={() => {}}>
<FileCopyIcon />
</IconButton>
<IconButton aria-label="Search" onClick={() => {}}>
<IconButton
aria-label="Search"
onClick={() => deleteQuiz(o.get('_id'))}
>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
Expand All @@ -92,6 +95,7 @@ export function mapStateToProps(state, ownProps) {
export function mapDispatchToProps(dispatch) {
return {
setQuiz: quiz => dispatch(setQuiz(quiz)),
deleteQuiz: quiz => dispatch(deleteQuiz(quiz)),
newQuiz: () => dispatch(addQuiz)
};
}
Expand Down
19 changes: 13 additions & 6 deletions src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {getUser, storage, findOrCreateType} from './openplatform';
import sampleQuiz from '../sampleQuizData';

export const adminQuizList = () => {
export const adminQuizList = () => async dispatch => {
// TODO sync quiz to store
return {
await searchQuizzes({})(dispatch);
dispatch({
type: 'ADMIN_QUIZ_LIST'
};
});
};

export const addDispatch = screen => {
return {
type: 'ADD_DISPATCH',
Expand Down Expand Up @@ -59,7 +61,7 @@ export const editScreen = ({screen}) => ({type: 'ADMIN_EDIT_SCREEN', screen});
export const screenAction = action => ({type: 'PAGE_ACTION', action});

let quizType, quizImageType, user;
export const searchQuizzes = async ({query, own, tags, title}) => {
export const searchQuizzes = ({query, own, tags, title}) => async dispatch => {
let result = await storage.scan({
reverse: true,
_type: quizType,
Expand All @@ -68,19 +70,25 @@ export const searchQuizzes = async ({query, own, tags, title}) => {
//limit: 10
});
result = await Promise.all(result.map(o => storage.get({_id: o.val})));
dispatch({type: 'SEARCH_RESULTS', searchResults: result});
return result;
};

export const addQuiz = async dispatch => {
const {_id} = await storage.put(
Object.assign({}, sampleQuiz, {_type: quizType, _id: undefined})
);
await searchQuizzes({})(dispatch);
dispatch({
type: 'SET_QUIZ',
quiz: await storage.get({_id})
});
};
export const setQuiz = quiz => ({type: 'SET_QUIZ', quiz});
export const deleteQuiz = quizId => async dispatch => {
await storage.delete({_id: quizId});
await searchQuizzes({})(dispatch);
};

export const init = () => async dispatch => {
dispatch({type: 'LOADING_STARTED'});
Expand Down Expand Up @@ -130,7 +138,6 @@ export const init = () => async dispatch => {
}
});

const searchResults = await searchQuizzes({query: '', own: true});
dispatch({type: 'SEARCH_RESULTS', searchResults});
await searchQuizzes({query: '', own: true})(dispatch);
dispatch({type: 'LOADING_DONE'});
};

0 comments on commit 857f3a1

Please sign in to comment.