Remove api keys from old world (#2548)
* Use apiKeyV2 for getApiKeys * Use apiKeyV2 for createApiKey * Use apiKeyV2 for getApiKey * Use apiKeyV2 to deleteapikey * Filter null revokedAt -> not working * Use apiKeyV2 to regenerate * Fix default values injected * Remove useless stuff * Fix type
This commit is contained in:
@ -2,7 +2,7 @@ import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { useOptimisticEffect } from '@/apollo/optimistic-effect/hooks/useOptimisticEffect';
|
||||
import { useCreateOneObjectRecord } from '@/object-record/hooks/useCreateOneObjectRecord';
|
||||
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
|
||||
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
|
||||
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
|
||||
@ -15,11 +15,10 @@ import { TextInput } from '@/ui/input/components/TextInput';
|
||||
import { SubMenuTopBarContainer } from '@/ui/layout/page/SubMenuTopBarContainer';
|
||||
import { Section } from '@/ui/layout/section/components/Section';
|
||||
import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb';
|
||||
import { useInsertOneApiKeyMutation } from '~/generated/graphql';
|
||||
import { useGenerateOneApiKeyTokenMutation } from '~/generated/graphql';
|
||||
|
||||
export const SettingsDevelopersApiKeysNew = () => {
|
||||
const [insertOneApiKey] = useInsertOneApiKeyMutation();
|
||||
const { triggerOptimisticEffects } = useOptimisticEffect('ApiKeyV2');
|
||||
const [generateOneApiKeyToken] = useGenerateOneApiKeyTokenMutation();
|
||||
const navigate = useNavigate();
|
||||
const setGeneratedApi = useGeneratedApiKeys();
|
||||
const [formValues, setFormValues] = useState<{
|
||||
@ -29,35 +28,36 @@ export const SettingsDevelopersApiKeysNew = () => {
|
||||
expirationDate: ExpirationDates[0].value,
|
||||
name: '',
|
||||
});
|
||||
|
||||
const { createOneObject: createOneApiKey } = useCreateOneObjectRecord({
|
||||
objectNamePlural: 'apiKeysV2',
|
||||
});
|
||||
const onSave = async () => {
|
||||
const apiKey = await insertOneApiKey({
|
||||
const expiresAt = formValues.expirationDate
|
||||
? DateTime.now().plus({ days: formValues.expirationDate }).toString()
|
||||
: null;
|
||||
const newApiKey = await createOneApiKey?.({
|
||||
name: formValues.name,
|
||||
expiresAt,
|
||||
});
|
||||
const tokenData = await generateOneApiKeyToken({
|
||||
variables: {
|
||||
data: {
|
||||
name: formValues.name,
|
||||
expiresAt: formValues.expirationDate
|
||||
? DateTime.now()
|
||||
.plus({ days: formValues.expirationDate })
|
||||
.toString()
|
||||
: null,
|
||||
id: newApiKey.createApiKeyV2.id,
|
||||
expiresAt: newApiKey.createApiKeyV2.expiresAt,
|
||||
name: newApiKey.createApiKeyV2.name, // TODO update typing to remove useless name param here
|
||||
},
|
||||
},
|
||||
update: (_cache, { data }) => {
|
||||
if (data?.createOneApiKey) {
|
||||
triggerOptimisticEffects('ApiKey', [data?.createOneApiKey]);
|
||||
}
|
||||
},
|
||||
});
|
||||
if (apiKey.data?.createOneApiKey) {
|
||||
if (tokenData.data?.generateApiKeyV2Token) {
|
||||
setGeneratedApi(
|
||||
apiKey.data.createOneApiKey.id,
|
||||
apiKey.data.createOneApiKey.token,
|
||||
);
|
||||
navigate(
|
||||
`/settings/developers/api-keys/${apiKey.data.createOneApiKey.id}`,
|
||||
newApiKey.createApiKeyV2.id,
|
||||
tokenData.data.generateApiKeyV2Token.token,
|
||||
);
|
||||
navigate(`/settings/developers/api-keys/${newApiKey.createApiKeyV2.id}`);
|
||||
}
|
||||
};
|
||||
const canSave = !!formValues.name;
|
||||
const canSave = !!formValues.name && createOneApiKey;
|
||||
return (
|
||||
<SubMenuTopBarContainer Icon={IconSettings} title="Settings">
|
||||
<SettingsPageContainer>
|
||||
|
||||
Reference in New Issue
Block a user