Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
poor man redux
Browse files Browse the repository at this point in the history
  • Loading branch information
sballesteros committed Oct 11, 2019
1 parent 7f2cfb4 commit 0b4a821
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 57 deletions.
13 changes: 8 additions & 5 deletions src/components/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ export default function Home() {
console.log(action);
history.push('/');
}}
onViewInContext={({ identifier, preprint, tab }) => {
history.push(`/${unprefix(identifier)}`, {
preprint,
tab
});
onViewInContext={({ preprint, tab }) => {
history.push(
`/${unprefix(preprint.doi || preprint.arXivId)}`,
{
preprint,
tab
}
);
}}
/>
</Modal>
Expand Down
80 changes: 33 additions & 47 deletions src/components/new-preprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import identifiersArxiv from 'identifiers-arxiv';
import doiRegex from 'doi-regex';
import { format } from 'date-fns';
import Value from './value';
import { createPreprintIdentifierCurie } from '../utils/ids';
import { getId, arrayify, unprefix } from '../utils/jsonld';
import { usePostAction, usePreprint } from '../hooks/api-hooks';
import RapidFormFragment from './rapid-form-fragment';
Expand Down Expand Up @@ -45,6 +46,8 @@ export default function NewPreprint({
: 'NEW_PREPRINT'
);

if (!preprint) return null;

return (
<div className="new-preprint">
{step === 'NEW_PREPRINT' ? (
Expand All @@ -61,7 +64,6 @@ export default function NewPreprint({
onCancel={e => {
setStep('NEW_PREPRINT');
}}
identifier={identifier}
preprint={preprint}
onReviewed={onReviewed}
onViewInContext={onViewInContext}
Expand All @@ -71,7 +73,6 @@ export default function NewPreprint({
onCancel={e => {
setStep('NEW_PREPRINT');
}}
identifier={identifier}
preprint={preprint}
onRequested={onRequested}
onViewInContext={onViewInContext}
Expand Down Expand Up @@ -159,43 +160,43 @@ function StepPreprint({
value={value}
/>
{/* <label
htmlFor="step-preprint-input"
className="step-preprint__input-label step-preprint__input-label--large"
>
Enter a <abbr title="Digital Object Identifier">DOI</abbr> or an arXiv
ID
</label>
htmlFor="step-preprint-input"
className="step-preprint__input-label step-preprint__input-label--large"
>
Enter a <abbr title="Digital Object Identifier">DOI</abbr> or an arXiv
ID
</label>
<input
id="step-preprint-input"
className="step-preprint__text-input"
type="text"
autoComplete="off"
placeholder=""
onChange={e => {
<input
id="step-preprint-input"
className="step-preprint__text-input"
type="text"
autoComplete="off"
placeholder=""
onChange={e => {
const value = e.target.value;
const [arxivId] = identifiersArxiv.extract(value);
let nextIdentifier;
if (arxivId) {
nextIdentifier = `arXiv:${arxivId}`;
nextIdentifier = `arXiv:${arxivId}`;
} else {
const doiMatch = value.match(doiRegex());
const doi = doiMatch && doiMatch[0];
if (doi) {
nextIdentifier = `doi:${doi}`;
} else {
nextIdentifier = '';
}
const doiMatch = value.match(doiRegex());
const doi = doiMatch && doiMatch[0];
if (doi) {
nextIdentifier = `doi:${doi}`;
} else {
nextIdentifier = '';
}
}
if (nextIdentifier !== identifier) {
onIdentifier(nextIdentifier);
onIdentifier(nextIdentifier);
}
setValue(value);
}}
value={value}
/> */}
}}
value={value}
/> */}
</div>

{preprint ? (
Expand Down Expand Up @@ -250,13 +251,7 @@ StepPreprint.propTypes = {
resolvePreprintStatus: PropTypes.object.isRequired
};

function StepReview({
identifier,
preprint,
onViewInContext,
onCancel,
onReviewed
}) {
function StepReview({ preprint, onViewInContext, onCancel, onReviewed }) {
const [user] = useUser();
const [post, postData] = usePostAction();
const [answerMap, setAnswerMap] = useState({}); // TODO read from local storage ?
Expand Down Expand Up @@ -299,7 +294,7 @@ function StepReview({
'@type': 'RapidPREreviewAction',
actionStatus: 'CompletedActionStatus',
agent: getId(arrayify(user.hasRole)[0]),
object: identifier,
object: createPreprintIdentifierCurie(preprint),
resultReview: {
'@type': 'RapidPREreview',
reviewAnswer: getReviewAnswers(answerMap)
Expand All @@ -315,7 +310,6 @@ function StepReview({
<Button
onClick={e => {
onViewInContext({
identifier,
preprint,
tab: 'review',
answerMap
Expand All @@ -331,20 +325,13 @@ function StepReview({
);
}
StepReview.propTypes = {
identifier: PropTypes.string.isRequired,
preprint: PropTypes.object.isRequired,
onCancel: PropTypes.func.isRequired,
onReviewed: PropTypes.func.isRequired,
onViewInContext: PropTypes.func.isRequired
};

function StepRequest({
identifier,
preprint,
onViewInContext,
onCancel,
onRequested
}) {
function StepRequest({ preprint, onViewInContext, onCancel, onRequested }) {
const [user] = useUser();
const [post, postData] = usePostAction();

Expand All @@ -370,7 +357,7 @@ function StepRequest({
'@type': 'RequestForRapidPREreviewAction',
actionStatus: 'CompletedActionStatus',
agent: getId(arrayify(user.hasRole)[0]),
object: identifier
object: createPreprintIdentifierCurie(preprint)
},
onRequested
);
Expand All @@ -381,7 +368,7 @@ function StepRequest({
</Button>
<Button
onClick={e => {
onViewInContext({ identifier, preprint, tab: 'request' });
onViewInContext({ preprint, tab: 'request' });
}}
disabled={postData.isActive}
>
Expand All @@ -392,7 +379,6 @@ function StepRequest({
);
}
StepRequest.propTypes = {
identifier: PropTypes.string.isRequired,
preprint: PropTypes.object.isRequired,
onCancel: PropTypes.func.isRequired,
onRequested: PropTypes.func.isRequired,
Expand Down
5 changes: 3 additions & 2 deletions src/components/shell-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Button from './button';
import RapidFormFragment from './rapid-form-fragment';
import { getReviewAnswers, checkIfAllAnswered } from '../utils/actions';
import { getId, arrayify } from '../utils/jsonld';
import { createPreprintIdentifierCurie } from '../utils/ids';

// TODO required login modal

Expand Down Expand Up @@ -137,7 +138,7 @@ function ShellContentReview({ preprint, onSubmit, disabled, error }) {
'@type': 'RapidPREreviewAction',
actionStatus: 'CompletedActionStatus',
agent: getId(arrayify(user.hasRole)[0]),
object: preprint.doi || preprint.arXivId,
object: createPreprintIdentifierCurie(preprint),
resultReview: {
'@type': 'RapidPREreview',
reviewAnswer: getReviewAnswers(answerMap)
Expand Down Expand Up @@ -173,7 +174,7 @@ function ShellContentRequest({ preprint, onSubmit, disabled, error }) {
'@type': 'RequestForRapidPREreviewAction',
actionStatus: 'CompletedActionStatus',
agent: getId(arrayify(user.hasRole)[0]),
object: preprint.doi || preprint.arXivId
object: createPreprintIdentifierCurie(preprint)
});
}}
>
Expand Down
28 changes: 27 additions & 1 deletion src/hooks/api-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,32 @@ export function usePreprintSearchResults(

const [results, setResults] = useState(defaultResults);

useEffect(() => {
// keep `results` up-to-date
function update(preprint) {
if (
arrayify(results.rows).some(row => getId(row.doc) === getId(preprint))
) {
setResults(prevResults => {
return Object.assign({}, prevResults, {
rows: arrayify(prevResults.rows).map(row => {
if (!row.doc || getId(row.doc) !== getId(preprint)) {
return row;
}
return Object.assign({}, row, { doc: preprint });
})
});
});
}
}

preprintsWithActionsStore.addListener('SET', update);

return () => {
preprintsWithActionsStore.removeListener('SET', update);
};
}, [results]);

useEffect(() => {
setProgress({
isActive: true,
Expand Down Expand Up @@ -310,7 +336,7 @@ export function usePreprintSearchResults(
.then(data => {
arrayify(data.rows).forEach(row => {
if (row.doc) {
preprintsWithActionsStore.set(row.doc, { emit: !false });
preprintsWithActionsStore.set(row.doc, { emit: false });
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/stores/preprint-stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ class PreprintsWithActionsStore extends EventEmitter {
action['@type'] === 'RequestForRapidPREreviewAction') &&
getId(action.object)
) {
const preprint = this.peek(createPreprintId(getId(action.object)));
const preprint = this.peek(createPreprintId(action.object));

if (preprint) {
const nextPreprint = Object.assign({}, preprint, {
potentialAction: arrayify(preprint.potentialAction)
.filter(_action => getId(_action) === getId(action))
.filter(_action => getId(_action) !== getId(action))
.concat(action)
});
this.set(nextPreprint);
Expand Down
27 changes: 27 additions & 0 deletions src/utils/ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,30 @@ export function createPreprintId(

return `preprint:${vendor}-${slug(unprefix(id).replace('/', '-'))}`;
}

export function createPreprintIdentifierCurie(
value // preprint or identifer (arXivId or DOI, unprefixed)
) {
if (!value) {
throw createError(500, `invalid value for createIdentifierCurie`);
}

if (value.doi) {
return `doi:${value.doi}`;
} else if (value.arXivId) {
return `arXiv:${value.arXivId}`;
} else {
const id = getId(value);
if (!id) {
throw createError(500, `invalid value for createIdentifierCurie`);
}

if (doiRegex().test(id)) {
return `doi:${value.doi}`;
} else if (identifiersArxiv.extract(id)[0]) {
return `arXiv:${value.arXivId}`;
} else {
throw createError(500, `invalid value for createIdentifierCurie`);
}
}
}

0 comments on commit 0b4a821

Please sign in to comment.