Files
astuto/app/javascript/actions/PostStatusChange/requestPostStatusChanges.ts
Riccardo Graziosi dad382d2b1 Post follow and updates notifications V1 (#111)
* It is now possible to follow a post in order to receive updates about it
* Notifications are now sent when updates are published
* Post status changes are now tracked
* Update sidebar now shows the post status history
* Mark a comment as a post update using the comment form
* ... more ...
2022-05-28 11:03:36 +02:00

58 lines
1.9 KiB
TypeScript

import { Action } from 'redux';
import { ThunkAction } from 'redux-thunk';
import IPostStatusChangeJSON from '../../interfaces/json/IPostStatusChange';
import { State } from '../../reducers/rootReducer';
export const POST_STATUS_CHANGES_REQUEST_START = 'POST_STATUS_CHANGES_REQUEST_START';
interface PostStatusChangesRequestStartAction {
type: typeof POST_STATUS_CHANGES_REQUEST_START;
}
export const POST_STATUS_CHANGES_REQUEST_SUCCESS = 'POST_STATUS_CHANGES_REQUEST_SUCCESS';
interface PostStatusChangesRequestSuccessAction {
type: typeof POST_STATUS_CHANGES_REQUEST_SUCCESS;
postStatusChanges: Array<IPostStatusChangeJSON>;
}
export const POST_STATUS_CHANGES_REQUEST_FAILURE = 'POST_STATUS_CHANGES_REQUEST_FAILURE';
interface PostStatusChangesRequestFailureAction {
type: typeof POST_STATUS_CHANGES_REQUEST_FAILURE;
error: string;
}
export type PostStatusChangesRequestActionTypes =
PostStatusChangesRequestStartAction |
PostStatusChangesRequestSuccessAction |
PostStatusChangesRequestFailureAction;
const postStatusChangesRequestStart = (): PostStatusChangesRequestActionTypes => ({
type: POST_STATUS_CHANGES_REQUEST_START,
});
const postStatusChangesRequestSuccess = (
postStatusChanges: Array<IPostStatusChangeJSON>,
): PostStatusChangesRequestActionTypes => ({
type: POST_STATUS_CHANGES_REQUEST_SUCCESS,
postStatusChanges,
});
const postStatusChangesRequestFailure = (error: string): PostStatusChangesRequestActionTypes => ({
type: POST_STATUS_CHANGES_REQUEST_FAILURE,
error,
});
export const requestPostStatusChanges = (
postId: number,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
dispatch(postStatusChangesRequestStart());
try {
const response = await fetch(`/posts/${postId}/post_status_changes`);
const json = await response.json();
dispatch(postStatusChangesRequestSuccess(json));
} catch (e) {
dispatch(postStatusChangesRequestFailure(e));
}
};