diff --git a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/actions.js b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/actions.js index 1c140ae..fc72d16 100644 --- a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/actions.js +++ b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/actions.js @@ -1,25 +1,22 @@ -import React, { Component } from "react"; -import PropTypes from "prop-types"; import { APIRoutes } from "./routes"; import { http } from "./config"; -const getResource = async ( - apiEndpoint, - pid, -) => { +const getResource = async (apiEndpoint, pid) => { return await http.get(APIRoutes.get(apiEndpoint, pid)); }; -const deleteResource = async ( - resource, - apiEndpoint, - idKeyPath = "pid" -) => { - return await http.delete(APIRoutes.detailsView(apiEndpoint, resource, idKeyPath)); +const searchResource = async (apiEndpoint, query, sort, page, size) => { + return await http.get(APIRoutes.search(apiEndpoint, query, sort, page, size)); }; -const editResource = async(apiEndpoint, pid, payload) => { - return await http.put(APIRoutes.get(apiEndpoint,pid), payload); +const deleteResource = async (resource, apiEndpoint, idKeyPath = "pid") => { + return await http.delete( + APIRoutes.detailsView(apiEndpoint, resource, idKeyPath) + ); +}; + +const editResource = async (apiEndpoint, pid, payload) => { + return await http.put(APIRoutes.get(apiEndpoint, pid), payload); }; const createResource = () => {}; @@ -30,5 +27,5 @@ export const InvenioAdministrationActionsApi = { deleteResource: deleteResource, editResource: editResource, getResource: getResource, -}; - + searchResource: searchResource, +}; \ No newline at end of file diff --git a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/config.js b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/config.js index 5130261..c62bfd2 100644 --- a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/config.js +++ b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/config.js @@ -10,7 +10,9 @@ const apiConfig = { withCredentials: true, xsrfCookieName: "csrftoken", xsrfHeaderName: "X-CSRFToken", + baseURL: "/", headers: { + Accept: "application/json", "Accept": "application/json", "Content-Type": "application/json", }, diff --git a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/routes.js b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/routes.js index ee803ee..52ca304 100644 --- a/invenio_administration/assets/semantic-ui/js/invenio_administration/api/routes.js +++ b/invenio_administration/assets/semantic-ui/js/invenio_administration/api/routes.js @@ -1,14 +1,17 @@ import _get from "lodash/get"; const APIRoutesGenerators = { - detailsView: (routePrefix, resource, idKeyPath="pid") => { + detailsView: (routePrefix, resource, idKeyPath = "pid") => { return `${routePrefix}/${_get(resource, idKeyPath)}`; }, get: (routePrefix, pid) => { return `${routePrefix}/${pid}`; }, -} + search: (routePrefix, query, sort, page, size) => { + return `${routePrefix}?q=${query}&sort=${sort}&page=${page}&size=${size}`; + }, +}; export const APIRoutes = { - ...APIRoutesGenerators -} + ...APIRoutesGenerators, +}; \ No newline at end of file diff --git a/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/AdministrationStatistics.js b/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/AdministrationStatistics.js new file mode 100644 index 0000000..0ae38a9 --- /dev/null +++ b/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/AdministrationStatistics.js @@ -0,0 +1,82 @@ +/* + * This file is part of Invenio. + * Copyright (C) 2022 CERN. + * + * Invenio is free software; you can redistribute it and/or modify it + * under the terms of the MIT License; see LICENSE file for more details. + */ +import _get from "lodash/get"; +import _isEmpty from "lodash/isEmpty"; +import React from "react"; +import { InvenioAdministrationActionsApi } from "../api/actions"; +import { Component } from "react"; +import { Item } from "semantic-ui-react"; +import PropTypes from "prop-types"; + +export class AdministrationStatistics extends Component { + constructor() { + super(); + this.state = { + intervalHits: [], + isLoading: false, + }; + } + + fetchValues = async () => { + const { apiEndpoint, query, sort, page, size } = this.props; + return await InvenioAdministrationActionsApi.searchResource( + apiEndpoint, + query, + sort, + page, + size + ); + }; + + async componentDidMount() { + this.setState({ isLoading: true }); + const response = await this.fetchValues(); + console.log(response.data); + this.setState({ + intervalHits: response.data.hits.hits.length, + isLoading: false, + }); + } + + render() { + const { records, timeInterval, label } = this.props; + const { intervalHits } = this.state; + return ( + + + {records} + {intervalHits} + + {label} in the past {timeInterval} + + + + ); + } +} + +AdministrationStatistics.propTypes = { + records: PropTypes.string, + timeInterval: PropTypes.string, + label: PropTypes.string, + apiEndpoint: PropTypes.string.isRequired, + query: PropTypes.string, + sort: PropTypes.string, + page: PropTypes.number, + size: PropTypes.number, +}; + +AdministrationStatistics.defaultProps = { + records: "", + timeInterval: "week", + label: "published", + query: "created:[2017 TO 2023]", + sort: "newest", + page: 1, + size: 5, +}; diff --git a/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/index.js b/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/index.js new file mode 100644 index 0000000..eab2f88 --- /dev/null +++ b/invenio_administration/assets/semantic-ui/js/invenio_administration/statistics/index.js @@ -0,0 +1,32 @@ +/* + * This file is part of Invenio. + * Copyright (C) 2022 CERN. + * + * Invenio is free software; you can redistribute it and/or modify it + * under the terms of the MIT License; see LICENSE file for more details. + */ + +import _get from "lodash/get"; +import React from "react"; +import ReactDOM from "react-dom"; +import { AdministrationStatistics } from "./AdministrationStatistics"; + +const domContainer = document.getElementById("statistics-widget-1"); + +const dataAttr = domContainer.dataset; +const records = dataAttr.records; +const timeInterval = dataAttr.timeInterval; +const label = dataAttr.label; + +console.log(domContainer); + +ReactDOM.render( + , + domContainer +); +export default AdministrationStatistics; diff --git a/invenio_administration/templates/semantic-ui/invenio_administration/dashboard/dashboard.html b/invenio_administration/templates/semantic-ui/invenio_administration/dashboard/dashboard.html new file mode 100644 index 0000000..209fb44 --- /dev/null +++ b/invenio_administration/templates/semantic-ui/invenio_administration/dashboard/dashboard.html @@ -0,0 +1,26 @@ +{# + Copyright (C) 2022 CERN. + + Invenio App RDM is free software; you can redistribute it and/or modify it + under the terms of the MIT License; see LICENSE file for more details. +#} + +{% extends admin_base_template %} + +{% block admin_page_content %} + +Put content here + +
+ +{% endblock admin_page_content %} + +{% block javascript %} + {{ super() }} + {{ webpack['invenio-administration-statistics.js'] }} +{% endblock %} diff --git a/invenio_administration/views/dashboard.py b/invenio_administration/views/dashboard.py index 004d4c0..6059e5f 100644 --- a/invenio_administration/views/dashboard.py +++ b/invenio_administration/views/dashboard.py @@ -14,6 +14,6 @@ class AdminDashboardView(AdminView): """Admin dashboard view.""" - template = "invenio_administration/index.html" + template = "invenio_administration/dashboard/dashboard.html" name = "dashboard" url = "/" diff --git a/invenio_administration/webpack.py b/invenio_administration/webpack.py index e77a087..f76f312 100644 --- a/invenio_administration/webpack.py +++ b/invenio_administration/webpack.py @@ -21,6 +21,8 @@ entry={ "invenio-administration-search": "./js/invenio_administration/search/search.js", + "invenio-administration-statistics": + "./js/invenio_administration/statistics/index.js", "base-admin-theme": "./js/invenio_administration/theme.js" },