0.12.1 • Published 5 years ago

@codetanzania/emis-api-states v0.12.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

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.

0.12.1

5 years ago

0.12.0

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago