Skip to content

Instantly share code, notes, and snippets.

@mkoppanen
Created August 22, 2017 08:32
Show Gist options
  • Save mkoppanen/3199794a9ce653e5a9f238ec3d6b970f to your computer and use it in GitHub Desktop.
Save mkoppanen/3199794a9ce653e5a9f238ec3d6b970f to your computer and use it in GitHub Desktop.
import "whatwg-fetch";
import fromJSON from 'tcomb/lib/fromJSON'
import { combineReducers, bindActionCreators } from 'redux'
import reduxApi from 'redux-api'
import {
async,
transformers
} from 'redux-api'
import adapterFetch from 'store/rest/adapters/fetch'
import {
selectOrganisationId
} from 'store/selectors'
import {
showSuccessMessage
} from 'store/statusMessage'
import {
FormDTO,
} from 'entities/'
const rest = reduxApi({
all: {
url : '/organisation/:organisationId/form',
transformer(data) {
if (typeof data == 'undefined') {
return null;
}
return fromJSON(o, FormDTO)
},
helpers : {
exec() {
const args = {
organisationId : selectOrganisationId(this.getState()),
}
const params = {
method : 'GET'
};
return [
args,
params
]
}
},
postfetch : [
({ actions, request : { params }, dispatch}) => {
}
]
},
create: {
url : '/organisation/:organisationId/form',
transformer(data) {
if (typeof data == 'undefined') {
return null;
}
return fromJSON(o, FormDTO)
},
helpers : {
exec( model) {
const args = {
organisationId : selectOrganisationId(this.getState()),
}
const params = {
body : JSON.stringify(model),
method : 'POST'
};
return [
args,
params
]
}
},
postfetch : [
({ actions, request : { params }, dispatch}) => {
dispatch(showSuccessMessage('Form Created'))
dispatch(actions.all.exec())
}
]
},
update: {
url : '/organisation/:organisationId/form/:id',
transformer(data) {
if (typeof data == 'undefined') {
return null;
}
return fromJSON(o, FormDTO)
},
helpers : {
exec(id, model) {
const args = {
organisationId : selectOrganisationId(this.getState()),
id
}
const params = {
body : JSON.stringify(model),
method : 'PATCH'
};
return [
args,
params
]
}
},
postfetch : [
({ actions, request : { params }, dispatch}) => {
dispatch(showSuccessMessage('Form Updated'))
dispatch(actions.all.exec())
}
]
},
delete: {
url : '/organisation/:organisationId/form/:id',
transformer(data) {
if (typeof data == 'undefined') {
return null;
}
return fromJSON(o, FormDTO)
},
helpers : {
exec(id) {
const args = {
organisationId : selectOrganisationId(this.getState()),
id
}
const params = {
method : 'DELETE'
};
return [
args,
params
]
}
},
postfetch : [
({ actions, request : { params }, dispatch}) => {
dispatch(showSuccessMessage('Form Deleted'))
dispatch(actions.all.exec())
}
]
},
}, {
prefix : 'rest_form'
})
.use("rootUrl", YES_CONFIG.api.base_url)
.use("fetch", adapterFetch(fetch))
.use("options", (url, params, getState) => {
const { idToken } = getState().auth
const headers = {
"Accept": "application/json",
"Content-Type": "application/json",
};
if (idToken) {
return { headers: { Authorization: `Bearer ${idToken}` } };
}
return { headers };
});
const allAlways = (store, ignore = false) => {
const { dispatch } = store
return (
async(dispatch,
(cb) => rest.actions.all.exec( cb)
)
);
}
const allIfNeeded = (store, force = false) => {
const { dispatch, getState } = store
const slice = getState().rest_form.all
if (!force && slice.loaded) {
return Promise.resolve(slice.data)
}
return all(store, force)
}
const api = {
all : rest.actions.all.exec,
create : rest.actions.create.exec,
update : rest.actions.update.exec,
delete : rest.actions.delete.exec,
}
const mapApiToDispatch = (dispatch) => bindActionCreators(api, dispatch)
const loaders = {
all : allAlways,
allIfNeeded : allIfNeeded,
}
const selectors = {
selectAll : (state) => state.rest_form.all.data || [],
selectById : (state, id) => (state.rest_form.all.data || []).find(o => o.id == id),
isLoadingAll : (state) => state.rest_form.all.loading || false,
errorAll : (state) => (state.rest_form.all.error || {}).message,
isLoadingCreate : (state) => state.rest_form.create.loading || false,
errorCreate : (state) => (state.rest_form.create.error || {}).message,
isLoadingUpdate : (state) => state.rest_form.update.loading || false,
errorUpdate : (state) => (state.rest_form.update.error || {}).message,
isLoadingDelete : (state) => state.rest_form.delete.loading || false,
errorDelete : (state) => (state.rest_form.delete.error || {}).message,
}
const formExports = {
api,
mapApiToDispatch,
loaders,
selectors,
reducer : {
rest_form : combineReducers(rest.reducers)
},
}
export { formExports }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment