Skip to content

Commit

Permalink
Moving useSelector into Metadata so the store is not access every…
Browse files Browse the repository at this point in the history
… time `Description` is rendered.
  • Loading branch information
erinesullivan committed Nov 22, 2024
1 parent c8b82d2 commit d9c4005
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
33 changes: 23 additions & 10 deletions src/modules/metadata/components/Description/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import PropTypes from 'prop-types';
import React from 'react';
import { stringifySearch } from '../../../search';
import { TrimString } from '../../../core';
import { useSelector } from 'react-redux';

const Description = ({ data, viewType }) => {
const { active: activeInstitution, defaultInstitution } = useSelector((state) => {
return state.institution;
});
const { active: activeDatastore, datastores } = useSelector((state) => {
return state.datastores;
});
const Description = ({ activeDatastore, data, datastores, institution, viewType }) => {
if (Array.isArray(data)) {
return (
<ol className='list__unstyled'>
{data.map((datum, index) => {
return (
<li key={index}>
{index > 0 && <Icon icon='navigate_next' className='text-grey__light' />}
<Description activeDatastore={activeDatastore} data={datum} datastores={datastores} institution={institution} viewType={viewType} />
</li>
);
})}
</ol>
);
}

const { browse, href, icon, search, text } = data;
const searchScope = search?.scope || {};
Expand All @@ -26,7 +33,7 @@ const Description = ({ data, viewType }) => {
const { scope, type, value } = search;
anchorAttributes.to = `/${slug}?${stringifySearch({
filter: type === 'filtered' ? { [scope]: value } : {},
library: uid === 'mirlyn' ? (activeInstitution || defaultInstitution) : {},
library: uid === 'mirlyn' ? institution : '',
query: type === 'fielded' ? `${scope}:${value}` : value
})}`;
}
Expand Down Expand Up @@ -60,7 +67,13 @@ const Description = ({ data, viewType }) => {
};

Description.propTypes = {
data: PropTypes.object,
activeDatastore: PropTypes.string,
data: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object
]),
datastores: PropTypes.array,
institution: PropTypes.string,
viewType: PropTypes.string
};

Expand Down
28 changes: 12 additions & 16 deletions src/modules/metadata/components/Metadata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import {
ContextProvider,
Expandable,
ExpandableButton,
ExpandableChildren,
Icon
ExpandableChildren
} from '../../../reusable';
import Description from '../Description';
import PropTypes from 'prop-types';
import React from 'react';
import { useSelector } from 'react-redux';

const Metadata = ({ metadata = {} }) => {
const { active: activeInstitution, defaultInstitution } = useSelector((state) => {
return state.institution;
});
const { active: activeDatastore, datastores } = useSelector((state) => {
return state.datastores;
});

const institution = activeInstitution || defaultInstitution;

return (
<ContextProvider
render={({ viewType }) => {
Expand Down Expand Up @@ -40,20 +49,7 @@ const Metadata = ({ metadata = {} }) => {
{description.map((descriptor, index) => {
return (
<dd key={index}>
{Array.isArray(descriptor)
? (
<ol className='list__unstyled'>
{descriptor.map((descript, number) => {
return (
<li key={number}>
{number > 0 && <Icon icon='navigate_next' className='text-grey__light' />}
<Description data={descript} />
</li>
);
})}
</ol>
)
: <Description data={descriptor} viewType={viewType} />}
<Description activeDatastore={activeDatastore} data={descriptor} datastores={datastores} institution={institution} viewType={viewType} />
</dd>
);
})}
Expand Down

0 comments on commit d9c4005

Please sign in to comment.