Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use highSignalNotificationFilter to api #3588

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions apps/web/src/components/Notification/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import ToggleWithHelper from '@components/Shared/ToggleWithHelper';
import { BellIcon, CogIcon, ColorSwatchIcon } from '@heroicons/react/outline';
import { PREFERENCES_WORKER_URL } from '@lenster/data/constants';
import { Localstorage } from '@lenster/data/storage';
import { NOTIFICATION } from '@lenster/data/tracking';
import { Modal, Tooltip } from '@lenster/ui';
import { Leafwatch } from '@lib/leafwatch';
import { t } from '@lingui/macro';
import axios from 'axios';
import type { FC } from 'react';
import { useState } from 'react';
import { toast } from 'react-hot-toast';
import { useAppStore } from 'src/store/app';
import { usePreferencesStore } from 'src/store/preferences';

const Settings: FC = () => {
const currentProfile = useAppStore((state) => state.currentProfile);
const highSignalNotificationFilter = usePreferencesStore(
(state) => state.highSignalNotificationFilter
);
Expand All @@ -16,6 +24,26 @@ const Settings: FC = () => {
const [showNotificationSettings, setShowNotificationSettings] =
useState(false);

const toggleHighSignalNotificationFilter = () => {
toast.promise(
axios.post(`${PREFERENCES_WORKER_URL}/update`, {
id: currentProfile?.id,
highSignalNotificationFilter: !highSignalNotificationFilter,
accessToken: localStorage.getItem(Localstorage.AccessToken)
}),
{
loading: t`Updating notification settings...`,
success: () => {
setHighSignalNotificationFilter(!highSignalNotificationFilter);
Leafwatch.track(NOTIFICATION.TOGGLE_HIGH_SIGNAL_NOTIFICATION_FILTER);

return t`Notification settings updated`;
},
error: t`Error updating notification settings`
}
);
};

return (
<>
<button
Expand All @@ -35,9 +63,7 @@ const Settings: FC = () => {
<div className="p-5">
<ToggleWithHelper
on={highSignalNotificationFilter}
setOn={() => {
setHighSignalNotificationFilter(!highSignalNotificationFilter);
}}
setOn={toggleHighSignalNotificationFilter}
heading={t`Signal filter`}
description={t`Turn on high-signal notification filter`}
icon={<ColorSwatchIcon className="h-4 w-4" />}
Expand Down
4 changes: 3 additions & 1 deletion packages/data/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const PUBLICATION = {
};

export const NOTIFICATION = {
SWITCH_NOTIFICATION_TAB: 'Switch notifications tab'
SWITCH_NOTIFICATION_TAB: 'Switch notifications tab',
TOGGLE_HIGH_SIGNAL_NOTIFICATION_FILTER:
'Toggle high signal notification filter'
};

export const HOME = {
Expand Down
12 changes: 9 additions & 3 deletions packages/workers/preferences/src/handlers/updatePreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ExtensionRequest = {
isGardener?: boolean;
isTrustedMember?: boolean;
isVerified?: boolean;
highSignalNotificationFilter?: boolean;
updateByAdmin?: boolean;
accessToken: string;
};
Expand All @@ -27,6 +28,7 @@ const validationSchema = object({
isGardener: boolean().optional(),
isTrustedMember: boolean().optional(),
isVerified: boolean().optional(),
highSignalNotificationFilter: boolean().optional(),
updateByAdmin: boolean().optional(),
accessToken: string().regex(Regex.accessToken)
});
Expand All @@ -50,6 +52,7 @@ export default async (request: IRequest, env: Env) => {
isTrustedMember,
updateByAdmin,
isVerified,
highSignalNotificationFilter,
accessToken
} = body as ExtensionRequest;

Expand Down Expand Up @@ -82,7 +85,8 @@ export default async (request: IRequest, env: Env) => {
is_gardener: isGardener,
is_trusted_member: isTrustedMember,
is_verified: isVerified
})
}),
high_signal_notification_filter: highSignalNotificationFilter
})
.select()
.single();
Expand All @@ -91,8 +95,10 @@ export default async (request: IRequest, env: Env) => {
throw error;
}

// Clear cache in Cloudflare KV
await env.PREFERENCES.delete('verified-list');
if (updateByAdmin) {
// Clear cache in Cloudflare KV
await env.PREFERENCES.delete('verified-list');
}

return response({ success: true, result: data });
} catch (error) {
Expand Down
Loading