Files
talk/src/core/client/stream/mutations/CreateCommentReactionMutation.ts
T
2018-10-03 10:25:25 -03:00

67 lines
1.8 KiB
TypeScript

import { graphql } from "react-relay";
import { Environment } from "relay-runtime";
import {
commitMutationPromiseNormalized,
createMutationContainer,
} from "talk-framework/lib/relay";
import { Omit } from "talk-framework/types";
import { CreateCommentReactionMutation as MutationTypes } from "talk-stream/__generated__/CreateCommentReactionMutation.graphql";
export type CreateCommentReactionInput = Omit<
MutationTypes["variables"]["input"],
"clientMutationId"
>;
const mutation = graphql`
mutation CreateCommentReactionMutation($input: CreateCommentReactionInput!) {
createCommentReaction(input: $input) {
comment {
...ReactionButtonContainer_comment
}
clientMutationId
}
}
`;
let clientMutationId = 0;
function commit(environment: Environment, input: CreateCommentReactionInput) {
const source = environment.getStore().getSource();
const currentCount = source.get(
source.get(source.get(input.commentID)!.actionCounts.__ref)!.reaction.__ref
)!.total;
return commitMutationPromiseNormalized<MutationTypes>(environment, {
mutation,
variables: {
input: {
...input,
clientMutationId: clientMutationId.toString(),
},
},
optimisticResponse: {
createCommentReaction: {
comment: {
id: input.commentID,
myActionPresence: {
reaction: true,
},
actionCounts: currentCount + 1,
},
clientMutationId: (clientMutationId++).toString(),
},
} as any, // TODO: (cvle) generated types should contain one for the optimistic response.
});
}
export const withCreateCommentReactionMutation = createMutationContainer(
"createCommentReaction",
commit
);
export type CreateCommentReactionMutation = (
input: CreateCommentReactionInput
) => Promise<MutationTypes["response"]["createCommentReaction"]>;