Files
talk/src/core/client/stream/mutations/CreateCommentReactionMutation.ts
T
KiwiandWyatt Johnson 939152ee81 [next] Report Comment + Stream Refactor & Test Improvements (#2144)
* fix: stabilize mutations

* feat: Refactor Stream + Implement Report Comment

* test: add unit tests

* chore: Improve stream integration tests

* test: add integration test for reaction

* test: add feature/integration tests for report comment

* fix: order import

* fix: performance issues + keep active button styling until clicked away
2019-01-16 22:31:43 +00:00

72 lines
2.0 KiB
TypeScript

import { pick } from "lodash";
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: {
...pick(input, ["commentID", "commentRevisionID"]),
clientMutationId: clientMutationId.toString(),
},
},
optimisticResponse: {
createCommentReaction: {
comment: {
id: input.commentID,
myActionPresence: {
reaction: true,
},
actionCounts: {
reaction: {
total: 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"]>;