Skip to content

Commit 1601e0f

Browse files
committed
Adjust the totalSize when deleting an asset.
1 parent 7e5c6fc commit 1601e0f

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

client/constants.js

-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ export const CLEAR_PERSISTED_STATE = 'CLEAR_PERSISTED_STATE';
131131

132132
export const HIDE_RUNTIME_ERROR_WARNING = 'HIDE_RUNTIME_ERROR_WARNING';
133133
export const SHOW_RUNTIME_ERROR_WARNING = 'SHOW_RUNTIME_ERROR_WARNING';
134-
export const SET_ASSETS = 'SET_ASSETS';
135-
export const DELETE_ASSET = 'DELETE_ASSET';
136134

137135
export const TOGGLE_DIRECTION = 'TOGGLE_DIRECTION';
138136
export const SET_SORTING = 'SET_SORTING';

client/modules/IDE/actions/assets.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33
import { startLoader, stopLoader } from './loader';
4+
import { assetsActions } from '../reducers/assets';
45

5-
function setAssets(assets, totalSize) {
6-
return {
7-
type: ActionTypes.SET_ASSETS,
8-
assets,
9-
totalSize
10-
};
11-
}
6+
const { setAssets, deleteAsset } = assetsActions;
127

138
export function getAssets() {
149
return (dispatch) => {
1510
dispatch(startLoader());
1611
apiClient
1712
.get('/S3/objects')
1813
.then((response) => {
19-
dispatch(setAssets(response.data.assets, response.data.totalSize));
14+
dispatch(
15+
setAssets({
16+
list: response.data.assets,
17+
totalSize: response.data.totalSize
18+
})
19+
);
2020
dispatch(stopLoader());
2121
})
2222
.catch(() => {
@@ -28,13 +28,6 @@ export function getAssets() {
2828
};
2929
}
3030

31-
export function deleteAsset(assetKey) {
32-
return {
33-
type: ActionTypes.DELETE_ASSET,
34-
key: assetKey
35-
};
36-
}
37-
3831
export function deleteAssetRequest(assetKey) {
3932
return (dispatch) => {
4033
apiClient

client/modules/IDE/reducers/assets.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import * as ActionTypes from '../../../constants';
1+
import { createSlice } from '@reduxjs/toolkit';
22

3-
// 1,000,000 bytes in a MB. can't upload if totalSize is bigger than this.
43
const initialState = {
54
list: [],
65
totalSize: 0
76
};
87

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+
}
1722
}
18-
};
23+
});
24+
25+
export const assetsActions = assetsSlice.actions;
1926

20-
export default assets;
27+
export default assetsSlice.reducer;

0 commit comments

Comments
 (0)