Skip to content

Commit

Permalink
Merge pull request #46 from EmmanuelDemey/master
Browse files Browse the repository at this point in the history
zenika
  • Loading branch information
alicela authored May 21, 2019
2 parents 6b1e2a2 + dd7e7be commit 9b6cf50
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 23 deletions.
38 changes: 38 additions & 0 deletions src/js/actions/concepts/delete.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import remove from './delete';
import * as A from 'js/actions/constants';
import api from 'js/remote-api/concepts-api';

const dispatch = jest.fn();
jest.mock('js/remote-api/concepts-api');

describe('Concepts actions', () => {
it('should call dispatch DELETE_CONCEPT_SUCCESS action with the sorted array', async () => {
api.deleteConcept = function() {
return Promise.resolve([{ label: 'bbb' }, { label: 'aaa' }]);
};
await remove(1)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.DELETE_CONCEPT,
payload: { id: 1 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.DELETE_CONCEPT_SUCCESS,
payload: { id: 1 },
});
});

it('should call dispatch DELETE_CONCEPT_FAILURE action with an error object', async () => {
api.deleteConcept = function() {
return Promise.reject('error');
};
await remove(1)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.DELETE_CONCEPT,
payload: { id: 1 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.DELETE_CONCEPT_FAILURE,
payload: { err: 'error', id: 1 },
});
});
});
38 changes: 38 additions & 0 deletions src/js/actions/concepts/list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import get from './list';
import * as A from 'js/actions/constants';
import api from 'js/remote-api/concepts-api';

const dispatch = jest.fn();
jest.mock('js/remote-api/concepts-api');

