2019-10-02 16:43:13 +02:00
|
|
|
import { ThunkAction } from "redux-thunk";
|
|
|
|
|
import { State } from "../reducers/rootReducer";
|
|
|
|
|
import { Action } from "redux";
|
|
|
|
|
|
2022-04-09 10:45:43 +02:00
|
|
|
import buildRequestHeaders from "../helpers/buildRequestHeaders";
|
|
|
|
|
|
2019-10-02 16:43:13 +02:00
|
|
|
export const TOGGLE_COMMENT_IS_UPDATE_SUCCESS = 'TOGGLE_COMMENT_IS_UPDATE_SUCCESS';
|
|
|
|
|
export interface ToggleIsUpdateSuccessAction {
|
|
|
|
|
type: typeof TOGGLE_COMMENT_IS_UPDATE_SUCCESS;
|
|
|
|
|
commentId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toggleIsUpdateSuccess = (
|
|
|
|
|
commentId: number,
|
|
|
|
|
): ToggleIsUpdateSuccessAction => ({
|
|
|
|
|
type: TOGGLE_COMMENT_IS_UPDATE_SUCCESS,
|
|
|
|
|
commentId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const toggleCommentIsUpdate = (
|
|
|
|
|
postId: number,
|
|
|
|
|
commentId: number,
|
|
|
|
|
currentIsPostUpdate: boolean,
|
|
|
|
|
authenticityToken: string,
|
|
|
|
|
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`/posts/${postId}/comments/${commentId}`, {
|
|
|
|
|
method: 'PATCH',
|
2022-04-09 10:45:43 +02:00
|
|
|
headers: buildRequestHeaders(authenticityToken),
|
2019-10-02 16:43:13 +02:00
|
|
|
body: JSON.stringify({
|
|
|
|
|
comment: {
|
|
|
|
|
is_post_update: !currentIsPostUpdate,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
dispatch(toggleIsUpdateSuccess(commentId));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
}
|
|
|
|
|
}
|