Fix bug and remove useless stuff (#3861)
This commit is contained in:
@ -1,50 +0,0 @@
|
|||||||
import { useApolloClient } from '@apollo/client';
|
|
||||||
import { MockedProvider } from '@apollo/client/testing';
|
|
||||||
import { act, renderHook } from '@testing-library/react';
|
|
||||||
import { RecoilRoot } from 'recoil';
|
|
||||||
|
|
||||||
import { useOptimisticEvict } from '@/apollo/optimistic-effect/hooks/useOptimisticEvict';
|
|
||||||
|
|
||||||
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
||||||
<MockedProvider addTypename={false}>
|
|
||||||
<RecoilRoot>{children}</RecoilRoot>
|
|
||||||
</MockedProvider>
|
|
||||||
);
|
|
||||||
|
|
||||||
describe('useOptimisticEvict', () => {
|
|
||||||
it('should perform cache eviction', async () => {
|
|
||||||
const mockedData = {
|
|
||||||
'someType:1': { __typename: 'someType', id: '1', fieldName: 'value1' },
|
|
||||||
'someType:2': { __typename: 'someType', id: '2', fieldName: 'value2' },
|
|
||||||
'otherType:1': { __typename: 'otherType', id: '1', fieldName: 'value3' },
|
|
||||||
};
|
|
||||||
|
|
||||||
const { result } = renderHook(
|
|
||||||
() => {
|
|
||||||
const { cache } = useApolloClient();
|
|
||||||
cache.restore(mockedData);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...useOptimisticEvict(),
|
|
||||||
cache,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{
|
|
||||||
wrapper: Wrapper,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const { performOptimisticEvict, cache } = result.current;
|
|
||||||
|
|
||||||
act(() => {
|
|
||||||
performOptimisticEvict('someType', 'fieldName', 'value1');
|
|
||||||
});
|
|
||||||
|
|
||||||
const cacheSnapshot = cache.extract();
|
|
||||||
|
|
||||||
expect(cacheSnapshot).toEqual({
|
|
||||||
'someType:2': { __typename: 'someType', id: '2', fieldName: 'value2' },
|
|
||||||
'otherType:1': { __typename: 'otherType', id: '1', fieldName: 'value3' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { useApolloClient } from '@apollo/client';
|
|
||||||
|
|
||||||
export const useOptimisticEvict = () => {
|
|
||||||
const cache = useApolloClient().cache;
|
|
||||||
|
|
||||||
const performOptimisticEvict = (
|
|
||||||
typename: string,
|
|
||||||
fieldName: string,
|
|
||||||
fieldValue: string,
|
|
||||||
) => {
|
|
||||||
const serializedCache = cache.extract();
|
|
||||||
|
|
||||||
Object.values(serializedCache)
|
|
||||||
.filter((item) => item.__typename === typename)
|
|
||||||
.forEach((item) => {
|
|
||||||
if (item[fieldName] === fieldValue) {
|
|
||||||
cache.evict({ id: cache.identify(item) });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
performOptimisticEvict,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -4,7 +4,6 @@ import styled from '@emotion/styled';
|
|||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { useRecoilState } from 'recoil';
|
import { useRecoilState } from 'recoil';
|
||||||
|
|
||||||
import { useOptimisticEvict } from '@/apollo/optimistic-effect/hooks/useOptimisticEvict';
|
|
||||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||||
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
||||||
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
|
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
|
||||||
@ -48,8 +47,6 @@ export const SettingsDevelopersApiKeyDetail = () => {
|
|||||||
const [generatedApiKey] = useRecoilState(
|
const [generatedApiKey] = useRecoilState(
|
||||||
generatedApiKeyFamilyState(apiKeyId),
|
generatedApiKeyFamilyState(apiKeyId),
|
||||||
);
|
);
|
||||||
const { performOptimisticEvict } = useOptimisticEvict();
|
|
||||||
|
|
||||||
const [generateOneApiKeyToken] = useGenerateApiKeyTokenMutation();
|
const [generateOneApiKeyToken] = useGenerateApiKeyTokenMutation();
|
||||||
const { createOneRecord: createOneApiKey } = useCreateOneRecord<ApiKey>({
|
const { createOneRecord: createOneApiKey } = useCreateOneRecord<ApiKey>({
|
||||||
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
||||||
@ -68,7 +65,6 @@ export const SettingsDevelopersApiKeyDetail = () => {
|
|||||||
idToUpdate: apiKeyId,
|
idToUpdate: apiKeyId,
|
||||||
updateOneRecordInput: { revokedAt: DateTime.now().toString() },
|
updateOneRecordInput: { revokedAt: DateTime.now().toString() },
|
||||||
});
|
});
|
||||||
performOptimisticEvict('ApiKey', 'id', apiKeyId);
|
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
navigate('/settings/developers');
|
navigate('/settings/developers');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,7 +36,6 @@ export const SettingsDevelopersApiKeys = () => {
|
|||||||
const { records: apiKeys } = useFindManyRecords({
|
const { records: apiKeys } = useFindManyRecords({
|
||||||
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
||||||
filter: { revokedAt: { is: 'NULL' } },
|
filter: { revokedAt: { is: 'NULL' } },
|
||||||
orderBy: {},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -33,7 +33,6 @@ export const SettingsDevelopersWebhooks = () => {
|
|||||||
|
|
||||||
const { records: webhooks } = useFindManyRecords({
|
const { records: webhooks } = useFindManyRecords({
|
||||||
objectNameSingular: CoreObjectNameSingular.Webhook,
|
objectNameSingular: CoreObjectNameSingular.Webhook,
|
||||||
orderBy: {},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user