How to use the redux-tools.createThunkAction function in redux-tools

To help you get started, we’ve selected a few redux-tools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github resource-watch / resource-watch / components / modal / subscriptions-modal / actions.js View on Github external
.then((userAreas = []) => {
        dispatch(setUserAreas(userAreas));
        dispatch(setUserAreasLoading(false));
      })
      .catch((err) => {
        dispatch(setUserAreasError(err));
        toastr.error('Error loading user areas', err);
      });
  });

// actions – datasets
export const setDatasets = createAction('SUBSCRIPTIONS__SET-DATASETS');
export const setDatasetsLoading = createAction('SUBSCRIPTIONS__SET-DATASETS-LOADING');
export const setDatasetsError = createAction('SUBSCRIPTIONS__SET-DATASETS-ERROR');

export const getDatasets = createThunkAction('SUBSCRIPTIONS__GET-DATASETS', () =>
  (dispatch, getState) => {
    const { common } = getState();
    const { locale } = common;
    const datasetService = new DatasetService(null, {
      apiURL: process.env.WRI_API_URL,
      language: locale
    });

    dispatch(setDatasetsLoading(true));

    datasetService.getSubscribableDatasets('metadata')
      .then((datasets = []) => {
        const parsedDatasets = WRISerializer({ data: datasets });
        dispatch(setDatasets(parsedDatasets));
        dispatch(setDatasetsLoading(false));
      })
github resource-watch / resource-watch / layout / widget-detail / widget-detail-actions.js View on Github external
import 'isomorphic-fetch';
import { createAction, createThunkAction } from 'redux-tools';

// Actions
export const setWidget = createAction('WIDGET-DETAIL/setWidget');
export const setWidgetLoading = createAction('WIDGET-DETAIL/setWidgetLoading');
export const setWidgetError = createAction('WIDGET-DETAIL/setWidgetError');

// Async actions
export const fetchWidget = createThunkAction('WIDGET-DETAIL/fetchWidget', (payload = {}) => (dispatch) => {
  dispatch(setWidgetLoading(true));
  dispatch(setWidgetError(null));

  return fetch(new Request(`${process.env.WRI_API_URL}/widget/${payload.id}`))
    .then((response) => {
      if (response.ok) return response.json();
      throw new Error(response.statusText);
    })
    .then(({ data }) => {
      dispatch(setWidgetLoading(false));
      dispatch(setWidgetError(null));
      dispatch(setWidget({ id: data.id, ...data.attributes }));
    })
    .catch((err) => {
      dispatch(setWidgetLoading(false));
      dispatch(setWidgetError(err));
github Vizzuality / gfw / app / javascript / app / providers / datasets-provider / datasets-provider-actions.js View on Github external
fetchGLADLatest,
  fetchFormaLatest,
  fetchTerraLatest,
  fetchSADLatest,
  fetchGranChacoLatest
} from 'services/alerts';

import thresholdOptions from 'data/thresholds.json';

import { reduceParams, reduceSqlParams } from './datasets-utils';
import decodeLayersConfig from './datasets-decode-config';

export const setDatasetsLoading = createAction('setDatasetsLoading');
export const setDatasets = createAction('setDatasets');

export const getDatasets = createThunkAction(
  'getDatasets',
  () => (dispatch, getState) => {
    const state = getState();
    if (!state.datasets.datasets.length) {
      dispatch(setDatasetsLoading({ loading: true, error: false }));
      axios
        .all([
          getDatasetsProvider(),
          fetchGLADLatest(),
          fetchFormaLatest(),
          fetchTerraLatest(),
          fetchSADLatest(),
          fetchGranChacoLatest()
        ])
        .then(
          axios.spread((allDatasets, glads, forma, terra, sad, granChaco) => {
github Vizzuality / gfw / app / javascript / providers / datasets-provider / datasets-provider-actions.js View on Github external
import wriAPISerializer from 'wri-json-api-serializer';
import flatten from 'lodash/flatten';
import sortBy from 'lodash/sortBy';
import chroma from 'chroma-js';

import { getDatasetsProvider } from 'services/datasets';
import thresholdOptions from 'data/thresholds.json';

import { reduceParams, reduceSqlParams } from './datasets-utils';
import decodeLayersConfig from './datasets-decode-config';
import decodeLayersClusters from './datasets-decode-clusters';

export const setDatasetsLoading = createAction('setDatasetsLoading');
export const setDatasets = createAction('setDatasets');

export const getDatasets = createThunkAction('getDatasets', () => dispatch => {
  getDatasetsProvider()
    .then(allDatasets => {
      const parsedDatasets = wriAPISerializer(allDatasets.data)
        .filter(
          d =>
            d.published &&
            d.layer.length &&
            (d.env === 'production' || d.env === process.env.FEATURE_ENV)
        )
        .map(d => {
          const { layer, metadata } = d;
          const appMeta =
            (metadata && metadata.find(m => m.application === 'gfw')) || {};
          const { info } = appMeta || {};
          const defaultLayer =
            (layer &&
github resource-watch / resource-watch / components / wysiwyg / widget-block / widget-block-actions.js View on Github external
visible: true,
          layers: [{
            active: true,
            id: data.id,
            ...data.attributes
          }]
        }]
      }));
    })
    .catch((err) => {
      dispatch(setLayersLoading({ id, value: false }));
      dispatch(setLayersError({ id, value: err }));
    });
});

export const fetchWidget = createThunkAction('WIDGET_BLOCK_FETCH_DATA', (payload = {}) => (dispatch) => {
  const id = `${payload.id}/${payload.itemId}`;

  dispatch(setWidgetLoading({ id, value: true }));
  dispatch(setWidgetError({ id, value: null }));

  fetch(`${process.env.WRI_API_URL}/widget/${payload.id}?&application=${process.env.APPLICATIONS}&includes=${payload.includes}`)
    .then((response) => {
      if (response.status >= 400) throw Error(response.statusText);
      return response.json();
    })
    .then(({ data }) => {
      const widget = { id: data.id, ...data.attributes };
      const { widgetConfig } = widget;

      dispatch(setWidgetLoading({ id, value: false }));
      dispatch(setWidgetError({ id, value: null }));

redux-tools

Redux Tools to make redux easy

MIT
Latest version published 6 years ago

Package Health Score

42 / 100
Full package analysis

Similar packages