Skip to content

Instantly share code, notes, and snippets.

@paprikka
Created December 5, 2022 20:49
Show Gist options
  • Save paprikka/69f80d703172b8e05b45d65bebfd41b5 to your computer and use it in GitHub Desktop.
Save paprikka/69f80d703172b8e05b45d65bebfd41b5 to your computer and use it in GitHub Desktop.
import {createModal} from '../../stores/modals';
import Intents from '../../intents';
const deactivateNotificaton = entityID => () => {
Intents.update( state => {
return state.updateIn(['currentGame', 'entities'], entities => {
const ind = entities.findIndex( ent => ent.get('_id') === entityID );
if(ind === -1) return entities;
// debugger
return entities.set(ind, entities.get(ind).set('notification:active', false));
});
});
};
function createNotificationModal(entity) {
const entityID = entity.get('_id');
return createModal(
'NotificationModal',
entityID,
{
message: entity.get('notification:message'),
onClose: deactivateNotificaton(entityID)
}
);
}
const hasNotificationComponent = ent => (
ent.get('component:notification') === true
);
const findModalForEntity = (modals, entity) => (
modals && modals.find( m => m.get('uuid') === entity.get('_id'))
);
export default function notifySystem(state) {
const entities = state.getIn(['currentGame', 'entities']);
const applicableEntities = entities.filter( hasNotificationComponent );
const modals = state.get('modals');
if(!modals) return state;
let nextState = state;
applicableEntities.forEach( entity => {
const entityModal = findModalForEntity(modals, entity);
const isNotificationActive = entity.get('notification:active');
if(isNotificationActive === true && !entityModal) {
nextState = nextState.updateIn(
['modals'],
modals => modals.push(createNotificationModal(entity))
);
} else if(isNotificationActive === false && entityModal) {
if(
modals && modals.find( m => m.get('uuid') === entity.get('_id'))
) {
nextState = nextState.updateIn(
['modals'],
modals => modals.filter( m => m.get('uuid') !== entity.get('_id'))
);
}
}
});
return nextState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment