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

[Platform] Use React for state management in literature section #416

Open
wants to merge 13 commits into
base: ot-literature-section
Choose a base branch
from
107 changes: 58 additions & 49 deletions packages/sections/src/common/Literature/Body.jsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,71 @@
import { useEffect, useState } from "react";
import {
LiteratureProvider,
useLiterature,
useLiteratureDispatch,
defaultLiteratureState,
} from "./LiteratureContext";
import { fetchSimilarEntities } from "./requests";
import { Box } from "@mui/material";
import { useSetRecoilState, useRecoilValue, useResetRecoilState, RecoilRoot } from "recoil";
import { SectionItem } from "ui";
import PublicationsList from "./PublicationsList";
import Description from "./Description";
import { literatureState, updateLiteratureState, fetchSimilarEntities } from "./atoms";
import Entities from "./Entities";
import Category from "./Category";
import CountInfo from "./CountInfo";
import { DateFilter } from "./DateFilter";

function LiteratureList({ id, name, entity, BODY_QUERY, definition }) {
const [requestObj, setRequestObj] = useState({});
// type LiteratureListParameter = {
// id: string,
// name: string,
// entity: any, // ADD LATER
// BODY_QUERY: string,
// definition: any, // ADD LATER
// };
Comment on lines +18 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented


const setLiteratureUpdate = useSetRecoilState(updateLiteratureState);
const resetLiteratureState = useResetRecoilState(literatureState);
function LiteratureList(
{ id, name, entity, BODY_QUERY, definition }) {
// { id, name, entity, BODY_QUERY, definition }: LiteratureListParameter) {

const bibliographyState = useRecoilValue(literatureState);
const { category, startYear, startMonth, endYear, endMonth } = bibliographyState;
const [requestObj, setRequestObj] = useState({});
const literature = useLiterature();
const { category, startYear, startMonth, endYear, endMonth } = literature;
const literatureDispatch = useLiteratureDispatch();

useEffect(
() => {
async function startRequest() {
const inintRequest = await fetchSimilarEntities({
id,
query: BODY_QUERY,
category,
startYear,
startMonth,
endYear,
endMonth,
});
setRequestObj(inintRequest);
const data = inintRequest.data[entity];
const update = {
entities: data.similarEntities,
litsIds: data.literatureOcurrences?.rows?.map(({ pmid }) => ({
id: pmid,
status: "ready",
publication: null,
})),
litsCount: data.literatureOcurrences?.filteredCount,
earliestPubYear: data.literatureOcurrences?.earliestPubYear,
cursor: data.literatureOcurrences?.cursor,
id,
query: BODY_QUERY,
globalEntity: entity,
};
setLiteratureUpdate(update);
}
startRequest();
return function cleanUp() {
resetLiteratureState();
useEffect(() => {
async function startRequest() {
const initRequest = await fetchSimilarEntities({
id,
query: BODY_QUERY,
category,
startYear,
startMonth,
endYear,
endMonth,
});
setRequestObj(initRequest);
const data = initRequest.data[entity];
const update = {
entities: data.similarEntities,
litsIds: data.literatureOcurrences?.rows?.map(({ pmid }) => pmid),
litsCount: data.literatureOcurrences?.filteredCount,
earliestPubYear: data.literatureOcurrences?.earliestPubYear,
cursor: data.literatureOcurrences?.cursor,
id,
query: BODY_QUERY,
globalEntity: entity,
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
literatureDispatch({ type: 'stateUpdate', value: update });
}
startRequest();
// return function cleanUp() {
// literatureDispatch({
// type: 'stateUpdate',
// value: defaultLiteratureState
// });
// };
}, []);

return (
<SectionItem
definition={definition}
Expand All @@ -82,16 +91,16 @@ function LiteratureList({ id, name, entity, BODY_QUERY, definition }) {

function Body({ definition, name, id, entity, BODY_QUERY }) {
return (
<RecoilRoot>
<LiteratureProvider>
<LiteratureList
id={id}
name={name}
entity={entity}
BODY_QUERY={BODY_QUERY}
definition={definition}
/>
</RecoilRoot>
/>
</LiteratureProvider>
);
}

export default Body;
export default Body;
34 changes: 13 additions & 21 deletions packages/sections/src/common/Literature/Category.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { InputLabel, FormGroup, Checkbox, FormControlLabel } from "@mui/material";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import {
selectedCategoriesState,
loadingEntitiesState,
literatureState,
fetchSimilarEntities,
updateLiteratureState,
} from "./atoms";
import { InputLabel, FormGroup, Checkbox, FormControlLabel }
from "@mui/material";
import { useLiterature, useSelectedCategories, useLiteratureDispatch }
from "./LiteratureContext";
import { fetchSimilarEntities } from "./requests";

const toggleValue = (selected, categories) => {
const isChecked = categories.indexOf(selected) !== -1;
Expand All @@ -21,11 +17,10 @@ const categories = [
];

export default function Category() {
const category = useRecoilValue(selectedCategoriesState);
const setLiteratureUpdate = useSetRecoilState(updateLiteratureState);
const [loadingEntities, setLoadingEntities] = useRecoilState(loadingEntitiesState);

const bibliographyState = useRecoilValue(literatureState);
const literature = useLiterature();
const category = useSelectedCategories();
const literatureDispatch = useLiteratureDispatch();

const handleChange = async event => {
const {
Expand All @@ -39,12 +34,10 @@ export default function Category() {
endMonth,
startYear,
startMonth,
} = bibliographyState;
const {
target: { name: clicked },
} = event;
} = literature;
const { target: { name: clicked } } = event;
const newCategories = toggleValue(clicked, bibliographyCategory);
setLoadingEntities(true);
literatureDispatch({ type: 'loadingEntities', value: true });
const request = await fetchSimilarEntities({
query,
id,
Expand All @@ -57,15 +50,14 @@ export default function Category() {
startMonth,
});
const data = request.data[globalEntity];

const update = {
entities: data.similarEntities,
earliestPubYear: data.literatureOcurrences?.earliestPubYear,
litsCount: data.literatureOcurrences?.filteredCount,
loadingEntities: false,
category: newCategories,
};
setLiteratureUpdate(update);
literatureDispatch({ type: 'stateUpdate', value: update });
};

return (
Expand All @@ -88,7 +80,7 @@ export default function Category() {
onChange={handleChange}
name={name}
color="primary"
disabled={loadingEntities}
disabled={literature.loadingEntities}
/>
}
label={label}
Expand Down
13 changes: 5 additions & 8 deletions packages/sections/src/common/Literature/CountInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { InputLabel, Box } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { useRecoilValue } from "recoil";
import { litsCountState, loadingEntitiesState, tablePageSizeState } from "./atoms";
import { useLiterature } from "./LiteratureContext";

const useStyles = makeStyles(() => ({
resultCount: {
Expand All @@ -11,14 +10,12 @@ const useStyles = makeStyles(() => ({
}));

function CountInfo() {
const { pageSize, litsCount, loadingEntities } = useLiterature();
const classes = useStyles();
const pageSize = useRecoilValue(tablePageSizeState);
const count = useRecoilValue(litsCountState);
const loadingEntities = useRecoilValue(loadingEntitiesState);

const getLabelText = () => {
if (loadingEntities) return "Loading count...";
return `Showing ${count > pageSize ? pageSize : count} of ${count} results`;
return `Showing ${
litsCount > pageSize ? pageSize : litsCount} of ${litsCount} results`;
};

return (
Expand All @@ -28,4 +25,4 @@ function CountInfo() {
);
}

export default CountInfo;
export default CountInfo;
Loading
Loading