Skip to content

Commit

Permalink
Revert "Add purchased + reposted tracks to library PAY-1633 (#3820)" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoback2 committed Jul 31, 2023
1 parent 8dc4c8a commit 6c88515
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 191 deletions.
5 changes: 5 additions & 0 deletions packages/web/src/common/store/pages/saved/lineups/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ function* getTracks({ offset, limit }) {
trackIds: allSavedTrackIds.filter((id) => id !== null)
})
const tracksMap = tracks.reduce((map, track) => {
// If the track hasn't confirmed save from the backend, pretend it is for the client.
if (!track.has_current_user_saved) {
track.has_current_user_saved = true
track.save_count += 1
}
track.dateSaved = allSavedTrackTimestamps[track.track_id]

map[track.track_id] = track
Expand Down
142 changes: 142 additions & 0 deletions packages/web/src/common/store/pages/saved/sagas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {
accountSelectors,
savedPageTracksLineupActions as tracksActions,
savedPageActions as actions,
savedPageSelectors,
waitForValue,
getContext
} from '@audius/common'
import { takeLatest, call, put, select, fork } from 'redux-saga/effects'

import { processAndCacheTracks } from 'common/store/cache/tracks/utils'
import { waitForRead } from 'utils/sagaHelpers'

import tracksSagas from './lineups/sagas'
const { getSaves } = savedPageSelectors
const { getAccountUser } = accountSelectors

function* fetchLineupMetadatas(offset, limit) {
const isNativeMobile = yield getContext('isNativeMobile')

// Mobile currently uses infinite scroll instead of a virtualized list
// so we need to apply the offset & limit
if (isNativeMobile) {
yield put(tracksActions.fetchLineupMetadatas(offset, limit))
} else {
yield put(tracksActions.fetchLineupMetadatas())
}
}

function* watchFetchSaves() {
let currentQuery = ''
let currentSortMethod = ''
let currentSortDirection = ''

yield takeLatest(actions.FETCH_SAVES, function* (props) {
yield waitForRead()
const apiClient = yield getContext('apiClient')
const account = yield call(waitForValue, getAccountUser)
const userId = account.user_id
const offset = props.offset ?? 0
const limit = props.limit ?? account.track_save_count
const query = props.query ?? ''
const sortMethod = props.sortMethod ?? ''
const sortDirection = props.sortDirection ?? ''
const saves = yield select(getSaves)

const isSameParams =
query === currentQuery &&
currentSortDirection === sortDirection &&
currentSortMethod === sortMethod

// Don't refetch saves in the same session
if (saves && saves.length && isSameParams) {
yield fork(fetchLineupMetadatas, offset, limit)
} else {
try {
currentQuery = query
currentSortDirection = sortDirection
currentSortMethod = sortMethod
yield put(actions.fetchSavesRequested())

const savedTracks = yield apiClient.getFavoritedTracks({
currentUserId: userId,
profileUserId: userId,
offset,
limit,
query,
sortMethod,
sortDirection
})
const tracks = savedTracks.map((save) => save.track)

yield processAndCacheTracks(tracks)

const saves = savedTracks.map((save) => ({
created_at: save.timestamp,
save_item_id: save.track.track_id
}))

const fullSaves = Array(account.track_save_count)
.fill(0)
.map((_) => ({}))

fullSaves.splice(offset, saves.length, ...saves)

yield put(actions.fetchSavesSucceeded(fullSaves))
if (limit > 0 && saves.length < limit) {
yield put(actions.endFetching(offset + saves.length))
}
yield fork(fetchLineupMetadatas, offset, limit)
} catch (e) {
yield put(actions.fetchSavesFailed())
}
}
})
}

function* watchFetchMoreSaves() {
yield waitForRead()
const apiClient = yield getContext('apiClient')
yield takeLatest(actions.FETCH_MORE_SAVES, function* (props) {
const account = yield call(waitForValue, getAccountUser)
const userId = account.user_id
const offset = props.offset ?? 0
const limit = props.limit ?? account.track_save_count
const query = props.query ?? ''
const sortMethod = props.sortMethod ?? ''
const sortDirection = props.sortDirection ?? ''

try {
const savedTracks = yield apiClient.getFavoritedTracks({
currentUserId: userId,
profileUserId: userId,
offset,
limit,
query,
sortMethod,
sortDirection
})
const tracks = savedTracks.map((save) => save.track)

yield processAndCacheTracks(tracks)

const saves = savedTracks.map((save) => ({
created_at: save.timestamp,
save_item_id: save.track.track_id
}))
yield put(actions.fetchMoreSavesSucceeded(saves, offset))

if (limit > 0 && saves.length < limit) {
yield put(actions.endFetching(offset + saves.length))
}
yield fork(fetchLineupMetadatas, offset, limit)
} catch (e) {
yield put(actions.fetchMoreSavesFailed())
}
})
}

export default function sagas() {
return [...tracksSagas(), watchFetchSaves, watchFetchMoreSaves]
}
191 changes: 0 additions & 191 deletions packages/web/src/common/store/pages/saved/sagas.ts

This file was deleted.

0 comments on commit 6c88515

Please sign in to comment.