Basic download implementation

This commit is contained in:
Lei Nelissen
2022-01-02 02:28:52 +01:00
parent 464747d0c4
commit d2fb4a4aea
30 changed files with 605 additions and 102 deletions

View File

@@ -0,0 +1,60 @@
import { createSlice, Dictionary, EntityId } from '@reduxjs/toolkit';
import { completeDownload, downloadAdapter, failDownload, initializeDownload, progressDownload, removeDownloadedTrack } from './actions';
import { DownloadEntity } from './types';
interface State {
entities: Dictionary<DownloadEntity>;
ids: EntityId[];
}
const initialState: State = {
entities: {},
ids: [],
};
const downloads = createSlice({
name: 'downloads',
initialState,
reducers: {},
extraReducers: builder => {
builder.addCase(initializeDownload, (state, action) => {
downloadAdapter.upsertOne(state, {
...action.payload,
progress: 0,
isFailed: false,
isComplete: false,
});
});
builder.addCase(progressDownload, (state, action) => {
downloadAdapter.updateOne(state, {
id: action.payload.id,
changes: action.payload
});
});
builder.addCase(completeDownload, (state, action) => {
downloadAdapter.updateOne(state, {
id: action.payload.id,
changes: {
...action.payload,
isFailed: false,
isComplete: true,
}
});
});
builder.addCase(failDownload, (state, action) => {
downloadAdapter.updateOne(state, {
id: action.payload.id,
changes: {
isComplete: false,
isFailed: true,
progress: 0,
}
});
});
builder.addCase(removeDownloadedTrack.fulfilled, (state, action) => {
downloadAdapter.removeOne(state, action.meta.arg);
});
},
});
export default downloads;