diff --git a/client/constants.js b/client/constants.js index 565d716fa6..9c5f9ae353 100644 --- a/client/constants.js +++ b/client/constants.js @@ -130,8 +130,6 @@ export const CLEAR_PERSISTED_STATE = 'CLEAR_PERSISTED_STATE'; export const HIDE_RUNTIME_ERROR_WARNING = 'HIDE_RUNTIME_ERROR_WARNING'; export const SHOW_RUNTIME_ERROR_WARNING = 'SHOW_RUNTIME_ERROR_WARNING'; -export const SET_ASSETS = 'SET_ASSETS'; -export const DELETE_ASSET = 'DELETE_ASSET'; export const TOGGLE_DIRECTION = 'TOGGLE_DIRECTION'; export const SET_SORTING = 'SET_SORTING'; diff --git a/client/modules/IDE/actions/assets.js b/client/modules/IDE/actions/assets.js index 0a7a13e3a0..66e03e835c 100644 --- a/client/modules/IDE/actions/assets.js +++ b/client/modules/IDE/actions/assets.js @@ -1,14 +1,9 @@ import apiClient from '../../../utils/apiClient'; import * as ActionTypes from '../../../constants'; import { startLoader, stopLoader } from './loader'; +import { assetsActions } from '../reducers/assets'; -function setAssets(assets, totalSize) { - return { - type: ActionTypes.SET_ASSETS, - assets, - totalSize - }; -} +const { setAssets, deleteAsset } = assetsActions; export function getAssets() { return async (dispatch) => { @@ -26,13 +21,6 @@ export function getAssets() { }; } -export function deleteAsset(assetKey) { - return { - type: ActionTypes.DELETE_ASSET, - key: assetKey - }; -} - export function deleteAssetRequest(assetKey) { return async (dispatch) => { try { diff --git a/client/modules/IDE/reducers/assets.js b/client/modules/IDE/reducers/assets.js index f1dd0f40cb..e2fb58f625 100644 --- a/client/modules/IDE/reducers/assets.js +++ b/client/modules/IDE/reducers/assets.js @@ -1,20 +1,27 @@ -import * as ActionTypes from '../../../constants'; +import { createSlice } from '@reduxjs/toolkit'; -// 1,000,000 bytes in a MB. can't upload if totalSize is bigger than this. const initialState = { list: [], totalSize: 0 }; -const assets = (state = initialState, action) => { - switch (action.type) { - case ActionTypes.SET_ASSETS: - return { list: action.assets, totalSize: action.totalSize }; - case ActionTypes.DELETE_ASSET: - return { list: state.list.filter((asset) => asset.key !== action.key) }; - default: - return state; +const assetsSlice = createSlice({ + name: 'assets', + initialState, + reducers: { + setAssets: (state, action) => action.payload, + deleteAsset: (state, action) => { + const key = action.payload; + const index = state.list.findIndex((asset) => asset.key === key); + if (index !== -1) { + const asset = state.list[index]; + state.totalSize -= asset.size; + state.list.splice(index, 1); + } + } } -}; +}); + +export const assetsActions = assetsSlice.actions; -export default assets; +export default assetsSlice.reducer;