File tree 3 files changed +21
-28
lines changed
3 files changed +21
-28
lines changed Original file line number Diff line number Diff line change @@ -130,8 +130,6 @@ export const CLEAR_PERSISTED_STATE = 'CLEAR_PERSISTED_STATE';
130
130
131
131
export const HIDE_RUNTIME_ERROR_WARNING = 'HIDE_RUNTIME_ERROR_WARNING' ;
132
132
export const SHOW_RUNTIME_ERROR_WARNING = 'SHOW_RUNTIME_ERROR_WARNING' ;
133
- export const SET_ASSETS = 'SET_ASSETS' ;
134
- export const DELETE_ASSET = 'DELETE_ASSET' ;
135
133
136
134
export const TOGGLE_DIRECTION = 'TOGGLE_DIRECTION' ;
137
135
export const SET_SORTING = 'SET_SORTING' ;
Original file line number Diff line number Diff line change 1
1
import apiClient from '../../../utils/apiClient' ;
2
2
import * as ActionTypes from '../../../constants' ;
3
3
import { startLoader , stopLoader } from './loader' ;
4
+ import { assetsActions } from '../reducers/assets' ;
4
5
5
- function setAssets ( assets , totalSize ) {
6
- return {
7
- type : ActionTypes . SET_ASSETS ,
8
- assets,
9
- totalSize
10
- } ;
11
- }
6
+ const { setAssets, deleteAsset } = assetsActions ;
12
7
13
8
export function getAssets ( ) {
14
9
return async ( dispatch ) => {
@@ -26,13 +21,6 @@ export function getAssets() {
26
21
} ;
27
22
}
28
23
29
- export function deleteAsset ( assetKey ) {
30
- return {
31
- type : ActionTypes . DELETE_ASSET ,
32
- key : assetKey
33
- } ;
34
- }
35
-
36
24
export function deleteAssetRequest ( assetKey ) {
37
25
return async ( dispatch ) => {
38
26
try {
Original file line number Diff line number Diff line change 1
- import * as ActionTypes from '../../../constants ' ;
1
+ import { createSlice } from '@reduxjs/toolkit ' ;
2
2
3
- // 1,000,000 bytes in a MB. can't upload if totalSize is bigger than this.
4
3
const initialState = {
5
4
list : [ ] ,
6
5
totalSize : 0
7
6
} ;
8
7
9
- const assets = ( state = initialState , action ) => {
10
- switch ( action . type ) {
11
- case ActionTypes . SET_ASSETS :
12
- return { list : action . assets , totalSize : action . totalSize } ;
13
- case ActionTypes . DELETE_ASSET :
14
- return { list : state . list . filter ( ( asset ) => asset . key !== action . key ) } ;
15
- default :
16
- return state ;
8
+ const assetsSlice = createSlice ( {
9
+ name : 'assets' ,
10
+ initialState,
11
+ reducers : {
12
+ setAssets : ( state , action ) => action . payload ,
13
+ deleteAsset : ( state , action ) => {
14
+ const key = action . payload ;
15
+ const index = state . list . findIndex ( ( asset ) => asset . key === key ) ;
16
+ if ( index !== - 1 ) {
17
+ const asset = state . list [ index ] ;
18
+ state . totalSize -= asset . size ;
19
+ state . list . splice ( index , 1 ) ;
20
+ }
21
+ }
17
22
}
18
- } ;
23
+ } ) ;
24
+
25
+ export const assetsActions = assetsSlice . actions ;
19
26
20
- export default assets ;
27
+ export default assetsSlice . reducer ;
You can’t perform that action at this time.
0 commit comments