From 47ebbc9ffeb9aff02f8ae9fbb8da7f1c0048bba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edison=20Pe=C3=B1uela?= Date: Fri, 9 Apr 2021 00:15:35 -0500 Subject: [PATCH] Add Data Mapper to Admin --- admin/src/components/DataMapper/index.js | 98 ++++++++++++++++++++ admin/src/components/DataMapper/styles.js | 73 +++++++++++++++ admin/src/containers/App/index.js | 14 +-- admin/src/containers/ExportPage/index.js | 12 ++- admin/src/containers/ImportPage/index.js | 104 ++++++++++++++++++---- 5 files changed, 277 insertions(+), 24 deletions(-) create mode 100644 admin/src/components/DataMapper/index.js create mode 100644 admin/src/components/DataMapper/styles.js diff --git a/admin/src/components/DataMapper/index.js b/admin/src/components/DataMapper/index.js new file mode 100644 index 0000000..f0fda4f --- /dev/null +++ b/admin/src/components/DataMapper/index.js @@ -0,0 +1,98 @@ +import React, { useState } from "react"; +import PropTypes from "prop-types"; + +import { Button } from "@buffetjs/core"; +import { Row } from "../common"; +import { TableWrapper } from "./styles"; + +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faTrashAlt } from "@fortawesome/free-solid-svg-icons"; + +function DataMapper({ data, mapper, onSuccess, onCancel }) { + const { fieldsInfo, parsedData } = data; + const [importItems, setImportItems] = useState(parsedData); + + console.log(mapper); + + const deleteItem = (item) => () => { + const importItemsFiltered = importItems.filter( + (importItems) => importItems !== item + ); + setImportItems(importItemsFiltered); + }; + + const handleUploadItems = () => onSuccess(importItems); + return ( +
+ +

Map the Import Data to Destination Field

+ + + + + {fieldsInfo.map(({ fieldName }) => ( + + ))} + + + + + {importItems.map((row, i) => ( + + {fieldsInfo.map(({ fieldName }, j) => { + const cell = row[fieldName]; + + if (cell === undefined || cell === null) + return ; + + if (typeof cell === "object") + return ; + + return ; + })} + + + ))} + +
{fieldName}Del
-{JSON.stringify(cell)}{`${cell}`} + +
+
+
+ + { +

+ Count of Items to Import: {importItems.length} +

+ } +
+ +
+ ); +} + +DataMapper.defaultProps = { + data: {}, + mapper: {}, + onSuccess: () => {}, + onCancel: () => {}, +}; + +DataMapper.propTypes = { + data: PropTypes.any, + mapper: PropTypes.any, + onSuccess: PropTypes.func, + onCancel: PropTypes.func, +}; + +export default DataMapper; diff --git a/admin/src/components/DataMapper/styles.js b/admin/src/components/DataMapper/styles.js new file mode 100644 index 0000000..9fac148 --- /dev/null +++ b/admin/src/components/DataMapper/styles.js @@ -0,0 +1,73 @@ +import styled from "styled-components"; + +const TableWrapper = styled.div` + width: 100%; + overflow-x: auto; + background: white; + margin-top: 1rem; + border-radius: 4px; + border: 1px solid #e3e9f3; + max-height: 250px; + + table { + width: 100%; + text-align: center; + + th, + td { + padding: 15px; + } + + th { + min-width: 15ch; + background-color: #f3f3f4; + font-weight: bold; + } + + th:last-child { + min-width: 50px; + width: 50px; + } + + td:last-child { + opacity: 0.5; + border-left: 1px solid #ccc; + cursor: pointer; + max-width: 50px; + padding: 0; + + button { + border: none; + outline: none; + padding: 15px; + background: transparent; + font-size: 1.2rem; + } + + &:hover { + opacity: 1; + } + } + + tbody { + tr { + &:nth-child(even) { + background-color: #fafafa; + } + + &:hover { + background-color: #efefef; + } + } + } + + td { + max-width: 15ch; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + } +`; + +export { TableWrapper }; diff --git a/admin/src/containers/App/index.js b/admin/src/containers/App/index.js index 94f2b2c..b623651 100644 --- a/admin/src/containers/App/index.js +++ b/admin/src/containers/App/index.js @@ -28,21 +28,25 @@ const navLinks = [ }, ]; -const App = () => { +function App() { const userContentTypes = useContentTypes(); - console.log(userContentTypes); return ( - - + + + + + + + {/* Default Route Retur to Import Page */} ); -}; +} export default App; diff --git a/admin/src/containers/ExportPage/index.js b/admin/src/containers/ExportPage/index.js index bfd7cd0..a5e31cf 100644 --- a/admin/src/containers/ExportPage/index.js +++ b/admin/src/containers/ExportPage/index.js @@ -5,10 +5,10 @@ */ import React, { memo } from "react"; -// import PropTypes from "prop-types"; +import PropTypes from "prop-types"; import { Block } from "../../components/common"; -function ImportPage() { +function ImportPage({ contentTypes }) { return ( { setSourceImports(value); }; + const handleSelectImportDestination = ({ target: { value } }) => { + setImportDest(value); + }; + const analizeImports = async (body) => { + // if (importDest === "") + // return strapi.notification.toggle({ + // type: "warning", + // message: "import.destination.empty", + // }); + try { const response = await request(`/${pluginId}/pre-analyze`, { method: "POST", body, }); - console.log(response); + setMapper(contentTypes.find(({ uid }) => uid === importDest)); + setAnalysis(response); + strapi.notification.toggle({ type: "success", message: "import.analyze.success", @@ -47,30 +64,83 @@ function ImportPage() { } }; + const uploadData = (dataToUpload) => { + console.log(dataToUpload); + if (dataToUpload.length === 0) { + strapi.notification.toggle({ + type: "warning", + message: "import.items.empty", + }); + } + + setMapper(null); + setAnalysis(null); + }; + + const cancelUpload = () => { + setMapper(null); + setAnalysis(null); + }; + return ( - -
- - +
+
+ +