Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inputlistloc selector reducer #513

Merged
merged 3 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions __tests__/components/editor/InputListLOC.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const plProps = {
"propertyLabel": "Frequency (RDA 2.14)",
"remark": "http://access.rdatoolkit.org/2.14.html",
"mandatory": "false",
"repeatable": "true",
"repeatable": "false",
"type": "lookup",
"valueConstraint": {
"repeatable": "true",
"defaults": [{
"defaultURI": "http://id.loc.gov/vocabulary/carriers/nc",
"defaultLiteral": "volume"
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('<InputList />', () => {
expect(wrapper.find(Typeahead).props().placeholder).toMatch("Frequency (RDA 2.14)")
})

it('sets the typeahead component multiple attribute according to the repeatable value from the template', () => {
it('sets the typeahead component multiple attribute according to the repeatable value from valueContraints in the property template', () => {
expect(wrapper.find('#targetComponent').props().multiple).toBe(true)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeAllContent, removeMyItem, setMyItems } from '../../src/reducers/literal'
import { removeAllContent, removeMyItem, setMyItems, setMySelections } from '../../src/reducers/inputs'

describe('literal reducer functions', () => {

Expand Down Expand Up @@ -134,6 +134,78 @@ describe('literal reducer functions', () => {
})
})

it('CHANGE_SELECTIONS adds items to state', () => {
const listSetSelections = setMySelections({ "resourceTemplate:Monograph:Instance": {
'http://schema.org/name': { items: [] }
}}, {
type: 'CHANGE_SELECTIONS',
payload: {
id: 'abc123',
uri: 'http://schema.org/name',
reduxPath: ['resourceTemplate:Monograph:Instance', 'http://schema.org/name'],
items: [ { id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'} ]
}
})
expect(listSetSelections).toEqual({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [{ id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'}]
}
}
})
})

it('CHANGE_SELECTIONS overwites items in current state', () => {
const listSetSelections = setMySelections({ "resourceTemplate:Monograph:Instance": {
'http://schema.org/name': { items: [{ id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'}] }
}}, {
type: 'CHANGE_SELECTIONS',
payload: {
id: 'def456',
uri: 'http://schema.org/name',
reduxPath: ['resourceTemplate:Monograph:Instance', 'http://schema.org/name'],
items: [
{ id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'},
{ id: 1, label: 'See if they pass', uri: 'http://schema.org/def'}
]
}
})
expect(listSetSelections).toEqual({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{ id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'},
{ id: 1, label: 'See if they pass', uri: 'http://schema.org/def'}
]
}
}
})
})

it('CHANGE_SELECTIONS removes all items in current state by overwriting with an empty object', () => {
const listSetSelections = setMySelections({ "resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{ id: 0, label: 'Run the tests', uri: 'http://schema.org/abc'},
{ id: 1, label: 'See if they pass', uri: 'http://schema.org/def'}
]
}
}}, {
type: 'CHANGE_SELECTIONS',
payload: {
id: 'nomatter',
uri: 'http://not/importanr',
reduxPath: ['resourceTemplate:Monograph:Instance', 'http://schema.org/name'],
items: []
}
})
expect(listSetSelections).toEqual({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {items: []}
}
})
})

it('REMOVE_ITEM removes an item from state', () => {
expect(removeMyItem({
"resourceTemplate:Monograph:Instance": {
Expand Down
21 changes: 8 additions & 13 deletions src/components/editor/InputListLOC.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import React, { Component } from 'react';
import { Typeahead } from 'react-bootstrap-typeahead'
import PropTypes from 'prop-types'
import PropertyRemark from './PropertyRemark'

import { connect } from 'react-redux'
import { changeSelections } from '../../actions/index'
import shortid from 'shortid'

class InputListLOC extends Component {
constructor(props) {
Expand All @@ -22,8 +22,8 @@ class InputListLOC extends Component {
const defaultValue = this.props.propertyTemplate.valueConstraint.defaults[0]
const defaults = [{
id: defaultValue.defaultURI,
uri: defaultValue.defaultURI,
label: defaultValue.defaultLiteral
label: defaultValue.defaultLiteral,
uri: defaultValue.defaultURI
}]
this.state.defaults = defaults
this.setPayLoad(defaults)
Expand All @@ -36,7 +36,7 @@ class InputListLOC extends Component {
let payload = {
id: this.props.propertyTemplate.propertyURI,
items: items,
rtId: this.props.rtId
reduxPath: this.props.reduxPath,
}
this.props.handleSelectedChange(payload)
}
Expand All @@ -53,7 +53,7 @@ class InputListLOC extends Component {
let lookupUri, isMandatory, isRepeatable
try {
isMandatory = JSON.parse(this.props.propertyTemplate.mandatory)
isRepeatable = JSON.parse(this.props.propertyTemplate.repeatable)
isRepeatable = JSON.parse(this.props.propertyTemplate.valueConstraint.repeatable)
lookupUri = this.props.lookupConfig.value.uri
} catch (error) {
console.log(`Some properties were not defined in the json file: ${error}`)
Expand Down Expand Up @@ -82,10 +82,11 @@ class InputListLOC extends Component {
.then(json => {
for(var i in json){
try{
const newId = shortid.generate()
const item = Object.getOwnPropertyDescriptor(json, i)
const uri = item.value["@id"]
const label = item.value["http://www.loc.gov/mads/rdf/v1#authoritativeLabel"][0]["@value"]
opts.push({ id: uri, uri: uri, label: label })
opts.push({ id: newId, label: label, uri: uri })
} catch (error) {
//ignore
}
Expand Down Expand Up @@ -119,13 +120,7 @@ InputListLOC.propTypes = {
}

const mapStatetoProps = (state) => {
// let data = state.lookups.formData
let result = Object.assign({}, state)
// if (data !== undefined){
// result = { formData: data }
// }

return result
return Object.assign({}, state)
}

const mapDispatchtoProps = dispatch => ({
Expand Down
4 changes: 3 additions & 1 deletion src/components/editor/ResourceTemplateForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export class ResourceTemplateForm extends Component {
if (listComponent === 'list'){
result = (
<PropertyPanel pt={pt} key={index} float={index} rtId={this.props.rtId}>
<InputListLOC propertyTemplate = {pt} lookupConfig = {lookupConfigItem} key = {index} rtId = {this.props.rtId} />
<InputListLOC key = {index} propertyTemplate = {pt}
lookupConfig = {lookupConfigItem}
reduxPath={[this.props.rtId, pt.propertyURI]} />
</PropertyPanel>
)
} else if (listComponent === 'lookup'){
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { combineReducers } from 'redux'
import lang from './lang'
import authenticate from './authenticate'
import { removeAllContent, setMyItems, removeMyItem } from './literal'
import {removeAllContent, setMyItems, removeMyItem, setMySelections} from './inputs'
import shortid from 'shortid'

const inputPropertySelector = (state, props) => {
Expand Down Expand Up @@ -98,6 +98,8 @@ const selectorReducer = (state={}, action) => {
return setResourceTemplate(state, action)
case 'SET_ITEMS':
return setMyItems(state, action)
case 'CHANGE_SELECTIONS':
return setMySelections(state, action)
case 'REFRESH_RESOURCE_TEMPLATE':
return refreshResourceTemplate(state, action)
case 'REMOVE_ITEM':
Expand Down
17 changes: 17 additions & 0 deletions src/reducers/literal.js → src/reducers/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ export const setMyItems = (state, action) => {
return newState
}

export const setMySelections = (state, action) => {
const newState = Object.assign({}, state)
const reduxPath = action.payload.reduxPath
let level = 0
reduxPath.reduce((obj, key) => {
level++
if (level === reduxPath.length) {
if ((key in obj) != true) {
obj[key] = { items: [] }
}
obj[key].items = action.payload.items
}
return obj[key]
}, newState)
return newState
}

export const removeMyItem = (state, action) => {
const newState = Object.assign({}, state)
const reduxPath = action.payload.reduxPath
Expand Down