Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
Complete Initial Import View
Browse files Browse the repository at this point in the history
  • Loading branch information
EdisonPeM committed Apr 10, 2021
1 parent 0d475be commit dad524e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 37 deletions.
41 changes: 27 additions & 14 deletions admin/src/components/DataMapper/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useMemo, memo } from "react";
import PropTypes from "prop-types";
import { Prompt } from "react-router-dom";

import { Row } from "../common";
import { TableWrapper } from "./styles";
Expand All @@ -8,6 +9,17 @@ import { Button } from "@buffetjs/core";
import DataHeader from "./DataHeader";
import DataBody from "./DataBody";

// FORMATS
import {
Bool as BoolIcon,
Json as JsonIcon,
Text as TextIcon,
NumberIcon,
Email as EmailIcon,
Calendar as DateIcon,
RichText as RichTextIcon,
} from "@buffetjs/icons";

const filterIgnoreFields = (fieldName) =>
![
"id",
Expand All @@ -25,10 +37,20 @@ function DataMapper({ data, mapper, onSuccess, onCancel }) {
const { fieldsInfo, parsedData } = data;
const { uid, attributes } = mapper;

const filteredAttributes = useMemo(
() =>
Object.keys(attributes)
.filter(filterIgnoreFields)
.filter((field) => filterIgnoreTypes(attributes[field].type)),
[attributes]
);

// Manipulation over maping columns
const [mappedFields, setMappedFields] = useState(
fieldsInfo.reduce((mappedFields, { fieldName }) => {
mappedFields[fieldName] = attributes[fieldName] ? fieldName : "none";
mappedFields[fieldName] = filteredAttributes.includes(fieldName)
? fieldName
: "none";
return mappedFields;
}, {})
);
Expand All @@ -37,18 +59,8 @@ function DataMapper({ data, mapper, onSuccess, onCancel }) {
setMappedFields({ ...mappedFields, [source]: value });
};

const destinationOptions = useMemo(
() =>
[{ label: "None", value: "none" }].concat(
Object.keys(attributes)
.filter(filterIgnoreFields)
.filter((field) => filterIgnoreTypes(attributes[field].type))
.map((field) => ({
label: field,
value: field,
}))
),
[attributes]
const destinationOptions = [{ label: "None", value: "none" }].concat(
filteredAttributes.map((field) => ({ label: field, value: field }))
);

// Manipulation over Rows
Expand All @@ -62,10 +74,11 @@ function DataMapper({ data, mapper, onSuccess, onCancel }) {

// UploadData
const handleUploadItems = () =>
onSuccess({ uid, fields: mappedFields, importItems });
onSuccess({ target: uid, fields: mappedFields, items: importItems });

return (
<div className="pt-3 col-12">
<Prompt message="import.mapper.unsaved" />
<Row>
<h2>Map the Import Data to Destination Field</h2>
<TableWrapper>
Expand Down
7 changes: 2 additions & 5 deletions admin/src/components/DataMapper/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ const TableWrapper = styled.div`
width: 100%;
text-align: center;
th,
td {
padding: 15px;
}
th {
min-width: 15ch;
background-color: #f3f3f4;
font-weight: bold;
padding: 10px;
}
th:last-child {
Expand Down Expand Up @@ -61,6 +57,7 @@ const TableWrapper = styled.div`
}
td {
padding: 15px;
cursor: auto;
max-width: 15ch;
white-space: nowrap;
Expand Down
8 changes: 4 additions & 4 deletions admin/src/components/DropFileZone/DragAndDropIcon.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/src/containers/ExportPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function ImportPage({ contentTypes }) {
description="Configure the Import Source & Destination"
style={{ marginBottom: 12 }}
>
<h1>Happy Coding</h1>
<h1>Work in Progress</h1>
</Block>
);
}
Expand Down
28 changes: 23 additions & 5 deletions admin/src/containers/ImportPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,37 @@ function ImportPage({ contentTypes }) {
};

// Upload Data
const uploadData = ({ uid, fields, importItems }) => {
const uploadData = async ({ target, fields, items }) => {
// Finish with the import
endImport();

// Prevent Upload Empty Data;
if (importItems.length === 0) {
if (items.length === 0) {
strapi.notification.toggle({
type: "warning",
message: "import.items.empty",
});
}

console.log(uid, fields, importItems);
try {
// Send Request
const response = await request(`/${pluginId}/import`, {
method: "POST",
body: { target, fields, items },
});

// Finish with the import
endImport();
console.log(response);

strapi.notification.toggle({
type: "success",
message: `import.items.succesfully`,
});
} catch (error) {
strapi.notification.toggle({
type: "warning",
message: `import.items.error`,
});
}
};

// Reset analysis and mapper
Expand Down
10 changes: 9 additions & 1 deletion admin/src/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"import.file.type.error": "File Type is not accepted",
"import.mapper.unsaved": "",
"import.destination.empty": "",
"import.analyze.success": "",
"import.analyze.error": "",
"import.items.empty": "",
"import.items.succesfully": "",
"import.items.error": "",
"import.file.content.error": "",
"import.file.type.error": "",
"plugin.name": "Import / Export Content"
}
12 changes: 9 additions & 3 deletions admin/src/translations/es.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"import.file.type.error": "El tipo de archivo no es valido",
"import.page.title": "Importar Datos",
"import.analyze.success": "Import Analyze Success (es)",
"import.mapper.unsaved": "",
"import.destination.empty": "",
"import.analyze.success": "",
"import.analyze.error": "",
"import.items.empty": "",
"import.items.succesfully": "",
"import.items.error": "",
"import.file.content.error": "",
"import.file.type.error": "",
"plugin.name": "Importar / Exportar Contenido"
}
2 changes: 1 addition & 1 deletion config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{
"method": "POST",
"path": "/import",
"handler": "import-export-content.importContent",
"handler": "import-export-content.importItems",
"config": {
"policies": []
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/import-export-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ module.exports = {
}
},

importContent: async (ctx) => {
importItems: async (ctx) => {
// const { user } = ctx.state;

const data = ctx.request.body;
const { targetModel, source, kind } = data;
if (!targetModel || !source || !kind) {
const { target, fields, items } = data;
if (!target || !fields || !items) {
return ctx.throw(400, "Required parameters missing");
}

Expand Down

0 comments on commit dad524e

Please sign in to comment.