Skip to content

Commit 2c0c943

Browse files
fix: Update the isMappingComplete flag when publishing items or pushing changes (#3170)
1 parent 34ee83f commit 2c0c943

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

src/modules/item/reducer.spec.ts

+87-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { ChainId } from '@dcl/schemas'
1+
import { ChainId, ContractAddress, ContractNetwork, Mapping, MappingType } from '@dcl/schemas'
22
import { saveCollectionSuccess } from 'modules/collection/actions'
33
import { Collection } from 'modules/collection/types'
4+
import {
5+
publishAndPushChangesThirdPartyItemsSuccess,
6+
PublishAndPushChangesThirdPartyItemsSuccessAction,
7+
publishThirdPartyItemsSuccess,
8+
PublishThirdPartyItemsSuccessAction,
9+
pushChangesThirdPartyItemsSuccess,
10+
PushChangesThirdPartyItemsSuccessAction
11+
} from 'modules/thirdParty/actions'
12+
import { PaginatedResource } from 'lib/api/pagination'
13+
import { CurationStatus } from 'modules/curations/types'
14+
import { ItemCuration } from 'modules/curations/itemCuration/types'
415
import { getChainIdByNetwork } from 'decentraland-dapps/dist/lib/eth'
516
import {
617
clearSaveMultipleItems,
@@ -19,7 +30,6 @@ import {
1930
} from './actions'
2031
import { INITIAL_STATE, itemReducer, ItemState } from './reducer'
2132
import { Item } from './types'
22-
import { PaginatedResource } from 'lib/api/pagination'
2333
import { toItemObject } from './utils'
2434

2535
jest.mock('decentraland-dapps/dist/lib/eth')
@@ -34,8 +44,14 @@ let itemsMap: Record<string, Item>
3444
let fileNames: string[]
3545

3646
beforeEach(() => {
47+
const mappings: Partial<Record<ContractNetwork, Record<ContractAddress, Mapping[]>>> = {
48+
[ContractNetwork.AMOY]: { '0x0': [{ type: MappingType.ANY }] }
49+
}
3750
state = { ...INITIAL_STATE }
38-
items = [{ id: 'anItemId' } as Item, { id: 'anotherItemId' } as Item]
51+
items = [
52+
{ id: 'anItemId', isPublished: false, mappings: mappings } as Item,
53+
{ id: 'anotherItemId', isPublished: false, mappings: null } as Item
54+
]
3955
itemsMap = {
4056
[items[0].id]: items[0],
4157
[items[1].id]: items[1]
@@ -422,3 +438,71 @@ describe('when an action of type FETCH_ORPHAN_ITEM_FAILURE is called', () => {
422438
})
423439
})
424440
})
441+
442+
describe.each([
443+
['pushing changes and publishing third party items', publishAndPushChangesThirdPartyItemsSuccess],
444+
['pushing changes third party items', pushChangesThirdPartyItemsSuccess],
445+
['publishing third party items', publishThirdPartyItemsSuccess]
446+
])('when reducing the successful action of %s', (_, fn) => {
447+
let action:
448+
| PublishThirdPartyItemsSuccessAction
449+
| PublishAndPushChangesThirdPartyItemsSuccessAction
450+
| PushChangesThirdPartyItemsSuccessAction
451+
452+
beforeEach(() => {
453+
const curations: ItemCuration[] = items.map(item => ({
454+
itemId: item.id,
455+
contentHash: 'aHash',
456+
id: 'aCurationId',
457+
updatedAt: 0,
458+
createdAt: 0,
459+
status: CurationStatus.PENDING
460+
}))
461+
462+
switch (fn) {
463+
case pushChangesThirdPartyItemsSuccess:
464+
action = pushChangesThirdPartyItemsSuccess('aCollectionId', curations)
465+
break
466+
case publishThirdPartyItemsSuccess:
467+
action = publishThirdPartyItemsSuccess('aThirdPartyId', 'aCollectionId', items, curations)
468+
break
469+
case publishAndPushChangesThirdPartyItemsSuccess:
470+
action = publishAndPushChangesThirdPartyItemsSuccess('aThirdParty', items, curations)
471+
break
472+
}
473+
})
474+
475+
describe('and the items are not in the state', () => {
476+
beforeEach(() => {
477+
state = {
478+
...INITIAL_STATE
479+
}
480+
})
481+
482+
it('should return the state as is', () => {
483+
expect(itemReducer(state, action)).toEqual(state)
484+
})
485+
})
486+
487+
describe('and the items are in the state', () => {
488+
beforeEach(() => {
489+
state = {
490+
...INITIAL_STATE,
491+
data: items.reduce((acc, item) => ({ ...acc, [item.id]: { ...item, isPublished: false, isMappingComplete: !!item.mappings } }), {})
492+
}
493+
})
494+
495+
it("should set the items as published and set the isMappingComplete property in accordance to the item's mapping", () => {
496+
expect(itemReducer(state, action)).toEqual({
497+
...state,
498+
data: items.reduce(
499+
(acc, item) => ({
500+
...acc,
501+
[item.id]: { ...item, isPublished: true, isMappingComplete: !!item.mappings }
502+
}),
503+
{}
504+
)
505+
})
506+
})
507+
})
508+
})

src/modules/item/reducer.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ import {
104104
} from './actions'
105105
import {
106106
PublishThirdPartyItemsSuccessAction,
107+
PushChangesThirdPartyItemsSuccessAction,
107108
PublishAndPushChangesThirdPartyItemsSuccessAction,
108109
PUBLISH_AND_PUSH_CHANGES_THIRD_PARTY_ITEMS_SUCCESS,
110+
PUSH_CHANGES_THIRD_PARTY_ITEMS_SUCCESS,
109111
PUBLISH_THIRD_PARTY_ITEMS_SUCCESS
110112
} from 'modules/thirdParty/actions'
111113
import { toItemObject } from './utils'
@@ -173,6 +175,7 @@ type ItemReducerAction =
173175
| FetchRaritiesFailureAction
174176
| PublishThirdPartyItemsSuccessAction
175177
| PublishAndPushChangesThirdPartyItemsSuccessAction
178+
| PushChangesThirdPartyItemsSuccessAction
176179
| RescueItemsRequestAction
177180
| RescueItemsSuccessAction
178181
| RescueItemsChunkSuccessAction
@@ -425,6 +428,25 @@ export function itemReducer(state: ItemState = INITIAL_STATE, action: ItemReduce
425428
error: null
426429
}
427430
}
431+
case PUBLISH_AND_PUSH_CHANGES_THIRD_PARTY_ITEMS_SUCCESS:
432+
case PUBLISH_THIRD_PARTY_ITEMS_SUCCESS:
433+
case PUSH_CHANGES_THIRD_PARTY_ITEMS_SUCCESS: {
434+
const { itemCurations } = action.payload
435+
return {
436+
...state,
437+
data: {
438+
...state.data,
439+
...itemCurations.reduce((accum, itemCuration) => {
440+
const item = state.data[itemCuration.itemId]
441+
if (!item) {
442+
return accum
443+
}
444+
accum[item.id] = { ...item, isPublished: true, isMappingComplete: !!item.mappings }
445+
return accum
446+
}, {} as ItemState['data'])
447+
}
448+
}
449+
}
428450
case FETCH_TRANSACTION_SUCCESS: {
429451
const transaction = action.payload.transaction
430452

@@ -463,8 +485,7 @@ export function itemReducer(state: ItemState = INITIAL_STATE, action: ItemReduce
463485
}
464486
}
465487
}
466-
case PUBLISH_AND_PUSH_CHANGES_THIRD_PARTY_ITEMS_SUCCESS:
467-
case PUBLISH_THIRD_PARTY_ITEMS_SUCCESS:
488+
468489
case PUBLISH_COLLECTION_SUCCESS: {
469490
const items: Item[] = transaction.payload.items
470491
return {

0 commit comments

Comments
 (0)