Skip to content

Commit

Permalink
Select input components and pass connection details as props based on…
Browse files Browse the repository at this point in the history
… template match to config details; Renamed Input components that do lookups
  • Loading branch information
jgreben committed Jan 11, 2019
1 parent a80c065 commit 84cc007
Show file tree
Hide file tree
Showing 9 changed files with 977 additions and 50 deletions.
@@ -1,7 +1,7 @@
// Copyright 2018 Stanford University see Apache2.txt for license
import React from 'react'
import { shallow } from 'enzyme'
import InputList from '../../../src/components/editor/InputList'
import InputList from '../../../src/components/editor/InputListLOC'

const plProps = {
"propertyTemplate":
Expand Down
4 changes: 2 additions & 2 deletions __tests__/components/editor/InputLiteral.test.js
Expand Up @@ -16,7 +16,7 @@ const plProps = {
}

describe('<InputLiteral />', () => {
const wrapper = shallow(<InputLiteral {...plProps} />)
const wrapper = shallow(<InputLiteral {...plProps} id={10}/>)

it('contains a label with "Instance of"', () => {
expect(wrapper.find('label').text()).toBe('Instance of')
Expand All @@ -41,7 +41,7 @@ describe('When the user enters input into field', ()=>{
// our mock formData function to replace the one provided by mapDispatchToProps
const mockFormDataFn = jest.fn()
const removeMockDataFn = jest.fn()
mock_wrapper = shallow(<InputLiteral {...plProps} id={"11"}
mock_wrapper = shallow(<InputLiteral {...plProps} id={11}
handleMyItemsChange={mockFormDataFn}
handleRemoveItem={removeMockDataFn}/>)

Expand Down
@@ -1,7 +1,7 @@
// Copyright 2018 Stanford University see Apache2.txt for license
import React from 'react'
import { shallow } from 'enzyme'
import InputLookup from '../../../src/components/editor/InputLookup'
import InputLookup from '../../../src/components/editor/InputLookupQA'

const plProps = {
"propertyTemplate":
Expand Down
17 changes: 13 additions & 4 deletions __tests__/components/editor/ResourceTemplateForm.test.js
Expand Up @@ -18,14 +18,19 @@ const rtProps = {
"type": "lookup",
"editable": "do not override me!",
"repeatable": "do not override me!",
"mandatory": "do not override me!"
"mandatory": "do not override me!",
"valueConstraint": {
"useValuesFrom": [
"urn:ld4p:qa:names:person"
]
}
},
{
"propertyLabel": "What's the frequency Kenneth?",
"type": "resource",
"valueConstraint": {
"useValuesFrom": [
"./static/spoofedFilesFromServer/fromQA/frequencies.json"
"http://id.loc.gov/vocabulary/frequencies"
]
}
},
Expand All @@ -46,6 +51,10 @@ const rtProps = {
"resourceTemplate:bf2:Note"
]
}
},
{
"propertyLabel": "Non-modal resource",
"type": "resource"
}
]
}
Expand All @@ -70,13 +79,13 @@ describe('<ResourceTemplateForm />', () => {

it('renders the InputLookup nested component (b/c we have a property of type "lookup")', () => {
expect(wrapper
.find('div.ResourceTemplateForm Connect(InputLookup)').length)
.find('div.ResourceTemplateForm Connect(InputLookupQA)').length)
.toEqual(1)
})

it('renders InputResource nested component (b/c we have a property of type "resource" with a "useValuesFrom" value)', () => {
expect(wrapper
.find('div.ResourceTemplateForm Connect(InputList)').length)
.find('div.ResourceTemplateForm Connect(InputListLOC)').length)
.toEqual(1)
})

Expand Down
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux'
import { changeSelections } from '../../actions/index'

class
InputList extends Component {
InputListLOC extends Component {
constructor(props) {
super(props)
this.state = {
Expand Down Expand Up @@ -39,11 +39,11 @@ class
}

render() {
let list_url, isMandatory, isRepeatable
let lookupUri, isMandatory, isRepeatable
try {
list_url = this.props.propertyTemplate.valueConstraint.useValuesFrom[0]
isMandatory = JSON.parse(this.props.propertyTemplate.mandatory)
isRepeatable = JSON.parse(this.props.propertyTemplate.repeatable)
lookupUri = this.props.lookupConfig.value.uri
} catch (error) {
console.log(`Some properties were not defined in the json file: ${error}`)
}
Expand All @@ -67,7 +67,7 @@ class
<Typeahead
onFocus={() => {
this.setState({isLoading: true})
fetch(`${list_url}.json`)
fetch(`${lookupUri}.json`)
.then(resp => resp.json())
.then(json => {
for(var i in json){
Expand Down Expand Up @@ -97,7 +97,7 @@ class
}
}

InputList.propTypes = {
InputListLOC.propTypes = {
propertyTemplate: PropTypes.shape({
propertyLabel: PropTypes.string,
propertyURI: PropTypes.string,
Expand All @@ -124,4 +124,4 @@ const mapDispatchtoProps = dispatch => ({
}
})

export default connect(mapStatetoProps, mapDispatchtoProps)(InputList)
export default connect(mapStatetoProps, mapDispatchtoProps)(InputListLOC)
Expand Up @@ -8,7 +8,7 @@ import { changeSelections } from '../../actions/index'

const AsyncTypeahead = asyncContainer(Typeahead)

class InputLookup extends Component {
class InputLookupQA extends Component {
constructor(props) {
super(props)
this.state = {
Expand All @@ -17,14 +17,13 @@ class InputLookup extends Component {
}

render() {
let isMandatory, isRepeatable, authority, vocab, subauthority, language
let isMandatory, isRepeatable, authority, subauthority, language
try {
isMandatory = JSON.parse(this.props.propertyTemplate.mandatory)
isRepeatable = JSON.parse(this.props.propertyTemplate.repeatable)
authority = this.props.propertyTemplate.valueConstraint.useValuesFrom[0]
vocab = authority.split(':')[0]
subauthority = authority.split(':')[1]
language = authority.split(':')[2]
authority = this.props.lookupConfig.value.authority
subauthority = this.props.lookupConfig.value.authority
language = this.props.lookupConfig.value.language
} catch (error) {
console.log(`Problem with properties fetched from resource template: ${error}`)
}
Expand Down Expand Up @@ -52,7 +51,7 @@ class InputLookup extends Component {
.SearchQuery
.GET_searchAuthority({
q: query,
vocab: vocab,
vocab: authority,
subauthority: subauthority,
maxRecords: 8,
lang: language
Expand All @@ -79,7 +78,7 @@ class InputLookup extends Component {
}
}

InputLookup.propTypes = {
InputLookupQA.propTypes = {
propertyTemplate: PropTypes.shape({
propertyLabel: PropTypes.string,
mandatory: PropTypes.oneOfType([ PropTypes.string, PropTypes.bool]),
Expand All @@ -105,4 +104,4 @@ const mapDispatchtoProps = dispatch => ({
}
})

export default connect(mapStatetoProps, mapDispatchtoProps)(InputLookup)
export default connect(mapStatetoProps, mapDispatchtoProps)(InputLookupQA)
81 changes: 55 additions & 26 deletions src/components/editor/ResourceTemplateForm.jsx
Expand Up @@ -5,9 +5,10 @@ import PropTypes from 'prop-types'
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup'
import ButtonToolbar from 'react-bootstrap/lib/ButtonToolbar'
import InputLiteral from './InputLiteral'
import InputList from './InputList'
import InputLookup from './InputLookup'
import InputListLOC from './InputListLOC'
import InputLookupQA from './InputLookupQA'
import ModalToggle from './ModalToggle'
import lookupConfig from '../../../static/spoofedFilesFromServer/fromSinopiaServer/lookupConfig.json'
const { getResourceTemplate } = require('../../sinopiaServerSpoof.js')

class ResourceTemplateForm extends Component {
Expand Down Expand Up @@ -62,35 +63,62 @@ class ResourceTemplateForm extends Component {
<p>BEGIN ResourceTemplateForm</p>
<div>
{this.props.propertyTemplates.map( (pt, index) => {
if(pt.type == 'literal'){
return(
<InputLiteral propertyTemplate = {pt} key = {index} id = {index} />

let isLookupWithConfig = Boolean(
lookupConfig !== undefined &&
pt.valueConstraint !== undefined &&
pt.valueConstraint.useValuesFrom
)

let lookupConfigItem, templateUri, listComponent

if (isLookupWithConfig) {
templateUri = pt.valueConstraint.useValuesFrom[0]
for(var i in lookupConfig){
lookupConfigItem = Object.getOwnPropertyDescriptor(lookupConfig, i)
if(lookupConfigItem.value.uri === templateUri){
listComponent = lookupConfigItem.value.component
break
}
}
}

let isResourceWithValueTemplateRefs = Boolean(
pt.type == 'resource' &&
pt.valueConstraint != null &&
pt.valueConstraint.valueTemplateRefs != null
&& pt.valueConstraint.valueTemplateRefs.length > 0
)

if (listComponent === 'list'){
return (
<InputListLOC propertyTemplate = {pt} lookupConfig = {lookupConfigItem} key = {index} />
)
}
else if (pt.type == 'resource') {
if (pt.valueConstraint != null && pt.valueConstraint.valueTemplateRefs != null && pt.valueConstraint.valueTemplateRefs.length > 0) {
// TODO: some valueTemplateRefs may be lookups??
return (
<ButtonToolbar key={index}>
<div>
<b>{pt.propertyLabel}</b>
</div>
{this.resourceTemplateButtons(pt.valueConstraint.valueTemplateRefs)}
</ButtonToolbar>
)
} else if (pt.valueConstraint.useValuesFrom[0] != null && pt.valueConstraint.useValuesFrom.length > 0) {
return (
<InputList propertyTemplate = {pt} key = {index} />
)
} else {
return (<p key={index}><b>{pt.propertyLabel}</b>: <i>NON-modal resource</i></p>)
}
else if (listComponent === 'lookup'){
return(
<InputLookupQA propertyTemplate = {pt} lookupConfig = {lookupConfigItem} key = {index} />
)
}
else if (pt.type == 'lookup'){
else if(pt.type == 'literal'){
return(
<InputLookup propertyTemplate = {pt} key = {index} />
<InputLiteral propertyTemplate = {pt} key = {index} id = {index} />
)
}
else if (isResourceWithValueTemplateRefs) {
// TODO: some valueTemplateRefs may be lookups??
return (
<ButtonToolbar key={index}>
<div>
<b>{pt.propertyLabel}</b>
</div>
{this.resourceTemplateButtons(pt.valueConstraint.valueTemplateRefs)}
</ButtonToolbar>
)
}
else if (pt.type == 'resource'){
return (<p key={index}><b>{pt.propertyLabel}</b>: <i>NON-modal resource</i></p>)
}
})}
</div>
<p>END ResourceTemplateForm</p>
Expand All @@ -102,7 +130,8 @@ class ResourceTemplateForm extends Component {
}

ResourceTemplateForm.propTypes = {
propertyTemplates: PropTypes.arrayOf(PropTypes.object).isRequired
propertyTemplates: PropTypes.arrayOf(PropTypes.object).isRequired,
lookupConfig: PropTypes.object
}

export default ResourceTemplateForm

0 comments on commit 84cc007

Please sign in to comment.