Orchestrate multiple entries based on an other collection #12022
-
Hi, We want to orchestrate a series of entries of various collections based on a entry in the collection A project can have different project types, which is also a collection. In the project type we have a few things, but at least a content field and an array with the available variables. erDiagram
p[Project] {
Map variables "Map<string, string> with variable key and value"
}
pt[ProjectType] {
RichText content
string[] variables "the available variable keys"
}
p ||--|| pt: has
The orchestration it self isn't really the problem, we can do that using the job system and generate all the necessary entries in the background. The problem we are facing is how can we prefill the So the question is: How should we make the following, when you create a new project, you select the ProjectType as a relationship, then based on that relationship an array field is prefilled with data from that relationship. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @robbertstevens, I think there are at least 2 ways to approach this The first way is using the react admin panel hooks. import { useDocumentInfo, useField } from '@payloadcms/ui'
// Component
const { value: projectTypes } = useField({ path: 'projectTypes' })
const { value: variables, setValue: setVariables } = useField({ path: 'variables' })
const { id } = useDocumentInfo() // if id is undefined that means we're currently creating a project, not editing
const isEditing = Boolean(id) Combine that with useEffect(() => {
const prefillVariables = async () => {
if (isEditing) {
return
}
const projectTypeIDs = Array.isArray(projectTypes) ? projectTypes : []
if (projectTypeIDs.length === 0) {
setVariables([])
return
}
// data for every project type is here
const populatedProjectTypes = await fetch(
`/api/project-types?where[id][in]=${projectTypes.join(',')}&depth=0`,
).then((res) => res.json())
// set data of the array based on project types
setVariables(
populatedProjectTypes.map((e) => ({
key: e.key,
})),
)
}
void prefillVariables()
}, [projectTypes, isEditing, setVariables]) Needs of course some adjustments based on your data but I'd expect this to work. The second way would be to use either the Hope I understood your logic correctly, let me know if you have further questions! |
Beta Was this translation helpful? Give feedback.
Hey @robbertstevens, I think there are at least 2 ways to approach this
The first way is using the react admin panel hooks.
You can use the
useField
hook here https://payloadcms.com/docs/admin/react-hooks#usefield to "watch" the changes of theprojectTypes
(or how it's called on your side) field and change thevariables
field.…