Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

action creators in redux

function doAddToDoItem(text) {
  return { type: 'TODO_ADDED', payload: text }
}
Comment

redux action creators

import * as ActionTypes from './ActionTypes';
import { baseUrl } from '../shared/baseUrl';

export const fetchComments = () => dispatch => {
    return fetch(baseUrl + 'comments')
        .then(response => {
                if (response.ok) {
                    return response;
                } else {
                    const error = new Error(`Error ${response.status}: ${response.statusText}`);
                    error.response = response;
                    throw error;
                }
            },
            error => {
                const errMess = new Error(error.message);
                throw errMess;
            })
        .then(response => response.json())
        .then(comments => dispatch(addComments(comments)))
        .catch(error => dispatch(commentsFailed(error.message)));
};

export const commentsFailed = errMess => ({
    type: ActionTypes.COMMENTS_FAILED,
    payload: errMess
});

export const addComments = comments => ({
    type: ActionTypes.ADD_COMMENTS,
    payload: comments
});

export const fetchCampsites = () => dispatch => {

    dispatch(campsitesLoading());

    return fetch(baseUrl + 'campsites')
        .then(response => {
                if (response.ok) {
                    return response;
                } else {
                    const error = new Error(`Error ${response.status}: ${response.statusText}`);
                    error.response = response;
                    throw error;
                }
            },
            error => {
                const errMess = new Error(error.message);
                throw errMess;
            })
        .then(response => response.json())
        .then(campsites => dispatch(addCampsites(campsites)))
        .catch(error => dispatch(campsitesFailed(error.message)));
};

export const campsitesLoading = () => ({
    type: ActionTypes.CAMPSITES_LOADING
});

export const campsitesFailed = errMess => ({
    type: ActionTypes.CAMPSITES_FAILED,
    payload: errMess
});

export const addCampsites = campsites => ({
    type: ActionTypes.ADD_CAMPSITES,
    payload: campsites
});

export const fetchPromotions = () => dispatch => {
    
    dispatch(promotionsLoading());

    return fetch(baseUrl + 'promotions')
        .then(response => {
                if (response.ok) {
                    return response;
                } else {
                    const error = new Error(`Error ${response.status}: ${response.statusText}`);
                    error.response = response;
                    throw error;
                }
            },
            error => {
                const errMess = new Error(error.message);
                throw errMess;
            })
        .then(response => response.json())
        .then(promotions => dispatch(addPromotions(promotions)))
        .catch(error => dispatch(promotionsFailed(error.message)));
};

export const promotionsLoading = () => ({
    type: ActionTypes.PROMOTIONS_LOADING
});

export const promotionsFailed = errMess => ({
    type: ActionTypes.PROMOTIONS_FAILED,
    payload: errMess
});

export const addPromotions = promotions => ({
    type: ActionTypes.ADD_PROMOTIONS,
    payload: promotions
});

export const fetchPartners = () => dispatch => {
    
    dispatch(partnersLoading());

    return fetch(baseUrl + 'partners')
        .then(response => {
                if (response.ok) {
                    return response;
                } else {
                    const error = new Error(`Error ${response.status}: ${response.statusText}`);
                    error.response = response;
                    throw error;
                }
            },
            error => {
                const errMess = new Error(error.message);
                throw errMess;
            })
        .then(response => response.json())
        .then(partners => dispatch(addPartners(partners)))
        .catch(error => dispatch(partnersFailed(error.message)));
};

export const partnersLoading = () => ({
    type: ActionTypes.PARTNERS_LOADING
});

export const partnersFailed = errMess => ({
    type: ActionTypes.PARTNERS_FAILED,
    payload: errMess
});

export const addPartners = partners => ({
    type: ActionTypes.ADD_PARTNERS,
    payload: partners
});
Comment

action creators in redux

const addTodoAction = {
  type: 'todos/todoAdded',
  payload: 'Buy milk'
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: acced to unknown obkect key js 
Javascript :: leaftjs 
Javascript :: javascript keyup original src element 
Javascript :: typeorm class validation 
Javascript :: automatic color change 
Javascript :: on change jquery kendo switch 
Javascript :: react get padding 
Javascript :: response conditions 
Javascript :: after storing array array state is empty 
Javascript :: javascript function template 
Javascript :: eslint failed to load react 
Javascript :: Using toLocaleString() to Print JavaScript Number Format with Commas 
Javascript :: Example to adds two colour palettes to the sidebar in wordpress 
Javascript :: Enable Cookies and JavaScript in Internet Explorer 9.0 
Javascript :: add script tag change on every new page in angular 8 
Javascript :: javascript coding test interview 
Javascript :: Nested Data Structures 
Javascript :: javascript dropdown options auto-updating 
Javascript :: keep form values after submit javascript 
Javascript :: refactor from data object 
Javascript :: convert base64 to image javascript 
Javascript :: how to add edit and delete rows of a html table with javascript 
Javascript :: mouselight js 
Javascript :: angular view not changing on model 
Javascript :: javascript compare dates old new value 
Javascript :: how insert variable dotenv password mangodb 
Javascript :: Spread Syntax for array literals or strings 
Javascript :: gsheet get cell background color 
Javascript :: how to check a user is using wifi or cellular data in php 
Javascript :: ubicar escrol en el final jquey 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =