mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 03:37:56 +01:00
Group actions into folders
This commit is contained in:
40
app/javascript/actions/Post/changePostBoard.ts
Normal file
40
app/javascript/actions/Post/changePostBoard.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
import { State } from '../../reducers/rootReducer';
|
||||
|
||||
import buildRequestHeaders from '../../helpers/buildRequestHeaders';
|
||||
|
||||
export const CHANGE_POST_BOARD_SUCCESS = 'CHANGE_POST_BOARD_SUCCESS';
|
||||
export interface ChangePostBoardSuccessAction {
|
||||
type: typeof CHANGE_POST_BOARD_SUCCESS;
|
||||
newBoardId: number;
|
||||
}
|
||||
|
||||
const changePostBoardSuccess = (newBoardId: number): ChangePostBoardSuccessAction => ({
|
||||
type: CHANGE_POST_BOARD_SUCCESS,
|
||||
newBoardId,
|
||||
});
|
||||
|
||||
export const changePostBoard = (
|
||||
postId: number,
|
||||
newBoardId: number,
|
||||
authenticityToken: string,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
try {
|
||||
const response = await fetch(`/posts/${postId}`, {
|
||||
method: 'PATCH',
|
||||
headers: buildRequestHeaders(authenticityToken),
|
||||
body: JSON.stringify({
|
||||
post: {
|
||||
board_id: newBoardId,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
if (response.status === 204) {
|
||||
dispatch(changePostBoardSuccess(newBoardId));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
40
app/javascript/actions/Post/changePostStatus.ts
Normal file
40
app/javascript/actions/Post/changePostStatus.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
import { State } from '../../reducers/rootReducer';
|
||||
|
||||
import buildRequestHeaders from '../../helpers/buildRequestHeaders';
|
||||
|
||||
export const CHANGE_POST_STATUS_SUCCESS = 'CHANGE_POST_STATUS_SUCCESS';
|
||||
export interface ChangePostStatusSuccessAction {
|
||||
type: typeof CHANGE_POST_STATUS_SUCCESS;
|
||||
newPostStatusId: number;
|
||||
}
|
||||
|
||||
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: buildRequestHeaders(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/Post/requestPost.ts
Normal file
57
app/javascript/actions/Post/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));
|
||||
}
|
||||
}
|
||||
68
app/javascript/actions/Post/requestPosts.ts
Normal file
68
app/javascript/actions/Post/requestPosts.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Action } from 'redux';
|
||||
import { ThunkAction } from 'redux-thunk';
|
||||
|
||||
import IPostJSON from '../../interfaces/json/IPost';
|
||||
|
||||
import { State } from '../../reducers/rootReducer';
|
||||
|
||||
export const POSTS_REQUEST_START = 'POSTS_REQUEST_START';
|
||||
interface PostsRequestStartAction {
|
||||
type: typeof POSTS_REQUEST_START;
|
||||
}
|
||||
|
||||
export const POSTS_REQUEST_SUCCESS = 'POSTS_REQUEST_SUCCESS';
|
||||
interface PostsRequestSuccessAction {
|
||||
type: typeof POSTS_REQUEST_SUCCESS;
|
||||
posts: Array<IPostJSON>;
|
||||
page: number;
|
||||
}
|
||||
|
||||
export const POSTS_REQUEST_FAILURE = 'POSTS_REQUEST_FAILURE';
|
||||
interface PostsRequestFailureAction {
|
||||
type: typeof POSTS_REQUEST_FAILURE;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type PostsRequestActionTypes =
|
||||
PostsRequestStartAction |
|
||||
PostsRequestSuccessAction |
|
||||
PostsRequestFailureAction;
|
||||
|
||||
|
||||
const postsRequestStart = (): PostsRequestActionTypes => ({
|
||||
type: POSTS_REQUEST_START,
|
||||
});
|
||||
|
||||
const postsRequestSuccess = (posts: Array<IPostJSON>, page: number): PostsRequestActionTypes => ({
|
||||
type: POSTS_REQUEST_SUCCESS,
|
||||
posts,
|
||||
page,
|
||||
});
|
||||
|
||||
const postsRequestFailure = (error: string): PostsRequestActionTypes => ({
|
||||
type: POSTS_REQUEST_FAILURE,
|
||||
error,
|
||||
});
|
||||
|
||||
export const requestPosts = (
|
||||
boardId: number,
|
||||
page: number,
|
||||
searchQuery: string,
|
||||
postStatusId: number,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
dispatch(postsRequestStart());
|
||||
|
||||
try {
|
||||
let params = '';
|
||||
params += `page=${page}`;
|
||||
params += `&board_id=${boardId}`;
|
||||
if (searchQuery) params += `&search=${searchQuery}`;
|
||||
if (postStatusId) params += `&post_status_id=${postStatusId}`;
|
||||
|
||||
const response = await fetch(`/posts?${params}`);
|
||||
const json = await response.json();
|
||||
dispatch(postsRequestSuccess(json, page));
|
||||
} catch (e) {
|
||||
dispatch(postsRequestFailure(e));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user