This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifications): implement connection to new API
- Loading branch information
Showing
9 changed files
with
171 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createActions } from '../utils/redux' | ||
|
||
/* Actions */ | ||
|
||
// Notifications | ||
export const notifications = createActions('NOTIFICATIONS') | ||
|
||
// Notification | ||
export const notification = createActions('NOTIFICATION') | ||
|
||
/* Action Creators */ | ||
|
||
// Notifications | ||
export const fetchNotifications = () => ({ type: notifications.FETCH }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import PropTypes from 'prop-types' | ||
|
||
import * as notificationActions from '../actions/notification' | ||
import createReducer, { createResource } from '../utils/redux' | ||
|
||
// Shapes | ||
const { | ||
shape: notificationsShape, | ||
initialState: notificationsInitialState | ||
} = createResource(PropTypes.arrayOf(PropTypes.string)) | ||
export { notificationsShape } | ||
|
||
// Reducer | ||
export default createReducer( | ||
{ | ||
notifications: notificationsInitialState | ||
}, | ||
{ | ||
[notificationActions.notification.RECEIVE]: (state, action) => ({ | ||
...state, | ||
notifications: { | ||
...state.notifications, | ||
data: [...state.notifications.data, action.payload.notification] | ||
} | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { eventChannel } from 'redux-saga' | ||
|
||
import { | ||
fork, | ||
take, | ||
race, | ||
takeLatest, | ||
select, | ||
call, | ||
put | ||
} from 'redux-saga/effects' | ||
|
||
import * as notificationActions from '../actions/notification' | ||
import * as walletSelectors from '../reducers/wallet' | ||
import * as walletActions from '../actions/wallet' | ||
import { kleros, ARBITRATOR_ADDRESS } from '../bootstrap/dapp-api' | ||
import { action, errorAction } from '../utils/action' | ||
|
||
/** | ||
* Listens for push notifications. | ||
*/ | ||
export function* pushNotificationsListener() { | ||
// Start after fetching whole list of notifications | ||
while (yield take(notificationActions.notifications.FETCH)) { | ||
const account = yield select(walletSelectors.getAccount) // Current account | ||
|
||
// Set up event channel with subscriber | ||
const channel = eventChannel(emitter => { | ||
kleros.watchForEvents(ARBITRATOR_ADDRESS, account, notification => | ||
emitter(notification) | ||
) | ||
|
||
return kleros.eventListener.clearArbitratorHandlers // Unsubscribe function | ||
}) | ||
|
||
// Keep listening while on the same account | ||
while (account === (yield select(walletSelectors.getAccount))) { | ||
const [notification, accounts] = yield race([ | ||
take(channel), // New notifications | ||
take(walletActions.accounts.RECEIVE) // Accounts refetch | ||
]) | ||
if (accounts) continue // Possible account change | ||
|
||
// Put new notification | ||
yield put( | ||
action(notificationActions.notification.RECEIVE, { notification }) | ||
) | ||
} | ||
|
||
// We changed accounts, so close the channel. This calls unsubscribe under the hood which clears handlers for the old account | ||
channel.close() | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the current account's notifications. | ||
*/ | ||
export function* fetchNotifications() { | ||
try { | ||
const account = yield select(walletSelectors.getAccount) | ||
const notifications = yield call( | ||
kleros.notifications.getNotifications, | ||
account | ||
) | ||
|
||
yield put( | ||
action(notificationActions.notifications.RECEIVE, { notifications }) | ||
) | ||
} catch (err) { | ||
yield put(errorAction(notificationActions.notifications.FAIL_FETCH, err)) | ||
} | ||
} | ||
|
||
/** | ||
* The root of the notification saga. | ||
*/ | ||
export default function* notificationSaga() { | ||
// Listeners | ||
yield fork(pushNotificationsListener) | ||
|
||
// Notifications | ||
yield takeLatest(notificationActions.notifications.FETCH, fetchNotifications) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters