refactor: remove immutable from medias state slice (#4740)
This commit is contained in:
committed by
GitHub
parent
04bc8ee74c
commit
f60c2871d3
@ -1,4 +1,3 @@
|
||||
import { Map, fromJS } from 'immutable';
|
||||
import {
|
||||
addAssets,
|
||||
addAsset,
|
||||
@ -14,37 +13,37 @@ describe('medias', () => {
|
||||
const asset = createAssetProxy({ url: 'url', path: 'path' });
|
||||
|
||||
it('should add assets', () => {
|
||||
expect(reducer(fromJS({}), addAssets([asset]))).toEqual(
|
||||
Map({ path: { asset, isLoading: false, error: null } }),
|
||||
);
|
||||
expect(reducer({}, addAssets([asset]))).toEqual({
|
||||
path: { asset, isLoading: false, error: null },
|
||||
});
|
||||
});
|
||||
|
||||
it('should add asset', () => {
|
||||
expect(reducer(fromJS({}), addAsset(asset))).toEqual(
|
||||
Map({ path: { asset, isLoading: false, error: null } }),
|
||||
);
|
||||
expect(reducer({}, addAsset(asset))).toEqual({
|
||||
path: { asset, isLoading: false, error: null },
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove asset', () => {
|
||||
expect(reducer(fromJS({ path: asset }), removeAsset(asset.path))).toEqual(Map());
|
||||
expect(
|
||||
reducer({ [asset.path]: { asset, isLoading: false, error: null } }, removeAsset(asset.path)),
|
||||
).toEqual({});
|
||||
});
|
||||
|
||||
it('should mark asset as loading', () => {
|
||||
expect(reducer(fromJS({}), loadAssetRequest(asset.path))).toEqual(
|
||||
Map({ path: { isLoading: true } }),
|
||||
);
|
||||
expect(reducer({}, loadAssetRequest(asset.path))).toEqual({ path: { isLoading: true } });
|
||||
});
|
||||
|
||||
it('should mark asset as not loading', () => {
|
||||
expect(reducer(fromJS({}), loadAssetSuccess(asset.path))).toEqual(
|
||||
Map({ path: { isLoading: false, error: null } }),
|
||||
);
|
||||
expect(reducer({}, loadAssetSuccess(asset.path))).toEqual({
|
||||
path: { isLoading: false, error: null },
|
||||
});
|
||||
});
|
||||
|
||||
it('should set loading error', () => {
|
||||
const error = new Error('some error');
|
||||
expect(reducer(fromJS({}), loadAssetFailure(asset.path, error))).toEqual(
|
||||
Map({ path: { isLoading: false, error } }),
|
||||
);
|
||||
expect(reducer({}, loadAssetFailure(asset.path, error))).toEqual({
|
||||
path: { isLoading: false, error },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { fromJS } from 'immutable';
|
||||
import { produce } from 'immer';
|
||||
import {
|
||||
ADD_ASSETS,
|
||||
ADD_ASSET,
|
||||
@ -6,46 +6,58 @@ import {
|
||||
LOAD_ASSET_REQUEST,
|
||||
LOAD_ASSET_SUCCESS,
|
||||
LOAD_ASSET_FAILURE,
|
||||
MediasAction,
|
||||
} from '../actions/media';
|
||||
import AssetProxy from '../valueObjects/AssetProxy';
|
||||
import { Medias, MediasAction } from '../types/redux';
|
||||
|
||||
const medias = (state: Medias = fromJS({}), action: MediasAction) => {
|
||||
switch (action.type) {
|
||||
case ADD_ASSETS: {
|
||||
const payload = action.payload as AssetProxy[];
|
||||
let newState = state;
|
||||
payload.forEach(asset => {
|
||||
newState = newState.set(asset.path, { asset, isLoading: false, error: null });
|
||||
});
|
||||
return newState;
|
||||
}
|
||||
case ADD_ASSET: {
|
||||
const asset = action.payload as AssetProxy;
|
||||
return state.set(asset.path, { asset, isLoading: false, error: null });
|
||||
}
|
||||
case REMOVE_ASSET: {
|
||||
const payload = action.payload as string;
|
||||
return state.delete(payload);
|
||||
}
|
||||
case LOAD_ASSET_REQUEST: {
|
||||
const { path } = action.payload as { path: string };
|
||||
return state.set(path, { ...state.get(path), isLoading: true });
|
||||
}
|
||||
case LOAD_ASSET_SUCCESS: {
|
||||
const { path } = action.payload as { path: string };
|
||||
return state.set(path, { ...state.get(path), isLoading: false, error: null });
|
||||
}
|
||||
case LOAD_ASSET_FAILURE: {
|
||||
const { path, error } = action.payload as { path: string; error: Error };
|
||||
return state.set(path, { ...state.get(path), isLoading: false, error });
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
export type Medias = {
|
||||
[path: string]: { asset: AssetProxy | undefined; isLoading: boolean; error: Error | null };
|
||||
};
|
||||
|
||||
const defaultState: Medias = {};
|
||||
|
||||
const medias = produce((state: Medias, action: MediasAction) => {
|
||||
switch (action.type) {
|
||||
case ADD_ASSETS: {
|
||||
const assets = action.payload;
|
||||
assets.forEach(asset => {
|
||||
state[asset.path] = { asset, isLoading: false, error: null };
|
||||
});
|
||||
break;
|
||||
}
|
||||
case ADD_ASSET: {
|
||||
const asset = action.payload;
|
||||
state[asset.path] = { asset, isLoading: false, error: null };
|
||||
break;
|
||||
}
|
||||
case REMOVE_ASSET: {
|
||||
const path = action.payload;
|
||||
delete state[path];
|
||||
break;
|
||||
}
|
||||
case LOAD_ASSET_REQUEST: {
|
||||
const { path } = action.payload;
|
||||
state[path] = state[path] || {};
|
||||
state[path].isLoading = true;
|
||||
break;
|
||||
}
|
||||
case LOAD_ASSET_SUCCESS: {
|
||||
const { path } = action.payload;
|
||||
state[path] = state[path] || {};
|
||||
state[path].isLoading = false;
|
||||
state[path].error = null;
|
||||
break;
|
||||
}
|
||||
case LOAD_ASSET_FAILURE: {
|
||||
const { path, error } = action.payload;
|
||||
state[path] = state[path] || {};
|
||||
state[path].isLoading = false;
|
||||
state[path].error = error;
|
||||
}
|
||||
}
|
||||
}, defaultState);
|
||||
|
||||
export const selectIsLoadingAsset = (state: Medias) =>
|
||||
Object.values(state.toJS()).some(state => state.isLoading);
|
||||
Object.values(state).some(state => state.isLoading);
|
||||
|
||||
export default medias;
|
||||
|
Reference in New Issue
Block a user