Skip to content

Commit

Permalink
feat(account recipient preferences): support account recipient prefer…
Browse files Browse the repository at this point in the history
…ences
  • Loading branch information
tk26 committed Aug 9, 2023
1 parent b4ffe11 commit 97c4dbf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
20 changes: 12 additions & 8 deletions packages/client-graphql/src/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ICourierClientBasicParams, ICourierClientJWTParams } from "./types";
import { createCourierClient } from "./client";

const RECIPIENT_PREFERENCES = `
query GetRecipientPreferences {
recipientPreferences {
query GetRecipientPreferences($accountId: String) {
recipientPreferences(accountId: $accountId) {
nodes {
templateId
status
Expand Down Expand Up @@ -88,19 +88,21 @@ const DRAFT_PREFERENCE_PAGE = `
}
`;

type GetRecipientPreferences = () => Promise<any>;
type GetRecipientPreferences = (accountId?: string) => Promise<any>;
export const getRecipientPreferences =
(client: Client | undefined): GetRecipientPreferences =>
async () => {
async (accountId?: string) => {
if (!client) {
return;
}

const results = await client.query(RECIPIENT_PREFERENCES).toPromise();
const results = await client
.query(RECIPIENT_PREFERENCES, { accountId })
.toPromise();
return results.data?.recipientPreferences.nodes;
};

type GetPreferencePage = () => Promise<any>;
type GetPreferencePage = (accountId?: string) => Promise<any>;
export const getPreferencePage =
(client: Client | undefined): GetPreferencePage =>
async (accountId?: string) => {
Expand All @@ -127,8 +129,8 @@ export const getDraftPreferencePage =
};

const UPDATE_RECIPIENT_PREFERENCES = `
mutation UpdateRecipientPreferences($id: String!, $preferences: PreferencesInput!) {
updatePreferences(templateId: $id preferences: $preferences)
mutation UpdateRecipientPreferences($id: String!, $preferences: PreferencesInput!, $accountId: String) {
updatePreferences(templateId: $id preferences: $preferences accountId: $accountId)
}
`;

Expand All @@ -138,6 +140,7 @@ type UpdateRecipientPreferences = (payload: {
hasCustomRouting: boolean;
routingPreferences: Array<string>;
digestSchedule: string;
accountId?: string;
}) => Promise<any>;
export const updateRecipientPreferences =
(client: Client | undefined): UpdateRecipientPreferences =>
Expand All @@ -155,6 +158,7 @@ export const updateRecipientPreferences =
routingPreferences: payload.routingPreferences,
digestSchedule: payload.digestSchedule,
},
accountId: payload.accountId,
})
.toPromise();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const usePreferencesActions = () => {
const preferences = Preferences({ client: courierClient });

return {
fetchRecipientPreferences: () => {
fetchRecipientPreferences: (accountId?: string) => {
dispatch({
type: "preferences/FETCH_RECIPIENT_PREFERENCES",
payload: () => preferences.getRecipientPreferences(),
payload: () => preferences.getRecipientPreferences(accountId),
});
},
fetchPreferencePage: (accountId?: string, draft = false) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-inbox/src/components/Messages2.0/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ const Messages: React.ForwardRefExoticComponent<
);

useEffect(() => {
fetchRecipientPreferences();
fetchRecipientPreferences(accountId);
}, []);

const handleCloseInbox = (event: React.MouseEvent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const PreferenceList: React.FunctionComponent<{
const preferences = usePreferences();

useEffect(() => {
preferences.fetchRecipientPreferences();
preferences.fetchRecipientPreferences(accountId);
preferences.fetchPreferencePage(accountId, draft);
}, []);

Expand Down
7 changes: 6 additions & 1 deletion packages/react-preferences/src/components/PreferencesV4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { StyledToggle } from "./StyledToggle";
import Toggle from "react-toggle";
import { PreferenceSection } from "@trycourier/react-hooks";
import DigestSchedules from "./DigestSchedule";
import { useCourier } from "@trycourier/react-provider";

export const ChannelOption = styled.div`
display: flex;
Expand Down Expand Up @@ -220,6 +221,7 @@ export const PreferenceTopic: React.FunctionComponent<{
hasCustomRouting,
defaultHasCustomRouting,
}) => {
const { accountId } = useCourier();
const { preferencePage, updateRecipientPreferences } = usePreferences();

//Temporary mapping until I update this in the backend
Expand All @@ -243,6 +245,7 @@ export const PreferenceTopic: React.FunctionComponent<{
updateRecipientPreferences({
templateId: topicId,
status: newStatus,
accountId,
});

setStatusToggle(!statusToggle);
Expand All @@ -263,6 +266,7 @@ export const PreferenceTopic: React.FunctionComponent<{
],
}),
status: statusToggle ? "OPTED_IN" : "OPTED_OUT",
accountId,
});

// If Customize Delivery is turned on, set the routing preferences to the default
Expand All @@ -282,6 +286,7 @@ export const PreferenceTopic: React.FunctionComponent<{
routingPreferences: newRouting,
hasCustomRouting: true,
status: statusToggle ? "OPTED_IN" : "OPTED_OUT",
accountId,
});

setRouting(newRouting);
Expand Down Expand Up @@ -431,7 +436,7 @@ export const PreferencesV4: React.FC<{ accountId?: string; draft?: boolean }> =
const pullPreferences = async () => {
if (!preferences.preferencePage && !preferences.recipientPreferences) {
await preferences.fetchPreferencePage(accountId, draft);
await preferences.fetchRecipientPreferences();
await preferences.fetchRecipientPreferences(accountId);
}
};
pullPreferences();
Expand Down

0 comments on commit 97c4dbf

Please sign in to comment.