Skip to content

Commit a420902

Browse files
committed
Update projects page when thumbnail focal point changes
1 parent 27973e6 commit a420902

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/collections/Media.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import type { CollectionConfig } from 'payload'
2+
import { getPayload } from 'payload'
3+
import config from '@payload-config'
4+
import { revalidatePath } from 'next/cache'
5+
import purgeRoute from '@/lib/utils/purge'
6+
import { routing } from '@/i18n/routing'
27

38
export const Media: CollectionConfig = {
49
slug: 'media',
@@ -33,4 +38,38 @@ export const Media: CollectionConfig = {
3338
},
3439
],
3540
},
41+
hooks: {
42+
afterChange: [
43+
async ({ req, data }) => {
44+
const payload = await getPayload({ config })
45+
46+
// Determine if the image is a thumbnail of any project.
47+
// Done to update the projects page in case the focal point was changed.
48+
// Individual project update not really required since the image will always be shown
49+
// in its entirety.
50+
const isThumbnail = await payload.find({
51+
collection: 'project',
52+
where: {
53+
thumbnail: {
54+
equals: data.id,
55+
},
56+
},
57+
req,
58+
pagination: false,
59+
})
60+
61+
if (isThumbnail.totalDocs > 0) {
62+
if (req.locale === 'all') {
63+
routing.locales.forEach((locale) => {
64+
revalidatePath(`/${locale}/projects`)
65+
purgeRoute(`${locale}/projects`)
66+
})
67+
} else {
68+
revalidatePath(`/${req.locale}/projects`)
69+
purgeRoute(`${req.locale}/projects`)
70+
}
71+
}
72+
},
73+
],
74+
},
3675
}

0 commit comments

Comments
 (0)