-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from mlibrary/history-react-router-dom-update…
…-part-2 `history` and `react-router-update` - Part 2
- Loading branch information
Showing
26 changed files
with
390 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 54 additions & 97 deletions
151
src/modules/institution/components/InstitutionSelect/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,72 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import _ from 'underscore'; | ||
import { withRouter } from 'react-router-dom'; | ||
import { useSelector } from 'react-redux'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { createSelector } from '@reduxjs/toolkit'; | ||
import { stringifySearchQueryForURL } from '../../../pride'; | ||
import PropTypes from 'prop-types'; | ||
|
||
class InstitutionSelect extends React.Component { | ||
handleChange (event) { | ||
const { searchQuery, activeFilters, activeDatastore, history } = this.props; | ||
const InstitutionSelect = ({ activeDatastore, institution }) => { | ||
const { uid, slug } = activeDatastore; | ||
const { activeFilters, searchQuery } = useSelector(createSelector( | ||
(state) => { | ||
return state.filters.active[uid]; | ||
}, | ||
(state) => { | ||
return state.search.query; | ||
}, | ||
(activeFilters, searchQuery) => { | ||
return { activeFilters, searchQuery }; | ||
} | ||
)); | ||
const history = useHistory(); | ||
|
||
if (uid !== 'mirlyn') { | ||
return null; | ||
} | ||
|
||
const { active, defaultInstitution, options } = institution; | ||
|
||
const handleChange = (event) => { | ||
const queryString = stringifySearchQueryForURL({ | ||
query: searchQuery, | ||
filter: activeFilters, | ||
library: event.target.value | ||
}); | ||
|
||
history.push(`/${activeDatastore.slug}?${queryString}`); | ||
} | ||
|
||
render () { | ||
const { activeDatastore, type } = this.props; | ||
const { active, defaultInstitution, options } = this.props.institution; | ||
|
||
// This feature is only for Mirlyn. | ||
if (activeDatastore.uid !== 'mirlyn') { | ||
return null; | ||
} | ||
|
||
if (type === 'switch') { | ||
const selectedOption = active || defaultInstitution; | ||
history.push(`/${slug}?${queryString}`); | ||
}; | ||
|
||
return ( | ||
<fieldset className='radio-fieldset'> | ||
<legend className='visually-hidden'>Institutions</legend> | ||
{options.map((option, index) => { | ||
return ( | ||
<span key={index}> | ||
<input | ||
id={`library-${index}`} | ||
type='radio' | ||
className='radio-input' | ||
checked={selectedOption === option} | ||
value={option} | ||
onChange={(event) => { | ||
return this.handleChange(event); | ||
}} | ||
/> | ||
<label | ||
htmlFor={`library-${index}`} | ||
className={`radio-label ${ | ||
selectedOption === option ? 'radio-selected' : '' | ||
}`} | ||
> | ||
<span className='radio-label-text'>{option}</span> | ||
</label> | ||
</span> | ||
); | ||
})} | ||
</fieldset> | ||
); | ||
} else { | ||
return ( | ||
<fieldset className='institution-select-container'> | ||
<legend className='visually-hidden'>Institutions</legend> | ||
<label | ||
className='institution-select-label institution-select-label-text' | ||
htmlFor='library-scope' | ||
> | ||
Library Scope | ||
</label> | ||
<select | ||
className='dropdown' | ||
value={active || defaultInstitution} | ||
onChange={(event) => { | ||
return this.handleChange(event); | ||
}} | ||
id='library-scope' | ||
autoComplete='off' | ||
> | ||
{options.map((option, index) => { | ||
return ( | ||
<option value={option} key={index}> | ||
{option} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
</fieldset> | ||
); | ||
} | ||
} | ||
} | ||
return ( | ||
<fieldset className='institution-select-container'> | ||
<legend className='visually-hidden'>Institutions</legend> | ||
<label | ||
className='institution-select-label institution-select-label-text' | ||
htmlFor='library-scope' | ||
> | ||
Library Scope | ||
</label> | ||
<select | ||
className='dropdown' | ||
value={active || defaultInstitution} | ||
onChange={handleChange} | ||
id='library-scope' | ||
autoComplete='off' | ||
> | ||
{options.map((option, index) => { | ||
return ( | ||
<option value={option} key={index}> | ||
{option} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
</fieldset> | ||
); | ||
}; | ||
|
||
InstitutionSelect.propTypes = { | ||
searchQuery: PropTypes.string, | ||
activeFilters: PropTypes.object, | ||
activeDatastore: PropTypes.object, | ||
history: PropTypes.object, | ||
type: PropTypes.string, | ||
institution: PropTypes.object | ||
}; | ||
|
||
function mapStateToProps (state) { | ||
return { | ||
activeDatastore: _.findWhere(state.datastores.datastores, { | ||
uid: state.datastores.active | ||
}), | ||
institution: state.institution, | ||
activeFilters: state.filters.active[state.datastores.active], | ||
searchQuery: state.search.query | ||
}; | ||
} | ||
|
||
export default withRouter(connect(mapStateToProps)(InstitutionSelect)); | ||
export default InstitutionSelect; |
Oops, something went wrong.