Skip to content

Commit 13e6035

Browse files
authored
feat(plugin-import-export)!: refactor plugin and add import functionality (#14782)
This PR works to refactor the entire plugin so it's easier for us to work with it going forward and also implements import functionality. It expands the int tests and adds some e2e tests as well. ## Previous config Previously the config for the plugin looked like this. If you wanted to disable jobs queue you'd have to do it for all collections, the `collections` config was optional and the plugin applied both import and export functionality to _all_ collections which isn't normally how our plugins work. If you want to customise certain behaviour you need to instantiate the plugin multiple times, which causes additional problems by doubling up providers, UI components and endpoints. As such we're committing some breaking changes while the plugin is in beta to standardise the config and allow more customisation. ```ts importExportPlugin({ overrideExportCollection: (collection) => { collection.admin.group = 'System' collection.upload.staticDir = path.resolve(dirname, 'uploads') return collection }, overrideImportCollection: (collection) => { collection.admin.group = 'System' collection.upload.staticDir = path.resolve(dirname, 'uploads') return collection }, disableJobsQueue: true, debug: true, // collections: ['posts'] // optionally provided - defaults to all collections }), ``` ## New config The new config pushes most customisation to a per collection level so that you only ever need to instantiate the plugin once. You can now provide export/import collections per collection config if needed, do not instantiate the plugin multiple times. `collections` is now required. ```ts importExportPlugin({ overrideExportCollection: (collection) => { collection.admin.group = 'System' collection.upload.staticDir = path.resolve(dirname, 'uploads') return collection }, overrideImportCollection: (collection) => { collection.admin.group = 'System' collection.upload.staticDir = path.resolve(dirname, 'uploads') return collection }, collections: [ { slug: 'posts', import: false, // disables import functionality, export enabled by default }, { slug: 'pages', // customise the export collection per collection if needed export: ({ collection }) => { collection.admin.group = 'System' collection.upload.staticDir = path.resolve(dirname, 'uploads') return collection }, disableJobsQueue: true, // disable jobs queue for this collection only }, ], debug: true, // debug remains top level }) ``` This PR also fixes the following: - Drafts being exported even if 'no' is selected to include them - Fetch endpoints from preview not respecting the API routes - Downloads not respecting drafts - Dependencies on `csv-parse` and `csv-stringify` are now pinned Todo: - [x] Regenerate all translations - [x] Add docs for import - [x] Review code structure/patterns - [x] Polish the preview UI to allow for pagination, show loading states and make it clear what data is being previewed
1 parent 7d80d21 commit 13e6035

109 files changed

Lines changed: 13899 additions & 877 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/plugins/import-export.mdx

Lines changed: 479 additions & 28 deletions
Large diffs are not rendered by default.

packages/plugin-ecommerce/src/collections/carts/beforeChange.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export const beforeChangeCart: (args: Props) => CollectionBeforeChangeHook =
1717
data.secret = secret
1818

1919
// Store in context so afterRead hook can include it in the creation response
20-
if (!req.context) { req.context = {} }
20+
if (!req.context) {
21+
req.context = {}
22+
}
2123
req.context.newCartSecret = secret
2224
}
2325

packages/plugin-import-export/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
"@faceless-ui/modal": "3.0.0",
7474
"@payloadcms/translations": "workspace:*",
7575
"@payloadcms/ui": "workspace:*",
76-
"csv-parse": "^5.6.0",
77-
"csv-stringify": "^6.5.2",
76+
"csv-parse": "5.6.0",
77+
"csv-stringify": "6.5.2",
7878
"qs-esm": "7.0.2"
7979
},
8080
"devDependencies": {

packages/plugin-import-export/src/components/CollectionField/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ import { useEffect } from 'react'
77
import { useImportExport } from '../ImportExportProvider/index.js'
88

99
export const CollectionField: React.FC = () => {
10-
const { id } = useDocumentInfo()
10+
const { id, collectionSlug } = useDocumentInfo()
1111
const { setValue } = useField({ path: 'collectionSlug' })
1212
const { collection } = useImportExport()
1313

1414
useEffect(() => {
1515
if (id) {
1616
return
1717
}
18-
setValue(collection)
19-
}, [id, collection, setValue])
18+
if (collection) {
19+
setValue(collection)
20+
} else if (collectionSlug) {
21+
setValue(collectionSlug)
22+
}
23+
}, [id, collection, setValue, collectionSlug])
2024

2125
return null
2226
}

packages/plugin-import-export/src/components/ExportListMenuItem/index.scss

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/plugin-import-export/src/components/ExportListMenuItem/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Translation,
77
useConfig,
88
useDocumentDrawer,
9+
useDocumentInfo,
910
useTranslation,
1011
} from '@payloadcms/ui'
1112
import React, { useEffect } from 'react'
@@ -16,7 +17,6 @@ import type {
1617
} from '../../translations/index.js'
1718

1819
import { useImportExport } from '../ImportExportProvider/index.js'
19-
import './index.scss'
2020

2121
const baseClass = 'export-list-menu-item'
2222

@@ -25,10 +25,12 @@ export const ExportListMenuItem: React.FC<{
2525
exportCollectionSlug: string
2626
}> = ({ collectionSlug, exportCollectionSlug }) => {
2727
const { getEntityConfig } = useConfig()
28+
2829
const { i18n, t } = useTranslation<
2930
PluginImportExportTranslations,
3031
PluginImportExportTranslationKeys
3132
>()
33+
3234
const currentCollectionConfig = getEntityConfig({ collectionSlug })
3335

3436
const [DocumentDrawer, DocumentDrawerToggler] = useDocumentDrawer({
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@layer payload-default {
2+
.export-preview {
3+
&__header {
4+
display: flex;
5+
justify-content: space-between;
6+
align-items: flex-end;
7+
margin-bottom: 10px;
8+
}
9+
10+
&__export-count {
11+
font-size: var(--font-size-small);
12+
color: var(--theme-elevation-500);
13+
}
14+
15+
&__pagination {
16+
display: flex;
17+
align-items: center;
18+
justify-content: space-between;
19+
gap: var(--base);
20+
margin-top: var(--base);
21+
padding-top: var(--base);
22+
border-top: 1px solid var(--theme-elevation-100);
23+
24+
// When only PerPage is present (no Pagination), keep it on the right
25+
.per-page:only-child {
26+
margin-left: auto;
27+
}
28+
}
29+
30+
&__page-info {
31+
font-size: var(--font-size-small);
32+
color: var(--theme-elevation-500);
33+
white-space: nowrap;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)