mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 11:04:05 +08:00
68 lines
1.8 KiB
TypeScript
68 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,
|
|
},
|
|
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"]>;
|