mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add Boards management to sitesettings (#107)
This commit is contained in:
committed by
GitHub
parent
7b8a4d6709
commit
6be2394dc5
80
app/javascript/actions/Board/updateBoard.ts
Normal file
80
app/javascript/actions/Board/updateBoard.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { Action } from "redux";
|
||||
import { ThunkAction } from "redux-thunk";
|
||||
|
||||
import HttpStatus from "../../constants/http_status";
|
||||
import buildRequestHeaders from "../../helpers/buildRequestHeaders";
|
||||
import IBoardJSON from "../../interfaces/json/IBoard";
|
||||
import { State } from "../../reducers/rootReducer";
|
||||
|
||||
export const BOARD_UPDATE_START = 'BOARD_UPDATE_START';
|
||||
interface BoardUpdateStartAction {
|
||||
type: typeof BOARD_UPDATE_START;
|
||||
}
|
||||
|
||||
export const BOARD_UPDATE_SUCCESS = 'BOARD_UPDATE_SUCCESS';
|
||||
interface BoardUpdateSuccessAction {
|
||||
type: typeof BOARD_UPDATE_SUCCESS;
|
||||
board: IBoardJSON;
|
||||
}
|
||||
|
||||
export const BOARD_UPDATE_FAILURE = 'BOARD_UPDATE_FAILURE';
|
||||
interface BoardUpdateFailureAction {
|
||||
type: typeof BOARD_UPDATE_FAILURE;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type BoardUpdateActionTypes =
|
||||
BoardUpdateStartAction |
|
||||
BoardUpdateSuccessAction |
|
||||
BoardUpdateFailureAction;
|
||||
|
||||
const boardUpdateStart = (): BoardUpdateStartAction => ({
|
||||
type: BOARD_UPDATE_START,
|
||||
});
|
||||
|
||||
const boardUpdateSuccess = (
|
||||
boardJSON: IBoardJSON,
|
||||
): BoardUpdateSuccessAction => ({
|
||||
type: BOARD_UPDATE_SUCCESS,
|
||||
board: boardJSON,
|
||||
});
|
||||
|
||||
const boardUpdateFailure = (error: string): BoardUpdateFailureAction => ({
|
||||
type: BOARD_UPDATE_FAILURE,
|
||||
error,
|
||||
});
|
||||
|
||||
export const updateBoard = (
|
||||
id: number,
|
||||
name: string,
|
||||
description: string,
|
||||
authenticityToken: string,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
dispatch(boardUpdateStart());
|
||||
|
||||
try {
|
||||
const res = await fetch(`/boards/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: buildRequestHeaders(authenticityToken),
|
||||
body: JSON.stringify({
|
||||
board: {
|
||||
name,
|
||||
description,
|
||||
},
|
||||
}),
|
||||
});
|
||||
const json = await res.json();
|
||||
|
||||
if (res.status === HttpStatus.OK) {
|
||||
dispatch(boardUpdateSuccess(json));
|
||||
} else {
|
||||
dispatch(boardUpdateFailure(json.error));
|
||||
}
|
||||
|
||||
return Promise.resolve(res);
|
||||
} catch (e) {
|
||||
dispatch(boardUpdateFailure(e));
|
||||
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user