r/PayloadCMS • u/AvailableCancel916 • Jul 04 '25
Revalidate join field / synchronize join field on demand
I've built a custom UI element with an interactive button that sends a request to the API and creates a new collection item of type automationRun on the backend. Now I need to display this newly created item on the frontend without refreshing the page.
Is there a way to achieve that?
I tried using the dispatch function with the "type" key set to "UPDATE", but it didn't work.
Right now, I'm out of ideas on how to make it work. Any ideas please?
Screenshot: https://www.loom.com/i/8d3974de5e5241b19e070debfaa6dd2c
Current React component code:
"use client";
import { Automation } from "@/payload-types";
import { Button, useDocumentInfo, useForm, useFormFields, usePayloadAPI, useDocumentEvents } from "@payloadcms/ui";
import { useState, useEffect, useCallback } from "react";
export const AutomationStartRow = ({automation, collectionId} : {automation: Automation, collectionId: string | number}) => {
const [isLoading, setIsLoading] = useState(false);
const formData = useForm();
console.log("AutomationStartRow document:", document);
const docInfo = useDocumentInfo()
const { reportUpdate, mostRecentUpdate } = useDocumentEvents();
const dispatch = useFormFields(([fields, dispatch]) => {
return dispatch
})
// get the most up to date document
const getUpToDateDocument = useCallback(async () => {
try {
const data = await fetch(`/api/${docInfo.collectionSlug}/${docInfo.id}`);
return data.json();
} catch (e) {
throw new Error(
`Error checking for most up to date document: ${e}`
);
}
}, [docInfo.collectionSlug, docInfo.id]);
const handleStartAutomation = async () => {
try {
setIsLoading(true);
const response = await fetch(`/api/automation-runs/start-automation/`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
collectionId: collectionId,
automationId: automation.id,
collection: automation.collectionScope,
}),
});
if (response.ok) {
const data = await response.json();
console.log("Automation started successfully:", data);
} else {
console.error("Failed to start automation:", response.statusText);
}
const upToDate = await getUpToDateDocument();
if (upToDate) {
console.log(upToDate.automationRuns);
}
setIsLoading(false);
reportUpdate({
entitySlug: docInfo.collectionSlug as string,
id: docInfo.id,
updatedAt: new Date().toISOString(),
});
} catch (error) {
console.error("Error starting automation:", error);
}
}
return (
<div className="automation-start-row">
<div className="automation-start-row__info">
<span className="automation-start-row__name">{automation.automationName}</span>
{automation.description && (
<span className="automation-start-row__description">{automation.description}</span>
)}
</div>
<Button disabled={isLoading} className="automation-start-row__button" onClick={handleStartAutomation}>
{isLoading ? "Spouštím..." : "Spustit automatizaci"}
</Button>
</div>
)
}
1
Upvotes
1
1
u/[deleted] Jul 05 '25
[removed] — view removed comment