Skip to content

Commit

Permalink
Merge pull request #1277 from CDL-Dryad/institution-autocomplete
Browse files Browse the repository at this point in the history
Disambiguate ROR institution selection autocompletes, apply autocomplete to tenant selection
  • Loading branch information
ryscher authored Jul 18, 2023
2 parents 7907db5 + 0b045e0 commit 5b51ab5
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 181 deletions.
45 changes: 35 additions & 10 deletions app/assets/stylesheets/stash_engine/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -5120,20 +5120,36 @@ td:last-child, th:last-child {
display: -ms-flexbox;
display: flex;
}
.c-auto_complete .c-ac__input_with_button {
padding-right: 3ch;
}
.c-auto_complete .c-ac__input-button {
padding: 0 1ch;
position: absolute;
right: 0;
top: 0;
background-color: transparent;
border: 0;
height: 30px;
outline: none;
}
.c-auto_complete .c-ac__menu {
max-height: 180px;
overflow-y: auto;
width: 100%;
min-width: 200px;
background-color: #fafafa;
position: absolute;
top: 1em;
top: 30px;
margin-top: 0;
z-index: 1000;
padding: 0;
list-style: none;
border: thin solid #ddd;
}
.c-auto_complete .c-ac__menu .c-ac__menu_item {
margin-bottom: 0.5em;
padding: 0.25em 0.5ch;
margin: 0;
}
.c-auto_complete .c-ac__menu .c-ac__menu_item.highlighted {
background-color: #bde4ff;
Expand Down Expand Up @@ -6668,10 +6684,17 @@ td:last-child, th:last-child {
margin-bottom: 25px;
margin-left: 0;
}
.t-login__box .c-institution__container select, .t-login__choose .c-institution__container select {
.t-login__box .c-institution__container .c-auto_complete, .t-login__choose .c-institution__container .c-auto_complete {
width: 20rem;
margin-right: 10px;
}
.t-login__box .c-institution__container .c-auto_complete .c-input__text.c-ac__input_with_button, .t-login__box .c-institution__container .c-auto_complete .c-ac__input-button, .t-login__choose .c-institution__container .c-auto_complete .c-input__text.c-ac__input_with_button, .t-login__choose .c-institution__container .c-auto_complete .c-ac__input-button {
height: 45px;
}
.t-login__box .c-institution__container .c-auto_complete .c-ac__menu, .t-login__choose .c-institution__container .c-auto_complete .c-ac__menu {
top: 45px;
text-align: left;
}

.t-login__info {
text-align: left;
Expand Down Expand Up @@ -6760,10 +6783,9 @@ td:last-child, th:last-child {
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
margin: 0 0 1em 5em;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.c-institution__container form {
display: -webkit-box;
Expand All @@ -6773,9 +6795,12 @@ td:last-child, th:last-child {
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: baseline;
-ms-flex-align: baseline;
align-items: baseline;
width: 100%;
}

Expand Down
8 changes: 2 additions & 6 deletions app/controllers/stash_engine/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,12 @@ def test_login
# rubocop:enable Metrics/AbcSize

def choose_sso
tenants = [OpenStruct.new(id: '', name: '')]
tenants << StashEngine::Tenant.partner_list.map do |t|
OpenStruct.new(id: t.tenant_id, name: t.short_name)
end

tenants = StashEngine::Tenant.partner_list.map { |t| { id: t.tenant_id, name: t.short_name } }
# If no tenants are defined redirect to the no_parter path
if tenants.empty?
redirect_to :no_partner, method: :post
else
@tenants = tenants.flatten
@tenants = tenants
end
end

Expand Down
141 changes: 0 additions & 141 deletions app/javascript/react/components/MetadataEntry/FacilityAutocomplete.jsx

This file was deleted.

100 changes: 100 additions & 0 deletions app/javascript/react/components/MetadataEntry/FacilityForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, {useRef, useState} from 'react';
import axios from 'axios';
import {Form, Formik} from 'formik';
import PropTypes from 'prop-types';
import RorAutocomplete from './RorAutocomplete';
import {showSavedMsg, showSavingMsg} from '../../../lib/utils';

export default function FacilityForm({
name, rorId, contribId, resourceId, createPath, updatePath, controlOptions,
}) {
const formRef = useRef(0);
// control options: htmlId, labelText, isRequired (t/f)

// in order to use this component, we need to track the state of the autocomplete text and the autocomplete id
// https://www.freecodecamp.org/news/what-is-lifting-state-up-in-react/ is a better functional example than the react docs.
// also tracking "autoBlurred" since we need to know when things exit to trigger form resubmission or sending to server.
const [acText, setAcText] = useState(name);
const [acID, setAcID] = useState(rorId);
const [contributorId, setContributorId] = useState(contribId);
const nameRef = useRef(null);

const submitData = () => {
showSavingMsg();
const authenticity_token = document.querySelector("meta[name='csrf-token']")?.getAttribute('content');

const values = {
authenticity_token,
contributor: {
id: contributorId,
resource_id: resourceId,
identifier_type: 'ror',
contributor_type: 'sponsor',
contributor_name: acText,
name_identifier_id: acID,
},
};

let url;
let method;
if (contributorId) {
url = updatePath;
method = 'patch';
} else {
url = createPath;
method = 'post';
}

axios({
method,
url,
data: values,
headers: {'Content-Type': 'application/json; charset=utf-8', Accept: 'application/json'},
}).then((data) => {
if (data.status !== 200) {
console.log('Response failure not a 200 response from research facility save');
}
setContributorId(data.data.id);
showSavedMsg();
});
};

return (
<Formik
initialValues={{}}
innerRef={formRef}
onSubmit={({setSubmitting}) => {
submitData().then(() => { setSubmitting(false); });
}}
>
{(formik) => (
<Form className="c-input" onSubmit={() => formik.handleSubmit()}>
<RorAutocomplete
formRef={formRef}
acText={acText}
setAcText={setAcText}
acID={acID}
setAcID={setAcID}
controlOptions={controlOptions}
/>
<input ref={nameRef} type="hidden" value={acText} className="js-affil-longname" name="contributor[name_identifier_id]" />
<input type="hidden" value={acID} className="js-affil-id" name="author[affiliation][ror_id]" />
</Form>
)}
</Formik>
);
/* eslint-enable react/jsx-no-bind */
}

// {name, rorId, contribId, resourceId, createPath, updatePath, controlOptions}
FacilityForm.propTypes = {
name: PropTypes.string.isRequired,
rorId: PropTypes.string.isRequired,
contribId: PropTypes.number,
resourceId: PropTypes.number.isRequired,
createPath: PropTypes.string.isRequired,
updatePath: PropTypes.string.isRequired,
controlOptions: PropTypes.object.isRequired,
};

FacilityForm.defaultProps = {contribId: ''};
Loading

0 comments on commit 5b51ab5

Please sign in to comment.