Fix api keys refresh (#2283)
* Move code to dedicated function * Format cached data with proper typename
This commit is contained in:
@ -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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,25 +69,32 @@ export const SettingsDevelopersApiKeyDetail = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const createIntegration = async (
|
||||||
|
name: string,
|
||||||
|
newExpiresAt: string | null,
|
||||||
|
) => {
|
||||||
|
return await insertOneApiKey({
|
||||||
|
variables: {
|
||||||
|
data: {
|
||||||
|
name: name,
|
||||||
|
expiresAt: newExpiresAt,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
update: (_cache, { data }) => {
|
||||||
|
if (data?.createOneApiKey) {
|
||||||
|
triggerOptimisticEffects('ApiKey', [data?.createOneApiKey]);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const regenerateApiKey = async () => {
|
const regenerateApiKey = async () => {
|
||||||
if (apiKeyData?.name) {
|
if (apiKeyData?.name) {
|
||||||
const newExpiresAt = computeNewExpirationDate(
|
const newExpiresAt = computeNewExpirationDate(
|
||||||
apiKeyData.expiresAt,
|
apiKeyData.expiresAt,
|
||||||
apiKeyData.createdAt,
|
apiKeyData.createdAt,
|
||||||
);
|
);
|
||||||
const apiKey = await insertOneApiKey({
|
const apiKey = await createIntegration(apiKeyData.name, newExpiresAt);
|
||||||
variables: {
|
|
||||||
data: {
|
|
||||||
name: apiKeyData.name,
|
|
||||||
expiresAt: newExpiresAt,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
update: (_cache, { data }) => {
|
|
||||||
if (data?.createOneApiKey) {
|
|
||||||
triggerOptimisticEffects('ApiKey', [data?.createOneApiKey]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await deleteIntegration(false);
|
await deleteIntegration(false);
|
||||||
if (apiKey.data?.createOneApiKey) {
|
if (apiKey.data?.createOneApiKey) {
|
||||||
setGeneratedApi(
|
setGeneratedApi(
|
||||||
|
|||||||
Reference in New Issue
Block a user