From 61893f2f173ab355dcbbcaf9a6d61871ebb6430f Mon Sep 17 00:00:00 2001 From: gaetanbrl Date: Fri, 22 Sep 2023 11:18:49 +0200 Subject: [PATCH] link doc with entity and fix admin view --- .../MainPanelBody/MainPanelBody.jsx | 115 +++++++++++------- .../stateManagement/actions/actions.js | 17 ++- .../stateManagement/epics/downloadDocument.js | 12 +- .../stateManagement/epics/getDocuments.js | 39 +++++- js/extension/stateManagement/epics/setup.js | 9 +- .../stateManagement/epics/uploadDocument.js | 45 ++++--- .../stateManagement/reducers/reducers.js | 10 +- .../stateManagement/selector/selector.js | 27 +++- 8 files changed, 185 insertions(+), 89 deletions(-) diff --git a/js/extension/components/MainPanelBody/MainPanelBody.jsx b/js/extension/components/MainPanelBody/MainPanelBody.jsx index 5302fac..088f1e2 100644 --- a/js/extension/components/MainPanelBody/MainPanelBody.jsx +++ b/js/extension/components/MainPanelBody/MainPanelBody.jsx @@ -7,15 +7,18 @@ import { deleteDocument, downloadDocument, getDocuments, + setEntityOnly, setIdToConsult, setIdToDelete, showDocument, } from "@js/extension/stateManagement/actions/actions"; import { getApiDocuments, + getDocEntityOnly, getEntity, getIdToConsult, getIdToDelete, + isAdmin, } from "@js/extension/stateManagement/selector/selector"; import { Col, Table, Checkbox } from "react-bootstrap"; import Toolbar from "@mapstore/components/misc/toolbar/Toolbar"; @@ -35,7 +38,10 @@ const MainPanelBody = ({ setIdToDelete = () => {}, setIdToConsult = () => {}, idToConsult, - entity, + entity = "1", + setEntityOnly = () => {}, + entityOnly, + isAdmin, }) => { const toolbarButtons = [ { @@ -50,6 +56,9 @@ const MainPanelBody = ({ }, ]; + const displayCheckBox = + (isAdmin && entity && isEmpty(documents)) || (isAdmin && entity); + if (idToDelete) { return ( - {isEmpty(documents) && ( + + + + {displayCheckBox && ( + + { + setEntityOnly(x.target.checked); + }} + > + Voir les documents de la sélection + + + )} + {entity && isEmpty(documents) && ( + )} + {!entity && !isAdmin && isEmpty(documents) && ( + )} + {!entity && isAdmin && isEmpty(documents) && ( + + )} + {!isEmpty(documents) && ( - <> - - - - - { - refresh( - x.target.checked && entity - ? { entity: entity } - : {} - ); - }} - > - Documents de l'entité - - - - - - {documents.map((document) => { - let docProps = { - deleteDocument: (id) => { - setIdToDelete(id); - }, - show, - download, - showAttributes: (id) => - setIdToConsult(id), - ...document, - }; - return ; - })} - -
- - + + + + {documents.map((document) => { + let docProps = { + deleteDocument: (id) => { + setIdToDelete(id); + }, + show, + download, + showAttributes: (id) => setIdToConsult(id), + ...document, + }; + return ; + })} + +
+ )} ); @@ -135,6 +159,8 @@ export default connect( idToDelete: getIdToDelete(state), idToConsult: getIdToConsult(state), entity: getEntity(state), + entityOnly: getDocEntityOnly(state), + isAdmin: isAdmin(state), }), { refresh: getDocuments, @@ -143,5 +169,6 @@ export default connect( download: downloadDocument, setIdToDelete: setIdToDelete, setIdToConsult: setIdToConsult, + setEntityOnly: setEntityOnly, } )(MainPanelBody); diff --git a/js/extension/stateManagement/actions/actions.js b/js/extension/stateManagement/actions/actions.js index 934d45d..dbf71dd 100644 --- a/js/extension/stateManagement/actions/actions.js +++ b/js/extension/stateManagement/actions/actions.js @@ -18,25 +18,26 @@ export const SET_ID_TO_DELETE = "SET_ID_TO_DELETE"; export const SET_ID_TO_CONSULT = "SET_ID_TO_CONSULT"; export const VALID_VALUES = "VALID_VALUES"; export const CONTROL_VALUES = "CONTROL_VALUES"; +export const SET_ENTITY_ONLY = "SET_ENTITY_ONLY"; export const controlValues = (values) => ({ type: CONTROL_VALUES, - values + values, }); export const validValues = (values) => ({ type: VALID_VALUES, - values + values, }); export const setIdToConsult = (id) => ({ type: SET_ID_TO_CONSULT, - id + id, }); export const setIdToDelete = (id) => ({ type: SET_ID_TO_DELETE, - id + id, }); export const setUploadVisibility = (visible) => ({ @@ -137,14 +138,20 @@ export const getDocument = (id, params = {}) => ({ params, }); +export const setEntityOnly = (checked) => ({ + type: SET_ENTITY_ONLY, + checked, +}); + /** * Get list of documents * @param {string} id * @returns {{id:string}} */ -export const getDocuments = (params = {}) => ({ +export const getDocuments = (params = {}, entity = false) => ({ type: GET_DOCUMENTS, params, + entity, }); /** diff --git a/js/extension/stateManagement/epics/downloadDocument.js b/js/extension/stateManagement/epics/downloadDocument.js index 570cc25..86bf851 100644 --- a/js/extension/stateManagement/epics/downloadDocument.js +++ b/js/extension/stateManagement/epics/downloadDocument.js @@ -59,11 +59,13 @@ export function showOnClick(action$, store) { return Rx.Observable.of([]); }) .switchMap((data) => { - showDocInTab(data); - return displayMsg( - "success", - "Téléchargement", - "Document réceptionné!" + return Rx.Observable.of( + showDocInTab(data), + displayMsg( + "success", + "Téléchargement", + "Document réceptionné!" + ) ); }); }); diff --git a/js/extension/stateManagement/epics/getDocuments.js b/js/extension/stateManagement/epics/getDocuments.js index 416ffcd..eef86da 100644 --- a/js/extension/stateManagement/epics/getDocuments.js +++ b/js/extension/stateManagement/epics/getDocuments.js @@ -2,29 +2,56 @@ import Rx from "rxjs"; import { GET_DOCUMENTS, setDocuments, + getDocuments as callDocuments, docsLoading as loading, + SET_ENTITY_ONLY, } from "../actions/actions"; -import { getEntity, getPluginCfg, isActive } from "../selector/selector"; +import { + getDocEntityOnly, + getEntity, + getPluginCfg, + isActive, + isAdmin, +} from "../selector/selector"; import { wrapStartStop } from "@mapstore/observables/epics"; import { getDocuments } from "@js/extension/requests/documentsApi"; +export function setShowAllDocs(action$, store) { + return action$ + .ofType(SET_ENTITY_ONLY) + .filter(() => isActive(store.getState())) + .switchMap((action) => { + return Rx.Observable.of(callDocuments()); + }); +} + export function getDocumentsById(action$, store) { return action$ .ofType(GET_DOCUMENTS) .filter(() => isActive(store.getState())) .switchMap((action) => { + const entity = getEntity(store.getState()); + const isAdminUser = isAdmin(store.getState()); const apiUrl = getPluginCfg(store.getState()).api; const idPlugin = getPluginCfg(store.getState()).id; + let params = action?.params; let observable$ = Rx.Observable.empty(); if (!apiUrl || !idPlugin) { return observable$; } - const entity = getEntity(store.getState()); - if (entity) { - params = {...action?.params, entity: entity} - } else { - params = action?.params; + if (!isAdminUser && !entity) { + return Rx.Observable.empty(); + } + + if (!isAdminUser && entity) { + params = { ...params, entity: entity }; + } else if ( + isAdminUser && + getDocEntityOnly(store.getState()) && + entity + ) { + params = { ...params, entity: entity }; } if (apiUrl) { observable$ = Rx.Observable.defer(() => diff --git a/js/extension/stateManagement/epics/setup.js b/js/extension/stateManagement/epics/setup.js index cfe8c76..aa92861 100644 --- a/js/extension/stateManagement/epics/setup.js +++ b/js/extension/stateManagement/epics/setup.js @@ -1,7 +1,12 @@ import Rx from "rxjs"; -import { SETUP, getDocuments } from "../actions/actions"; +import { SETUP, getDocuments, setEntityOnly } from "../actions/actions"; +import { getEntity } from "../selector/selector"; export const onSetup = (action$, store) => action$.ofType(SETUP).switchMap(() => { - return Rx.Observable.of(getDocuments()); + const entity = getEntity(store.getState()); + return Rx.Observable.of( + entity ? setEntityOnly(true) : setEntityOnly(false), + getDocuments() + ); }); diff --git a/js/extension/stateManagement/epics/uploadDocument.js b/js/extension/stateManagement/epics/uploadDocument.js index 10c0bd6..0bf095e 100644 --- a/js/extension/stateManagement/epics/uploadDocument.js +++ b/js/extension/stateManagement/epics/uploadDocument.js @@ -1,5 +1,10 @@ import Rx from "rxjs"; -import { UPLOAD_DOCUMENT, displayMsg, getDocuments, setUploadVisibility } from "../actions/actions"; +import { + UPLOAD_DOCUMENT, + displayMsg, + getDocuments, + setUploadVisibility, +} from "../actions/actions"; import { getPluginCfg, isActive, getEntity } from "../selector/selector"; import { uniqueId, get } from "lodash"; @@ -29,27 +34,23 @@ export function uploadEvent(action$, store) { console.log("Error - Get list of documents"); console.log(e); // fail message - return true + return true; }) - .switchMap(labelExists => { + .switchMap((labelExists) => { if (labelExists) { return Rx.Observable.of( - displayMsg("error", "Document", "Ce libellé est déjà utilisé !") + displayMsg( + "error", + "Document", + "Ce libellé est déjà utilisé !" + ) ); } + let params = {}; const entity = getEntity(store.getState()); - if (entity) { - params = {...action?.params, entity: entity} - } else { - params = action?.params; - } + params = { ...action?.params, entity: entity || "" }; return Rx.Observable.defer(() => - uploadDocument( - apiUrl, - idPlugin, - action.file, - params - ) + uploadDocument(apiUrl, idPlugin, action.file, params) ) .catch((e) => { console.log("Error - Get list of documents"); @@ -60,14 +61,22 @@ export function uploadEvent(action$, store) { .switchMap((data) => { if (data?.status && data.status == "200") { return Rx.Observable.of( - displayMsg("success", "Document", "Sauvegarde réussie !"), + displayMsg( + "success", + "Document", + "Sauvegarde réussie !" + ), getDocuments(), setUploadVisibility(false) ); } else { return Rx.Observable.of( - displayMsg("error", "Document", "Echec de la sauvegarde !"), - ) + displayMsg( + "error", + "Document", + "Echec de la sauvegarde !" + ) + ); } }); }); diff --git a/js/extension/stateManagement/reducers/reducers.js b/js/extension/stateManagement/reducers/reducers.js index 1b9309a..fd51ba2 100644 --- a/js/extension/stateManagement/reducers/reducers.js +++ b/js/extension/stateManagement/reducers/reducers.js @@ -9,6 +9,7 @@ import { SET_ID_TO_DELETE, SET_UPLOAD_VISIBILITY, VALID_VALUES, + SET_ENTITY_ONLY, } from "../actions/actions"; const initialState = { @@ -20,20 +21,23 @@ const initialState = { uploadVisibility: false, idToDelete: null, idToConsult: null, - uploadIsValid: false + uploadIsValid: false, + entityOnly: null, }; export default function reducers(state = initialState, action) { switch (action.type) { + case SET_ENTITY_ONLY: + return set("entityOnly", action.checked, state); case SET_ID_TO_CONSULT: return compose( set("idToConsult", action.id), - set("idToDelete", null), + set("idToDelete", null) )(state); case SET_ID_TO_DELETE: return compose( set("idToConsult", null), - set("idToDelete", action.id), + set("idToDelete", action.id) )(state); case VALID_VALUES: return set("uploadIsValid", action.values, state); diff --git a/js/extension/stateManagement/selector/selector.js b/js/extension/stateManagement/selector/selector.js index 633802d..f994145 100644 --- a/js/extension/stateManagement/selector/selector.js +++ b/js/extension/stateManagement/selector/selector.js @@ -18,28 +18,37 @@ export const isActive = (state) => { */ export const getPluginCfg = (state) => state?.docsManager?.pluginCfg; export const getApiDocuments = (state) => state?.docsManager?.documents; -export const getIdToDelete = state => state?.docsManager?.idToDelete; -export const getIdToConsult = state => state?.docsManager?.idToConsult; +export const getIdToDelete = (state) => state?.docsManager?.idToDelete; +export const getIdToConsult = (state) => state?.docsManager?.idToConsult; + +export const getDocEntityOnly = (state) => state?.docsManager?.entityOnly; export const getEntity = (state) => { let cfg = state?.docsManager?.pluginCfg; if (cfg?.entity?.get) { return get(state, cfg.entity.get); } - if (cfg?.entity && cfg?.entity?.plugin && cfg?.entity?.propCfg && cfg?.entity?.attribute) { + if ( + cfg?.entity && + cfg?.entity?.plugin && + cfg?.entity?.propCfg && + cfg?.entity?.attribute + ) { const plugin = cfg?.entity?.plugin; const propCfg = cfg?.entity?.propCfg; let pluginCfg = state[plugin]; - return pluginCfg ? pluginCfg[propCfg][cfg?.entity?.attribute] : null; + let exists = get(pluginCfg, `${propCfg}.${cfg?.entity?.attribute}`); + return exists ? exists : null; } -} +}; export const getUploadValidation = (state) => state?.docsManager?.uploadIsValid; export const getApi = (state) => state?.docsManager?.api; export const getId = (state) => state?.docsManager?.id; export const getStatus = (state) => state?.docsManager?.pluginCfg?.statusValues; export const getFields = (state) => state?.docsManager?.pluginCfg?.fields; -export const getRequired = (state) => state?.docsManager?.pluginCfg?.requiredFields; +export const getRequired = (state) => + state?.docsManager?.pluginCfg?.requiredFields; export const getUploadVisibility = (state) => state?.docsManager.uploadVisibility; @@ -58,3 +67,9 @@ export const getAuthLevel = (state) => { ); return fullyAuthorized; }; + +export const isAdmin = (state) => { + const groups = userGroupSecuritySelector(state) ?? []; + const groupNames = groups.map(({ groupName }) => groupName); + return groupNames.includes("MAPSTORE_ADMIN"); +};