From 81d4ea61c832f9511c3111b29f99417d974a1401 Mon Sep 17 00:00:00 2001 From: bedlam343 Date: Thu, 16 May 2024 16:07:31 -0700 Subject: [PATCH] add handledMessage to on widget update --- src/pages/Prototype.tsx | 2 -- src/prototype/reactToMessage.ts | 23 ++++++++--------------- src/redux/slices/minimapSlice.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/pages/Prototype.tsx b/src/pages/Prototype.tsx index 49c3954..04dc041 100644 --- a/src/pages/Prototype.tsx +++ b/src/pages/Prototype.tsx @@ -71,10 +71,8 @@ const Prototype = () => { //console.log('passing message to the selector'); // latest message in the last one in the list const currentMessage = messages[messages.length - 1]; - //console.log('currentMessage:', currentMessage); dispatch(addMessage(currentMessage)); - //console.log('hjere!', messages) reactToMessage({ dispatch, currentMessage, stressLevel }); }, [messages]); diff --git a/src/prototype/reactToMessage.ts b/src/prototype/reactToMessage.ts index 9926679..cf3050b 100644 --- a/src/prototype/reactToMessage.ts +++ b/src/prototype/reactToMessage.ts @@ -5,6 +5,7 @@ import store from 'src/redux/store'; import type { AppDispatch } from 'src/redux/store'; import { addElementToWidget, + addHandledMessageToWidget, addWidget, addWidgetToSection, } from 'src/redux/slices/minimapSlice'; @@ -48,7 +49,7 @@ const reactToMessage = ({ message: currentMessage, }); - if (index == -1) resolved = true; + if (index === -1) resolved = true; //check restrainer with all new widgets //if cluster is bad then go back to assim @@ -63,7 +64,7 @@ const reactToMessage = ({ } }); - if (index != -1 && restrainer({ widgetsToDeploy: newWidgets })) { + if (index !== -1 && restrainer({ widgetsToDeploy: newWidgets })) { //if the assim chose a cluster and the restrainer is okoay with the new widgets resolved = true; @@ -72,27 +73,19 @@ const reactToMessage = ({ const sectionID = widgetClusterToDeploy.sectionIds![widgetIndex]; const action = widgetClusterToDeploy.actions![widgetIndex]; - console.log('widgetToDeploy:', widgetToDeploy); - if (action !== 'newWidget') { //we should do something other than switch (action) { case 'updateWidget': - console.log('widget already exists, updating'); - // only have one widget in possibleWidgets right now, this is why this works - // furthermore, only have one element in the widget - // so we can just do possibleWidgets[0]... - // eventually, maybe assimilator returns the widget that needs to be updated - // assimilator should also say if to add a new element or remove one, etc. -- JAGJIT dispatch( - addElementToWidget( - widgetToDeploy.id, - widgetToDeploy.elements, //<- This should not be able to happen, why are we choosing one element if multiple need to be placed -Tom - ), + addElementToWidget(widgetToDeploy.id, widgetToDeploy.elements), // add new elements to the widget + ); + dispatch( + addHandledMessageToWidget(widgetToDeploy.id, currentMessage.id), // add the message to the widget's handledMessages array ); break; case 'none': - console.log('proposed widgets could not be placed'); + console.error('proposed widgets could not be placed'); break; } } else if (widgetToDeploy) { diff --git a/src/redux/slices/minimapSlice.ts b/src/redux/slices/minimapSlice.ts index 4328f97..8594e32 100644 --- a/src/redux/slices/minimapSlice.ts +++ b/src/redux/slices/minimapSlice.ts @@ -181,6 +181,35 @@ export const minimapSlice = createSlice({ }, }, + // add a new message to widget's handledMessages array + addHandledMessageToWidget: { + prepare(widgetId: string, messageId: string) { + return { + payload: { widgetId, messageId }, + }; + }, + + reducer: ( + state, + action: PayloadAction<{ widgetId: string; messageId: string }>, + ) => { + const { widgetId, messageId } = action.payload; + const widget = state.widgets[widgetId]; + + if (!widget) { + console.error(`Widget with id ${widgetId} not found`); + return; + } + + if (!widget.handledMessageIds) { + widget.handledMessageIds = []; + } + + widget.handledMessageIds.push(messageId); + state.widgets[widgetId] = widget; + }, + }, + deleteElementFromWidget: { prepare(widgetId: string, elementId: string) { return { @@ -398,6 +427,7 @@ export const { addMapSection, addMessage, addWidget, + addHandledMessageToWidget, addElementToWidget, addWidgetToSection, updateElementExpiration,