1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 18× 18× 18× 18× 9× 11× 7× 2× 11× 11× 11× 11× 1× 10× 11× 9× 9× 9× 5× 4× 4× 4× 12× 12× 5× 7× 3× 5× 4× 7× 7× 7× 12× 13× 7× 4× 4× 10× 3× 3× 4× 2× 9× 9× 18× 11× 9× 5× 12× 9× 3× 9× 12× | import get from 'lodash/get'; import mapValues from 'lodash/mapValues'; import isEqual from 'lodash/isEqual'; import { arrayFrom } from 'utils'; function addToManyProperty(state, action, relation) { const { entity: foreignEntity } = action.payload; const { through, foreign } = relation; const entityToUpdate = get(state, get(foreignEntity, foreign)); if ( ! entityToUpdate) { // Don't do anything if the parent entity doesn't exist return state; } if (get(entityToUpdate, through, []).find((id) => id == foreignEntity.id)) { return state; // Don't do anything if the entity is already on the state } return { ...state, [entityToUpdate.id]: { ...entityToUpdate, [through]: [ ...get(entityToUpdate, through, []), foreignEntity.id, ], }, }; } function deleteFromManyProperty(state, action, relation) { const { entity: foreignEntity } = action.payload; const { through, foreign } = relation; const entityToUpdate = get(state, get(foreignEntity, foreign)); if ( ! entityToUpdate) { // Don't do anything if the parent entity doesn't exist return state; } return { ...state, [entityToUpdate.id]: { ...entityToUpdate, [through]: get(entityToUpdate, through, []).filter((id) => id != foreignEntity.id) }, }; } function updateRelation(state, action, relation) { const { entity: foreignEntity, oldEntity: oldForeignEntity } = action.payload; const { through, foreign } = relation; if (get(foreignEntity, foreign) === undefined || isEqual(get(foreignEntity, foreign), get(oldForeignEntity, foreign))) { return state; // No need to update because the prop has not been updated } const newParentEntitiesIds = arrayFrom(get(foreignEntity, foreign)); const oldParentEntitiesIds = arrayFrom(get(oldForeignEntity, foreign)); return mapValues(state, (entity) => { const id = entity.id; if (newParentEntitiesIds.includes(id) && get(entity, through).find((id) => id == foreignEntity.id) === undefined) { return { ...entity, [through]: [ ...get(entity, through, []), foreignEntity.id, ], }; } if (oldParentEntitiesIds.includes(id) && ! newParentEntitiesIds.includes(entity.id)) { return { ...entity, [through]: get(entity, through, []).filter((id) => id != foreignEntity.id) }; } return entity; }); } function updateRelatedId(state, action, relation) { const { newId, oldId } = action.payload; const { isMany, through } = relation; if (isMany) { return mapValues(state, (entity) => ({ ...entity, [through]: get(entity, through, []).map((id) => id == oldId ? newId : id), })); } else { return mapValues(state, (entity) => ({ ...entity, [through]: get(entity, through) == oldId ? newId : get(entity, through), })); } } function deleteOneProperty(state, action, relation) { const { entity: foreignEntity } = action.payload; const { through } = relation; return mapValues(state, (entity) => ({ ...entity, [through]: get(entity, through) == foreignEntity.id ? null : get(entity, through), })); } function createOneRelationReactions(relation) { const { to } = relation; return { [`@@entman/DELETE_ENTITY_${to.toUpperCase()}`]: (state, action) => { return deleteOneProperty(state, action, relation); }, [`@@entman/UPDATE_ENTITY_ID_${to.toUpperCase()}`]: (state, action) => { return updateRelatedId(state, action, relation); }, }; } function createManyRelationReactions(relation) { const { to } = relation; return { [`@@entman/CREATE_ENTITY_${to.toUpperCase()}`]: (state, action) => { return addToManyProperty(state, action, relation); }, [`@@entman/DELETE_ENTITY_${to.toUpperCase()}`]: (state, action) => { return deleteFromManyProperty(state, action, relation); }, [`@@entman/UPDATE_ENTITY_${to.toUpperCase()}`]: (state, action) => { return updateRelation(state, action, relation); }, [`@@entman/UPDATE_ENTITY_ID_${to.toUpperCase()}`]: (state, action) => { return updateRelatedId(state, action, relation); } }; } // The entity in state reacts to the entity in relation function createReactionsToRelation(relation) { if (relation.isMany) { // relation is going to be an array (e.g. users) return createManyRelationReactions(relation); } else { return createOneRelationReactions(relation); } } export default function createReactions(relations) { return relations .map(createReactionsToRelation) .reduce((memo, reactions) => ({ ...memo, ...reactions }), {}); } |