diff --git a/src/app/needs/next-actions/components/NextActionsDisplay.tsx b/src/app/needs/next-actions/components/NextActionsDisplay.tsx index 9c9bbfe3..3a18b571 100644 --- a/src/app/needs/next-actions/components/NextActionsDisplay.tsx +++ b/src/app/needs/next-actions/components/NextActionsDisplay.tsx @@ -32,17 +32,20 @@ export interface NextActionDocument { export default function NextActionsDisplay() { const database = useDatabase(); const [highlightedNeeds, setHighlightedNeeds] = useState([]); - const [relatedNextActions, setRelatedNextActions] = useState([]); + const [relatedNextActions, setRelatedNextActions] = useState< + NextActionDocument[] + >([]); const [chainEnd, setChainEnd] = useState(0); const [actionState, setActionState] = useState(0); - useEffect(() => { /* Fetch Data */ + useEffect(() => { + /* Fetch Data */ async function fetchData() { const needsDocs = await database.getFromDb("needs"); - const allNeeds = needsDocs.map(doc => doc.toJSON() as NeedDocument); + const allNeeds = needsDocs.map((doc) => doc.toJSON() as NeedDocument); const now = new Date(); - const filteredNeeds = allNeeds.filter(need => { + const filteredNeeds = allNeeds.filter((need) => { const expiry = new Date(need.selectedExpiry); return expiry > now && need.priority && need.priority.order > 0; }); @@ -52,15 +55,13 @@ export default function NextActionsDisplay() { const nextActionsDocs = await database.getFromDb("next_actions"); const allNextActions = nextActionsDocs.map( - doc => doc.toJSON() as NextActionDocument + (doc) => doc.toJSON() as NextActionDocument ); - const highlightedNeedIds = new Set(filteredNeeds.map( - need => need.id - )); + const highlightedNeedIds = new Set(filteredNeeds.map((need) => need.id)); - const filteredNextActions = allNextActions.filter( - action => highlightedNeedIds.has(action.need) + const filteredNextActions = allNextActions.filter((action) => + highlightedNeedIds.has(action.need) ); setRelatedNextActions(filteredNextActions); @@ -73,20 +74,23 @@ export default function NextActionsDisplay() { if (highlightedNeeds.length === 0) return []; const needsWithPriority = highlightedNeeds.filter( - need => need.priority && need.priority.order + (need) => need.priority && need.priority.order ); - const sortedNeeds = needsWithPriority.sort((a, b) => - a.priority!.order - b.priority!.order + const sortedNeeds = needsWithPriority.sort( + (a, b) => a.priority!.order - b.priority!.order ); - const groupsMap: Record = {}; + const groupsMap: Record< + number, + { + priority: { + order: number; + name: string; + }; + needs: NeedDocument[]; + } + > = {}; for (const need of sortedNeeds) { const order = need.priority!.order; @@ -94,7 +98,7 @@ export default function NextActionsDisplay() { if (!groupsMap[order]) { groupsMap[order] = { priority: need.priority!, - needs: [] + needs: [], }; } @@ -105,7 +109,7 @@ export default function NextActionsDisplay() { }, [highlightedNeeds]); const getActionsForNeed = (needId: string) => { - return relatedNextActions.filter(action => action.need === needId); + return relatedNextActions.filter((action) => action.need === needId); }; const onToggleAction = async (action: NextActionDocument) => { @@ -115,7 +119,7 @@ export default function NextActionsDisplay() { if (highlighted) { const updatedTimestamps = [...action.selectedTimestamps]; updatedTimestamps.pop(); - + await database.updateDocument( collectionName, action.id, @@ -132,10 +136,15 @@ export default function NextActionsDisplay() { } else { // Highlight action: // Add new selectedTimestamp - const updatedTimestamps = [...action.selectedTimestamps, new Date().toISOString()]; + const updatedTimestamps = [ + ...action.selectedTimestamps, + new Date().toISOString(), + ]; // Find the parent need to copy its selectedExpiry - const parentNeed = highlightedNeeds.find((need) => need.id === action.need); + const parentNeed = highlightedNeeds.find( + (need) => need.id === action.need + ); if (!parentNeed) { console.error("Parent need not found for action:", action); return; @@ -156,7 +165,7 @@ export default function NextActionsDisplay() { ); } - setChainEnd(prev => prev + 1); + setChainEnd((prev) => prev + 1); }; const handleAddAction = async (newAction: string, need: NeedDocument) => { @@ -168,48 +177,51 @@ export default function NextActionsDisplay() { selectedTimestamps: [], selectedExpiry: new Date().toISOString(), timestamp: new Date().toISOString(), - } + }; try { await database.addToDb("next_actions", newActionDocument); console.log(`Action Created: ${newActionDocument.name}`); } catch (error) { console.error("Error creating Action:", error); } - - setActionState(prev => prev + 1); + + setActionState((prev) => prev + 1); } }; - + return (
- { priorityGroups.length === 0 ? - (

- You have no unmet needs selected. Review which needs might be unmet before we can recommend next actions to meet them. -

) : - (priorityGroups.map((group, i) => ( + {priorityGroups.length === 0 ? ( +

+ You have no unmet needs selected. Review which needs might be unmet + before we can recommend next actions to meet them. +

+ ) : ( + priorityGroups.map((group, i) => (

{changeCase(group.priority.name, "sentence")}

- + {group.needs.map((need) => { const actions = getActionsForNeed(need.id); return ( -
-

- To meet a need for {changeCase(need.name, "lower")}, which actions can you take next? +
+

+ To meet a need for {changeCase(need.name, "lower")}, which + actions can you take next?

- { actions.length > 0 ? ( + {actions.length > 0 ? ( No next actions available for this need.

- ) - } -
+ )} +

); })}
- ))) - } + )) + )}
); -} \ No newline at end of file +} diff --git a/src/app/needs/next-actions/components/NextActionsSection.tsx b/src/app/needs/next-actions/components/NextActionsSection.tsx index ad6d2958..8f7cacca 100644 --- a/src/app/needs/next-actions/components/NextActionsSection.tsx +++ b/src/app/needs/next-actions/components/NextActionsSection.tsx @@ -24,51 +24,58 @@ export default function NextActionsSection({ const [newAction, setNewAction] = useState(""); const handleInputChange = (e: React.ChangeEvent) => { - setNewAction(e.target.value) + setNewAction(e.target.value); }; const handleSubmitAction = async () => { await handleAddAction(newAction, need); setModalOpen(false); - } + }; - return ( <> -
- {actions.map((action) => { - const highlighted = new Date(action.selectedExpiry) > new Date(); + return ( + <> +
+ {actions.map((action) => { + const highlighted = new Date(action.selectedExpiry) > new Date(); - return ( -
+ +
- { - setNewAction(""); - setModalOpen(false); - }, - }} - /> - ); -} \ No newline at end of file + { + setNewAction(""); + setModalOpen(false); + }, + }} + /> + + ); +}