1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

feat: read productivity report from profile (#8662)

This commit is contained in:
Mateusz Kwasniewski 2024-11-05 16:14:19 +01:00 committed by GitHub
parent 7aa74cccd3
commit b00d449c72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 165 additions and 53 deletions

View File

@ -0,0 +1,105 @@
import { render } from 'utils/testRenderer';
import { screen } from '@testing-library/react';
import { testServerRoute, testServerSetup } from 'utils/testServer';
import { ProductivityEmailSubscription } from './ProductivityEmailSubscription';
import ToastRenderer from '../../../common/ToastRenderer/ToastRenderer';
const server = testServerSetup();
const setupSubscribeApi = () => {
testServerRoute(
server,
'/api/admin/email-subscription/productivity-report',
{},
'put',
202,
);
};
const setupUnsubscribeApi = () => {
testServerRoute(
server,
'/api/admin/email-subscription/productivity-report',
{},
'delete',
202,
);
};
const setupErrorApi = () => {
testServerRoute(
server,
'/api/admin/email-subscription/productivity-report',
{ message: 'user error' },
'delete',
400,
);
};
test('unsubscribe', async () => {
setupUnsubscribeApi();
let changed = false;
render(
<>
<ProductivityEmailSubscription
status='subscribed'
onChange={() => {
changed = true;
}}
/>
<ToastRenderer />
</>,
);
const checkbox = screen.getByLabelText('Productivity Email Subscription');
expect(checkbox).toBeChecked();
checkbox.click();
await screen.findByText('Unsubscribed from productivity report');
expect(changed).toBe(true);
});
test('subscribe', async () => {
setupSubscribeApi();
let changed = false;
render(
<>
<ProductivityEmailSubscription
status='unsubscribed'
onChange={() => {
changed = true;
}}
/>
<ToastRenderer />
</>,
);
const checkbox = screen.getByLabelText('Productivity Email Subscription');
expect(checkbox).not.toBeChecked();
checkbox.click();
await screen.findByText('Subscribed to productivity report');
expect(changed).toBe(true);
});
test('handle error', async () => {
setupErrorApi();
let changed = false;
render(
<>
<ProductivityEmailSubscription
status='subscribed'
onChange={() => {
changed = true;
}}
/>
<ToastRenderer />
</>,
);
const checkbox = screen.getByLabelText('Productivity Email Subscription');
checkbox.click();
await screen.findByText('user error');
expect(changed).toBe(true);
});

View File

@ -1,14 +1,14 @@
import { Box, Switch } from '@mui/material'; import { Box, FormControlLabel, Switch } from '@mui/material';
import { formatUnknownError } from 'utils/formatUnknownError'; import { formatUnknownError } from 'utils/formatUnknownError';
import { useState } from 'react'; import type { FC } from 'react';
import { useEmailSubscriptionApi } from 'hooks/api/actions/useEmailSubscriptionApi/useEmailSubscriptionApi'; import { useEmailSubscriptionApi } from 'hooks/api/actions/useEmailSubscriptionApi/useEmailSubscriptionApi';
import useToast from 'hooks/useToast'; import useToast from 'hooks/useToast';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker'; import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
export const ProductivityEmailSubscription = () => { export const ProductivityEmailSubscription: FC<{
// TODO: read data from user profile when available status: 'subscribed' | 'unsubscribed';
const [receiveProductivityReportEmail, setReceiveProductivityReportEmail] = onChange: () => void;
useState(false); }> = ({ status, onChange }) => {
const { const {
subscribe, subscribe,
unsubscribe, unsubscribe,
@ -19,44 +19,46 @@ export const ProductivityEmailSubscription = () => {
return ( return (
<Box> <Box>
Productivity Email Subscription <FormControlLabel
<Switch label='Productivity Email Subscription'
onChange={async () => { control={
try { <Switch
if (receiveProductivityReportEmail) { onChange={async () => {
await unsubscribe('productivity-report'); try {
setToastData({ if (status === 'subscribed') {
title: 'Unsubscribed from productivity report', await unsubscribe('productivity-report');
type: 'success', setToastData({
}); title: 'Unsubscribed from productivity report',
trackEvent('productivity-report', { type: 'success',
props: { });
eventType: 'subscribe', trackEvent('productivity-report', {
}, props: {
}); eventType: 'subscribe',
} else { },
await subscribe('productivity-report'); });
setToastData({ } else {
title: 'Subscribed to productivity report', await subscribe('productivity-report');
type: 'success', setToastData({
}); title: 'Subscribed to productivity report',
trackEvent('productivity-report', { type: 'success',
props: { });
eventType: 'unsubscribe', trackEvent('productivity-report', {
}, props: {
}); eventType: 'unsubscribe',
} },
} catch (error) { });
setToastApiError(formatUnknownError(error)); }
} } catch (error) {
setToastApiError(formatUnknownError(error));
}
setReceiveProductivityReportEmail( onChange();
!receiveProductivityReportEmail, }}
); name='productivity-email'
}} checked={status === 'subscribed'}
name='productivity-email' disabled={changingSubscriptionStatus}
checked={receiveProductivityReportEmail} />
disabled={changingSubscriptionStatus} }
/> />
</Box> </Box>
); );

View File

@ -87,7 +87,7 @@ interface IProfileTabProps {
} }
export const ProfileTab = ({ user }: IProfileTabProps) => { export const ProfileTab = ({ user }: IProfileTabProps) => {
const { profile } = useProfile(); const { profile, refetchProfile } = useProfile();
const navigate = useNavigate(); const navigate = useNavigate();
const { locationSettings, setLocationSettings } = useLocationSettings(); const { locationSettings, setLocationSettings } = useLocationSettings();
const [currentLocale, setCurrentLocale] = useState<string>(); const [currentLocale, setCurrentLocale] = useState<string>();
@ -223,7 +223,18 @@ export const ProfileTab = ({ user }: IProfileTabProps) => {
<> <>
<StyledDivider /> <StyledDivider />
<StyledSectionLabel>Email Settings</StyledSectionLabel> <StyledSectionLabel>Email Settings</StyledSectionLabel>
<ProductivityEmailSubscription /> {profile?.subscriptions && (
<ProductivityEmailSubscription
status={
profile.subscriptions.includes(
'productivity-report',
)
? 'subscribed'
: 'unsubscribed'
}
onChange={refetchProfile}
/>
)}
</> </>
) : null} ) : null}
</PageContent> </PageContent>

View File

@ -1,10 +1,10 @@
import useSWR from 'swr'; import useSWR from 'swr';
import { formatApiPath } from 'utils/formatPath'; import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler'; import handleErrorResponses from '../httpErrorResponseHandler';
import type { IProfile } from 'interfaces/profile'; import type { ProfileSchema } from '../../../../openapi';
export interface IUseProfileOutput { export interface IUseProfileOutput {
profile?: IProfile; profile?: ProfileSchema;
refetchProfile: () => void; refetchProfile: () => void;
loading: boolean; loading: boolean;
error?: Error; error?: Error;

View File

@ -1,6 +0,0 @@
import type { IRole } from './role';
export interface IProfile {
rootRole: IRole;
projects: string[];
}