Skip to content

Instantly share code, notes, and snippets.

View ger86's full-sized avatar
๐Ÿ 
Working from home

Gerardo Fernรกndez Moreno ger86

๐Ÿ 
Working from home
View GitHub Profile
const requestStartedAction = (id, cancelTokenSource) => ({
type: API_REQUEST_STARTED,
payload: { id, cancelTokenSource }
});
const requestSucceededAction = id => ({
type: API_REQUEST_SUCCEEDED,
payload: { id }
});
{
"getPosts": {
"cancelSource": null,
"cancelled": false,
"error": false,
"loading": false
},
"getCategories": {
"cancelSource": null,
"cancelled": false,
export default function(state = {}, action) {
switch (action.type) {
case API_REQUEST_STARTED:
return {
...state,
[action.payload.id]: {
loading: true,
cancelTokenSource: action.payload.cancelTokenSource
}
};
export const cancelRequestThunk = id => async (
dispatch,
getState
) => {
const apiRequest = getState().apiRequests[id];
if (apiRequest && apiRequest.loading) {
apiRequest.cancelTokenSource.cancel('cancelled');
}
dispatch(cancelRequestAction(id));
};
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
});
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}`,
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
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) {
export const requestIds = {
GET_POSTS: 'GET_POSTS'
};
export const getPosts = query => ({
requestParams: {
method: 'get',
url: `/posts`,
params: query
},
<?php
class ContactFormType extends AbstractType {
/**
* @inheritdoc
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name', TextType::class, ['label' => 'Nombre'])