Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GAL-4176] add optimisticUpdater to useFollowMutation #2053

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/shared/src/relay/useFollowUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';
import { graphql, useFragment } from 'react-relay';
import { SelectorStoreUpdater } from 'relay-runtime';

import { useFollowUserFragment$key } from '~/generated/useFollowUserFragment.graphql';
import { useFollowUserMutation } from '~/generated/useFollowUserMutation.graphql';
Expand All @@ -8,9 +9,10 @@ import { usePromisifiedMutation } from './usePromisifiedMutation';

type useFollowUserArgs = {
queryRef: useFollowUserFragment$key;
connectionIds: string[];
};

export default function useFollowUser({ queryRef }: useFollowUserArgs) {
export default function useFollowUser({ queryRef, connectionIds }: useFollowUserArgs) {
const query = useFragment(
graphql`
fragment useFollowUserFragment on Query {
Expand Down Expand Up @@ -62,9 +64,23 @@ export default function useFollowUser({ queryRef }: useFollowUserArgs) {
`
);

const updater: SelectorStoreUpdater<useFollowUserMutation['response']> = (store, response) => {
console.log('store', store);
if (response.followUser?.__typename === 'FollowUserPayload') {
/*
for (const connectionId of connectionIds) {
const pageInfo = store.get(connectionId)?.getLinkedRecord('pageInfo');

pageInfo?.setValue(((pageInfo?.getValue('total') as number) ?? 0) + 1, 'total');
}*/
}
};

return useCallback(
async (followeeId: string) => {
await followUserMutate({
updater,
optimisticUpdater: updater,
optimisticResponse: {
followUser: {
__typename: 'FollowUserPayload',
Expand All @@ -82,6 +98,12 @@ export default function useFollowUser({ queryRef }: useFollowUserArgs) {
variables: { userId: followeeId },
});
},
[followUserMutate, query.viewer?.id, query.viewer?.user?.following, query.viewer?.user?.id]
[
followUserMutate,
query.viewer?.id,
query.viewer?.user?.following,
query.viewer?.user?.id,
updater,
]
);
}
Loading