Skip to content

Commit 72efc84

Browse files
authored
templates: fix issue with populateAuthors hook breaking live-preview on website template (#11608)
Fixes #11468 The populateAuthors hook could break live preview if it returned a notFound error as we didn't catch these properly
1 parent 3ede7ab commit 72efc84

File tree

12 files changed

+59
-39
lines changed

12 files changed

+59
-39
lines changed

templates/website/src/collections/Posts/hooks/populateAuthors.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,31 @@ import { User } from 'src/payload-types'
66
// GraphQL will not return mutated user data that differs from the underlying schema
77
// So we use an alternative `populatedAuthors` field to populate the user data, hidden from the admin UI
88
export const populateAuthors: CollectionAfterReadHook = async ({ doc, req, req: { payload } }) => {
9-
if (doc?.authors) {
9+
if (doc?.authors && doc?.authors?.length > 0) {
1010
const authorDocs: User[] = []
1111

1212
for (const author of doc.authors) {
13-
const authorDoc = await payload.findByID({
14-
id: typeof author === 'object' ? author?.id : author,
15-
collection: 'users',
16-
depth: 0,
17-
req,
18-
})
13+
try {
14+
const authorDoc = await payload.findByID({
15+
id: typeof author === 'object' ? author?.id : author,
16+
collection: 'users',
17+
depth: 0,
18+
})
1919

20-
if (authorDoc) {
21-
authorDocs.push(authorDoc)
20+
if (authorDoc) {
21+
authorDocs.push(authorDoc)
22+
}
23+
24+
if (authorDocs.length > 0) {
25+
doc.populatedAuthors = authorDocs.map((authorDoc) => ({
26+
id: authorDoc.id,
27+
name: authorDoc.name,
28+
}))
29+
}
30+
} catch {
31+
// swallow error
2232
}
2333
}
24-
25-
doc.populatedAuthors = authorDocs.map((authorDoc) => ({
26-
id: authorDoc.id,
27-
name: authorDoc.name,
28-
}))
2934
}
3035

3136
return doc

templates/website/src/payload-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,4 +1700,4 @@ export interface Auth {
17001700

17011701
declare module 'payload' {
17021702
export interface GeneratedTypes extends Config {}
1703-
}
1703+
}

templates/with-vercel-website/src/blocks/Form/Checkbox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const Checkbox: React.FC<
3939
{label}
4040
</Label>
4141
</div>
42-
{errors[name] && <Error />}
42+
{errors[name] && <Error name={name} />}
4343
</Width>
4444
)
4545
}

templates/with-vercel-website/src/blocks/Form/Country/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const Country: React.FC<
5959
}}
6060
rules={{ required }}
6161
/>
62-
{errors[name] && <Error />}
62+
{errors[name] && <Error name={name} />}
6363
</Width>
6464
)
6565
}

templates/with-vercel-website/src/blocks/Form/Email/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const Email: React.FC<
3232
{...register(name, { pattern: /^\S[^\s@]*@\S+$/, required })}
3333
/>
3434

35-
{errors[name] && <Error />}
35+
{errors[name] && <Error name={name} />}
3636
</Width>
3737
)
3838
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
'use client'
2+
13
import * as React from 'react'
4+
import { useFormContext } from 'react-hook-form'
25

3-
export const Error: React.FC = () => {
4-
return <div className="mt-2 text-red-500 text-sm">This field is required</div>
6+
export const Error = ({ name }: { name: string }) => {
7+
const {
8+
formState: { errors },
9+
} = useFormContext()
10+
return (
11+
<div className="mt-2 text-red-500 text-sm">
12+
{(errors[name]?.message as string) || 'This field is required'}
13+
</div>
14+
)
515
}

templates/with-vercel-website/src/blocks/Form/Number/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const Number: React.FC<
3030
type="number"
3131
{...register(name, { required })}
3232
/>
33-
{errors[name] && <Error />}
33+
{errors[name] && <Error name={name} />}
3434
</Width>
3535
)
3636
}

templates/with-vercel-website/src/blocks/Form/Select/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const Select: React.FC<
5757
}}
5858
rules={{ required }}
5959
/>
60-
{errors[name] && <Error />}
60+
{errors[name] && <Error name={name} />}
6161
</Width>
6262
)
6363
}

templates/with-vercel-website/src/blocks/Form/State/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const State: React.FC<
5858
}}
5959
rules={{ required }}
6060
/>
61-
{errors[name] && <Error />}
61+
{errors[name] && <Error name={name} />}
6262
</Width>
6363
)
6464
}

templates/with-vercel-website/src/blocks/Form/Text/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const Text: React.FC<
2626
)}
2727
</Label>
2828
<Input defaultValue={defaultValue} id={name} type="text" {...register(name, { required })} />
29-
{errors[name] && <Error />}
29+
{errors[name] && <Error name={name} />}
3030
</Width>
3131
)
3232
}

0 commit comments

Comments
 (0)