Skip to content

Commit

Permalink
fixing extra channels and exposing customize on Required topics
Browse files Browse the repository at this point in the history
  • Loading branch information
bwebs committed Apr 16, 2024
1 parent c7d776f commit d29fb4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/react-hooks/src/preferences/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface PreferenceState {
export type ChannelClassification =
| "email"
| "push"
| "inbox"
| "direct_message"
| "sms"
| "webhook";
Expand Down
50 changes: 25 additions & 25 deletions packages/react-preferences/src/components/PreferencesV4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ChannelClassification,
IPreferenceTemplate,
IRecipientPreference,
PreferenceStatus,
} from "~/types";
import { StyledToggle } from "./StyledToggle";
import Toggle from "react-toggle";
Expand Down Expand Up @@ -91,12 +92,12 @@ export const Channel = styled.div`
user-select: none;
}
${Input}:checked ~ ${ChannelOption} {
${Input}.checked ~ ${ChannelOption} {
background: ${({ theme }) => theme?.primary ?? "#9121c2"};
border: 0;
}
${Input}:checked ~ ${ChannelOption} > div {
${Input}.checked ~ ${ChannelOption} > div {
color: white;
}
`;
Expand Down Expand Up @@ -184,7 +185,8 @@ const ChannelPreference: React.FunctionComponent<{
handleChannelRouting: (channel: ChannelClassification) => void;
routingPreferences: ChannelClassification[];
channel: ChannelClassification;
}> = ({ handleChannelRouting, routingPreferences, channel }) => {
defaultStatus: PreferenceStatus;
}> = ({ handleChannelRouting, routingPreferences, channel, defaultStatus }) => {
const [checked, setChecked] = useState(routingPreferences.includes(channel));
const { preferencePage } = usePreferences();

Expand All @@ -193,9 +195,19 @@ const ChannelPreference: React.FunctionComponent<{
<label>
<Input
type="checkbox"
onChange={() => {
handleChannelRouting(channel);
setChecked(!checked);
className={checked ? "checked" : undefined}
onClick={(e) => {
e.preventDefault();
const prevent_uncheck =
// if the topic is required and this is the only channel selected, don't let them uncheck
defaultStatus === "REQUIRED" &&
routingPreferences.length === 1 &&
routingPreferences.includes(channel) &&
checked;
if (!prevent_uncheck) {
setChecked(!checked);
handleChannelRouting(channel);
}
}}
checked={checked}
/>
Expand Down Expand Up @@ -258,36 +270,23 @@ export const PreferenceTopic: React.FunctionComponent<{
templateId: topicId,
hasCustomRouting: !customizeDelivery,
...(!customizeDelivery && {
routingPreferences: [
"direct_message",
"email",
"inbox",
"push",
"sms",
"webhook",
],
routingPreferences: defaultRoutingOptions,
}),
status: statusToggle ? "OPTED_IN" : "OPTED_OUT",
tenantId,
});

// If Customize Delivery is turned on, set the routing preferences to the default
!customizeDelivery &&
setRouting([
"direct_message",
"email",
"inbox",
"push",
"sms",
"webhook",
]);
!customizeDelivery && setRouting(defaultRoutingOptions);

setCustomizeDelivery(!customizeDelivery);
};

const handleChannelRouting = (channel: ChannelClassification) => {
const newRouting = routing.includes(channel)
? routing.filter((r) => r !== channel)
? routing.filter(
(r) => r !== channel && defaultRoutingOptions.includes(r)
)
: [...routing, channel];

updateRecipientPreferences({
Expand Down Expand Up @@ -351,7 +350,7 @@ export const PreferenceTopic: React.FunctionComponent<{
topicId={topicId}
/>
</div>
{statusToggle && defaultHasCustomRouting && defaultStatus !== "REQUIRED" && (
{statusToggle && defaultHasCustomRouting && (
<ChannelPreferenceStyles
theme={{ primary: preferencePage?.brand.settings.colors.primary }}
>
Expand All @@ -371,6 +370,7 @@ export const PreferenceTopic: React.FunctionComponent<{
channel={channel}
routingPreferences={routing}
handleChannelRouting={handleChannelRouting}
defaultStatus={defaultStatus}
/>
))}
</ChannelPreferenceStyles>
Expand Down

0 comments on commit d29fb4c

Please sign in to comment.