Fix api keys refresh (#2283)

* Move code to dedicated function

* Format cached data with proper typename
This commit is contained in:
martmull
2023-10-30 17:48:34 +01:00
committed by GitHub
parent 3fc1b74d9c
commit 236742e88c
2 changed files with 26 additions and 14 deletions

View File

@ -121,11 +121,16 @@ export const useOptimisticEffect = () => {
.getValue(); .getValue();
Object.values(optimisticEffects).forEach((optimisticEffect) => { Object.values(optimisticEffects).forEach((optimisticEffect) => {
// We need to update the typename when createObject type differs from listObject types
// It is the case for apiKey, where the creation route returns an ApiKeyToken type
const formattedNewData = newData.map((data) => {
return { ...data, __typename: typename };
});
if (optimisticEffect.typename === typename) { if (optimisticEffect.typename === typename) {
optimisticEffect.writer({ optimisticEffect.writer({
cache: apolloClient.cache, cache: apolloClient.cache,
query: optimisticEffect.query, query: optimisticEffect.query,
newData, newData: formattedNewData,
variables: optimisticEffect.variables, variables: optimisticEffect.variables,
}); });
} }

View File

@ -69,16 +69,14 @@ export const SettingsDevelopersApiKeyDetail = () => {
} }
}; };
const regenerateApiKey = async () => { const createIntegration = async (
if (apiKeyData?.name) { name: string,
const newExpiresAt = computeNewExpirationDate( newExpiresAt: string | null,
apiKeyData.expiresAt, ) => {
apiKeyData.createdAt, return await insertOneApiKey({
);
const apiKey = await insertOneApiKey({
variables: { variables: {
data: { data: {
name: apiKeyData.name, name: name,
expiresAt: newExpiresAt, expiresAt: newExpiresAt,
}, },
}, },
@ -88,6 +86,15 @@ export const SettingsDevelopersApiKeyDetail = () => {
} }
}, },
}); });
};
const regenerateApiKey = async () => {
if (apiKeyData?.name) {
const newExpiresAt = computeNewExpirationDate(
apiKeyData.expiresAt,
apiKeyData.createdAt,
);
const apiKey = await createIntegration(apiKeyData.name, newExpiresAt);
await deleteIntegration(false); await deleteIntegration(false);
if (apiKey.data?.createOneApiKey) { if (apiKey.data?.createOneApiKey) {
setGeneratedApi( setGeneratedApi(