describe('Concepts actions', () => {
it('should call dispatch LOAD_CONCEPT_LIST_SUCCESS action with the sorted array', async () => {
api.getConceptList = function() {
return Promise.resolve([{ label: 'bbb' }, { label: 'aaa' }]);
};
await get()(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CONCEPT_LIST,
payload: {},
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CONCEPT_LIST_SUCCESS,
payload: { results: [{ label: 'aaa' }, { label: 'bbb' }] },
});
});

it('should call dispatch LOAD_CONCEPT_LIST_FAILURE action with an error object', async () => {
api.getConceptList = function() {
return Promise.reject('error');
};
await get()(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CONCEPT_LIST,
payload: {},
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CONCEPT_LIST_FAILURE,
payload: { err: 'error' },
});
});
});
2 changes: 0 additions & 2 deletions src/js/components/concepts/visualization/home-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class ConceptVisualizationContainer extends Component {
deletionRequested: false,
showModalError: true,
});
//we need to load the concept again
this.props.loadConcept(id);
}
}
render() {
Expand Down
18 changes: 12 additions & 6 deletions src/js/components/operations/msd/documents/documents-bloc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import spinner from 'img/spinner.svg';
* @property {(string) => void } addHandler
* @property {import('js/types').SimsDocuments[]} documentStores
* @property {String} documentStoresStatus
* @property {String} objectType
*/

/**
Expand All @@ -38,6 +39,7 @@ export function DocumentsBloc({
addHandler,
documentStores = [],
documentStoresStatus,
objectType,
}) {
const [panelStatus, setPanelStatus] = useState(false);
const [filter, setFilter] = useState('');
Expand Down Expand Up @@ -105,8 +107,11 @@ export function DocumentsBloc({
</li>
);
}
const addTitle = objectType === 'documents' ? D.addDocument : D.addLink;
const title = objectType === 'documents' ? D.titleDocument : D.titleLink;
return (
<>
<h4>{title}</h4>
{documents && documents.length > 0 && (
<ul className="documentsbloc list-group">
{currentDocuments
Expand All @@ -120,7 +125,7 @@ export function DocumentsBloc({
<button
type="button"
className="btn documentsbloc__add documentsbloc__btn"
aria-label={D.addDocument}
aria-label={addTitle}
onClick={() => setPanelStatus(!panelStatus)}
>
<span
Expand All @@ -129,19 +134,19 @@ export function DocumentsBloc({
}`}
aria-hidden="true"
/>
{D.addDocument}{' '}
{addTitle}{' '}
{documentStoresStatus === LOADING ? (
<img src={spinner} width="30px" alt="loading" />
) : (
<span class="badge">{otherDocuments.length}</span>
<span className="badge">{otherDocuments.length}</span>
)}
</button>
</div>
{panelStatus && (
<div className="panel-body">
<div class="form-group">
<label className="sr-only" for="documentFilter">
Email address
<div className="form-group">
<label className="sr-only" htmlFor="documentFilter">
{D.search}
</label>
<input
className="form-control"
Expand Down Expand Up @@ -192,6 +197,7 @@ const mapDispatchToProps = {
};

const mapStateToProps = state => {
// TODO we will filter here the documents we should display based on the base URI
return {
documentStoresStatus: getOperationsDocumentsStatus(state),
documentStores: getOperationsDocuments(state),
Expand Down
43 changes: 29 additions & 14 deletions src/js/components/operations/msd/pages/sims-creation/sims-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,36 @@ class Field extends PureComponent {
this._handleChange({ value });
};

/**
* Handler when the user click on a button in order to delete a document
* @param {String} uri The uri of the document that we should remove
*/
handleDeleteDocument = uri => {
const documents = this.props.currentSection.documents || [];
_handleDeleteDocumentOrLinks = type => uri => {
const objects = this.props.currentSection[type] || [];

this._handleChange({
documents: documents.filter(doc => doc.uri !== uri),
[type]: objects.filter(doc => doc.uri !== uri),
});
};

/**
* Handler when the user add a new document to a rubric
* @param {SimsDocuments} document
*/
handleAddDocument = document => {
const documents = this.props.currentSection.documents || [];
_handleAddDocumentOrLinks = type => newObject => {
const objects = this.props.currentSection[type] || [];

this._handleChange({
documents: [...documents, document],
[type]: [...objects, newObject],
});
};

/**
* Handler when the user click on a button in order to delete a document
* @param {String} uri The uri of the document that we should remove
*/
handleDeleteDocument = uri =>
this._handleDeleteDocumentOrLinks('documents')(uri);

/**
* Handler when the user add a new document to a rubric
* @param {SimsDocuments} document
*/
handleAddDocument = document =>
this._handleAddDocumentOrLinks('documents')(document);

render() {
const {
msd,
Expand Down Expand Up @@ -143,6 +149,15 @@ class Field extends PureComponent {
editMode={true}
deleteHandler={this.handleDeleteDocument}
addHandler={this.handleAddDocument}
objectType="documents"
/>
<DocumentsBloc
documents={currentSection.documents}
localPrefix={secondLang ? 'Lg2' : 'Lg1'}
editMode={true}
deleteHandler={this.handleDeleteDocument}
addHandler={this.handleAddDocument}
objectType="links"
/>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export default function SimsVisualisation({
<DocumentsBloc
documents={currentSection.documents}
localPrefix={isSecondLang ? 'Lg2' : 'Lg1'}
objectType="documents"
/>
<DocumentsBloc
documents={currentSection.documents}
localPrefix={isSecondLang ? 'Lg2' : 'Lg1'}
objectType="links"
/>
</>
)}
Expand Down
6 changes: 6 additions & 0 deletions src/js/i18n/dictionary/operations/documents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export default {
titleDocument: { fr: 'Documents', en: 'Documents' },
titleLink: { fr: 'Liens', en: 'Links' },
addDocument: {
fr: 'Ajoutez un document',
en: 'Add a document',
},
addLink: {
fr: 'Ajoutez un lien',
en: 'Add a lien',
},
};
2 changes: 1 addition & 1 deletion src/js/reducers/concepts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const conceptList = handleReset(
A.LOAD_CONCEPT_LIST_SUCCESS,
A.LOAD_CONCEPT_LIST_FAILURE,
]),
[A.CREATE_CONCEPT, A.UPDATE_CONCEPT]
[A.CREATE_CONCEPT, A.UPDATE_CONCEPT, A.DELETE_CONCEPT_SUCCESS]
);

const conceptToValidateList = handleReset(
Expand Down

0 comments on commit 9b6cf50

Please sign in to comment.