Skip to content

Commit

Permalink
admin: Refactor the change publishing process
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrRip committed Dec 18, 2024
1 parent fc7f070 commit f932b13
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions packages/admin/app/pages/playlist.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,29 @@ function backendPlaylistPublishChanges (changesIds: Set<Song['$id']>) {
})
)

function cleanup (changesId: Song['$id'], newId?: Song['$id']) {
function succeed (changesId: Song['$id'], newId?: Song['$id']) {
const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
// @ts-expect-error
backendPlaylistPublishChangesState.value[
backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
].state = 'succeeded'
if (newId) {
// @ts-expect-error
backendPlaylistPublishChangesState.value[
backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
].newId = newId
}
else {
// @ts-expect-error
backendPlaylistPublishChangesState.value[
backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
].old = backendPlaylist.value.find(song => song.$id === changesId)
}
backendPlaylistPublishChangesState.value[publishIndex].state = 'succeeded'
// @ts-expect-error
if (newId) { backendPlaylistPublishChangesState.value[publishIndex].newId = newId }
}
function fail (changesId: Song['$id'], error: AppwriteException) {
const changesPublishStateIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
// @ts-expect-error
backendPlaylistPublishChangesState.value[changesPublishStateIndex].state = 'failed'
backendPlaylistPublishChangesState.value[publishIndex].state = 'failed'
// @ts-expect-error
backendPlaylistPublishChangesState.value[changesPublishStateIndex].error = error
backendPlaylistPublishChangesState.value[publishIndex].error = error
}
function backendPlaylistRefreshIfAllPublishingFinished () {
if (backendPlaylistPublishChangesState.value.find(_ => _.state === 'processing')) { return }
function cleanup (changesId: Song['$id']) {
const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId)
// @ts-expect-error
if (!backendPlaylistPublishChangesState.value[publishIndex].newId) {
// @ts-expect-error
backendPlaylistPublishChangesState.value[publishIndex].old =
backendPlaylist.value.find(song => song.$id === changesId)
}
if (backendPlaylistPublishChangesState.value.findIndex(_ => _.state === 'processing') !== -1) { return }
backendPlaylistRefresh()
.then(() => {
backendPlaylistPublishChangesState.value.forEach((_, index) => {
Expand All @@ -151,25 +147,25 @@ function backendPlaylistPublishChanges (changesIds: Set<Song['$id']>) {
delete changes.$id
backendDatabases.createDocument('home', 'playlist', ID.unique(), changes, [Permission.read(Role.any())])
// Save the ID generated by the backend for correct identifying of changes after the final refresh
.then(res => cleanup(changesId, res.$id), err => fail(changesId, err))
.finally(backendPlaylistRefreshIfAllPublishingFinished)
.then(res => succeed(changesId, res.$id), err => fail(changesId, err))
.finally(() => cleanup(changesId))
}
else

// Modify song
if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length > 1) {
delete changes.$id
backendDatabases.updateDocument('home', 'playlist', changesId, changes)
.then(() => cleanup(changesId), err => fail(changesId, err))
.finally(backendPlaylistRefreshIfAllPublishingFinished)
.then(() => succeed(changesId), err => fail(changesId, err))
.finally(() => cleanup(changesId))
}
else

// Delete song
if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length === 1) {
backendDatabases.deleteDocument('home', 'playlist', changesId)
.then(() => cleanup(changesId), err => fail(changesId, err))
.finally(backendPlaylistRefreshIfAllPublishingFinished)
.then(() => succeed(changesId), err => fail(changesId, err))
.finally(() => cleanup(changesId))
}
})
}
Expand Down

0 comments on commit f932b13

Please sign in to comment.