Skip to content

Commit

Permalink
link doc with entity and fix admin view
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaetanbrl committed Sep 22, 2023
1 parent 8efd8b2 commit 61893f2
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 89 deletions.
115 changes: 71 additions & 44 deletions js/extension/components/MainPanelBody/MainPanelBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -35,7 +38,10 @@ const MainPanelBody = ({
setIdToDelete = () => {},
setIdToConsult = () => {},
idToConsult,
entity,
entity = "1",
setEntityOnly = () => {},
entityOnly,
isAdmin,
}) => {
const toolbarButtons = [
{
Expand All @@ -50,6 +56,9 @@ const MainPanelBody = ({
},
];

const displayCheckBox =
(isAdmin && entity && isEmpty(documents)) || (isAdmin && entity);

if (idToDelete) {
return (
<DeleteArea
Expand All @@ -75,56 +84,71 @@ const MainPanelBody = ({

return (
<>
{isEmpty(documents) && (
<Col xs={entity ? 10 : 12}>
<Toolbar
id="docs-manager-header-toolbar"
buttons={toolbarButtons}
/>
</Col>
{displayCheckBox && (
<Col xs={12} className="text-right">
<Checkbox
id="docsEntityCheck"
checked={
entityOnly === null && entity ? true : entityOnly
}
onChange={(x) => {
setEntityOnly(x.target.checked);
}}
>
Voir les documents de la sélection
</Checkbox>
</Col>
)}
{entity && isEmpty(documents) && (
<InformationArea
isVisible
title="Aucun document"
message="La liste des documents est vide."
message="La liste des documents est vide pour cette sélection."
glyph="eye-close"
/>
)}
{!entity && !isAdmin && isEmpty(documents) && (
<InformationArea
isVisible
title="Sélection vide !"
message="Commencez par cliquer sur une entité pour voir ses documents"
glyph="eye-close"
/>
)}
{!entity && isAdmin && isEmpty(documents) && (
<InformationArea
isVisible
title="Aucun document !"
message="Il n'y a aucun document à consulter."
glyph="eye-close"
/>
)}

{!isEmpty(documents) && (
<>
<Col xs={entity ? 10 : 12}>
<Toolbar
id="docs-manager-header-toolbar"
buttons={toolbarButtons}
/>
</Col>
<Col xs={12} className="text-right">
<Checkbox
id="docsEntityCheck"
onChange={(x) => {
refresh(
x.target.checked && entity
? { entity: entity }
: {}
);
}}
>
Documents de l'entité
</Checkbox>
</Col>
<Col xs={12} className="docs-div-table">
<Table responsive className="docs-table">
<tbody className="docs-tbody">
{documents.map((document) => {
let docProps = {
deleteDocument: (id) => {
setIdToDelete(id);
},
show,
download,
showAttributes: (id) =>
setIdToConsult(id),
...document,
};
return <DocumentRow {...docProps} />;
})}
</tbody>
</Table>
</Col>
</>
<Col xs={12} className="docs-div-table">
<Table responsive className="docs-table">
<tbody className="docs-tbody">
{documents.map((document) => {
let docProps = {
deleteDocument: (id) => {
setIdToDelete(id);
},
show,
download,
showAttributes: (id) => setIdToConsult(id),
...document,
};
return <DocumentRow {...docProps} />;
})}
</tbody>
</Table>
</Col>
)}
</>
);
Expand All @@ -135,6 +159,8 @@ export default connect(
idToDelete: getIdToDelete(state),
idToConsult: getIdToConsult(state),
entity: getEntity(state),
entityOnly: getDocEntityOnly(state),
isAdmin: isAdmin(state),
}),
{
refresh: getDocuments,
Expand All @@ -143,5 +169,6 @@ export default connect(
download: downloadDocument,
setIdToDelete: setIdToDelete,
setIdToConsult: setIdToConsult,
setEntityOnly: setEntityOnly,
}
)(MainPanelBody);
17 changes: 12 additions & 5 deletions js/extension/stateManagement/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down Expand Up @@ -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,
});

/**
Expand Down
12 changes: 7 additions & 5 deletions js/extension/stateManagement/epics/downloadDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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é!"
)
);
});
});
Expand Down
39 changes: 33 additions & 6 deletions js/extension/stateManagement/epics/getDocuments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
9 changes: 7 additions & 2 deletions js/extension/stateManagement/epics/setup.js
Original file line number Diff line number Diff line change
@@ -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()
);
});
45 changes: 27 additions & 18 deletions js/extension/stateManagement/epics/uploadDocument.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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");
Expand All @@ -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 !"
)
);
}
});
});
Expand Down
Loading

0 comments on commit 61893f2

Please sign in to comment.