-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
59862ad
commit 35d1124
Showing
12 changed files
with
167 additions
and
2 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
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,8 @@ | ||
export { | ||
actions as playlistUpdatesActions, | ||
default as playlistUpdatesReducer | ||
} from './playlistUpdatesSlice' | ||
|
||
export * as playlistUpdatesSelectors from './playlistUpdatesSelectors' | ||
export { default as playlistUpdatesSagas } from './playlistUpdatesSagas' | ||
export * from './types' |
50 changes: 50 additions & 0 deletions
50
packages/common/src/store/playlist-updates/playlistUpdatesSagas.ts
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,50 @@ | ||
import { call, takeEvery, select, put } from 'typed-redux-saga' | ||
|
||
import { getContext } from 'store/effects' | ||
|
||
import { getUserId } from '../account/selectors' | ||
|
||
import { | ||
fetchPlaylistUpdates, | ||
playlistUpdatesReceived, | ||
updatedPlaylistViewed | ||
} from './playlistUpdatesSlice' | ||
import { UpdatedPlaylistViewedAction } from './types' | ||
|
||
function* watchFetchPlaylistUpdates() { | ||
yield* takeEvery( | ||
fetchPlaylistUpdates, | ||
function* fetchPlaylistUpdatesWorker() { | ||
const currentUserId = yield* select(getUserId) | ||
if (!currentUserId) return | ||
|
||
const apiClient = yield* getContext('apiClient') | ||
|
||
const playlistUpdates = yield* call( | ||
[apiClient, apiClient.getPlaylistUpdates], | ||
currentUserId | ||
) | ||
|
||
if (playlistUpdates && playlistUpdates.length > 0) { | ||
yield* put(playlistUpdatesReceived({ playlistUpdates })) | ||
} | ||
} | ||
) | ||
} | ||
|
||
function* watchUpdatedPlaylistViewedSaga() { | ||
yield* takeEvery( | ||
updatedPlaylistViewed.type, | ||
function* updatePlaylistLastViewedAt(action: UpdatedPlaylistViewedAction) { | ||
const audiusBackendInstance = yield* getContext('audiusBackendInstance') | ||
yield* call( | ||
audiusBackendInstance.updatePlaylistLastViewedAt, | ||
action.payload.playlistId | ||
) | ||
} | ||
) | ||
} | ||
|
||
export default function sagas() { | ||
return [watchFetchPlaylistUpdates, watchUpdatedPlaylistViewedSaga] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/common/src/store/playlist-updates/playlistUpdatesSelectors.ts
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,4 @@ | ||
import { playlistUpdatesEntityAdapter } from './playlistUpdatesSlice' | ||
|
||
export const { selectById: selectPlaylistUpdateById } = | ||
playlistUpdatesEntityAdapter.getSelectors() |
40 changes: 40 additions & 0 deletions
40
packages/common/src/store/playlist-updates/playlistUpdatesSlice.ts
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,40 @@ | ||
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit' | ||
|
||
import { | ||
PlaylistUpdate, | ||
PlaylistUpdatesReceivedAction, | ||
PlaylistUpdateState, | ||
UpdatedPlaylistViewedAction | ||
} from './types' | ||
|
||
export const playlistUpdatesEntityAdapter = createEntityAdapter<PlaylistUpdate>( | ||
{ | ||
selectId: (playlistUpdate) => playlistUpdate.playlist_id | ||
} | ||
) | ||
|
||
const initialState: PlaylistUpdateState = | ||
playlistUpdatesEntityAdapter.getInitialState() | ||
|
||
const slice = createSlice({ | ||
name: 'playlist-updates', | ||
initialState, | ||
reducers: { | ||
fetchPlaylistUpdates() {}, | ||
playlistUpdatesReceived(state, action: PlaylistUpdatesReceivedAction) { | ||
playlistUpdatesEntityAdapter.setAll(state, action.payload.playlistUpdates) | ||
}, | ||
updatedPlaylistViewed(state, action: UpdatedPlaylistViewedAction) { | ||
playlistUpdatesEntityAdapter.removeOne(state, action.payload.playlistId) | ||
} | ||
} | ||
}) | ||
|
||
export const { | ||
fetchPlaylistUpdates, | ||
playlistUpdatesReceived, | ||
updatedPlaylistViewed | ||
} = slice.actions | ||
|
||
export const { actions } = slice | ||
export default slice.reducer |
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,19 @@ | ||
import { EntityState, PayloadAction } from '@reduxjs/toolkit' | ||
|
||
import { ID } from 'models/Identifiers' | ||
|
||
export type PlaylistUpdate = { | ||
playlist_id: number | ||
updated_at: string | ||
last_seen_at: string | ||
} | ||
|
||
export type PlaylistUpdateState = EntityState<PlaylistUpdate> | ||
|
||
export type PlaylistUpdatesReceivedAction = PayloadAction<{ | ||
playlistUpdates: PlaylistUpdate[] | ||
}> | ||
|
||
export type UpdatedPlaylistViewedAction = PayloadAction<{ | ||
playlistId: ID | ||
}> |
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,5 @@ | ||
import { watchUpdatedPlaylistViewedSaga } from './updatedPlaylistViewedSaga' | ||
|
||
export default function sagas() { | ||
return [watchUpdatedPlaylistViewedSaga()] | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/web/src/common/store/playlist-updates/updatedPlaylistViewedSaga.ts
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,20 @@ | ||
import { | ||
getContext, | ||
playlistUpdatesActions, | ||
UpdatedPlaylistViewedAction | ||
} from '@audius/common' | ||
import { call, takeEvery } from 'typed-redux-saga' | ||
|
||
const { updatedPlaylistViewed } = playlistUpdatesActions | ||
|
||
export function* watchUpdatedPlaylistViewedSaga() { | ||
yield* takeEvery(updatedPlaylistViewed.type, updatePlaylistLastViewedAt) | ||
} | ||
|
||
function* updatePlaylistLastViewedAt(action: UpdatedPlaylistViewedAction) { | ||
const audiusBackendInstance = yield* getContext('audiusBackendInstance') | ||
yield* call( | ||
audiusBackendInstance.updatePlaylistLastViewedAt, | ||
action.payload.playlistId | ||
) | ||
} |
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