|
2 | 2 |
|
3 | 3 | # redux-saga-rn-alert |
4 | 4 |
|
| 5 | +Ever wanted to use Alert.alert() with callbacks within a side-effect or generator function? This library help along! |
| 6 | + |
| 7 | +It allows us to show a typical alert-modal while we can pass callbacks |
| 8 | +for the user-action the redux-saga-way with `yield put()` or `yield put()`. |
| 9 | +Other side-effects like `fork` or `spawn` aren't implemented yet. Feel |
| 10 | +free to contribute if you need it! |
| 11 | + |
5 | 12 | # Installation |
6 | | -``` |
| 13 | + |
| 14 | +```bash |
7 | 15 | yarn add redux-saga-rn-alert |
8 | 16 | ``` |
| 17 | +or |
| 18 | +```bash |
| 19 | +npm install redux-saga-rn-alert --save |
| 20 | +``` |
| 21 | + |
| 22 | +# Setup |
| 23 | + |
| 24 | +In the root reducer add the alert-reducer. For instance: |
| 25 | + |
| 26 | +**reducer.js:** |
| 27 | + |
| 28 | +```javascript |
| 29 | +import { alertReducer } from 'redux-saga-rn-alert'; |
| 30 | + |
| 31 | +const appReducer = combineReducers({ |
| 32 | + appStates, |
| 33 | + routes, |
| 34 | + ... |
| 35 | + alertReducer |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +In the root saga spawn the channel watcher: |
| 40 | + |
| 41 | +**saga.js** |
| 42 | + |
| 43 | +```javascript |
| 44 | +export default function * rootSaga() { |
| 45 | + yield [ |
| 46 | + // ... all your sagas here |
| 47 | + spawn(watchAlertChannel), |
| 48 | + ]; |
| 49 | +} |
| 50 | +``` |
9 | 51 |
|
10 | 52 | # Usage |
11 | 53 |
|
| 54 | +The alert-function has the same signature as the official [alert-method](https://facebook.github.io/react-native/docs/alert.html#alert) |
| 55 | +of reacht-native. |
| 56 | + |
| 57 | +```javascript |
| 58 | +static alert(title, message?, buttons?, options?) {} |
| 59 | +``` |
| 60 | +
|
| 61 | +## Example 1 |
| 62 | +
|
| 63 | +Show an alert with 2 Buttons and put some actions: |
| 64 | +
|
| 65 | +```javascript |
| 66 | + const buttons = [ |
| 67 | + {text: 'Cancel', put: {type: ACTIONS.S_CANCEL_EDIT}}, |
| 68 | + {text: 'OK', call: RouterActions.pop}, |
| 69 | + ] |
| 70 | + |
| 71 | + yield call(alert, 'Error', 'Foobar message', buttons) |
| 72 | +``` |
| 73 | +
|
| 74 | +In this example an alert-box will be shown with two buttons "Cancel" |
| 75 | +and "ok". If the user taps on "cancel", a ```yield put()``` will be |
| 76 | +executed with a user-defined action, while "ok" raises a ```yield call```` |
| 77 | +to the pop-method of the Router from [react-native-router-flux] (https://github.com/itinance/react-native-router-flux). |
| 78 | + |
| 79 | +Instead of executing a "plain" function, you can also call a method passing |
| 80 | +arguments: |
| 81 | + |
| 82 | +## Example 2 |
| 83 | + |
| 84 | +```javascript |
| 85 | + const buttons = [ |
| 86 | + {text: 'Cancel', put: {type: ACTIONS.S_CANCEL_EDIT}}, |
| 87 | + {text: 'OK', call: {method: myMethod, args: {name: '', street: ''}}, |
| 88 | + ] |
| 89 | + |
| 90 | + yield call(alert, 'Error', 'Happy to call alert with callbacks within a generator function', buttons) |
| 91 | +``` |
| 92 | + |
| 93 | +## Example 3 |
| 94 | + |
| 95 | +It is also allowed to pass an array of several side effects: |
| 96 | + |
| 97 | +```javascript |
| 98 | + const buttons = [ |
| 99 | + {text: 'Cancel', actions: |
| 100 | + [ |
| 101 | + {put: {type: ACTIONS.S_CANCEL_EDIT}}, |
| 102 | + {call: RouterActions.pop}, |
| 103 | + ], |
| 104 | + }, |
| 105 | + {text: 'OK', call: RouterActions.pop}, |
| 106 | + ] |
| 107 | + |
| 108 | + yield call(alert, 'Error', 'Foobar message', buttons) |
| 109 | +``` |
| 110 | +
|
| 111 | +In this example, the callback-function of the Cancel-button will |
| 112 | +first "yield put" an action to our reducers and then call the pop()-method |
| 113 | +of the router. |
0 commit comments