From 0a7e1f05df3c4f53bf5cdbaa1fe0c8181f61b3fb Mon Sep 17 00:00:00 2001 From: JurreBrandsenInfoSupport <149962077+JurreBrandsenInfoSupport@users.noreply.github.com> Date: Tue, 28 May 2024 14:53:05 +0200 Subject: [PATCH] Fix/find the expert (#138) * remove empty or duplicate communicationMethods * fix for empty communicationMethods --- .../select-communication-method.tsx | 22 +++++++++++++++---- src/utils/client-data-manipulation.ts | 20 +++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/components/select-communication-method.tsx b/src/components/select-communication-method.tsx index 8cfd2a7..8a181df 100644 --- a/src/components/select-communication-method.tsx +++ b/src/components/select-communication-method.tsx @@ -43,17 +43,27 @@ export default function SelectCommunicationMethod({ }, [methods]); const handleMethodChange = (method: CommunicationMethod) => { - let updatedSelection; + let updatedSelection: string[]; if (!selectedMethods.includes(method)) { updatedSelection = [...selectedMethods, method]; } else { updatedSelection = selectedMethods.filter((m) => m !== method); } - setSelectedMethods(updatedSelection); + + // remove empty strings + updatedSelection = updatedSelection.filter((m) => m !== ""); + + // remove duplicates + updatedSelection = [...new Set(updatedSelection)]; + + const updatedSelectionCommunication = + updatedSelection as CommunicationMethod[]; + + setSelectedMethods(updatedSelectionCommunication); setMethodMutate({ userId: session.user.id, - methods: updatedSelection, + methods: updatedSelectionCommunication, }); }; @@ -90,7 +100,11 @@ export default function SelectCommunicationMethod({ > 0 + ? selectedMethods.includes(method) + : methods.includes(method) + } onChange={() => handleMethodChange(method)} className={`mr-2 accent-custom-primary`} /> diff --git a/src/utils/client-data-manipulation.ts b/src/utils/client-data-manipulation.ts index 06a93d4..0b57609 100644 --- a/src/utils/client-data-manipulation.ts +++ b/src/utils/client-data-manipulation.ts @@ -77,10 +77,11 @@ const pushUserData = ( dataByRoleAndQuestion[roleName]![questionText]!.push({ name: userMap[entry.userId]?.name ?? "Unknown User", email: userMap[entry.userId]?.email ?? "Unknown Email", - communicationPreferences: - userMap[entry.userId]!.communicationPreferences?.length > 0 - ? userMap[entry.userId]?.communicationPreferences - : ["Do not contact"], + communicationPreferences: userMap[ + entry.userId + ]!.communicationPreferences?.some((pref) => pref.trim().length > 0) + ? userMap[entry.userId]?.communicationPreferences + : ["Do not contact"], answer: answerOptionMap[entry.answerId] ?? "Unknown Answer", roles: userMap[entry.userId]?.roles ?? [], }); @@ -148,10 +149,11 @@ const getUserDetails = (userMap: UserMap, entry: Entry) => { const userEmail = userMap[entry.userId]?.email ?? "Unknown Email"; let userCommunicationPreferences = userMap[entry.userId]?.communicationPreferences; - userCommunicationPreferences = - userCommunicationPreferences?.length ?? 0 > 0 - ? userCommunicationPreferences - : ["Do not contact"] ?? []; + userCommunicationPreferences = userCommunicationPreferences?.some( + (pref) => pref.trim().length > 0, + ) + ? userCommunicationPreferences + : ["Do not contact"]; return { userName, userEmail, userCommunicationPreferences }; }; @@ -205,7 +207,7 @@ export const aggregateDataByRole = ( roleName, userEmail, userName, - userCommunicationPreferences!, + userCommunicationPreferences, ); if (!isNaN(answerValue)) {