import { call, put, takeEvery } from 'redux-saga/effects';
import trelloCreateBoard from 'api/trelloCreateBoard/trelloCreateBoard';
import { trelloCreateBoardActions } from 'store/trelloCreateBoardReducer';
type DataBoard = [{ id: string; name: string }];
function* fetchTrelloBoardCreate(action: any) {
try {
const data: DataBoard = yield call(trelloCreateBoard, action.payload); // тут создается доска
console.log(data); // remove
debugger; // remove
if (data !== undefined) {
// тут создается доска undefined ? почему? тут данные должны просто передаваться в стейт
yield put(trelloCreateBoardActions.createBoard(data));
return data;
}
throw new Error('Ошибка запроса.');
} catch (error) {
console.log('error: ', error);
}
return null;
}
function* createTrelloBoard() {
yield takeEvery(trelloCreateBoardActions.createBoard.type, fetchTrelloBoardCreate);
}
export default createTrelloBoard;
// создание доски
// yield call(trelloCreateBoard, action.payload);
import { createRequest, makeRequest } from 'shared/helpers/index';
import { apiKey } from 'shared/constants';
const APIToken = 'выафвафыв';
const idOrganization = 'выапапыав';
function trelloCreateBoard(action: any) {
return makeRequest(
createRequest(`https://api.trello.com/1/boards/`, {
name: action.boardName,
desc: action.description,
prefs_background: `${action.color === 'skyblue' ? 'sky' : action.color}`,
key: apiKey,
token: APIToken,
idOrganization,
}),
{ method: 'POST' }
);
}
export default trelloCreateBoard;
// почему тут создается доска undefined ??
// на строке yield put(trelloCreateBoardActions.createBoard(data)); ??
import { createSlice } from '@reduxjs/toolkit';
type State = {
id: string;
shortUrl: string;
isBoardCreated: boolean;
};
const initialState: State = {
id: '',
shortUrl: '',
isBoardCreated: false,
};
const trelloCreateBoard = createSlice({
name: 'trelloCreateBoard',
initialState,
reducers: {
createBoard(state, action) {
state.id = action.payload.id;
state.shortUrl = action.payload.shortUrl;
state.isBoardCreated = true;
},
},
});
const trelloCreateBoardReducer = trelloCreateBoard.reducer;
const trelloCreateBoardActions = trelloCreateBoard.actions;
export default trelloCreateBoardReducer;
export { trelloCreateBoardActions };