@codetanzania/emis-api-states

0.12.1 • Public • Published

EMIS API States

Build Status Commitizen friendly Renovate enabled

Simplify API calls and state management on top of redux and others.

Installation

Using npm

npm install @codetanzania/emis-api-states

Using yarn

yarn add @codetanzania/emis-api-states

Usage

All actions exposed by emis-api-states are wrapped with dispatch function, so the is no need to dispatch them again just invoke them.

The following is the list of all resources exposed by this library.

  • activity

  • adjustment

  • agency

  • alert

  • alertSource

  • assessment

  • district

  • feature

  • focalPerson

  • incident

  • incidentType

  • indicator

  • item

  • itemCategory

  • itemUnit

  • plan

  • procedure

  • question

  • questionnaire

  • region

  • resource

  • role

  • stock

  • warehouse

    For each resource you can get it's exposed actions as follows;

    Replace activities/activity with the name of the module you want

import {
  clearActivityFilters,
  clearActivitiesSort,
  closeActivityForm,
  deleteActivity,
  filterActivities,
  getActivities,
  getActivity,
  selectActivity,
  openActivityForm,
  paginateActivities,
  postActivity,
  putActivity,
  refreshActivities,
  searchActivities,
  sortActivities,
} from '@codetanzania/emis-api-states';

Store Structure

The structure of the store for each resource is

const store = {
  activities: {
    list: [],
    selected: null,
    page: 1,
    total: 0,
    size: 0,
    pages: 1,
    loading: false,
    posting: false,
    showForm: false,
    schema: null,
    filter: null,
    sort: null,
    q: undefined
  },
  alerts: {
    list: [],
    selected: null,
    page: 1,
    total: 0,
    size: 0,
    pages: 1,
    loading: false,
    posting: false,
    showForm: false,
    schema: null,
    filter: null,
    sort: null,
    q: undefined
  },
  ...
};

StoreProvider

This is a wrapper around react-redux Provider component. You can use it as follows

import { render } from 'react-dom';
import { StoreProvider } from '@codetanzania/emis-api-states';

// store provider
render(
  <StoreProvider>
    <App />
  </StoreProvider>,
  document.getElementById('root')
);

Connect

This is a wrapper around react-redux connect HOC with little improvement over it. You can use it as follows

import {Connect} from '@codetanzania/emis-api-states';

// for component
function ActivityList({ activities }){
  return(
    // jsx stuff
  );
}

// connect AlertList component to store
export Connect(AlertList, {
    activities: 'activities.list'
});

How to use exposed actions

Some of these actions accepts two callback functions which will be executed on Success and Error scenarios as shown below;

Fetch Data

import { getActivities, getActivity } from '@codetanzania/emis-api-states';

getActivities();

getActivity(activityId);

Create Data

import { postActivity } from '@codetanzania/emis-api-states';

postActivity(activity, onSuccess, onError);

Update Data

Note the activity here should have a valid _id property

import { putActivity } from '@codetanzania/emis-api-states';

putActivity(activity, onSuccess, onError);

Archive/Delete Data

import { deleteActivity } from '@codetanzania/emis-api-states';

deleteActivity(activityId, onSuccess, onError);

Searching

import { searchActivities } from '@codetanzania/emis-api-states';

searchActivities(searchQueryString);

Filtering

import {
  filterActivities,
  clearActivityFilters,
} from '@codetanzania/emis-api-states';

filterActivities({ plan: planId }, onSuccess, onError);

// clearing filters
clearActivityFilters(onSuccess, onError);

//  keep some filters from being cleared. This won't reset plan field in filter object
clearActivityFilters(onSuccess, onError, ['plan']);

Pagination

import { paginateActivities } from '@codetanzania/emis-api-state';

paginateActivity(pageNumber);

Sorting

import {
  sortActivities,
  clearActivitiesSort,
} from '@codetanzania/emis-api-state';

sortActivities({ name: 1 }, onSuccess, onError);

// clear sort

clearActivitiesSort(onSuccess, onError);

Note: This library depends on emis-api-client to work, so in order to specify API URL add .env file on your project root folder and specify your API URL under REACT_APP_EMIS_API_URL=[specify API BASE URL here]

LICENSE

MIT License

Copyright (c) 2018 - present Code Tanzania & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i @codetanzania/emis-api-states

Weekly Downloads

34

Version

0.12.1

License

MIT

Unpacked Size

252 kB

Total Files

39

Last publish

Collaborators

  • benmaruchu
  • lykmapipo