Add post status administration (#105)

This commit is contained in:
Riccardo Graziosi
2022-05-01 18:00:38 +02:00
committed by GitHub
parent c5148147e3
commit 5256ea911a
47 changed files with 1580 additions and 32 deletions

View File

@@ -0,0 +1,62 @@
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import buildRequestHeaders from "../helpers/buildRequestHeaders";
import { State } from "../reducers/rootReducer";
export const POST_STATUS_DELETE_START = 'POST_STATUS_DELETE_START';
interface PostStatusDeleteStartAction {
type: typeof POST_STATUS_DELETE_START;
}
export const POST_STATUS_DELETE_SUCCESS = 'POST_STATUS_DELETE_SUCCESS';
interface PostStatusDeleteSuccessAction {
type: typeof POST_STATUS_DELETE_SUCCESS;
id: number;
}
export const POST_STATUS_DELETE_FAILURE = 'POST_STATUS_DELETE_FAILURE';
interface PostStatusDeleteFailureAction {
type: typeof POST_STATUS_DELETE_FAILURE;
error: string;
}
export type PostStatusDeleteActionTypes =
PostStatusDeleteStartAction |
PostStatusDeleteSuccessAction |
PostStatusDeleteFailureAction;
const postStatusDeleteStart = (): PostStatusDeleteStartAction => ({
type: POST_STATUS_DELETE_START,
});
const postStatusDeleteSuccess = (
id: number,
): PostStatusDeleteSuccessAction => ({
type: POST_STATUS_DELETE_SUCCESS,
id,
});
const postStatusDeleteFailure = (error: string): PostStatusDeleteFailureAction => ({
type: POST_STATUS_DELETE_FAILURE,
error,
});
export const deletePostStatus = (
id: number,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => (
async (dispatch) => {
dispatch(postStatusDeleteStart());
try {
const response = await fetch(`/post_statuses/${id}`, {
method: 'DELETE',
headers: buildRequestHeaders(authenticityToken),
});
const json = await response.json();
dispatch(postStatusDeleteSuccess(id));
} catch (e) {
dispatch(postStatusDeleteFailure(e));
}
}
);

View File

@@ -4,6 +4,7 @@ import { State } from '../reducers/rootReducer';
import ICommentJSON from '../interfaces/json/IComment';
import buildRequestHeaders from '../helpers/buildRequestHeaders';
import HttpStatus from '../constants/http_status';
export const COMMENT_SUBMIT_START = 'COMMENT_SUBMIT_START';
interface CommentSubmitStartAction {
@@ -68,7 +69,7 @@ export const submitComment = (
});
const json = await res.json();
if (res.status === 201) {
if (res.status === HttpStatus.Created) {
dispatch(commentSubmitSuccess(json));
} else {
dispatch(commentSubmitFailure(parentId, json.error));

View File

@@ -4,6 +4,7 @@ import { ThunkAction } from "redux-thunk";
import { State } from "../reducers/rootReducer";
import ILikeJSON from "../interfaces/json/ILike";
import buildRequestHeaders from "../helpers/buildRequestHeaders";
import HttpStatus from "../constants/http_status";
export const LIKE_SUBMIT_SUCCESS = 'LIKE_SUBMIT_SUCCESS';
interface LikeSubmitSuccessAction {
@@ -38,7 +39,7 @@ export const submitLike = (
});
const json = await res.json();
if (res.status === 201 || res.status === 202)
if (res.status === HttpStatus.Created || res.status === HttpStatus.Accepted)
dispatch(likeSubmitSuccess(postId, isLike, json));
} catch (e) {
console.log('An error occurred while liking a post');

View File

@@ -0,0 +1,78 @@
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import HttpStatus from "../constants/http_status";
import buildRequestHeaders from "../helpers/buildRequestHeaders";
import IPostStatusJSON from "../interfaces/json/IPostStatus";
import { State } from "../reducers/rootReducer";
export const POSTSTATUS_SUBMIT_START = 'POSTSTATUS_SUBMIT_START';
interface PostStatusSubmitStartAction {
type: typeof POSTSTATUS_SUBMIT_START;
}
export const POSTSTATUS_SUBMIT_SUCCESS = 'POSTSTATUS_SUBMIT_SUCCESS';
interface PostStatusSubmitSuccessAction {
type: typeof POSTSTATUS_SUBMIT_SUCCESS;
postStatus: IPostStatusJSON;
}
export const POSTSTATUS_SUBMIT_FAILURE = 'POSTSTATUS_SUBMIT_FAILURE';
interface PostStatusSubmitFailureAction {
type: typeof POSTSTATUS_SUBMIT_FAILURE;
error: string;
}
export type PostStatusSubmitActionTypes =
PostStatusSubmitStartAction |
PostStatusSubmitSuccessAction |
PostStatusSubmitFailureAction;
const postStatusSubmitStart = (): PostStatusSubmitStartAction => ({
type: POSTSTATUS_SUBMIT_START,
});
const postStatusSubmitSuccess = (
postStatusJSON: IPostStatusJSON,
): PostStatusSubmitSuccessAction => ({
type: POSTSTATUS_SUBMIT_SUCCESS,
postStatus: postStatusJSON,
});
const postStatusSubmitFailure = (error: string): PostStatusSubmitFailureAction => ({
type: POSTSTATUS_SUBMIT_FAILURE,
error,
});
export const submitPostStatus = (
name: string,
color: string,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
dispatch(postStatusSubmitStart());
try {
const res = await fetch(`/post_statuses`, {
method: 'POST',
headers: buildRequestHeaders(authenticityToken),
body: JSON.stringify({
post_status: {
name,
color,
},
}),
});
const json = await res.json();
if (res.status === HttpStatus.Created) {
dispatch(postStatusSubmitSuccess(json));
} else {
dispatch(postStatusSubmitFailure(json.error));
}
return Promise.resolve(res);
} catch (e) {
dispatch(postStatusSubmitFailure(e));
return Promise.resolve(null);
}
};

View File

@@ -0,0 +1,79 @@
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import HttpStatus from "../constants/http_status";
import buildRequestHeaders from "../helpers/buildRequestHeaders";
import IPostStatusJSON from "../interfaces/json/IPostStatus";
import { State } from "../reducers/rootReducer";
export const POSTSTATUS_UPDATE_START = 'POSTSTATUS_UPDATE_START';
interface PostStatusUpdateStartAction {
type: typeof POSTSTATUS_UPDATE_START;
}
export const POSTSTATUS_UPDATE_SUCCESS = 'POSTSTATUS_UPDATE_SUCCESS';
interface PostStatusUpdateSuccessAction {
type: typeof POSTSTATUS_UPDATE_SUCCESS;
postStatus: IPostStatusJSON;
}
export const POSTSTATUS_UPDATE_FAILURE = 'POSTSTATUS_UPDATE_FAILURE';
interface PostStatusUpdateFailureAction {
type: typeof POSTSTATUS_UPDATE_FAILURE;
error: string;
}
export type PostStatusUpdateActionTypes =
PostStatusUpdateStartAction |
PostStatusUpdateSuccessAction |
PostStatusUpdateFailureAction;
const postStatusUpdateStart = (): PostStatusUpdateStartAction => ({
type: POSTSTATUS_UPDATE_START,
});
const postStatusUpdateSuccess = (
postStatusJSON: IPostStatusJSON,
): PostStatusUpdateSuccessAction => ({
type: POSTSTATUS_UPDATE_SUCCESS,
postStatus: postStatusJSON,
});
const postStatusUpdateFailure = (error: string): PostStatusUpdateFailureAction => ({
type: POSTSTATUS_UPDATE_FAILURE,
error,
});
export const updatePostStatus = (
id: number,
name: string,
color: string,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
dispatch(postStatusUpdateStart());
try {
const res = await fetch(`/post_statuses/${id}`, {
method: 'PATCH',
headers: buildRequestHeaders(authenticityToken),
body: JSON.stringify({
post_status: {
name,
color,
},
}),
});
const json = await res.json();
if (res.status === HttpStatus.OK) {
dispatch(postStatusUpdateSuccess(json));
} else {
dispatch(postStatusUpdateFailure(json.error));
}
return Promise.resolve(res);
} catch (e) {
dispatch(postStatusUpdateFailure(e));
return Promise.resolve(null);
}
};

View File

@@ -0,0 +1,96 @@
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import HttpStatus from "../constants/http_status";
import buildRequestHeaders from "../helpers/buildRequestHeaders";
import IPostStatus from "../interfaces/IPostStatus";
import { State } from "../reducers/rootReducer";
export const POSTSTATUS_ORDER_UPDATE_START = 'POSTSTATUS_ORDER_UPDATE_START';
interface PostStatusOrderUpdateStartAction {
type: typeof POSTSTATUS_ORDER_UPDATE_START;
newOrder: Array<IPostStatus>;
}
export const POSTSTATUS_ORDER_UPDATE_SUCCESS = 'POSTSTATUS_ORDER_UPDATE_SUCCESS';
interface PostStatusOrderUpdateSuccessAction {
type: typeof POSTSTATUS_ORDER_UPDATE_SUCCESS;
}
export const POSTSTATUS_ORDER_UPDATE_FAILURE = 'POSTSTATUS_ORDER_UPDATE_FAILURE';
interface PostStatusOrderUpdateFailureAction {
type: typeof POSTSTATUS_ORDER_UPDATE_FAILURE;
error: string;
}
export type PostStatusOrderUpdateActionTypes =
PostStatusOrderUpdateStartAction |
PostStatusOrderUpdateSuccessAction |
PostStatusOrderUpdateFailureAction;
const postStatusOrderUpdateStart = (
newOrder: Array<IPostStatus>
): PostStatusOrderUpdateStartAction => ({
type: POSTSTATUS_ORDER_UPDATE_START,
newOrder,
});
const postStatusOrderUpdateSuccess = (): PostStatusOrderUpdateSuccessAction => ({
type: POSTSTATUS_ORDER_UPDATE_SUCCESS,
});
const postStatusOrderUpdateFailure = (
error: string
): PostStatusOrderUpdateFailureAction => ({
type: POSTSTATUS_ORDER_UPDATE_FAILURE,
error,
});
export const updatePostStatusOrder = (
id: number,
postStatuses: Array<IPostStatus>,
sourceIndex: number,
destinationIndex: number,
authenticityToken: string,
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
let newOrder = createNewOrder(postStatuses, sourceIndex, destinationIndex);
dispatch(postStatusOrderUpdateStart(newOrder));
try {
const res = await fetch(`/post_statuses/update_order`, {
method: 'PATCH',
headers: buildRequestHeaders(authenticityToken),
body: JSON.stringify({
post_status: {
id: id,
src_index: sourceIndex,
dst_index: destinationIndex,
},
}),
});
const json = await res.json();
if (res.status === HttpStatus.OK) {
dispatch(postStatusOrderUpdateSuccess());
} else {
dispatch(postStatusOrderUpdateFailure(json.error));
}
} catch (e) {
dispatch(postStatusOrderUpdateFailure(e));
}
};
function createNewOrder(
oldOrder: Array<IPostStatus>,
sourceIndex: number,
destinationIndex: number
) {
let newOrder = JSON.parse(JSON.stringify(oldOrder));
const [reorderedItem] = newOrder.splice(sourceIndex, 1);
newOrder.splice(destinationIndex, 0, reorderedItem);
return newOrder;
}