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: productivity report subscription UI #8639

Merged
merged 2 commits into from
Nov 4, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Box, Switch } from '@mui/material';
import { formatUnknownError } from 'utils/formatUnknownError';
import { useState } from 'react';
import { useEmailSubscriptionApi } from 'hooks/api/actions/useEmailSubscriptionApi/useEmailSubscriptionApi';
import useToast from 'hooks/useToast';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';

export const ProductivityEmailSubscription = () => {
// TODO: read data from user profile when available
const [receiveProductivityReportEmail, setReceiveProductivityReportEmail] =
useState(false);
const {
subscribe,
unsubscribe,
loading: changingSubscriptionStatus,
} = useEmailSubscriptionApi();
const { setToastData, setToastApiError } = useToast();
const { trackEvent } = usePlausibleTracker();

return (
<Box>
Productivity Email Subscription
<Switch
onChange={async () => {
try {
if (receiveProductivityReportEmail) {
await unsubscribe('productivity-report');
setToastData({
title: 'Unsubscribed from productivity report',
type: 'success',
});
trackEvent('productivity-report', {
props: {
eventType: 'subscribe',
},
});
} else {
await subscribe('productivity-report');
setToastData({
title: 'Subscribed to productivity report',
type: 'success',
});
trackEvent('productivity-report', {
props: {
eventType: 'unsubscribe',
},
});
}
} catch (error) {
setToastApiError(formatUnknownError(error));
}

setReceiveProductivityReportEmail(
!receiveProductivityReportEmail,
);
}}
name='productivity-email'
checked={receiveProductivityReportEmail}
disabled={changingSubscriptionStatus}
/>
</Box>
);
};
13 changes: 12 additions & 1 deletion frontend/src/component/user/Profile/ProfileTab/ProfileTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { useNavigate } from 'react-router-dom';
import { PageContent } from 'component/common/PageContent/PageContent';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { RoleBadge } from 'component/common/RoleBadge/RoleBadge';
import { useUiFlag } from 'hooks/useUiFlag';
import { ProductivityEmailSubscription } from './ProductivityEmailSubscription';

const StyledHeader = styled('div')(({ theme }) => ({
display: 'flex',
Expand Down Expand Up @@ -121,6 +123,8 @@ export const ProfileTab = ({ user }: IProfileTabProps) => {
setLocationSettings({ locale });
};

const productivityReportEmailEnabled = useUiFlag('productivityReportEmail');

return (
<>
<StyledHeader>
Expand Down Expand Up @@ -187,7 +191,7 @@ export const ProfileTab = ({ user }: IProfileTabProps) => {
</Box>
</StyledAccess>
<StyledDivider />
<StyledSectionLabel>Settings</StyledSectionLabel>
<StyledSectionLabel>Date/Time Settings</StyledSectionLabel>
<Typography variant='body2'>
This is the format used across the system for time and date
</Typography>
Expand Down Expand Up @@ -215,6 +219,13 @@ export const ProfileTab = ({ user }: IProfileTabProps) => {
})}
</Select>
</StyledFormControl>
{productivityReportEmailEnabled ? (
<>
<StyledDivider />
<StyledSectionLabel>Email Settings</StyledSectionLabel>
<ProductivityEmailSubscription />
</>
) : null}
</PageContent>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import useAPI from '../useApi/useApi';

export const useEmailSubscriptionApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});

const subscribe = async (subscriptionName: string) => {
const path = `api/admin/email-subscription/${subscriptionName}`;
const req = createRequest(path, {
method: 'PUT',
});

await makeRequest(req.caller, req.id);
};

const unsubscribe = async (subscriptionName: string) => {
const path = `api/admin/email-subscription/${subscriptionName}`;
const req = createRequest(path, {
method: 'DELETE',
});

await makeRequest(req.caller, req.id);
};

return {
subscribe,
unsubscribe,
errors,
loading,
};
};
3 changes: 2 additions & 1 deletion frontend/src/hooks/usePlausibleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export type CustomEvents =
| 'personal-dashboard'
| 'order-environments'
| 'unleash-ai-chat'
| 'project-navigation';
| 'project-navigation'
| 'productivity-report';

export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/interfaces/uiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export type UiFlags = {
releasePlans?: boolean;
'enterprise-payg'?: boolean;
simplifyProjectOverview?: boolean;
productivityReportEmail?: boolean;
};

export interface IVersionInfo {
Expand Down
Loading