This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Custom postInvokes
Constantine Dergachev edited this page Feb 17, 2020
·
1 revision
To create handlers for your own postInvoke types you can use customEpic
mechanic.
// src/epics/view.ts of your application (or other module with epics if you organize them differently)
import {combineEpics} from 'redux-observable'
import {Observable} from 'rxjs/Observable'
import {coreActions, $do} from '@tesler-ui/core'
import {Epic} from '@tesler-ui/core/actions/actions'
import {OperationPostInvoke} from '@tesler-ui/core/interfaces/operation'
// Some new postInvoke with additional string array field
interface CustomPostInvoke extends OperationPostInvoke {
type: 'customPostInvoke',
customPayload: string[]
}
// Catch coreActions.processPostInvoke
const processPostInvoke: Epic = (action$, store) => action$.ofType(coreActions.processPostInvoke)
.mergeMap(action => {
switch (action.payload.postInvoke.type) {
// Catch specific type of your custom postInvoke
case 'customPostInvoke': {
// Cast postInvoke to have access to additional fields
const postInvoke = action.payload.postInvoke as CustomPostInvoke
// Here you can put your custom logic and call come other action
console.info(`Handled by client application: ${postInvoke.customPayload}`)
return Observable.of($do.logout(null))
}
default:
return Observable.empty()
}
})
// Export your postInvoke
export const viewEpics = combineEpics(
processPostInvoke
)
// src/epics/index.ts
import {combineEpics} from 'redux-observable'
import {viewEpics} from 'epics/view' // Import module created at Step One
export const epics = combineEpics(
viewEpics // Put it as an argument for combineEpics function
)
import React from 'react'
import {render} from 'react-dom'
import {Provider} from '@tesler-ui/core'
import Layout from 'components/AppLayout/AppLayout'
import {epics} from 'epics' // import your src/epics/index.ts...
// ...and pass it to customEpics property
const App = <Provider customEpics={epics}>
<Layout />
</Provider>
render(App, document.getElementById('root'))