mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 19:27:52 +01:00
Add basic version of post show page
This commit is contained in:
42
app/javascript/actions/changePostStatus.ts
Normal file
42
app/javascript/actions/changePostStatus.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
import { State } from '../reducers/rootReducer';
|
||||
|
||||
export const CHANGE_POST_STATUS_SUCCESS = 'CHANGE_POST_STATUS_SUCCESS';
|
||||
interface ChangePostStatusSuccessAction {
|
||||
type: typeof CHANGE_POST_STATUS_SUCCESS;
|
||||
newPostStatusId;
|
||||
}
|
||||
|
||||
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: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': authenticityToken,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
post: {
|
||||
post_status_id: newPostStatusId,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
if (response.status === 204) {
|
||||
dispatch(changePostStatusSuccess(newPostStatusId));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
57
app/javascript/actions/requestPost.ts
Normal file
57
app/javascript/actions/requestPost.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
|
||||
import IPostJSON from '../interfaces/json/IPost';
|
||||
|
||||
import { State } from '../reducers/rootReducer';
|
||||
|
||||
export const POST_REQUEST_START = 'POST_REQUEST_START';
|
||||
interface PostRequestStartAction {
|
||||
type: typeof POST_REQUEST_START;
|
||||
}
|
||||
|
||||
export const POST_REQUEST_SUCCESS = 'POST_REQUEST_SUCCESS';
|
||||
interface PostRequestSuccessAction {
|
||||
type: typeof POST_REQUEST_SUCCESS;
|
||||
post: IPostJSON;
|
||||
}
|
||||
|
||||
export const POST_REQUEST_FAILURE = 'POST_REQUEST_FAILURE';
|
||||
interface PostRequestFailureAction {
|
||||
type: typeof POST_REQUEST_FAILURE;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type PostRequestActionTypes =
|
||||
PostRequestStartAction |
|
||||
PostRequestSuccessAction |
|
||||
PostRequestFailureAction;
|
||||
|
||||
|
||||
const postRequestStart = (): PostRequestActionTypes => ({
|
||||
type: POST_REQUEST_START,
|
||||
});
|
||||
|
||||
export const postRequestSuccess = (post: IPostJSON): PostRequestActionTypes => ({
|
||||
type: POST_REQUEST_SUCCESS,
|
||||
post,
|
||||
});
|
||||
|
||||
const postRequestFailure = (error: string): PostRequestActionTypes => ({
|
||||
type: POST_REQUEST_FAILURE,
|
||||
error,
|
||||
});
|
||||
|
||||
export const requestPost = (
|
||||
postId: number,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
dispatch(postRequestStart());
|
||||
|
||||
try {
|
||||
const response = await fetch(`/posts/${postId}.json`);
|
||||
const json = await response.json();
|
||||
dispatch(postRequestSuccess(json));
|
||||
} catch (e) {
|
||||
dispatch(postRequestFailure(e));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user