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 ...
This commit is contained in:
Riccardo Graziosi
2022-05-28 11:03:36 +02:00
committed by GitHub
parent ce7be1b30c
commit dad382d2b1
59 changed files with 1080 additions and 71 deletions

View File

@@ -0,0 +1,58 @@
import { Action } from 'redux';
import { ThunkAction } from 'redux-thunk';
import IFollowJSON from '../../interfaces/json/IFollow';
import { State } from '../../reducers/rootReducer';
export const FOLLOW_REQUEST_START = 'FOLLOW_REQUEST_START';
interface FollowRequestStartAction {
type: typeof FOLLOW_REQUEST_START;
}
export const FOLLOW_REQUEST_SUCCESS = 'FOLLOW_REQUEST_SUCCESS';
interface FollowRequestSuccessAction {
type: typeof FOLLOW_REQUEST_SUCCESS;
follow: IFollowJSON;
}
export const FOLLOW_REQUEST_FAILURE = 'FOLLOW_REQUEST_FAILURE';
interface FollowRequestFailureAction {
type: typeof FOLLOW_REQUEST_FAILURE;
error: string;
}
export type FollowRequestActionTypes =
FollowRequestStartAction |
FollowRequestSuccessAction |
FollowRequestFailureAction;
const followRequestStart = (): FollowRequestActionTypes => ({
type: FOLLOW_REQUEST_START,
});
const followRequestSuccess = (
follow: IFollowJSON,
): FollowRequestActionTypes => ({
type: FOLLOW_REQUEST_SUCCESS,
follow,
});
const followRequestFailure = (error: string): FollowRequestActionTypes => ({
type: FOLLOW_REQUEST_FAILURE,
error,
});
export const requestFollow = (
postId: number,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
dispatch(followRequestStart());
try {
const response = await fetch(`/posts/${postId}/follows`);
const json = await response.json();
dispatch(followRequestSuccess(json));
} catch (e) {
dispatch(followRequestFailure(e));
}
};

View File

@@ -0,0 +1,47 @@
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import { State } from "../../reducers/rootReducer";
import buildRequestHeaders from "../../helpers/buildRequestHeaders";
import HttpStatus from "../../constants/http_status";
import IFollowJSON from "../../interfaces/json/IFollow";
export const FOLLOW_SUBMIT_SUCCESS = 'FOLLOW_SUBMIT_SUCCESS';
interface FollowSubmitSuccessAction {
type: typeof FOLLOW_SUBMIT_SUCCESS,
postId: number;
isFollow: boolean;
follow: IFollowJSON;
}
export type FollowActionTypes = FollowSubmitSuccessAction;
const followSubmitSuccess = (
postId: number,
isFollow: boolean,
follow: IFollowJSON,
): FollowSubmitSuccessAction => ({
type: FOLLOW_SUBMIT_SUCCESS,
postId,
isFollow,
follow,
});
export const submitFollow = (
postId: number,
isFollow: boolean,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
try {
const res = await fetch(`/posts/${postId}/follows`, {
method: isFollow ? 'POST' : 'DELETE',
headers: buildRequestHeaders(authenticityToken),
});
const json = await res.json();
if (res.status === HttpStatus.Created || res.status === HttpStatus.Accepted)
dispatch(followSubmitSuccess(postId, isFollow, json));
} catch (e) {
console.log('An error occurred while following a post');
}
}