Skip to content

Commit

Permalink
OCLOMRS-173: Changes to create concept form options for Q-and-A conce…
Browse files Browse the repository at this point in the history
…pt (#133)
  • Loading branch information
dotNesh authored and dkayiwa committed Nov 7, 2018
1 parent c50952e commit 49681a1
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 17 deletions.
92 changes: 92 additions & 0 deletions src/components/dictionaryConcepts/components/AnswersRow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { Component } from 'react';
import autoBind from 'react-autobind';
import PropTypes from 'prop-types';

class AnswersRow extends Component {
static propTypes = {
newRow: PropTypes.object.isRequired,
addDataFromAnswer: PropTypes.func.isRequired,
removeAnswer: PropTypes.func.isRequired,
removeDataFromRow: PropTypes.func.isRequired,
existingConcept: PropTypes.object.isRequired,
}
constructor(props) {
super(props);
this.state = {
id: this.props.newRow.uuid,
answer: [],
};
autoBind(this);
}

componentDidMount() {
if (this.props.existingConcept) {
this.updateState();
}
}

componentDidUpdate(prevProps, prevState) {
if (prevState !== this.state) {
this.sendToTopComponent();
}
}

updateState() {
const { newRow } = this.props;
this.setState({
...this.state,
id: newRow.uuid,
answer: newRow.answer || '',
});
}

handleChange(event) {
const {
target: { value, name },
} = event;
this.setState(() => ({ [name]: value }));
this.sendToTopComponent();
}

sendToTopComponent() {
this.props.addDataFromAnswer(this.state);
}

handleRemove(event, id) {
event.preventDefault();
const { removeAnswer, removeDataFromRow } = this.props;
removeAnswer(id);
removeDataFromRow(id, 'answers');
}

render() {
return (
<tr>
<td>
<input
type="text"
className="form-control answer"
placeholder="eg. /orgs/Regenstrief/sources/loinc2/concepts/32700-7/"
name="answer"
value={this.state.answer}
onChange={this.handleChange}
id="concept-answer"
required
/>
</td>
<td>
<a
href="#!"
className="concept-form-table-link answer"
id="remove-answer"
onClick={event => this.handleRemove(event, this.props.newRow.uuid)}
>
remove
</a>
</td>
</tr>
);
}
}

export default AnswersRow;
28 changes: 28 additions & 0 deletions src/components/dictionaryConcepts/components/AnswersTable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import PropTypes from 'prop-types';
import AnswersRow from './AnswersRow';

const AnswersTable = props => (
<table className="table table-striped table-bordered concept-form-table">
<thead className="header text-white">
<tr>
<th scope="col">To Concept URL</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{props.answer.map(newRow => (
<AnswersRow
newRow={{ uuid: newRow }}
key={newRow}
{...props}
/>))}
</tbody>
</table>
);

AnswersTable.propTypes = {
answer: PropTypes.array.isRequired,
};

export default AnswersTable;
12 changes: 2 additions & 10 deletions src/components/dictionaryConcepts/components/ConceptNameRows.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import React, { Component } from 'react';
import autoBind from 'react-autobind';
import Select from 'react-select';
import 'react-select/dist/react-select';
import PropTypes from 'prop-types';
import locale from '../../dashboard/components/dictionary/common/Languages';

class ConceptNameRows extends Component {
static propTypes = {
newRow: PropTypes.shape({
id: PropTypes.string,
uuid: PropTypes.string.isRequired,
name: PropTypes.string,
locale: PropTypes.string,
locale_full: PropTypes.string,
locale_preferred: PropTypes.bool,
name_type: PropTypes.string,
}),
newRow: PropTypes.object.isRequired,
addDataFromRow: PropTypes.func.isRequired,
removeRow: PropTypes.func.isRequired,
removeDataFromRow: PropTypes.func.isRequired,
Expand All @@ -24,6 +15,7 @@ class ConceptNameRows extends Component {
};

static defaultProps = {
// eslint-disable-next-line react/default-props-match-prop-types
newRow: {
id: '',
name: '',
Expand Down
16 changes: 16 additions & 0 deletions src/components/dictionaryConcepts/components/CreateConceptForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import CreateConceptTable from './CreateConceptTable';
import DescriptionTable from './DescriptionTable';
import AnswersTable from './AnswersTable';
import { classes } from './helperFunction';

const CreateConceptForm = props => (
Expand Down Expand Up @@ -132,6 +133,19 @@ const CreateConceptForm = props => (
<option>Coded</option>
</select>
</div>

{props.concept.toString().trim() === 'question' ?
<div className="form-group answer">
<div className="row col-12 custom-concept-list">
<h6 className="text-left section-header">Answers</h6>
<AnswersTable {...props} />
<a href="#!" className="text-left add-more-answers" id="add-more-answers" onClick={props.addAnswer}>
Add a new answer mapping
</a>
</div>
</div>
: null}

<div className="form-group">
<div className="row col-12 custom-concept-list">
<h6 className="text-left section-header">Names</h6>
Expand Down Expand Up @@ -190,6 +204,8 @@ CreateConceptForm.propTypes = {
editable: PropTypes.bool.isRequired,
isEditConcept: PropTypes.bool,
existingConcept: PropTypes.object,
addAnswer: PropTypes.func.isRequired,
answer: PropTypes.array.isRequired,
};

CreateConceptForm.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from 'react';
import Select from 'react-select';
import 'react-select/dist/react-select';
import autoBind from 'react-autobind';
import PropTypes from 'prop-types';
import uuid from 'uuid/v4';
Expand Down
37 changes: 37 additions & 0 deletions src/components/dictionaryConcepts/containers/CreateConcept.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
removeDescription,
clearSelections,
createNewConcept,
addNewAnswer,
removeAnswer,
} from '../../../redux/actions/concepts/dictionaryConcepts';

export class CreateConcept extends Component {
Expand Down Expand Up @@ -43,6 +45,9 @@ export class CreateConcept extends Component {
descriptions: PropTypes.array,
}).isRequired,
addedConcept: PropTypes.array.isRequired,
addNewAnswer: PropTypes.func.isRequired,
removeAnswer: PropTypes.func.isRequired,
answer: PropTypes.array.isRequired,
};

constructor(props) {
Expand All @@ -54,6 +59,7 @@ export class CreateConcept extends Component {
datatype: 'None',
names: [],
descriptions: [],
answers: [],
};

autoBind(this);
Expand All @@ -68,6 +74,7 @@ export class CreateConcept extends Component {
const concept = conceptType || '';
this.props.createNewName();
this.props.addNewDescription();
this.props.addNewAnswer();
// eslint-disable-next-line
this.setState({ concept_class: concept });
}
Expand Down Expand Up @@ -117,6 +124,13 @@ export class CreateConcept extends Component {
this.props.removeDescription(id);
}

addNewAnswer() {
this.props.addNewAnswer();
}
removeAnswer(id) {
this.props.removeAnswer(id);
}

handleUUID(event) {
event.preventDefault();
this.setState(prevState => ({
Expand Down Expand Up @@ -193,6 +207,22 @@ export class CreateConcept extends Component {
}
}

addDataFromAnswer(data) {
const currentAnswer = this.state.answers.filter(answer => answer.id === data.id);
if (currentAnswer.length) {
const newList = this.state.answers.map(answer => (
answer.id === data.id ? data : answer
));
this.setState(() => ({
answers: newList,
}));
} else {
this.setState(prevState => ({
answers: [...prevState.answers, data],
}));
}
}

render() {
const {
match: {
Expand Down Expand Up @@ -241,6 +271,10 @@ Concept
addDataFromDescription={this.addDataFromDescription}
removeDataFromRow={this.removeDataFromRow}
pathName={this.props.match.params}
addAnswer={this.addNewAnswer}
answer={this.props.answer}
addDataFromAnswer={this.addDataFromAnswer}
removeAnswer={this.removeAnswer}
/>
</div>
</div>
Expand All @@ -255,6 +289,7 @@ export const mapStateToProps = state => ({
description: state.concepts.description,
newConcept: state.concepts.newConcept,
addedConcept: state.concepts.addConceptToDictionary,
answer: state.concepts.answer,
});
export default connect(
mapStateToProps,
Expand All @@ -265,5 +300,7 @@ export default connect(
removeDescription,
clearSelections,
createNewConcept,
addNewAnswer,
removeAnswer,
},
)(CreateConcept);
36 changes: 36 additions & 0 deletions src/components/dictionaryConcepts/containers/EditConcept.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
clearPreviousConcept,
createNewNameForEditConcept,
removeNameForEditConcept,
addNewAnswer,
removeAnswer,
} from '../../../redux/actions/concepts/dictionaryConcepts';

export class EditConcept extends Component {
Expand Down Expand Up @@ -45,6 +47,9 @@ export class EditConcept extends Component {
removeNameForEditConcept: PropTypes.func.isRequired,
existingConcept: PropTypes.object.isRequired,
updateConcept: PropTypes.func.isRequired,
answer: PropTypes.array.isRequired,
addNewAnswer: PropTypes.func.isRequired,
removeAnswer: PropTypes.func.isRequired,
};

constructor(props) {
Expand All @@ -56,6 +61,7 @@ export class EditConcept extends Component {
datatype: 'None',
names: [],
descriptions: [],
answers: [],
isEditConcept: true,
};
this.conceptUrl = '';
Expand Down Expand Up @@ -117,6 +123,13 @@ export class EditConcept extends Component {
this.props.removeDescriptionForEditConcept(descriptionRow.uuid);
}

addNewAnswer() {
this.props.addNewAnswer();
}
removeAnswer(id) {
this.props.removeAnswer(id);
}

handleUUID(event) {
event.preventDefault();
this.setState(prevState => ({
Expand Down Expand Up @@ -185,6 +198,22 @@ export class EditConcept extends Component {
}
}

addDataFromAnswer(data) {
const currentAnswer = this.state.answers.filter(answer => answer.id === data.id);
if (currentAnswer.length) {
const newList = this.state.answers.map(answer => (
answer.id === data.id ? data : answer
));
this.setState(() => ({
answers: newList,
}));
} else {
this.setState(prevState => ({
answers: [...prevState.answers, data],
}));
}
}

render() {
const {
match: {
Expand Down Expand Up @@ -238,6 +267,10 @@ Concept
pathName={this.props.match.params}
existingConcept={existingConcept}
isEditConcept={this.state.isEditConcept}
answer={this.props.answer}
addDataFromAnswer={this.addDataFromAnswer}
addAnswer={this.addNewAnswer}
removeAnswer={this.removeAnswer}
/>
)
}
Expand All @@ -255,6 +288,7 @@ export const mapStateToProps = state => ({
newConcept: state.concepts.newConcept,
addedConcept: state.concepts.addConceptToDictionary,
existingConcept: state.concepts.existingConcept,
answer: state.concepts.answer,
});
export default connect(
mapStateToProps,
Expand All @@ -271,5 +305,7 @@ export default connect(
clearPreviousConcept,
createNewNameForEditConcept,
removeNameForEditConcept,
addNewAnswer,
removeAnswer,
},
)(EditConcept);
Loading

0 comments on commit 49681a1

Please sign in to comment.