alpinesmuseum-public/assets/js/redux/middleware/participate.js

39 lines
1.1 KiB
JavaScript

import {
SEND_PARTICIPATION,
SEND_PARTICIPATION_SUCCESS,
SEND_PARTICIPATION_ERROR
} from '../actions/participate';
import { showSpinner, hideSpinner, toggleParticipated } from '../actions/ui';
export const sendParticipationFlow = ({ dispatch }) => next => action => {
next(action);
if (action.type === SEND_PARTICIPATION) {
const formData = new FormData();
for (let key of Object.getOwnPropertyNames(action.payload)) {
formData.append(key, action.payload[key])
}
fetch('/participate/',
{ method: "POST", body: formData }
)
.then(response => response.json())
.then(data => dispatch({ type: SEND_PARTICIPATION_SUCCESS, payload: data }))
.catch(error => dispatch({ type: SEND_PARTICIPATION_ERROR, payload: error }))
dispatch(showSpinner());
dispatch(toggleParticipated(false));
}
};
export const successfulParticipationFlow = ({ dispatch }) => next => action => {
next(action);
if (action.type === SEND_PARTICIPATION_SUCCESS) {
dispatch(hideSpinner());
dispatch(toggleParticipated(true));
}
};
export const participateMdl = [sendParticipationFlow, successfulParticipationFlow];