2062 view edit an api key (#2231)

* Add query to get api keys

* Add a link to apiKey detail page

* Reset generatedApiKey when leaving page

* Simplify stuff

* Regenerate key when clicking on button

* Simplify

* Fix test

* Refetch apiKeys when delete or create one

* Add test for utils

* Create utils function

* Enable null expiration dates

* Update formatExpiration

* Fix display

* Fix noteCard

* Fix errors

* Fix reset

* Fix display

* Fix renaming

* Fix tests

* Fix ci

* Fix mocked data

* Fix test

* Update coverage requiremeents

* Rename folder

* Code review returns

* Symplify sht code
This commit is contained in:
martmull 2023-10-26 11:32:44 +02:00 committed by GitHub
parent 2b1945a3e1
commit fc4075b372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 434 additions and 183 deletions

View File

@ -17,9 +17,9 @@ const modulesCoverage = {
};
const pagesCoverage = {
"statements": 60,
"lines": 60,
"functions": 55,
"statements": 50,
"lines": 50,
"functions": 45,
"exclude": [
"src/generated/**/*",
"src/modules/**/*",

View File

@ -3807,22 +3807,7 @@ export type GetActivitiesQueryVariables = Exact<{
}>;
export type GetActivitiesQuery = {
__typename?: 'Query',
findManyActivities: Array<{
__typename?: 'Activity';
id: string;
createdAt: string,
title?: string | null,
body?: string | null,
type: ActivityType,
completedAt?: string | null,
dueAt?: string | null,
assignee?: { __typename?: 'User', id: string, firstName?: string | null, lastName?: string | null, displayName: string, avatarUrl?: string | null } | null,
author: { __typename?: 'User', id: string, firstName?: string | null, lastName?: string | null, displayName: string },
comments?: Array<Comment>,
activityTargets?: Array<{ __typename?: 'ActivityTarget', id: string, companyId?: string | null, personId?: string | null, company?: { __typename?: 'Company', id: string, name: string, domainName: string } | null, person?: { __typename?: 'Person', id: string, displayName: string, avatarUrl?: string | null } | null }> | null
}> };
export type GetActivitiesQuery = { __typename?: 'Query', findManyActivities: Array<{ __typename?: 'Activity', id: string, createdAt: string, title?: string | null, body?: string | null, type: ActivityType, completedAt?: string | null, dueAt?: string | null, assignee?: { __typename?: 'User', id: string, firstName?: string | null, lastName?: string | null, displayName: string, avatarUrl?: string | null } | null, author: { __typename?: 'User', id: string, firstName?: string | null, lastName?: string | null, displayName: string }, comments?: Array<{ __typename?: 'Comment', id: string, body: string, createdAt: string, updatedAt: string, author: { __typename?: 'User', id: string, displayName: string, firstName?: string | null, lastName?: string | null, avatarUrl?: string | null } }> | null, activityTargets?: Array<{ __typename?: 'ActivityTarget', id: string, companyId?: string | null, personId?: string | null, company?: { __typename?: 'Company', id: string, name: string, domainName: string } | null, person?: { __typename?: 'Person', id: string, displayName: string, avatarUrl?: string | null } | null }> | null }> };
export type GetActivitiesByTargetsQueryVariables = Exact<{
activityTargetIds: Array<Scalars['String']> | Scalars['String'];
@ -4225,7 +4210,12 @@ export type GetApiKeyQueryVariables = Exact<{
}>;
export type GetApiKeyQuery = { __typename?: 'Query', findManyApiKey: Array<{ __typename?: 'ApiKey', id: string, name: string, expiresAt?: string | null }> };
export type GetApiKeyQuery = { __typename?: 'Query', findManyApiKey: Array<{ __typename?: 'ApiKey', id: string, name: string, expiresAt?: string | null, createdAt: string }> };
export type GetApiKeysQueryVariables = Exact<{ [key: string]: never; }>;
export type GetApiKeysQuery = { __typename?: 'Query', findManyApiKey: Array<{ __typename?: 'ApiKey', id: string, name: string, expiresAt?: string | null, createdAt: string }> };
export type UserFieldsFragmentFragment = { __typename?: 'User', id: string, email: string, displayName: string, firstName?: string | null, lastName?: string | null };
@ -6954,6 +6944,7 @@ export const GetApiKeyDocument = gql`
id
name
expiresAt
createdAt
}
}
`;
@ -6985,6 +6976,43 @@ export function useGetApiKeyLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<
export type GetApiKeyQueryHookResult = ReturnType<typeof useGetApiKeyQuery>;
export type GetApiKeyLazyQueryHookResult = ReturnType<typeof useGetApiKeyLazyQuery>;
export type GetApiKeyQueryResult = Apollo.QueryResult<GetApiKeyQuery, GetApiKeyQueryVariables>;
export const GetApiKeysDocument = gql`
query GetApiKeys {
findManyApiKey {
id
name
expiresAt
createdAt
}
}
`;
/**
* __useGetApiKeysQuery__
*
* To run a query within a React component, call `useGetApiKeysQuery` and pass it any options that fit your needs.
* When your component renders, `useGetApiKeysQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useGetApiKeysQuery({
* variables: {
* },
* });
*/
export function useGetApiKeysQuery(baseOptions?: Apollo.QueryHookOptions<GetApiKeysQuery, GetApiKeysQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useQuery<GetApiKeysQuery, GetApiKeysQueryVariables>(GetApiKeysDocument, options);
}
export function useGetApiKeysLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetApiKeysQuery, GetApiKeysQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<GetApiKeysQuery, GetApiKeysQueryVariables>(GetApiKeysDocument, options);
}
export type GetApiKeysQueryHookResult = ReturnType<typeof useGetApiKeysQuery>;
export type GetApiKeysLazyQueryHookResult = ReturnType<typeof useGetApiKeysLazyQuery>;
export type GetApiKeysQueryResult = Apollo.QueryResult<GetApiKeysQuery, GetApiKeysQueryVariables>;
export const DeleteUserAccountDocument = gql`
mutation DeleteUserAccount {
deleteUserAccount {

View File

@ -9,7 +9,7 @@ import {
GenericFieldContextType,
} from '@/ui/data/field/contexts/FieldContext';
import { IconComment } from '@/ui/display/icon';
import { Activity, ActivityTarget } from '~/generated/graphql';
import { Activity, ActivityTarget, Comment } from '~/generated/graphql';
const StyledCard = styled.div`
align-items: flex-start;
@ -76,9 +76,10 @@ export const NoteCard = ({
}: {
note: Pick<
Activity,
'id' | 'title' | 'body' | 'type' | 'completedAt' | 'dueAt' | 'comments'
'id' | 'title' | 'body' | 'type' | 'completedAt' | 'dueAt'
> & {
activityTargets?: Array<Pick<ActivityTarget, 'id'>> | null;
comments?: Array<Pick<Comment, 'id'>> | null;
};
}) => {
const theme = useTheme();

View File

@ -5,7 +5,6 @@ import { IconCopy } from '@/ui/display/icon';
import { useSnackBar } from '@/ui/feedback/snack-bar/hooks/useSnackBar';
import { Button } from '@/ui/input/button/components/Button';
import { TextInput } from '@/ui/input/components/TextInput';
import { beautifyDateDiff } from '~/utils/date-utils';
const StyledContainer = styled.div`
display: flex;
@ -17,22 +16,16 @@ const StyledLinkContainer = styled.div`
margin-right: ${({ theme }) => theme.spacing(2)};
`;
type ApiKeyInputProps = { expiresAt?: string | null; apiKey: string };
type ApiKeyInputProps = { apiKey: string };
export const ApiKeyInput = ({ expiresAt, apiKey }: ApiKeyInputProps) => {
export const ApiKeyInput = ({ apiKey }: ApiKeyInputProps) => {
const theme = useTheme();
const computeInfo = () => {
if (!expiresAt) {
return '';
}
return `This key will expire in ${beautifyDateDiff(expiresAt)}`;
};
const { enqueueSnackBar } = useSnackBar();
return (
<StyledContainer>
<StyledLinkContainer>
<TextInput info={computeInfo()} value={apiKey} fullWidth />
<TextInput value={apiKey} fullWidth />
</StyledLinkContainer>
<Button
Icon={IconCopy}

View File

@ -1,12 +1,11 @@
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { ApiFieldItem } from '@/settings/developers/types/ApiFieldItem';
import { IconChevronRight } from '@/ui/display/icon';
import { TableCell } from '@/ui/layout/table/components/TableCell';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { ApisFiedlItem } from '../types/ApisFieldItem';
export const StyledApisFieldTableRow = styled(TableRow)`
grid-template-columns: 180px 148px 148px 36px;
`;
@ -27,13 +26,15 @@ const StyledIconChevronRight = styled(IconChevronRight)`
export const SettingsApiKeysFieldItemTableRow = ({
fieldItem,
onClick,
}: {
fieldItem: ApisFiedlItem;
fieldItem: ApiFieldItem;
onClick: () => void;
}) => {
const theme = useTheme();
return (
<StyledApisFieldTableRow onClick={() => {}}>
<StyledApisFieldTableRow onClick={() => onClick()}>
<StyledNameTableCell>{fieldItem.name}</StyledNameTableCell>
<TableCell color={theme.font.color.tertiary}>Internal</TableCell>{' '}
<TableCell

View File

@ -8,7 +8,6 @@ const meta: Meta<typeof ApiKeyInput> = {
component: ApiKeyInput,
decorators: [ComponentDecorator],
args: {
expiresAt: '2123-11-06T23:59:59.825Z',
apiKey:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0d2VudHktN2VkOWQyMTItMWMyNS00ZDAyLWJmMjUtNmFlY2NmN2VhNDE5IiwiaWF0IjoxNjk4MTQyODgyLCJleHAiOjE2OTk0MDE1OTksImp0aSI6ImMyMmFiNjQxLTVhOGYtNGQwMC1iMDkzLTk3MzUwYTM2YzZkOSJ9.JIe2TX5IXrdNl3n-kRFp3jyfNUE7unzXZLAzm2Gxl98',
},

View File

@ -1,5 +1,5 @@
export const ExpirationDates: {
value: number;
value: number | null;
label: string;
}[] = [
{ label: '15 days', value: 15 },
@ -7,5 +7,5 @@ export const ExpirationDates: {
{ label: '90 days', value: 90 },
{ label: '1 year', value: 365 },
{ label: '2 years', value: 2 * 365 },
{ label: 'Never', value: 10 * 365 },
{ label: 'Never', value: null },
];

View File

@ -1,36 +0,0 @@
import { v4 } from 'uuid';
import { ApisFiedlItem } from '../types/ApisFieldItem';
export const activeApiKeyItems: ApisFiedlItem[] = [
{
id: v4(),
name: 'Zapier key',
type: 'internal',
expiration: 'In 80 days',
},
{
id: v4(),
name: 'Notion',
type: 'internal',
expiration: 'Expired',
},
{
id: v4(),
name: 'Trello',
type: 'internal',
expiration: 'In 1 year',
},
{
id: v4(),
name: 'Cargo',
type: 'published',
expiration: 'Never',
},
{
id: v4(),
name: 'Backoffice',
type: 'published',
expiration: 'In 32 days',
},
];

View File

@ -6,6 +6,7 @@ export const GET_API_KEY = gql`
id
name
expiresAt
createdAt
}
}
`;

View File

@ -0,0 +1,12 @@
import { gql } from '@apollo/client';
export const GET_API_KEYS = gql`
query GetApiKeys {
findManyApiKey {
id
name
expiresAt
createdAt
}
}
`;

View File

@ -0,0 +1,12 @@
import { useRecoilCallback } from 'recoil';
import { generatedApiKeyFamilyState } from '@/settings/developers/states/generatedApiKeyFamilyState';
export const useGeneratedApiKeys = () => {
return useRecoilCallback(
({ set }) =>
(apiKeyId: string, apiKey: string | null) => {
set(generatedApiKeyFamilyState(apiKeyId), apiKey);
},
);
};

View File

@ -0,0 +1,9 @@
import { atomFamily } from 'recoil';
export const generatedApiKeyFamilyState = atomFamily<
string | null | undefined,
string
>({
key: 'generatedApiKeyFamilyState',
default: null,
});

View File

@ -1,6 +0,0 @@
import { atom } from 'recoil';
export const generatedApiKeyState = atom<string | null | undefined>({
key: 'generatedApiKeyState',
default: null,
});

View File

@ -1,4 +1,4 @@
export type ApisFiedlItem = {
export type ApiFieldItem = {
id: string;
name: string;
type: 'internal' | 'published';

View File

@ -0,0 +1,23 @@
import { computeNewExpirationDate } from '@/settings/developers/utils/compute-new-expiration-date';
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
describe('computeNewExpirationDate', () => {
it('should compute properly', () => {
const expiresAt = '2023-01-10T00:00:00.000Z';
const createdAt = '2023-01-01T00:00:00.000Z';
const result = computeNewExpirationDate(expiresAt, createdAt);
expect(result).toEqual('2024-01-10T00:00:00.000Z');
});
it('should compute properly with same values', () => {
const expiresAt = '2023-01-01T10:00:00.000Z';
const createdAt = '2023-01-01T10:00:00.000Z';
const result = computeNewExpirationDate(expiresAt, createdAt);
expect(result).toEqual('2024-01-01T00:00:00.000Z');
});
it('should compute properly with no expiration', () => {
const createdAt = '2023-01-01T10:00:00.000Z';
const result = computeNewExpirationDate(undefined, createdAt);
expect(result).toEqual(null);
});
});

View File

@ -0,0 +1,34 @@
import { formatExpiration } from '@/settings/developers/utils/format-expiration';
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
describe('formatExpiration', () => {
it('should format properly when expiresAt is null', () => {
const expiresAt = null;
const result = formatExpiration(expiresAt);
expect(result).toEqual('Never');
const resultWithExpiresMention = formatExpiration(expiresAt, true);
expect(resultWithExpiresMention).toEqual('Never expires');
});
it('should format properly when expiresAt is not null', () => {
const expiresAt = '2024-01-10T00:00:00.000Z';
const result = formatExpiration(expiresAt);
expect(result).toEqual('In 9 days');
const resultWithExpiresMention = formatExpiration(expiresAt, true);
expect(resultWithExpiresMention).toEqual('Expires in 9 days');
});
it('should format properly when expiresAt is large', () => {
const expiresAt = '2034-01-10T00:00:00.000Z';
const result = formatExpiration(expiresAt);
expect(result).toEqual('In 10 years');
const resultWithExpiresMention = formatExpiration(expiresAt, true);
expect(resultWithExpiresMention).toEqual('Expires in 10 years');
});
it('should format properly when expiresAt is large and long version', () => {
const expiresAt = '2034-01-10T00:00:00.000Z';
const result = formatExpiration(expiresAt, undefined, false);
expect(result).toEqual('In 10 years and 9 days');
const resultWithExpiresMention = formatExpiration(expiresAt, true, false);
expect(resultWithExpiresMention).toEqual('Expires in 10 years and 9 days');
});
});

View File

@ -0,0 +1,14 @@
import { DateTime } from 'luxon';
export const computeNewExpirationDate = (
expiresAt: string | null | undefined,
createdAt: string,
): string | null => {
if (!expiresAt) {
return null;
}
const days = DateTime.fromISO(expiresAt).diff(DateTime.fromISO(createdAt), [
'days',
]).days;
return DateTime.utc().plus({ days }).toISO();
};

View File

@ -0,0 +1,31 @@
import { ApiFieldItem } from '@/settings/developers/types/ApiFieldItem';
import { GetApiKeysQuery } from '~/generated/graphql';
import { beautifyDateDiff } from '~/utils/date-utils';
export const formatExpiration = (
expiresAt: string | null,
withExpiresMention: boolean = false,
short: boolean = true,
) => {
if (expiresAt) {
const dateDiff = beautifyDateDiff(expiresAt, undefined, short);
if (dateDiff.includes('-')) {
return 'Expired';
}
return withExpiresMention ? `Expires in ${dateDiff}` : `In ${dateDiff}`;
}
return withExpiresMention ? 'Never expires' : 'Never';
};
export const formatExpirations = (
apiKeysQuery: GetApiKeysQuery,
): ApiFieldItem[] => {
return apiKeysQuery.findManyApiKey.map(({ id, name, expiresAt }) => {
return {
id,
name,
expiration: formatExpiration(expiresAt || null),
type: 'internal',
};
});
};

View File

@ -76,6 +76,7 @@ export {
IconPlug,
IconPlus,
IconProgressCheck,
IconRepeat,
IconRobot,
IconSearch,
IconSettings,

View File

@ -11,7 +11,7 @@ import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { SelectHotkeyScope } from '../types/SelectHotkeyScope';
export type SelectProps<Value extends string | number> = {
export type SelectProps<Value extends string | number | null> = {
dropdownScopeId: string;
onChange: (value: Value) => void;
options: { value: Value; label: string; Icon?: IconComponent }[];
@ -38,7 +38,7 @@ const StyledLabel = styled.div`
gap: ${({ theme }) => theme.spacing(1)};
`;
export const Select = <Value extends string | number>({
export const Select = <Value extends string | number | null>({
dropdownScopeId,
onChange,
options,

View File

@ -25,7 +25,6 @@ export type TextInputComponentProps = Omit<
> & {
className?: string;
label?: string;
info?: string;
onChange?: (text: string) => void;
fullWidth?: boolean;
disableHotkeys?: boolean;
@ -46,13 +45,6 @@ const StyledLabel = styled.span`
text-transform: uppercase;
`;
const StyledInfo = styled.span`
color: ${({ theme }) => theme.font.color.light};
font-size: ${({ theme }) => theme.font.size.sm};
font-weight: ${({ theme }) => theme.font.weight.regular};
margin-top: ${({ theme }) => theme.spacing(1)};
`;
const StyledInputContainer = styled.div`
display: flex;
flex-direction: row;
@ -120,7 +112,6 @@ const TextInputComponent = (
{
className,
label,
info,
value,
onChange,
onFocus,
@ -212,7 +203,6 @@ const TextInputComponent = (
)}
</StyledTrailingIconContainer>
</StyledInputContainer>
{info && <StyledInfo>{info}</StyledInfo>}
{error && <StyledErrorHelper>{error}</StyledErrorHelper>}
</StyledContainer>
);

View File

@ -6,11 +6,11 @@ import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator';
import { Select, SelectProps } from '../Select';
type RenderProps = SelectProps<string | number>;
type RenderProps = SelectProps<string | number | null>;
const Render = (args: RenderProps) => {
const [value, setValue] = useState(args.value);
const handleChange = (value: string | number) => {
const handleChange = (value: string | number | null) => {
args.onChange?.(value);
setValue(value);
};

View File

@ -38,7 +38,3 @@ export const Filled: Story = {
export const Disabled: Story = {
args: { disabled: true, value: 'Tim' },
};
export const WithInfo: Story = {
args: { info: 'Some info displayed below the input', value: 'Tim' },
};

View File

@ -1,11 +1,18 @@
import { useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { getOperationName } from '@apollo/client/utilities';
import styled from '@emotion/styled';
import { useRecoilState } from 'recoil';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { ApiKeyInput } from '@/settings/developers/components/ApiKeyInput';
import { generatedApiKeyState } from '@/settings/developers/states/generatedApiKeyState';
import { IconSettings, IconTrash } from '@/ui/display/icon';
import { GET_API_KEYS } from '@/settings/developers/graphql/queries/getApiKeys';
import { useGeneratedApiKeys } from '@/settings/developers/hooks/useGeneratedApiKeys';
import { generatedApiKeyFamilyState } from '@/settings/developers/states/generatedApiKeyFamilyState';
import { computeNewExpirationDate } from '@/settings/developers/utils/compute-new-expiration-date';
import { formatExpiration } from '@/settings/developers/utils/format-expiration';
import { IconRepeat, IconSettings, IconTrash } from '@/ui/display/icon';
import { H2Title } from '@/ui/display/typography/components/H2Title';
import { Button } from '@/ui/input/button/components/Button';
import { TextInput } from '@/ui/input/components/TextInput';
@ -15,62 +22,159 @@ import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb';
import {
useDeleteOneApiKeyMutation,
useGetApiKeyQuery,
useInsertOneApiKeyMutation,
} from '~/generated/graphql';
const StyledInfo = styled.span`
color: ${({ theme }) => theme.font.color.light};
font-size: ${({ theme }) => theme.font.size.sm};
font-weight: ${({ theme }) => theme.font.weight.regular};
`;
const StyledInputContainer = styled.div`
align-items: center;
display: flex;
flex-direction: row;
gap: ${({ theme }) => theme.spacing(2)};
width: 100%;
`;
export const SettingsDevelopersApiKeyDetail = () => {
const navigate = useNavigate();
const { apiKeyId = '' } = useParams();
const [generatedApiKey] = useRecoilState(generatedApiKeyState);
const apiKeyQuery = useGetApiKeyQuery({
const setGeneratedApi = useGeneratedApiKeys();
const [generatedApiKey] = useRecoilState(
generatedApiKeyFamilyState(apiKeyId),
);
const [deleteApiKey] = useDeleteOneApiKeyMutation();
const [insertOneApiKey] = useInsertOneApiKeyMutation();
const apiKeyData = useGetApiKeyQuery({
variables: {
apiKeyId,
},
});
const [deleteApiKey] = useDeleteOneApiKeyMutation();
const deleteIntegration = async () => {
await deleteApiKey({ variables: { apiKeyId } });
navigate('/settings/developers/api-keys');
}).data?.findManyApiKey[0];
const deleteIntegration = async (redirect = true) => {
await deleteApiKey({
variables: { apiKeyId },
refetchQueries: [getOperationName(GET_API_KEYS) ?? ''],
});
if (redirect) {
navigate('/settings/developers/api-keys');
}
};
const { expiresAt, name } = apiKeyQuery.data?.findManyApiKey[0] || {};
const regenerateApiKey = async () => {
if (apiKeyData?.name) {
const newExpiresAt = computeNewExpirationDate(
apiKeyData.expiresAt,
apiKeyData.createdAt,
);
const apiKey = await insertOneApiKey({
variables: {
data: {
name: apiKeyData.name,
expiresAt: newExpiresAt,
},
},
refetchQueries: [getOperationName(GET_API_KEYS) ?? ''],
});
await deleteIntegration(false);
if (apiKey.data?.createOneApiKey) {
setGeneratedApi(
apiKey.data.createOneApiKey.id,
apiKey.data.createOneApiKey.token,
);
navigate(
`/settings/developers/api-keys/${apiKey.data.createOneApiKey.id}`,
);
}
}
};
useEffect(() => {
if (apiKeyData) {
return () => {
setGeneratedApi(apiKeyId, null);
};
}
});
return (
<SubMenuTopBarContainer Icon={IconSettings} title="Settings">
<SettingsPageContainer>
<SettingsHeaderContainer>
<Breadcrumb
links={[
{ children: 'APIs', href: '/settings/developers/api-keys' },
{ children: name || '' },
]}
/>
</SettingsHeaderContainer>
<Section>
<H2Title
title="Api Key"
description="Copy this key as it will only be visible this one time"
/>
<ApiKeyInput expiresAt={expiresAt} apiKey={generatedApiKey || ''} />
</Section>
<Section>
<H2Title title="Name" description="Name of your API key" />
<TextInput
placeholder="E.g. backoffice integration"
value={name || ''}
disabled={true}
fullWidth
/>
</Section>
<Section>
<H2Title title="Danger zone" description="Delete this integration" />
<Button
accent="danger"
variant="secondary"
title="Disable"
Icon={IconTrash}
onClick={deleteIntegration}
/>
</Section>
</SettingsPageContainer>
</SubMenuTopBarContainer>
<>
{apiKeyData?.name && (
<SubMenuTopBarContainer Icon={IconSettings} title="Settings">
<SettingsPageContainer>
<SettingsHeaderContainer>
<Breadcrumb
links={[
{ children: 'APIs', href: '/settings/developers/api-keys' },
{ children: apiKeyData.name },
]}
/>
</SettingsHeaderContainer>
<Section>
{generatedApiKey ? (
<>
<H2Title
title="Api Key"
description="Copy this key as it will only be visible this one time"
/>
<ApiKeyInput apiKey={generatedApiKey} />
<StyledInfo>
{formatExpiration(apiKeyData?.expiresAt || '', true, false)}
</StyledInfo>
</>
) : (
<>
<H2Title
title="Api Key"
description="Regenerate an Api key"
/>
<StyledInputContainer>
<Button
title="Regenerate Key"
Icon={IconRepeat}
onClick={regenerateApiKey}
/>
<StyledInfo>
{formatExpiration(
apiKeyData?.expiresAt || '',
true,
false,
)}
</StyledInfo>
</StyledInputContainer>
</>
)}
</Section>
<Section>
<H2Title title="Name" description="Name of your API key" />
<TextInput
placeholder="E.g. backoffice integration"
value={apiKeyData.name}
disabled
fullWidth
/>
</Section>
<Section>
<H2Title
title="Danger zone"
description="Delete this integration"
/>
<Button
accent="danger"
variant="secondary"
title="Disable"
Icon={IconTrash}
onClick={() => deleteIntegration()}
/>
</Section>
</SettingsPageContainer>
</SubMenuTopBarContainer>
)}
</>
);
};

View File

@ -3,7 +3,7 @@ import styled from '@emotion/styled';
import { objectSettingsWidth } from '@/settings/data-model/constants/objectSettings';
import { SettingsApiKeysFieldItemTableRow } from '@/settings/developers/components/SettingsApiKeysFieldItemTableRow';
import { activeApiKeyItems } from '@/settings/developers/constants/mockObjects';
import { formatExpirations } from '@/settings/developers/utils/format-expiration';
import { IconPlus, IconSettings } from '@/ui/display/icon';
import { H1Title } from '@/ui/display/typography/components/H1Title';
import { H2Title } from '@/ui/display/typography/components/H2Title';
@ -12,6 +12,7 @@ import { SubMenuTopBarContainer } from '@/ui/layout/page/SubMenuTopBarContainer'
import { Table } from '@/ui/layout/table/components/Table';
import { TableHeader } from '@/ui/layout/table/components/TableHeader';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { useGetApiKeysQuery } from '~/generated/graphql';
const StyledContainer = styled.div`
height: fit-content;
@ -36,6 +37,8 @@ const StyledH1Title = styled(H1Title)`
export const SettingsDevelopersApiKeys = () => {
const navigate = useNavigate();
const apiKeysQuery = useGetApiKeysQuery();
const apiKeys = apiKeysQuery.data ? formatExpirations(apiKeysQuery.data) : [];
return (
<SubMenuTopBarContainer Icon={IconSettings} title="Settings">
@ -63,10 +66,13 @@ export const SettingsDevelopersApiKeys = () => {
<TableHeader>Expiration</TableHeader>
<TableHeader></TableHeader>
</StyledTableRow>
{activeApiKeyItems.map((fieldItem) => (
{apiKeys.map((fieldItem) => (
<SettingsApiKeysFieldItemTableRow
key={fieldItem.id}
fieldItem={fieldItem}
onClick={() => {
navigate(`/settings/developers/api-keys/${fieldItem.id}`);
}}
/>
))}
</Table>

View File

@ -1,13 +1,14 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { getOperationName } from '@apollo/client/utilities';
import { DateTime } from 'luxon';
import { useRecoilState } from 'recoil';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { ExpirationDates } from '@/settings/developers/constants/expirationDates';
import { generatedApiKeyState } from '@/settings/developers/states/generatedApiKeyState';
import { GET_API_KEYS } from '@/settings/developers/graphql/queries/getApiKeys';
import { useGeneratedApiKeys } from '@/settings/developers/hooks/useGeneratedApiKeys';
import { IconSettings } from '@/ui/display/icon';
import { H2Title } from '@/ui/display/typography/components/H2Title';
import { Select } from '@/ui/input/components/Select';
@ -20,10 +21,10 @@ import { useInsertOneApiKeyMutation } from '~/generated/graphql';
export const SettingsDevelopersApiKeysNew = () => {
const [insertOneApiKey] = useInsertOneApiKeyMutation();
const navigate = useNavigate();
const [, setGeneratedApiKey] = useRecoilState(generatedApiKeyState);
const setGeneratedApi = useGeneratedApiKeys();
const [formValues, setFormValues] = useState<{
name: string;
expirationDate: number;
expirationDate: number | null;
}>({
expirationDate: ExpirationDates[0].value,
name: '',
@ -33,16 +34,24 @@ export const SettingsDevelopersApiKeysNew = () => {
variables: {
data: {
name: formValues.name,
expiresAt: DateTime.now()
.plus({ days: formValues.expirationDate })
.toISODate(),
expiresAt: formValues.expirationDate
? DateTime.now()
.plus({ days: formValues.expirationDate })
.toISODate()
: null,
},
},
refetchQueries: [getOperationName(GET_API_KEYS) ?? ''],
});
setGeneratedApiKey(apiKey.data?.createOneApiKey?.token);
navigate(
`/settings/developers/api-keys/${apiKey.data?.createOneApiKey?.id}`,
);
if (apiKey.data?.createOneApiKey) {
setGeneratedApi(
apiKey.data.createOneApiKey.id,
apiKey.data.createOneApiKey.token,
);
navigate(
`/settings/developers/api-keys/${apiKey.data.createOneApiKey.id}`,
);
}
};
const canSave = !!formValues.name;
return (

View File

@ -6,7 +6,6 @@ import {
PageDecoratorArgs,
} from '~/testing/decorators/PageDecorator';
import { graphqlMocks } from '~/testing/graphqlMocks';
import { mockedApiKeyToken } from '~/testing/mock-data/api-keys';
import { sleep } from '~/testing/sleep';
const meta: Meta<PageDecoratorArgs> = {
@ -15,7 +14,6 @@ const meta: Meta<PageDecoratorArgs> = {
decorators: [PageDecorator],
args: {
routePath: '/settings/apis/f7c6d736-8fcd-4e9c-ab99-28f6a9031570',
state: mockedApiKeyToken,
},
parameters: {
msw: graphqlMocks,

View File

@ -11,38 +11,29 @@ import { FullHeightStorybookLayout } from '../FullHeightStorybookLayout';
export type PageDecoratorArgs = {
routePath: string;
routeParams: RouteParams;
state?: string;
};
type RouteParams = {
[param: string]: string;
};
const computeLocation = (
routePath: string,
routeParams: RouteParams,
state?: string,
) => {
const computeLocation = (routePath: string, routeParams: RouteParams) => {
return {
pathname: routePath.replace(
/:(\w+)/g,
(paramName) => routeParams[paramName] ?? '',
),
state,
};
};
export const PageDecorator: Decorator<{
routePath: string;
routeParams: RouteParams;
state?: string;
}> = (Story, { args }) => (
<UserProvider>
<ClientConfigProvider>
<MemoryRouter
initialEntries={[
computeLocation(args.routePath, args.routeParams, args.state),
]}
initialEntries={[computeLocation(args.routePath, args.routeParams)]}
>
<FullHeightStorybookLayout>
<HelmetProvider>

View File

@ -18,6 +18,7 @@ import { SEARCH_COMPANY_QUERY } from '@/search/graphql/queries/searchCompanyQuer
import { SEARCH_PEOPLE_QUERY } from '@/search/graphql/queries/searchPeopleQuery';
import { SEARCH_USER_QUERY } from '@/search/graphql/queries/searchUserQuery';
import { GET_API_KEY } from '@/settings/developers/graphql/queries/getApiKey';
import { GET_API_KEYS } from '@/settings/developers/graphql/queries/getApiKeys';
import { GET_CURRENT_USER } from '@/users/graphql/queries/getCurrentUser';
import { GET_VIEW_FIELDS } from '@/views/graphql/queries/getViewFields';
import { GET_VIEWS } from '@/views/graphql/queries/getViews';
@ -283,7 +284,14 @@ export const graphqlMocks = [
graphql.query(getOperationName(GET_API_KEY) ?? '', (req, res, ctx) => {
return res(
ctx.data({
findManyApiKey: mockedApiKeys[0],
findManyApiKey: [mockedApiKeys[0]],
}),
);
}),
graphql.query(getOperationName(GET_API_KEYS) ?? '', (req, res, ctx) => {
return res(
ctx.data({
findManyApiKey: mockedApiKeys,
}),
);
}),

View File

@ -4,8 +4,6 @@ type MockedApiKey = Pick<
ApiKey,
'id' | 'name' | 'createdAt' | 'updatedAt' | 'expiresAt' | '__typename'
>;
export const mockedApiKeyToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0d2VudHktN2VkOWQyMTItMWMyNS00ZDAyLWJmMjUtNmFlY2NmN2VhNDE5IiwiaWF0IjoxNjk4MDkzMDU0LCJleHAiOjE2OTkzMTUxOTksImp0aSI6IjY0Njg3ZWNmLWFhYzktNDNmYi1hY2I4LTE1M2QzNzgwYmIzMSJ9.JkQ3u7aRiqOFQkgHcC-mgCU37096HRSo40A_9X8gEng';
export const mockedApiKeys: Array<MockedApiKey> = [
{
id: 'f7c6d736-8fcd-4e9c-ab99-28f6a9031570',
@ -15,4 +13,20 @@ export const mockedApiKeys: Array<MockedApiKey> = [
expiresAt: '2100-11-06T23:59:59.825Z',
__typename: 'ApiKey',
},
{
id: 'f7c6d736-8fcd-4e9c-ab99-28f6a9031571',
name: 'Gmail Integration',
createdAt: '2023-04-26T10:12:42.33625+00:00',
updatedAt: '2023-04-26T10:23:42.33625+00:00',
expiresAt: null,
__typename: 'ApiKey',
},
{
id: 'f7c6d736-8fcd-4e9c-ab99-28f6a9031572',
name: 'Github Integration',
createdAt: '2023-04-26T10:12:42.33625+00:00',
updatedAt: '2023-04-26T10:23:42.33625+00:00',
expiresAt: '2022-11-06T23:59:59.825Z',
__typename: 'ApiKey',
},
];

View File

@ -14,6 +14,7 @@ import {
import { logError } from '../logError';
jest.mock('~/utils/logError');
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z'));
describe('beautifyExactDateTime', () => {
it('should return the date in the correct format with time', () => {
@ -277,8 +278,20 @@ describe('beautifyDateDiff', () => {
expect(result).toEqual('1 year and 2 days');
});
it('should compare to now', () => {
const date = '2200-11-01T00:00:00.000Z';
const date = '2027-01-10T00:00:00.000Z';
const result = beautifyDateDiff(date);
expect(result).toContain('years');
expect(result).toEqual('3 years and 9 days');
});
it('should return short version', () => {
const date = '2033-11-05T00:00:00.000Z';
const dateToCompareWith = '2023-11-01T00:00:00.000Z';
const result = beautifyDateDiff(date, dateToCompareWith, true);
expect(result).toEqual('10 years');
});
it('should return short version for short differences', () => {
const date = '2023-11-05T00:00:00.000Z';
const dateToCompareWith = '2023-11-01T00:00:00.000Z';
const result = beautifyDateDiff(date, dateToCompareWith, true);
expect(result).toEqual('4 days');
});
});

View File

@ -109,7 +109,11 @@ export const hasDatePassed = (date: Date | string | number) => {
}
};
export const beautifyDateDiff = (date: string, dateToCompareWith?: string) => {
export const beautifyDateDiff = (
date: string,
dateToCompareWith?: string,
short: boolean = false,
) => {
const dateDiff = DateTime.fromISO(date).diff(
dateToCompareWith ? DateTime.fromISO(dateToCompareWith) : DateTime.now(),
['years', 'days'],
@ -117,6 +121,7 @@ export const beautifyDateDiff = (date: string, dateToCompareWith?: string) => {
let result = '';
if (dateDiff.years) result = result + `${dateDiff.years} year`;
if (![0, 1].includes(dateDiff.years)) result = result + 's';
if (short && dateDiff.years) return result;
if (dateDiff.years && dateDiff.days) result = result + ' and ';
if (dateDiff.days) result = result + `${Math.floor(dateDiff.days)} day`;
if (![0, 1].includes(dateDiff.days)) result = result + 's';

View File

@ -14,7 +14,7 @@ SIGN_IN_PREFILLED=true
# DEBUG_MODE=true
# ACCESS_TOKEN_EXPIRES_IN=30m
# LOGIN_TOKEN_EXPIRES_IN=15m
# API_TOKEN_EXPIRES_IN=2y
# API_TOKEN_EXPIRES_IN=1000y
# REFRESH_TOKEN_EXPIRES_IN=90d
# FRONT_AUTH_CALLBACK_URL=http://localhost:3001/verify
# AUTH_GOOGLE_ENABLED=false

View File

@ -86,7 +86,7 @@ export class EnvironmentService {
}
getApiTokenExpiresIn(): string {
return this.configService.get<string>('API_TOKEN_EXPIRES_IN') ?? '2y';
return this.configService.get<string>('API_TOKEN_EXPIRES_IN') ?? '1000y';
}
getFrontAuthCallbackUrl(): string {