@payloadcms/plugin-import-export 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/components/CollectionField/index.d.ts +3 -0
- package/dist/components/CollectionField/index.d.ts.map +1 -0
- package/dist/components/CollectionField/index.js +24 -0
- package/dist/components/CollectionField/index.js.map +1 -0
- package/dist/components/ExportListMenuItem/index.d.ts +7 -0
- package/dist/components/ExportListMenuItem/index.d.ts.map +1 -0
- package/dist/components/ExportListMenuItem/index.js +40 -0
- package/dist/components/ExportListMenuItem/index.js.map +1 -0
- package/dist/components/ExportListMenuItem/index.scss +52 -0
- package/dist/components/ExportSaveButton/index.d.ts +3 -0
- package/dist/components/ExportSaveButton/index.d.ts.map +1 -0
- package/dist/components/ExportSaveButton/index.js +27 -0
- package/dist/components/ExportSaveButton/index.js.map +1 -0
- package/dist/components/FieldsToExport/index.d.ts +3 -0
- package/dist/components/FieldsToExport/index.d.ts.map +1 -0
- package/dist/components/FieldsToExport/index.js +99 -0
- package/dist/components/FieldsToExport/index.js.map +1 -0
- package/dist/components/FieldsToExport/index.scss +0 -0
- package/dist/components/FieldsToExport/reduceFields.d.ts +12 -0
- package/dist/components/FieldsToExport/reduceFields.d.ts.map +1 -0
- package/dist/components/FieldsToExport/reduceFields.js +96 -0
- package/dist/components/FieldsToExport/reduceFields.js.map +1 -0
- package/dist/components/ImportExportProvider/index.d.ts +12 -0
- package/dist/components/ImportExportProvider/index.d.ts.map +1 -0
- package/dist/components/ImportExportProvider/index.js +20 -0
- package/dist/components/ImportExportProvider/index.js.map +1 -0
- package/dist/components/Preview/index.d.ts +4 -0
- package/dist/components/Preview/index.d.ts.map +1 -0
- package/dist/components/Preview/index.js +123 -0
- package/dist/components/Preview/index.js.map +1 -0
- package/dist/components/Preview/index.scss +8 -0
- package/dist/components/SortBy/index.d.ts +3 -0
- package/dist/components/SortBy/index.d.ts.map +1 -0
- package/dist/components/SortBy/index.js +91 -0
- package/dist/components/SortBy/index.js.map +1 -0
- package/dist/components/SortBy/index.scss +4 -0
- package/dist/components/WhereField/index.d.ts +4 -0
- package/dist/components/WhereField/index.d.ts.map +1 -0
- package/dist/components/WhereField/index.js +72 -0
- package/dist/components/WhereField/index.js.map +1 -0
- package/dist/components/WhereField/index.scss +0 -0
- package/dist/export/createExport.d.ts +24 -0
- package/dist/export/createExport.d.ts.map +1 -0
- package/dist/export/createExport.js +72 -0
- package/dist/export/createExport.js.map +1 -0
- package/dist/export/flattenObject.d.ts +2 -0
- package/dist/export/flattenObject.d.ts.map +1 -0
- package/dist/export/flattenObject.js +22 -0
- package/dist/export/flattenObject.js.map +1 -0
- package/dist/export/getCreateExportCollectionTask.d.ts +3 -0
- package/dist/export/getCreateExportCollectionTask.d.ts.map +1 -0
- package/dist/export/getCreateExportCollectionTask.js +47 -0
- package/dist/export/getCreateExportCollectionTask.js.map +1 -0
- package/dist/export/getFields.d.ts +3 -0
- package/dist/export/getFields.d.ts.map +1 -0
- package/dist/export/getFields.js +175 -0
- package/dist/export/getFields.js.map +1 -0
- package/dist/export/getFilename.d.ts +2 -0
- package/dist/export/getFilename.d.ts.map +1 -0
- package/dist/export/getFilename.js +10 -0
- package/dist/export/getFilename.js.map +1 -0
- package/dist/export/getSelect.d.ts +7 -0
- package/dist/export/getSelect.d.ts.map +1 -0
- package/dist/export/getSelect.js +27 -0
- package/dist/export/getSelect.js.map +1 -0
- package/dist/exports/rsc.d.ts +9 -0
- package/dist/exports/rsc.d.ts.map +1 -0
- package/dist/exports/rsc.js +10 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/exports/types.d.ts +2 -0
- package/dist/exports/types.d.ts.map +1 -0
- package/dist/exports/types.js +3 -0
- package/dist/exports/types.js.map +1 -0
- package/dist/getExportCollection.d.ts +7 -0
- package/dist/getExportCollection.d.ts.map +1 -0
- package/dist/getExportCollection.js +65 -0
- package/dist/getExportCollection.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/translations/en.d.ts +3 -0
- package/dist/translations/en.d.ts.map +1 -0
- package/dist/translations/en.js +9 -0
- package/dist/translations/en.js.map +1 -0
- package/dist/translations/index.d.ts +7 -0
- package/dist/translations/index.d.ts.map +1 -0
- package/dist/translations/index.js +6 -0
- package/dist/translations/index.js.map +1 -0
- package/dist/translations/translation-schema.json +24 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/license.md +22 -0
- package/package.json +79 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Payload Import/Export Plugin
|
|
2
|
+
|
|
3
|
+
A plugin for [Payload](https://github.com/payloadcms/payload) to easily import and export data.
|
|
4
|
+
|
|
5
|
+
- [Source code](https://github.com/payloadcms/payload/tree/main/packages/plugin-import-export)
|
|
6
|
+
- [Documentation](https://payloadcms.com/docs/plugins/import-export)
|
|
7
|
+
- [Documentation source](https://github.com/payloadcms/payload/tree/main/docs/plugins/import-export.mdx)
|
|
8
|
+
|
|
9
|
+
[//]: # 'TODO: Remove requirements'
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
### Exports
|
|
14
|
+
|
|
15
|
+
- [ ] The export button should be visible on the collection list.
|
|
16
|
+
|
|
17
|
+
Create writable streams for each collection and write the data to the streams. The streams should be piped to a zip stream and sent to the client.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/CollectionField/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAO9B,eAAO,MAAM,eAAe,EAAE,KAAK,CAAC,EAanC,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useDocumentInfo, useField } from '@payloadcms/ui';
|
|
3
|
+
import { useEffect } from 'react';
|
|
4
|
+
import { useImportExport } from '../ImportExportProvider/index.js';
|
|
5
|
+
export const CollectionField = ()=>{
|
|
6
|
+
const { id } = useDocumentInfo();
|
|
7
|
+
const { setValue } = useField({
|
|
8
|
+
path: 'collectionSlug'
|
|
9
|
+
});
|
|
10
|
+
const { collection } = useImportExport();
|
|
11
|
+
useEffect(()=>{
|
|
12
|
+
if (id) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
setValue(collection);
|
|
16
|
+
}, [
|
|
17
|
+
id,
|
|
18
|
+
collection,
|
|
19
|
+
setValue
|
|
20
|
+
]);
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/CollectionField/index.tsx"],"sourcesContent":["'use client'\nimport type React from 'react'\n\nimport { useDocumentInfo, useField } from '@payloadcms/ui'\nimport { useEffect } from 'react'\n\nimport { useImportExport } from '../ImportExportProvider/index.js'\n\nexport const CollectionField: React.FC = () => {\n const { id } = useDocumentInfo()\n const { setValue } = useField({ path: 'collectionSlug' })\n const { collection } = useImportExport()\n\n useEffect(() => {\n if (id) {\n return\n }\n setValue(collection)\n }, [id, collection, setValue])\n\n return null\n}\n"],"names":["useDocumentInfo","useField","useEffect","useImportExport","CollectionField","id","setValue","path","collection"],"mappings":"AAAA;AAGA,SAASA,eAAe,EAAEC,QAAQ,QAAQ,iBAAgB;AAC1D,SAASC,SAAS,QAAQ,QAAO;AAEjC,SAASC,eAAe,QAAQ,mCAAkC;AAElE,OAAO,MAAMC,kBAA4B;IACvC,MAAM,EAAEC,EAAE,EAAE,GAAGL;IACf,MAAM,EAAEM,QAAQ,EAAE,GAAGL,SAAS;QAAEM,MAAM;IAAiB;IACvD,MAAM,EAAEC,UAAU,EAAE,GAAGL;IAEvBD,UAAU;QACR,IAAIG,IAAI;YACN;QACF;QACAC,SAASE;IACX,GAAG;QAACH;QAAIG;QAAYF;KAAS;IAE7B,OAAO;AACT,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/ExportListMenuItem/index.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAoB,MAAM,OAAO,CAAA;AAGxC,OAAO,cAAc,CAAA;AAIrB,eAAO,MAAM,kBAAkB,EAAE,KAAK,CAAC,EAAE,CAAC;IACxC,cAAc,EAAE,MAAM,CAAA;IACtB,oBAAoB,EAAE,MAAM,CAAA;CAC7B,CAuBA,CAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { getTranslation } from '@payloadcms/translations';
|
|
4
|
+
import { PopupList, useConfig, useDocumentDrawer, useTranslation } from '@payloadcms/ui';
|
|
5
|
+
import React, { useEffect } from 'react';
|
|
6
|
+
import { useImportExport } from '../ImportExportProvider/index.js';
|
|
7
|
+
import './index.scss';
|
|
8
|
+
const baseClass = 'export-list-menu-item';
|
|
9
|
+
export const ExportListMenuItem = ({ collectionSlug, exportCollectionSlug })=>{
|
|
10
|
+
const { getEntityConfig } = useConfig();
|
|
11
|
+
const { i18n } = useTranslation();
|
|
12
|
+
const currentCollectionConfig = getEntityConfig({
|
|
13
|
+
collectionSlug
|
|
14
|
+
});
|
|
15
|
+
const [DocumentDrawer, DocumentDrawerToggler] = useDocumentDrawer({
|
|
16
|
+
collectionSlug: exportCollectionSlug
|
|
17
|
+
});
|
|
18
|
+
const { setCollection } = useImportExport();
|
|
19
|
+
// Set collection and selected items on mount or when selection changes
|
|
20
|
+
useEffect(()=>{
|
|
21
|
+
setCollection(currentCollectionConfig.slug ?? '');
|
|
22
|
+
}, [
|
|
23
|
+
currentCollectionConfig,
|
|
24
|
+
setCollection
|
|
25
|
+
]);
|
|
26
|
+
return /*#__PURE__*/ _jsxs(PopupList.Button, {
|
|
27
|
+
className: baseClass,
|
|
28
|
+
children: [
|
|
29
|
+
/*#__PURE__*/ _jsxs(DocumentDrawerToggler, {
|
|
30
|
+
children: [
|
|
31
|
+
"Export ",
|
|
32
|
+
getTranslation(currentCollectionConfig.labels.plural, i18n)
|
|
33
|
+
]
|
|
34
|
+
}),
|
|
35
|
+
/*#__PURE__*/ _jsx(DocumentDrawer, {})
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/ExportListMenuItem/index.tsx"],"sourcesContent":["'use client'\n\nimport { getTranslation } from '@payloadcms/translations'\nimport { PopupList, useConfig, useDocumentDrawer, useTranslation } from '@payloadcms/ui'\nimport React, { useEffect } from 'react'\n\nimport { useImportExport } from '../ImportExportProvider/index.js'\nimport './index.scss'\n\nconst baseClass = 'export-list-menu-item'\n\nexport const ExportListMenuItem: React.FC<{\n collectionSlug: string\n exportCollectionSlug: string\n}> = ({ collectionSlug, exportCollectionSlug }) => {\n const { getEntityConfig } = useConfig()\n const { i18n } = useTranslation()\n const currentCollectionConfig = getEntityConfig({ collectionSlug })\n\n const [DocumentDrawer, DocumentDrawerToggler] = useDocumentDrawer({\n collectionSlug: exportCollectionSlug,\n })\n const { setCollection } = useImportExport()\n\n // Set collection and selected items on mount or when selection changes\n useEffect(() => {\n setCollection(currentCollectionConfig.slug ?? '')\n }, [currentCollectionConfig, setCollection])\n\n return (\n <PopupList.Button className={baseClass}>\n <DocumentDrawerToggler>\n Export {getTranslation(currentCollectionConfig.labels.plural, i18n)}\n </DocumentDrawerToggler>\n <DocumentDrawer />\n </PopupList.Button>\n )\n}\n"],"names":["getTranslation","PopupList","useConfig","useDocumentDrawer","useTranslation","React","useEffect","useImportExport","baseClass","ExportListMenuItem","collectionSlug","exportCollectionSlug","getEntityConfig","i18n","currentCollectionConfig","DocumentDrawer","DocumentDrawerToggler","setCollection","slug","Button","className","labels","plural"],"mappings":"AAAA;;AAEA,SAASA,cAAc,QAAQ,2BAA0B;AACzD,SAASC,SAAS,EAAEC,SAAS,EAAEC,iBAAiB,EAAEC,cAAc,QAAQ,iBAAgB;AACxF,OAAOC,SAASC,SAAS,QAAQ,QAAO;AAExC,SAASC,eAAe,QAAQ,mCAAkC;AAClE,OAAO,eAAc;AAErB,MAAMC,YAAY;AAElB,OAAO,MAAMC,qBAGR,CAAC,EAAEC,cAAc,EAAEC,oBAAoB,EAAE;IAC5C,MAAM,EAAEC,eAAe,EAAE,GAAGV;IAC5B,MAAM,EAAEW,IAAI,EAAE,GAAGT;IACjB,MAAMU,0BAA0BF,gBAAgB;QAAEF;IAAe;IAEjE,MAAM,CAACK,gBAAgBC,sBAAsB,GAAGb,kBAAkB;QAChEO,gBAAgBC;IAClB;IACA,MAAM,EAAEM,aAAa,EAAE,GAAGV;IAE1B,uEAAuE;IACvED,UAAU;QACRW,cAAcH,wBAAwBI,IAAI,IAAI;IAChD,GAAG;QAACJ;QAAyBG;KAAc;IAE3C,qBACE,MAAChB,UAAUkB,MAAM;QAACC,WAAWZ;;0BAC3B,MAACQ;;oBAAsB;oBACbhB,eAAec,wBAAwBO,MAAM,CAACC,MAAM,EAAET;;;0BAEhE,KAACE;;;AAGP,EAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
@import '~@payloadcms/ui/scss';
|
|
2
|
+
|
|
3
|
+
@layer payload-default {
|
|
4
|
+
.export-list-menu-item {
|
|
5
|
+
.doc-drawer__toggler {
|
|
6
|
+
height: 100%;
|
|
7
|
+
width: 100%;
|
|
8
|
+
text-align: left;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// TODO: is any of this css needed?
|
|
13
|
+
&__subheader,
|
|
14
|
+
&__header {
|
|
15
|
+
padding: 0 var(--gutter-h);
|
|
16
|
+
display: flex;
|
|
17
|
+
align-items: center;
|
|
18
|
+
justify-content: space-between;
|
|
19
|
+
border-bottom: 1px solid var(--theme-border-color);
|
|
20
|
+
|
|
21
|
+
& h2 {
|
|
22
|
+
margin: calc(var(--gutter-h) * 0.5) 0;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
&__options,
|
|
27
|
+
&__preview {
|
|
28
|
+
padding: calc(var(--gutter-h) * 0.5) var(--gutter-h);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
&__preview-title {
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
justify-content: space-between;
|
|
35
|
+
margin-bottom: calc(var(--gutter-h) * 0.5);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
&__close {
|
|
39
|
+
@include btn-reset;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
&__icon {
|
|
43
|
+
width: 3rem;
|
|
44
|
+
height: 3rem;
|
|
45
|
+
cursor: pointer;
|
|
46
|
+
|
|
47
|
+
&:hover {
|
|
48
|
+
opacity: 0.8;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/ExportSaveButton/index.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAiBpC,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Button, SaveButton, useTranslation } from '@payloadcms/ui';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
export const ExportSaveButton = ()=>{
|
|
6
|
+
const { t } = useTranslation();
|
|
7
|
+
const label = t('general:save');
|
|
8
|
+
const handleDownload = ()=>{
|
|
9
|
+
console.log('Download');
|
|
10
|
+
// TODO: create a post to the download endpoint to stream data back to the client
|
|
11
|
+
};
|
|
12
|
+
return /*#__PURE__*/ _jsxs(React.Fragment, {
|
|
13
|
+
children: [
|
|
14
|
+
/*#__PURE__*/ _jsx(SaveButton, {
|
|
15
|
+
label: label
|
|
16
|
+
}),
|
|
17
|
+
/*#__PURE__*/ _jsx(Button, {
|
|
18
|
+
onClick: handleDownload,
|
|
19
|
+
size: "medium",
|
|
20
|
+
type: "button",
|
|
21
|
+
children: "Download"
|
|
22
|
+
})
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/ExportSaveButton/index.tsx"],"sourcesContent":["'use client'\n\nimport { Button, SaveButton, useTranslation } from '@payloadcms/ui'\nimport React from 'react'\n\nexport const ExportSaveButton: React.FC = () => {\n const { t } = useTranslation()\n const label = t('general:save')\n\n const handleDownload = () => {\n console.log('Download')\n // TODO: create a post to the download endpoint to stream data back to the client\n }\n\n return (\n <React.Fragment>\n <SaveButton label={label}></SaveButton>\n <Button onClick={handleDownload} size=\"medium\" type=\"button\">\n Download\n </Button>\n </React.Fragment>\n )\n}\n"],"names":["Button","SaveButton","useTranslation","React","ExportSaveButton","t","label","handleDownload","console","log","Fragment","onClick","size","type"],"mappings":"AAAA;;AAEA,SAASA,MAAM,EAAEC,UAAU,EAAEC,cAAc,QAAQ,iBAAgB;AACnE,OAAOC,WAAW,QAAO;AAEzB,OAAO,MAAMC,mBAA6B;IACxC,MAAM,EAAEC,CAAC,EAAE,GAAGH;IACd,MAAMI,QAAQD,EAAE;IAEhB,MAAME,iBAAiB;QACrBC,QAAQC,GAAG,CAAC;IACZ,iFAAiF;IACnF;IAEA,qBACE,MAACN,MAAMO,QAAQ;;0BACb,KAACT;gBAAWK,OAAOA;;0BACnB,KAACN;gBAAOW,SAASJ;gBAAgBK,MAAK;gBAASC,MAAK;0BAAS;;;;AAKnE,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/FieldsToExport/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAmB,0BAA0B,EAAE,MAAM,SAAS,CAAA;AAkB1E,eAAO,MAAM,cAAc,EAAE,0BAiF5B,CAAA"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { FieldLabel, ReactSelect, useConfig, useDocumentInfo, useField, usePreferences } from '@payloadcms/ui';
|
|
4
|
+
import React, { useEffect, useState } from 'react';
|
|
5
|
+
import { useImportExport } from '../ImportExportProvider/index.js';
|
|
6
|
+
import { reduceFields } from './reduceFields.js';
|
|
7
|
+
const baseClass = 'fields-to-export';
|
|
8
|
+
export const FieldsToExport = (props)=>{
|
|
9
|
+
const { id } = useDocumentInfo();
|
|
10
|
+
const { path } = props;
|
|
11
|
+
const { setValue, value } = useField({
|
|
12
|
+
path
|
|
13
|
+
});
|
|
14
|
+
const { value: collectionSlug } = useField({
|
|
15
|
+
path: 'collectionSlug'
|
|
16
|
+
});
|
|
17
|
+
const { getEntityConfig } = useConfig();
|
|
18
|
+
const { collection } = useImportExport();
|
|
19
|
+
const { getPreference } = usePreferences();
|
|
20
|
+
const [displayedValue, setDisplayedValue] = useState([]);
|
|
21
|
+
const collectionConfig = getEntityConfig({
|
|
22
|
+
collectionSlug: collectionSlug ?? collection
|
|
23
|
+
});
|
|
24
|
+
const fieldOptions = reduceFields({
|
|
25
|
+
fields: collectionConfig?.fields
|
|
26
|
+
});
|
|
27
|
+
useEffect(()=>{
|
|
28
|
+
if (value && value.length > 0) {
|
|
29
|
+
setDisplayedValue((prevDisplayedValue)=>{
|
|
30
|
+
if (prevDisplayedValue.length > 0) {
|
|
31
|
+
return prevDisplayedValue;
|
|
32
|
+
} // Prevent unnecessary updates
|
|
33
|
+
return value.map((field)=>{
|
|
34
|
+
const match = fieldOptions.find((option)=>option.value === field);
|
|
35
|
+
return match ? {
|
|
36
|
+
...match,
|
|
37
|
+
id: field
|
|
38
|
+
} : {
|
|
39
|
+
id: field,
|
|
40
|
+
label: field,
|
|
41
|
+
value: field
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}, [
|
|
47
|
+
value,
|
|
48
|
+
fieldOptions
|
|
49
|
+
]);
|
|
50
|
+
useEffect(()=>{
|
|
51
|
+
if (id || !collectionSlug) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const doAsync = async ()=>{
|
|
55
|
+
const currentPreferences = await getPreference(`${collectionSlug}-list`);
|
|
56
|
+
const columns = currentPreferences?.columns?.filter((a)=>a.active).map((b)=>b.accessor);
|
|
57
|
+
setValue(columns ?? collectionConfig?.admin?.defaultColumns ?? []);
|
|
58
|
+
};
|
|
59
|
+
void doAsync();
|
|
60
|
+
}, [
|
|
61
|
+
getPreference,
|
|
62
|
+
collection,
|
|
63
|
+
setValue,
|
|
64
|
+
collectionSlug,
|
|
65
|
+
id,
|
|
66
|
+
collectionConfig?.admin?.defaultColumns
|
|
67
|
+
]);
|
|
68
|
+
const onChange = (options)=>{
|
|
69
|
+
if (!options) {
|
|
70
|
+
setValue([]);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const updatedValue = options?.map((option)=>typeof option === 'object' ? option.value : option);
|
|
74
|
+
setValue(updatedValue);
|
|
75
|
+
setDisplayedValue(options);
|
|
76
|
+
};
|
|
77
|
+
return /*#__PURE__*/ _jsxs("div", {
|
|
78
|
+
className: baseClass,
|
|
79
|
+
children: [
|
|
80
|
+
/*#__PURE__*/ _jsx(FieldLabel, {
|
|
81
|
+
label: "Columns to Export"
|
|
82
|
+
}),
|
|
83
|
+
/*#__PURE__*/ _jsx(ReactSelect, {
|
|
84
|
+
className: baseClass,
|
|
85
|
+
disabled: props.readOnly,
|
|
86
|
+
getOptionValue: (option)=>String(option.value),
|
|
87
|
+
isClearable: true,
|
|
88
|
+
isMulti: true,
|
|
89
|
+
isSortable: true,
|
|
90
|
+
// @ts-expect-error react select option
|
|
91
|
+
onChange: onChange,
|
|
92
|
+
options: fieldOptions,
|
|
93
|
+
value: displayedValue
|
|
94
|
+
})
|
|
95
|
+
]
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/FieldsToExport/index.tsx"],"sourcesContent":["'use client'\n\nimport type { ListPreferences, SelectFieldClientComponent } from 'payload'\nimport type { ReactNode } from 'react'\n\nimport {\n FieldLabel,\n ReactSelect,\n useConfig,\n useDocumentInfo,\n useField,\n usePreferences,\n} from '@payloadcms/ui'\nimport React, { useEffect, useState } from 'react'\n\nimport { useImportExport } from '../ImportExportProvider/index.js'\nimport { reduceFields } from './reduceFields.js'\n\nconst baseClass = 'fields-to-export'\n\nexport const FieldsToExport: SelectFieldClientComponent = (props) => {\n const { id } = useDocumentInfo()\n const { path } = props\n const { setValue, value } = useField<string[]>({ path })\n const { value: collectionSlug } = useField<string>({ path: 'collectionSlug' })\n const { getEntityConfig } = useConfig()\n const { collection } = useImportExport()\n const { getPreference } = usePreferences()\n const [displayedValue, setDisplayedValue] = useState<\n { id: string; label: ReactNode; value: string }[]\n >([])\n\n const collectionConfig = getEntityConfig({ collectionSlug: collectionSlug ?? collection })\n const fieldOptions = reduceFields({ fields: collectionConfig?.fields })\n\n useEffect(() => {\n if (value && value.length > 0) {\n setDisplayedValue((prevDisplayedValue) => {\n if (prevDisplayedValue.length > 0) {\n return prevDisplayedValue\n } // Prevent unnecessary updates\n\n return value.map((field) => {\n const match = fieldOptions.find((option) => option.value === field)\n return match ? { ...match, id: field } : { id: field, label: field, value: field }\n })\n })\n }\n }, [value, fieldOptions])\n\n useEffect(() => {\n if (id || !collectionSlug) {\n return\n }\n const doAsync = async () => {\n const currentPreferences = await getPreference<{\n columns: ListPreferences['columns']\n }>(`${collectionSlug}-list`)\n\n const columns = currentPreferences?.columns?.filter((a) => a.active).map((b) => b.accessor)\n setValue(columns ?? collectionConfig?.admin?.defaultColumns ?? [])\n }\n\n void doAsync()\n }, [\n getPreference,\n collection,\n setValue,\n collectionSlug,\n id,\n collectionConfig?.admin?.defaultColumns,\n ])\n const onChange = (options: { id: string; label: ReactNode; value: string }[]) => {\n if (!options) {\n setValue([])\n return\n }\n const updatedValue = options?.map((option) =>\n typeof option === 'object' ? option.value : option,\n )\n setValue(updatedValue)\n setDisplayedValue(options)\n }\n\n return (\n <div className={baseClass}>\n <FieldLabel label=\"Columns to Export\" />\n <ReactSelect\n className={baseClass}\n disabled={props.readOnly}\n getOptionValue={(option) => String(option.value)}\n isClearable={true}\n isMulti={true}\n isSortable={true}\n // @ts-expect-error react select option\n onChange={onChange}\n options={fieldOptions}\n value={displayedValue}\n />\n </div>\n )\n}\n"],"names":["FieldLabel","ReactSelect","useConfig","useDocumentInfo","useField","usePreferences","React","useEffect","useState","useImportExport","reduceFields","baseClass","FieldsToExport","props","id","path","setValue","value","collectionSlug","getEntityConfig","collection","getPreference","displayedValue","setDisplayedValue","collectionConfig","fieldOptions","fields","length","prevDisplayedValue","map","field","match","find","option","label","doAsync","currentPreferences","columns","filter","a","active","b","accessor","admin","defaultColumns","onChange","options","updatedValue","div","className","disabled","readOnly","getOptionValue","String","isClearable","isMulti","isSortable"],"mappings":"AAAA;;AAKA,SACEA,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,eAAe,EACfC,QAAQ,EACRC,cAAc,QACT,iBAAgB;AACvB,OAAOC,SAASC,SAAS,EAAEC,QAAQ,QAAQ,QAAO;AAElD,SAASC,eAAe,QAAQ,mCAAkC;AAClE,SAASC,YAAY,QAAQ,oBAAmB;AAEhD,MAAMC,YAAY;AAElB,OAAO,MAAMC,iBAA6C,CAACC;IACzD,MAAM,EAAEC,EAAE,EAAE,GAAGX;IACf,MAAM,EAAEY,IAAI,EAAE,GAAGF;IACjB,MAAM,EAAEG,QAAQ,EAAEC,KAAK,EAAE,GAAGb,SAAmB;QAAEW;IAAK;IACtD,MAAM,EAAEE,OAAOC,cAAc,EAAE,GAAGd,SAAiB;QAAEW,MAAM;IAAiB;IAC5E,MAAM,EAAEI,eAAe,EAAE,GAAGjB;IAC5B,MAAM,EAAEkB,UAAU,EAAE,GAAGX;IACvB,MAAM,EAAEY,aAAa,EAAE,GAAGhB;IAC1B,MAAM,CAACiB,gBAAgBC,kBAAkB,GAAGf,SAE1C,EAAE;IAEJ,MAAMgB,mBAAmBL,gBAAgB;QAAED,gBAAgBA,kBAAkBE;IAAW;IACxF,MAAMK,eAAef,aAAa;QAAEgB,QAAQF,kBAAkBE;IAAO;IAErEnB,UAAU;QACR,IAAIU,SAASA,MAAMU,MAAM,GAAG,GAAG;YAC7BJ,kBAAkB,CAACK;gBACjB,IAAIA,mBAAmBD,MAAM,GAAG,GAAG;oBACjC,OAAOC;gBACT,EAAE,8BAA8B;gBAEhC,OAAOX,MAAMY,GAAG,CAAC,CAACC;oBAChB,MAAMC,QAAQN,aAAaO,IAAI,CAAC,CAACC,SAAWA,OAAOhB,KAAK,KAAKa;oBAC7D,OAAOC,QAAQ;wBAAE,GAAGA,KAAK;wBAAEjB,IAAIgB;oBAAM,IAAI;wBAAEhB,IAAIgB;wBAAOI,OAAOJ;wBAAOb,OAAOa;oBAAM;gBACnF;YACF;QACF;IACF,GAAG;QAACb;QAAOQ;KAAa;IAExBlB,UAAU;QACR,IAAIO,MAAM,CAACI,gBAAgB;YACzB;QACF;QACA,MAAMiB,UAAU;YACd,MAAMC,qBAAqB,MAAMf,cAE9B,GAAGH,eAAe,KAAK,CAAC;YAE3B,MAAMmB,UAAUD,oBAAoBC,SAASC,OAAO,CAACC,IAAMA,EAAEC,MAAM,EAAEX,IAAI,CAACY,IAAMA,EAAEC,QAAQ;YAC1F1B,SAASqB,WAAWb,kBAAkBmB,OAAOC,kBAAkB,EAAE;QACnE;QAEA,KAAKT;IACP,GAAG;QACDd;QACAD;QACAJ;QACAE;QACAJ;QACAU,kBAAkBmB,OAAOC;KAC1B;IACD,MAAMC,WAAW,CAACC;QAChB,IAAI,CAACA,SAAS;YACZ9B,SAAS,EAAE;YACX;QACF;QACA,MAAM+B,eAAeD,SAASjB,IAAI,CAACI,SACjC,OAAOA,WAAW,WAAWA,OAAOhB,KAAK,GAAGgB;QAE9CjB,SAAS+B;QACTxB,kBAAkBuB;IACpB;IAEA,qBACE,MAACE;QAAIC,WAAWtC;;0BACd,KAACX;gBAAWkC,OAAM;;0BAClB,KAACjC;gBACCgD,WAAWtC;gBACXuC,UAAUrC,MAAMsC,QAAQ;gBACxBC,gBAAgB,CAACnB,SAAWoB,OAAOpB,OAAOhB,KAAK;gBAC/CqC,aAAa;gBACbC,SAAS;gBACTC,YAAY;gBACZ,uCAAuC;gBACvCX,UAAUA;gBACVC,SAASrB;gBACTR,OAAOK;;;;AAIf,EAAC"}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ClientField } from 'payload';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export declare const reduceFields: ({ fields, labelPrefix, path, }: {
|
|
4
|
+
fields: ClientField[];
|
|
5
|
+
labelPrefix?: React.ReactNode;
|
|
6
|
+
path?: string;
|
|
7
|
+
}) => {
|
|
8
|
+
id: string;
|
|
9
|
+
label: React.ReactNode;
|
|
10
|
+
value: string;
|
|
11
|
+
}[];
|
|
12
|
+
//# sourceMappingURL=reduceFields.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reduceFields.d.ts","sourceRoot":"","sources":["../../../src/components/FieldsToExport/reduceFields.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAG1C,OAAO,KAAmB,MAAM,OAAO,CAAA;AAyCvC,eAAO,MAAM,YAAY,mCAItB;IACD,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,WAAW,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,KAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EA0DxD,CAAA"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { fieldAffectsData, fieldHasSubFields } from 'payload/shared';
|
|
3
|
+
import React, { Fragment } from 'react';
|
|
4
|
+
const createNestedClientFieldPath = (parentPath, field)=>{
|
|
5
|
+
if (parentPath) {
|
|
6
|
+
if (fieldAffectsData(field)) {
|
|
7
|
+
return `${parentPath}.${field.name}`;
|
|
8
|
+
}
|
|
9
|
+
return parentPath;
|
|
10
|
+
}
|
|
11
|
+
if (fieldAffectsData(field)) {
|
|
12
|
+
return field.name;
|
|
13
|
+
}
|
|
14
|
+
return '';
|
|
15
|
+
};
|
|
16
|
+
const combineLabel = ({ field, prefix })=>{
|
|
17
|
+
return /*#__PURE__*/ _jsxs(Fragment, {
|
|
18
|
+
children: [
|
|
19
|
+
prefix ? /*#__PURE__*/ _jsxs(Fragment, {
|
|
20
|
+
children: [
|
|
21
|
+
/*#__PURE__*/ _jsx("span", {
|
|
22
|
+
style: {
|
|
23
|
+
display: 'inline-block'
|
|
24
|
+
},
|
|
25
|
+
children: prefix
|
|
26
|
+
}),
|
|
27
|
+
' > '
|
|
28
|
+
]
|
|
29
|
+
}) : null,
|
|
30
|
+
/*#__PURE__*/ _jsx("span", {
|
|
31
|
+
style: {
|
|
32
|
+
display: 'inline-block'
|
|
33
|
+
},
|
|
34
|
+
children: 'label' in field && typeof field.label === 'string' ? field.label : ('name' in field && field.name) ?? 'unnamed field'
|
|
35
|
+
})
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
export const reduceFields = ({ fields, labelPrefix = null, path = '' })=>{
|
|
40
|
+
if (!fields) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
return fields.reduce((fieldsToUse, field)=>{
|
|
44
|
+
// escape for a variety of reasons, include ui fields as they have `name`.
|
|
45
|
+
if (field.type === 'ui') {
|
|
46
|
+
return fieldsToUse;
|
|
47
|
+
}
|
|
48
|
+
if (!(field.type === 'array' || field.type === 'blocks') && fieldHasSubFields(field)) {
|
|
49
|
+
return [
|
|
50
|
+
...fieldsToUse,
|
|
51
|
+
...reduceFields({
|
|
52
|
+
fields: field.fields,
|
|
53
|
+
labelPrefix: combineLabel({
|
|
54
|
+
field,
|
|
55
|
+
prefix: labelPrefix
|
|
56
|
+
}),
|
|
57
|
+
path: createNestedClientFieldPath(path, field)
|
|
58
|
+
})
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
if (field.type === 'tabs' && 'tabs' in field) {
|
|
62
|
+
return [
|
|
63
|
+
...fieldsToUse,
|
|
64
|
+
...field.tabs.reduce((tabFields, tab)=>{
|
|
65
|
+
if ('fields' in tab) {
|
|
66
|
+
const isNamedTab = 'name' in tab && tab.name;
|
|
67
|
+
return [
|
|
68
|
+
...tabFields,
|
|
69
|
+
...reduceFields({
|
|
70
|
+
fields: tab.fields,
|
|
71
|
+
labelPrefix,
|
|
72
|
+
path: isNamedTab ? createNestedClientFieldPath(path, field) : path
|
|
73
|
+
})
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
return tabFields;
|
|
77
|
+
}, [])
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
const val = createNestedClientFieldPath(path, field);
|
|
81
|
+
const formattedField = {
|
|
82
|
+
id: val,
|
|
83
|
+
label: combineLabel({
|
|
84
|
+
field,
|
|
85
|
+
prefix: labelPrefix
|
|
86
|
+
}),
|
|
87
|
+
value: val
|
|
88
|
+
};
|
|
89
|
+
return [
|
|
90
|
+
...fieldsToUse,
|
|
91
|
+
formattedField
|
|
92
|
+
];
|
|
93
|
+
}, []);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//# sourceMappingURL=reduceFields.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/FieldsToExport/reduceFields.tsx"],"sourcesContent":["import type { ClientField } from 'payload'\n\nimport { fieldAffectsData, fieldHasSubFields } from 'payload/shared'\nimport React, { Fragment } from 'react'\n\nconst createNestedClientFieldPath = (parentPath: string, field: ClientField): string => {\n if (parentPath) {\n if (fieldAffectsData(field)) {\n return `${parentPath}.${field.name}`\n }\n return parentPath\n }\n\n if (fieldAffectsData(field)) {\n return field.name\n }\n\n return ''\n}\n\nconst combineLabel = ({\n field,\n prefix,\n}: {\n field: ClientField\n prefix?: React.ReactNode\n}): React.ReactNode => {\n return (\n <Fragment>\n {prefix ? (\n <Fragment>\n <span style={{ display: 'inline-block' }}>{prefix}</span>\n {' > '}\n </Fragment>\n ) : null}\n <span style={{ display: 'inline-block' }}>\n {'label' in field && typeof field.label === 'string'\n ? field.label\n : (('name' in field && field.name) ?? 'unnamed field')}\n </span>\n </Fragment>\n )\n}\n\nexport const reduceFields = ({\n fields,\n labelPrefix = null,\n path = '',\n}: {\n fields: ClientField[]\n labelPrefix?: React.ReactNode\n path?: string\n}): { id: string; label: React.ReactNode; value: string }[] => {\n if (!fields) {\n return []\n }\n\n return fields.reduce<{ id: string; label: React.ReactNode; value: string }[]>(\n (fieldsToUse, field) => {\n // escape for a variety of reasons, include ui fields as they have `name`.\n if (field.type === 'ui') {\n return fieldsToUse\n }\n\n if (!(field.type === 'array' || field.type === 'blocks') && fieldHasSubFields(field)) {\n return [\n ...fieldsToUse,\n ...reduceFields({\n fields: field.fields,\n labelPrefix: combineLabel({ field, prefix: labelPrefix }),\n path: createNestedClientFieldPath(path, field),\n }),\n ]\n }\n\n if (field.type === 'tabs' && 'tabs' in field) {\n return [\n ...fieldsToUse,\n ...field.tabs.reduce<{ id: string; label: React.ReactNode; value: string }[]>(\n (tabFields, tab) => {\n if ('fields' in tab) {\n const isNamedTab = 'name' in tab && tab.name\n return [\n ...tabFields,\n ...reduceFields({\n fields: tab.fields,\n labelPrefix,\n path: isNamedTab ? createNestedClientFieldPath(path, field) : path,\n }),\n ]\n }\n return tabFields\n },\n [],\n ),\n ]\n }\n\n const val = createNestedClientFieldPath(path, field)\n\n const formattedField = {\n id: val,\n label: combineLabel({ field, prefix: labelPrefix }),\n value: val,\n }\n\n return [...fieldsToUse, formattedField]\n },\n [],\n )\n}\n"],"names":["fieldAffectsData","fieldHasSubFields","React","Fragment","createNestedClientFieldPath","parentPath","field","name","combineLabel","prefix","span","style","display","label","reduceFields","fields","labelPrefix","path","reduce","fieldsToUse","type","tabs","tabFields","tab","isNamedTab","val","formattedField","id","value"],"mappings":";AAEA,SAASA,gBAAgB,EAAEC,iBAAiB,QAAQ,iBAAgB;AACpE,OAAOC,SAASC,QAAQ,QAAQ,QAAO;AAEvC,MAAMC,8BAA8B,CAACC,YAAoBC;IACvD,IAAID,YAAY;QACd,IAAIL,iBAAiBM,QAAQ;YAC3B,OAAO,GAAGD,WAAW,CAAC,EAAEC,MAAMC,IAAI,EAAE;QACtC;QACA,OAAOF;IACT;IAEA,IAAIL,iBAAiBM,QAAQ;QAC3B,OAAOA,MAAMC,IAAI;IACnB;IAEA,OAAO;AACT;AAEA,MAAMC,eAAe,CAAC,EACpBF,KAAK,EACLG,MAAM,EAIP;IACC,qBACE,MAACN;;YACEM,uBACC,MAACN;;kCACC,KAACO;wBAAKC,OAAO;4BAAEC,SAAS;wBAAe;kCAAIH;;oBAC1C;;iBAED;0BACJ,KAACC;gBAAKC,OAAO;oBAAEC,SAAS;gBAAe;0BACpC,WAAWN,SAAS,OAAOA,MAAMO,KAAK,KAAK,WACxCP,MAAMO,KAAK,GACV,AAAC,CAAA,UAAUP,SAASA,MAAMC,IAAI,AAAD,KAAM;;;;AAIhD;AAEA,OAAO,MAAMO,eAAe,CAAC,EAC3BC,MAAM,EACNC,cAAc,IAAI,EAClBC,OAAO,EAAE,EAKV;IACC,IAAI,CAACF,QAAQ;QACX,OAAO,EAAE;IACX;IAEA,OAAOA,OAAOG,MAAM,CAClB,CAACC,aAAab;QACZ,0EAA0E;QAC1E,IAAIA,MAAMc,IAAI,KAAK,MAAM;YACvB,OAAOD;QACT;QAEA,IAAI,CAAEb,CAAAA,MAAMc,IAAI,KAAK,WAAWd,MAAMc,IAAI,KAAK,QAAO,KAAMnB,kBAAkBK,QAAQ;YACpF,OAAO;mBACFa;mBACAL,aAAa;oBACdC,QAAQT,MAAMS,MAAM;oBACpBC,aAAaR,aAAa;wBAAEF;wBAAOG,QAAQO;oBAAY;oBACvDC,MAAMb,4BAA4Ba,MAAMX;gBAC1C;aACD;QACH;QAEA,IAAIA,MAAMc,IAAI,KAAK,UAAU,UAAUd,OAAO;YAC5C,OAAO;mBACFa;mBACAb,MAAMe,IAAI,CAACH,MAAM,CAClB,CAACI,WAAWC;oBACV,IAAI,YAAYA,KAAK;wBACnB,MAAMC,aAAa,UAAUD,OAAOA,IAAIhB,IAAI;wBAC5C,OAAO;+BACFe;+BACAR,aAAa;gCACdC,QAAQQ,IAAIR,MAAM;gCAClBC;gCACAC,MAAMO,aAAapB,4BAA4Ba,MAAMX,SAASW;4BAChE;yBACD;oBACH;oBACA,OAAOK;gBACT,GACA,EAAE;aAEL;QACH;QAEA,MAAMG,MAAMrB,4BAA4Ba,MAAMX;QAE9C,MAAMoB,iBAAiB;YACrBC,IAAIF;YACJZ,OAAOL,aAAa;gBAAEF;gBAAOG,QAAQO;YAAY;YACjDY,OAAOH;QACT;QAEA,OAAO;eAAIN;YAAaO;SAAe;IACzC,GACA,EAAE;AAEN,EAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type ImportExportContext = {
|
|
3
|
+
collection: string;
|
|
4
|
+
setCollection: (collection: string) => void;
|
|
5
|
+
};
|
|
6
|
+
export declare const ImportExportContext: React.Context<ImportExportContext>;
|
|
7
|
+
export declare const ImportExportProvider: React.FC<{
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
}>;
|
|
10
|
+
export declare const useImportExport: () => ImportExportContext;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/ImportExportProvider/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAA2D,MAAM,OAAO,CAAA;AAE/E,KAAK,mBAAmB,GAAG;IACzB,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;CAC5C,CAAA;AAED,eAAO,MAAM,mBAAmB,oCAA2C,CAAA;AAE3E,eAAO,MAAM,oBAAoB,EAAE,KAAK,CAAC,EAAE,CAAC;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,CAiBxE,CAAA;AAED,eAAO,MAAM,eAAe,QAAO,mBAAsD,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import React, { createContext, useCallback, useContext, useState } from 'react';
|
|
4
|
+
export const ImportExportContext = /*#__PURE__*/ createContext({});
|
|
5
|
+
export const ImportExportProvider = ({ children })=>{
|
|
6
|
+
const [collection, setCollectionState] = useState('');
|
|
7
|
+
const setCollection = useCallback((collection)=>{
|
|
8
|
+
setCollectionState(collection);
|
|
9
|
+
}, []);
|
|
10
|
+
return /*#__PURE__*/ _jsx(ImportExportContext.Provider, {
|
|
11
|
+
value: {
|
|
12
|
+
collection,
|
|
13
|
+
setCollection
|
|
14
|
+
},
|
|
15
|
+
children: children
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
export const useImportExport = ()=>useContext(ImportExportContext);
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/ImportExportProvider/index.tsx"],"sourcesContent":["'use client'\nimport React, { createContext, useCallback, useContext, useState } from 'react'\n\ntype ImportExportContext = {\n collection: string\n setCollection: (collection: string) => void\n}\n\nexport const ImportExportContext = createContext({} as ImportExportContext)\n\nexport const ImportExportProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {\n const [collection, setCollectionState] = useState<string>('')\n\n const setCollection = useCallback((collection: string) => {\n setCollectionState(collection)\n }, [])\n\n return (\n <ImportExportContext.Provider\n value={{\n collection,\n setCollection,\n }}\n >\n {children}\n </ImportExportContext.Provider>\n )\n}\n\nexport const useImportExport = (): ImportExportContext => useContext(ImportExportContext)\n"],"names":["React","createContext","useCallback","useContext","useState","ImportExportContext","ImportExportProvider","children","collection","setCollectionState","setCollection","Provider","value","useImportExport"],"mappings":"AAAA;;AACA,OAAOA,SAASC,aAAa,EAAEC,WAAW,EAAEC,UAAU,EAAEC,QAAQ,QAAQ,QAAO;AAO/E,OAAO,MAAMC,oCAAsBJ,cAAc,CAAC,GAAyB;AAE3E,OAAO,MAAMK,uBAAgE,CAAC,EAAEC,QAAQ,EAAE;IACxF,MAAM,CAACC,YAAYC,mBAAmB,GAAGL,SAAiB;IAE1D,MAAMM,gBAAgBR,YAAY,CAACM;QACjCC,mBAAmBD;IACrB,GAAG,EAAE;IAEL,qBACE,KAACH,oBAAoBM,QAAQ;QAC3BC,OAAO;YACLJ;YACAE;QACF;kBAECH;;AAGP,EAAC;AAED,OAAO,MAAMM,kBAAkB,IAA2BV,WAAWE,qBAAoB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/Preview/index.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,OAAO,cAAc,CAAA;AAIrB,eAAO,MAAM,OAAO,yBA8FnB,CAAA"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Table, useConfig, useField } from '@payloadcms/ui';
|
|
4
|
+
import { fieldAffectsData } from 'payload/shared';
|
|
5
|
+
import * as qs from 'qs-esm';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import { useImportExport } from '../ImportExportProvider/index.js';
|
|
8
|
+
import './index.scss';
|
|
9
|
+
const baseClass = 'preview';
|
|
10
|
+
export const Preview = ()=>{
|
|
11
|
+
const { collection } = useImportExport();
|
|
12
|
+
const { config } = useConfig();
|
|
13
|
+
const { value: where } = useField({
|
|
14
|
+
path: 'where'
|
|
15
|
+
});
|
|
16
|
+
const { value: limit } = useField({
|
|
17
|
+
path: 'limit'
|
|
18
|
+
});
|
|
19
|
+
const { value: fields } = useField({
|
|
20
|
+
path: 'fields'
|
|
21
|
+
});
|
|
22
|
+
const { value: sort } = useField({
|
|
23
|
+
path: 'sort'
|
|
24
|
+
});
|
|
25
|
+
const { value: draft } = useField({
|
|
26
|
+
path: 'draft'
|
|
27
|
+
});
|
|
28
|
+
const [dataToRender, setDataToRender] = React.useState([]);
|
|
29
|
+
const [resultCount, setResultCount] = React.useState('');
|
|
30
|
+
const [columns, setColumns] = React.useState([]);
|
|
31
|
+
const collectionSlug = typeof collection === 'string' && collection;
|
|
32
|
+
const collectionConfig = config.collections.find((collection)=>collection.slug === collectionSlug);
|
|
33
|
+
React.useEffect(()=>{
|
|
34
|
+
const fetchData = async ()=>{
|
|
35
|
+
if (!collectionSlug) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const whereQuery = qs.stringify({
|
|
40
|
+
depth: 0,
|
|
41
|
+
draft,
|
|
42
|
+
limit: limit > 10 ? 10 : limit,
|
|
43
|
+
sort,
|
|
44
|
+
where
|
|
45
|
+
}, {
|
|
46
|
+
addQueryPrefix: true
|
|
47
|
+
});
|
|
48
|
+
const response = await fetch(`/api/${collectionSlug}${whereQuery}`, {
|
|
49
|
+
headers: {
|
|
50
|
+
'Content-Type': 'application/json'
|
|
51
|
+
},
|
|
52
|
+
method: 'GET'
|
|
53
|
+
});
|
|
54
|
+
if (response.ok) {
|
|
55
|
+
const data = await response.json();
|
|
56
|
+
setResultCount(limit && limit < data.totalDocs ? limit : data.totalDocs);
|
|
57
|
+
// TODO: check if this data is in the correct format for the table
|
|
58
|
+
const filteredFields = collectionConfig?.fields?.filter((field)=>{
|
|
59
|
+
if (!fieldAffectsData(field)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (fields?.length > 0) {
|
|
63
|
+
return fields.includes(field.name);
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}) ?? [];
|
|
67
|
+
setColumns(filteredFields.map((field)=>({
|
|
68
|
+
accessor: field.name || '',
|
|
69
|
+
active: true,
|
|
70
|
+
field: field,
|
|
71
|
+
Heading: field?.label || field.name,
|
|
72
|
+
renderedCells: data.docs.map((doc)=>{
|
|
73
|
+
if (!field.name || !doc[field.name]) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
if (typeof doc[field.name] === 'object') {
|
|
77
|
+
return JSON.stringify(doc[field.name]);
|
|
78
|
+
}
|
|
79
|
+
return String(doc[field.name]);
|
|
80
|
+
})
|
|
81
|
+
})));
|
|
82
|
+
setDataToRender(data.docs);
|
|
83
|
+
}
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error('Error fetching data:', error);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
void fetchData();
|
|
89
|
+
}, [
|
|
90
|
+
collectionConfig?.fields,
|
|
91
|
+
collectionSlug,
|
|
92
|
+
draft,
|
|
93
|
+
fields,
|
|
94
|
+
limit,
|
|
95
|
+
sort,
|
|
96
|
+
where
|
|
97
|
+
]);
|
|
98
|
+
return /*#__PURE__*/ _jsxs("div", {
|
|
99
|
+
className: baseClass,
|
|
100
|
+
children: [
|
|
101
|
+
/*#__PURE__*/ _jsxs("div", {
|
|
102
|
+
className: `${baseClass}__header`,
|
|
103
|
+
children: [
|
|
104
|
+
/*#__PURE__*/ _jsx("h3", {
|
|
105
|
+
children: "Preview"
|
|
106
|
+
}),
|
|
107
|
+
resultCount && /*#__PURE__*/ _jsxs("span", {
|
|
108
|
+
children: [
|
|
109
|
+
resultCount,
|
|
110
|
+
" total documents"
|
|
111
|
+
]
|
|
112
|
+
})
|
|
113
|
+
]
|
|
114
|
+
}),
|
|
115
|
+
dataToRender && /*#__PURE__*/ _jsx(Table, {
|
|
116
|
+
columns: columns,
|
|
117
|
+
data: dataToRender
|
|
118
|
+
})
|
|
119
|
+
]
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
//# sourceMappingURL=index.js.map
|