Skip to content

Commit

Permalink
Fix/find the expert (#138)
Browse files Browse the repository at this point in the history
* remove empty or duplicate communicationMethods

* fix for empty communicationMethods
  • Loading branch information
JurreBrandsenInfoSupport committed May 28, 2024
1 parent 99ce864 commit 0a7e1f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
22 changes: 18 additions & 4 deletions src/components/select-communication-method.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

Expand Down Expand Up @@ -90,7 +100,11 @@ export default function SelectCommunicationMethod({
>
<input
type="checkbox"
checked={selectedMethods.includes(method)}
checked={
selectedMethods.length > 0
? selectedMethods.includes(method)
: methods.includes(method)
}
onChange={() => handleMethodChange(method)}
className={`mr-2 accent-custom-primary`}
/>
Expand Down
20 changes: 11 additions & 9 deletions src/utils/client-data-manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [],
});
Expand Down Expand Up @@ -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 };
};

Expand Down Expand Up @@ -205,7 +207,7 @@ export const aggregateDataByRole = (
roleName,
userEmail,
userName,
userCommunicationPreferences!,
userCommunicationPreferences,
);

if (!isNaN(answerValue)) {
Expand Down

0 comments on commit 0a7e1f0

Please sign in to comment.