Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyLand refresh/navigation fix #333

Merged
merged 21 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decentraland/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const listsAreEqual = (first, second) => {
);
};

// Update any list having id as key field
// Update item from any list of objects having id as key field
export const updateListItem = (list, toUpdateId, entriesToUpdate) => {
return list.map(item => {
return item.id === toUpdateId ? { ...item, ...entriesToUpdate } : item;
Expand Down
178 changes: 104 additions & 74 deletions decentraland/redux/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,90 +21,105 @@ import { removeFromList, updateListItem, toListIfNot } from "@helpers";
import AccountCreationStatus from "@constants/AccountCreationStatus";
const { NOT_STARTED } = AccountCreationStatus;

function accountInfo(
state = {
// Reducing boilerplate from reducers
// Refs: https://redux.js.org/recipes/structuring-reducers/refactoring-reducer-example#reducing-boilerplate
function createReducer(initialState, handlers) {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action);
} else {
return state;
}
};
}

//
// accountInfo reducer
//
const setAccount = (state, action) => {
const { account } = action;
return { ...state, account };
};
const setAccountCreationStatus = (state, action) => {
const { creationStatus } = action;
return { ...state, creationStatus };
};
const updateActionIdForAccountCreationStatus = (state, action) => {
const { creationStatusAction } = action;
const { status, actionId } = creationStatusAction;
let { creationActions } = state;
creationActions = { ...creationActions, [status]: actionId };
return { ...state, creationActions };
};
const setAccountCreationActions = (state, action) => {
const { creationActions } = action;
return { ...state, creationActions };
};

const accountInfo = createReducer(
{
account: null,
creationStatus: NOT_STARTED,
creationActions: {},
},
action
) {
const {
type,
account,
creationStatus,
creationStatusAction,
creationActions,
} = action;
switch (type) {
case SET_ACCOUNT:
return { ...state, account };
case SET_ACCOUNT_CREATION_STATUS:
return { ...state, creationStatus };
case UPDATE_ACTION_ID_FOR_ACCOUNT_CREATION_STATUS: {
const { status, actionId } = creationStatusAction;
let { creationActions } = state;
creationActions = { ...creationActions, [status]: actionId };
return { ...state, creationActions };
}
case SET_ACCOUNT_CREATION_ACTIONS: {
return { ...state, creationActions };
}
default:
return state;
{
[SET_ACCOUNT]: setAccount,
[SET_ACCOUNT_CREATION_STATUS]: setAccountCreationStatus,
[UPDATE_ACTION_ID_FOR_ACCOUNT_CREATION_STATUS]: updateActionIdForAccountCreationStatus,
[SET_ACCOUNT_CREATION_ACTIONS]: setAccountCreationActions,
}
}
);

function selectedLandToBuy(state = null, action) {
const { type, landForSale } = action;
switch (type) {
case SELECT_LAND_TO_BUY:
return landForSale;
default:
return state;
}
}
//
// selectedLandToBuy reducer
//
const selectLandToBuy = (state, action) => {
const { landForSale } = action;
return landForSale;
};

function assetsForSale(state = { list: [], loadingInProgress: true }, action) {
const { type, landForSale, loadingInProgress } = action;
switch (type) {
case PREPEND_LAND_FOR_SALE_TO_LIST:
return { ...state, list: [landForSale, ...state.list] };
case APPEND_LAND_FOR_SALE_TO_LIST:
return { ...state, list: [...state.list, landForSale] };
case REMOVE_LAND_FOR_SALE: {
let { list: assetsForSale } = state;
const list = removeFromList(assetsForSale, landForSale);
return { ...state, list };
}
case SET_LOADING_ASSETS_FOR_SALE_IN_PROGRESS:
return { ...state, loadingInProgress };
default:
return state;
}
}
const selectedLandToBuy = createReducer(null, {
[SELECT_LAND_TO_BUY]: selectLandToBuy,
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
});

function myAssets(state = { list: [] }, action) {
const { type } = action;

switch (type) {
case PREPEND_TO_MY_ASSETS_LIST:
return prependToMyAssetsList(state, action);
case APPEND_TO_MY_ASSETS_LIST:
return appendToMyAssetsList(state, action);
case REMOVE_FROM_MY_ASSETS_LIST:
return removeFromMyAssetsList(state, action);
case SET_MY_ASSETS_LIST:
return setMyAssetsList(state, action);
case SET_ACTION_ID_FOR_MY_ASSET:
return setActionIdForMyAsset(state, action);
case UPDATE_MY_ASSET_STATUS:
return updateMyAssetStatus(state, action);
default:
return state;
//
// assetsForSale reducer
//
const prependLandForSaleToList = (state, action) => {
const { landForSale } = action;
return { ...state, list: [landForSale, ...state.list] };
};

const appendLandForSaleToList = (state, action) => {
const { landForSale } = action;
return { ...state, list: [...state.list, landForSale] };
};

const removeLandForSale = (state, action) => {
const { landForSale } = action;
let { list: assetsForSale } = state;
const list = removeFromList(assetsForSale, landForSale);
return { ...state, list };
};

const setLoadingAssetsForSaleInProgress = (state, action) => {
const { loadingInProgress } = action;
return { ...state, loadingInProgress };
};

const assetsForSale = createReducer(
{ list: [], loadingInProgress: true },
{
[PREPEND_LAND_FOR_SALE_TO_LIST]: prependLandForSaleToList,
[APPEND_LAND_FOR_SALE_TO_LIST]: appendLandForSaleToList,
[REMOVE_LAND_FOR_SALE]: removeLandForSale,
[SET_LOADING_ASSETS_FOR_SALE_IN_PROGRESS]: setLoadingAssetsForSaleInProgress,
}
}
);

//
// myAssets reducer
//
const prependToMyAssetsList = (state, action) => {
const { myAsset } = action;
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
return { ...state, list: [myAsset, ...state.list] };
Expand Down Expand Up @@ -148,6 +163,21 @@ const updateMyAssetStatus = (state, action) => {
return { ...state, list };
};

const myAssets = createReducer(
{ list: [] },
{
[PREPEND_TO_MY_ASSETS_LIST]: prependToMyAssetsList,
[APPEND_TO_MY_ASSETS_LIST]: appendToMyAssetsList,
[REMOVE_FROM_MY_ASSETS_LIST]: removeFromMyAssetsList,
[SET_MY_ASSETS_LIST]: setMyAssetsList,
[SET_ACTION_ID_FOR_MY_ASSET]: setActionIdForMyAsset,
[UPDATE_MY_ASSET_STATUS]: updateMyAssetStatus,
}
);

//
// All all reducers
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
//
const decentralandApp = combineReducers({
accountInfo,
selectedLandToBuy,
Expand Down
2 changes: 1 addition & 1 deletion decentraland/screens/MyAssetsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MyAssetsScreen extends React.Component {
if (account) {
const { address } = account;

// Note: If an stored asset hasn't a status, assuming 'bought'.
// Note: If a stored asset doesn't have a status, assuming 'bought'.
const boughtAssets = assetsFromState
.map(asset => (!asset.status ? { ...asset, status: BOUGHT } : asset))
.filter(asset => asset.status === BOUGHT);
Expand Down