Files
astuto/app/javascript/actions/changePostStatus.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-09-12 15:51:45 +02:00
import { Action } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { State } from '../reducers/rootReducer';
import buildRequestHeaders from '../helpers/buildRequestHeaders';
2019-09-12 15:51:45 +02:00
export const CHANGE_POST_STATUS_SUCCESS = 'CHANGE_POST_STATUS_SUCCESS';
export interface ChangePostStatusSuccessAction {
2019-09-12 15:51:45 +02:00
type: typeof CHANGE_POST_STATUS_SUCCESS;
2019-09-26 16:03:41 +02:00
newPostStatusId: number;
2019-09-12 15:51:45 +02:00
}
const changePostStatusSuccess = (newPostStatusId: number): ChangePostStatusSuccessAction => ({
type: CHANGE_POST_STATUS_SUCCESS,
newPostStatusId,
});
export const changePostStatus = (
postId: number,
newPostStatusId: number,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
try {
const response = await fetch(`/posts/${postId}`, {
method: 'PATCH',
headers: buildRequestHeaders(authenticityToken),
2019-09-12 15:51:45 +02:00
body: JSON.stringify({
post: {
post_status_id: newPostStatusId,
},
})
});
if (response.status === 204) {
dispatch(changePostStatusSuccess(newPostStatusId));
}
} catch (e) {
console.log(e);
}
}