Fix bulk update document front counts

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-07-15 17:17:30 +02:00
parent be6c668aa6
commit 578289f053
3 changed files with 53 additions and 29 deletions

View File

@@ -10,7 +10,7 @@ import type { MutationParameters, GraphQLTaggedNode } from "relay-runtime";
export function useMutationWithToasts<T extends MutationParameters>(
query: GraphQLTaggedNode,
baseOptions?: {
successMessage?: string;
successMessage?: string | ((response: T["response"]) => string);
errorMessage?: string;
}
) {
@@ -21,7 +21,7 @@ export function useMutationWithToasts<T extends MutationParameters>(
(
queryOptions: UseMutationConfig<T> & {
onSuccess?: () => void;
successMessage?: string;
successMessage?: string | ((response: T["response"]) => string);
errorMessage?: string;
}
) => {
@@ -42,10 +42,14 @@ export function useMutationWithToasts<T extends MutationParameters>(
reject(error);
return;
}
const successMessage = typeof options.successMessage === "function"
? options.successMessage(response)
: options.successMessage;
toast({
title: __("Success"),
description:
options.successMessage ??
successMessage ??
__("Operation completed successfully"),
variant: "success",
});

View File

@@ -14,6 +14,7 @@ import { useFormWithSchema } from "/hooks/useFormWithSchema.ts";
import { graphql } from "relay-runtime";
import { sprintf } from "@probo/helpers";
import { useMutationWithToasts } from "/hooks/useMutationWithToasts.ts";
import type { PublishDocumentsDialogMutation } from "./__generated__/PublishDocumentsDialogMutation.graphql.ts";
type Props = {
documentIds: string[];
@@ -36,10 +37,6 @@ const documentsPublishMutation = graphql`
}
`;
const schema = z.object({
changelog: z.string().min(1, "Changelog is required"),
});
export function PublishDocumentsDialog({
documentIds,
children,
@@ -47,6 +44,22 @@ export function PublishDocumentsDialog({
}: Props) {
const { __ } = useTranslate();
const dialogRef = useDialogRef();
const schema = z.object({
changelog: z.string().min(1, __("Changelog is required")),
});
const [publishMutation] = useMutationWithToasts<PublishDocumentsDialogMutation>(documentsPublishMutation, {
successMessage: (response) => {
const actualPublishedCount = response.bulkPublishDocumentVersions.documentEdges.length;
return sprintf(__("%s documents published"), actualPublishedCount);
},
errorMessage: sprintf(
__("Failed to publish %s documents"),
documentIds.length
),
});
const {
handleSubmit,
register,
@@ -56,6 +69,7 @@ export function PublishDocumentsDialog({
changelog: "",
},
});
const onSubmit = handleSubmit(async (data) => {
await publishMutation({
variables: {
@@ -64,17 +78,11 @@ export function PublishDocumentsDialog({
changelog: data.changelog,
},
},
onSuccess: () => {
dialogRef.current?.close();
onSave();
},
});
dialogRef.current?.close();
onSave();
});
const [publishMutation] = useMutationWithToasts(documentsPublishMutation, {
successMessage: sprintf(__("%s documents published"), documentIds.length),
errorMessage: sprintf(
__("Failed to publish %s documents"),
documentIds.length
),
});
return (

View File

@@ -14,12 +14,15 @@ import {
Table,
IconChevronDown,
} from "@probo/ui";
import { Suspense, type FormEventHandler, type ReactNode } from "react";
import { Suspense, type ReactNode } from "react";
import { useTranslate } from "@probo/i18n";
import { graphql } from "relay-runtime";
import { sprintf } from "@probo/helpers";
import { useMutationWithToasts } from "/hooks/useMutationWithToasts.ts";
import { useOrganizationId } from "/hooks/useOrganizationId.ts";
import type { SignatureDocumentsDialogMutation } from "./__generated__/SignatureDocumentsDialogMutation.graphql.ts";
import { useFormWithSchema } from "/hooks/useFormWithSchema.ts";
import z from "zod";
import {
paginatedPeopleFragment,
paginatedPeopleQuery,
@@ -58,16 +61,23 @@ export function SignatureDocumentsDialog({
const { __ } = useTranslate();
const dialogRef = useDialogRef();
const { list: selectedPeople, toggle } = useList<string>([]);
const [publishMutation] = useMutationWithToasts(documentsSignatureMutation, {
successMessage: sprintf(
__("%s signature requests sent"),
documentIds.length
),
const schema = z.object({});
const [publishMutation] = useMutationWithToasts<SignatureDocumentsDialogMutation>(documentsSignatureMutation, {
successMessage: (response) => {
const actualRequestsCount = response.bulkRequestSignatures.documentVersionSignatureEdges.length;
return sprintf(__("%s signature requests sent"), actualRequestsCount);
},
errorMessage: __("Failed to send signature requests"),
});
const onSubmit: FormEventHandler<HTMLFormElement> = async (e) => {
e.preventDefault();
const {
handleSubmit,
formState: { isSubmitting },
} = useFormWithSchema(schema, {});
const onSubmit = handleSubmit(async () => {
await publishMutation({
variables: {
input: {
@@ -75,10 +85,12 @@ export function SignatureDocumentsDialog({
signatoryIds: selectedPeople,
},
},
onSuccess: () => {
dialogRef.current?.close();
onSave();
},
});
dialogRef.current?.close();
onSave();
};
});
return (
<Dialog
@@ -94,7 +106,7 @@ export function SignatureDocumentsDialog({
</Suspense>
</DialogContent>
<DialogFooter>
<Button type="submit" disabled={selectedPeople.length === 0}>
<Button type="submit" disabled={selectedPeople.length === 0 || isSubmitting}>
{__("Send signature requests")}
</Button>
</DialogFooter>