Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions admin/src/components/CoreStore/ExportCoreStoreFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from 'styled-components';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { request } from 'strapi-helper-plugin';
import { Button } from '@buffetjs/core';
import { Button, Padded } from '@buffetjs/core';

import CardWidget from '../data-display/CardWidget';
import ShowMoreCollapse from '../data-display/ShowMoreCollapse';
Expand All @@ -15,11 +15,38 @@ export const StyledCardWidgetFile = styled(CardWidget)`
}
`;

export const ExportCoreStoreButton = ({ fileName, label }) => {
const handleExport = async () => {
export const ExportCoreStoreButton = ({ fileName, label, showOptions }) => {
const [models, setModels] = useState();
const [selectedModel, setSelectedModel] = useState('');

useEffect(() => {
async function fetchData() {
const { data } = await request('/content-type-builder/content-types');
setModels(
data
.map((model) => ({
value: model.uid,
name: model.schema.name,
}))
.sort((a, b) => a.name.localeCompare(b.name))
);
}

if (showOptions) fetchData();
}, [models && models.length]);

const handleExport = async (model) => {
try {
const userRoles = await request(`/migrate/getCoreStoreJSON`);
downloadNamedJson(userRoles, fileName || 'settings-layouts-strapi-migrate');
const path = model
? `/migrate/getCoreStoreJSON/${model}`
: '/migrate/getCoreStoreJSON';
const data = await request(path);

const defaultFilename = model
? `settings-layouts-strapi-migrate-${model}`
: 'settings-layouts-strapi-migrate';
downloadNamedJson(data, fileName || defaultFilename);

strapi.notification.toggle({
message: 'Settings and layouts exported successfully.',
timeout: 3500,
Expand All @@ -36,13 +63,30 @@ export const ExportCoreStoreButton = ({ fileName, label }) => {
}
};

const handleChange = (e) => {
setSelectedModel(e.currentTarget.value);
};

return (
<div id="download">
<Button
color="primary"
label={label || 'Download'}
onClick={handleExport}
/>
{showOptions && (
<Padded top size="smd">
<select onChange={handleChange} disabled={!models}>
<option value="">All models</option>
{models && models.map(({ name, value }) => (
<option key={value} value={value}>{name}</option>
))}
</select>
</Padded>
)}
<Padded top size="smd">
<Button
color="primary"
disabled={showOptions && !models}
label={label || 'Download'}
onClick={() => handleExport(selectedModel)}
/>
</Padded>
</div>
);
};
Expand All @@ -55,7 +99,7 @@ const ExportCoreStoreFile = () => {
Clicking the button will download a JSON file with your Strapi Settings and layouts data.
</p>

<ExportCoreStoreButton />
<ExportCoreStoreButton showOptions={true} />

<ShowMoreCollapse openLabel="Details">
<div style={{ paddingTop: 16 }}>
Expand All @@ -69,4 +113,4 @@ const ExportCoreStoreFile = () => {
);
};

export default ExportCoreStoreFile;
export default ExportCoreStoreFile;
4 changes: 2 additions & 2 deletions config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
{
"method": "GET",
"path": "/getCoreStoreJSON",
"path": "/getCoreStoreJSON/:model?",
"handler": "spm-file-export-core-store.getCoreStoreJSON",
"config": {
"policies": []
Expand Down Expand Up @@ -97,4 +97,4 @@
}
}
]
}
}
14 changes: 11 additions & 3 deletions controllers/spm-file-export-core-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ module.exports = {
},
getCoreStoreJSON: async ctx => {
const { user } = ctx.state;
const { model } = ctx.params;

if (user.roles[0].code !== 'strapi-super-admin') {
return ctx.unauthorized('You must be an admin to export permissions.');
return ctx.unauthorized('You must be an admin to export settings and layouts.');
}

const queryOptions = { _limit: -1 };
if (model) {
const prefix = 'plugin_content_manager_configuration_content_types::';
queryOptions.key = prefix + model;
}

const coreStoreAPI = strapi.query('core_store');
const coreStore = await coreStoreAPI.find({ _limit: -1 });
const coreStore = await coreStoreAPI.find(queryOptions);
const withoutIds = coreStore.map(({id, ...coreStoreNoIds}) => coreStoreNoIds)

return withoutIds;
}
};
};