This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const requestStartedAction = (id, cancelTokenSource) => ({ | |
type: API_REQUEST_STARTED, | |
payload: { id, cancelTokenSource } | |
}); | |
const requestSucceededAction = id => ({ | |
type: API_REQUEST_SUCCEEDED, | |
payload: { id } | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"getPosts": { | |
"cancelSource": null, | |
"cancelled": false, | |
"error": false, | |
"loading": false | |
}, | |
"getCategories": { | |
"cancelSource": null, | |
"cancelled": false, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function(state = {}, action) { | |
switch (action.type) { | |
case API_REQUEST_STARTED: | |
return { | |
...state, | |
[action.payload.id]: { | |
loading: true, | |
cancelTokenSource: action.payload.cancelTokenSource | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const cancelRequestThunk = id => async ( | |
dispatch, | |
getState | |
) => { | |
const apiRequest = getState().apiRequests[id]; | |
if (apiRequest && apiRequest.loading) { | |
apiRequest.cancelTokenSource.cancel('cancelled'); | |
} | |
dispatch(cancelRequestAction(id)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const createRequest = ( | |
{ requestId, requestParams } | |
) => async (dispatch, getState) => { | |
const cancelTokenSource = request.getCancelTokenSource(); | |
dispatch(requestStartedAction(requestId, cancelTokenSource)); | |
try { | |
const res = await request.send({ | |
...requestParams, | |
cancelToken: cancelTokenSource.token | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import { BACKEND } from 'src/consts'; | |
function Request() { | |
this.client = null; | |
} | |
Request.prototype.init = function init() { | |
this.client = axios.create({ | |
baseURL: `${BACKEND.API_URL}`, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const createRequest = ( | |
{ requestId, requestParams }, | |
needsAuthentication = false | |
) => async (dispatch, getState) => { | |
const cancelTokenSource = request.getCancelTokenSource(); | |
dispatch(requestStartedAction(requestId, cancelTokenSource)); | |
try { | |
const res = await request.send({ | |
...requestParams, | |
cancelToken: cancelTokenSource.token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getPostsThunk(params) { | |
return async dispatch => { | |
try { | |
const res = await dispatch(createRequest(getPosts(params))); | |
const posts = res.data; | |
dispatch({ | |
type: GET_POSTS_SUCCEEDED, | |
payload: treasures | |
}); | |
} catch (error) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const requestIds = { | |
GET_POSTS: 'GET_POSTS' | |
}; | |
export const getPosts = query => ({ | |
requestParams: { | |
method: 'get', | |
url: `/posts`, | |
params: query | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ContactFormType extends AbstractType { | |
/** | |
* @inheritdoc | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) { | |
$builder | |
->add('name', TextType::class, ['label' => 'Nombre']) |