From fe98542e64865ca8582ee91176401563f5cf445b Mon Sep 17 00:00:00 2001 From: cociugsergiu <sergiu.cociug@endava.com> Date: Fri, 9 Apr 2021 13:50:21 +0300 Subject: [PATCH] feat: adds DOI validation field property --- .../components/builderComponents/RadioBox.js | 7 +++++- .../src/components/config/Elements.js | 17 ++++++++++++++ .../src/components/FormTemplate.js | 21 +++++++++++++++++- app/queries/index.js | 12 ++++++++++ app/storage/forms-ncrc/submit.json | 2 +- config/permissions.js | 1 + server/model-manuscript/src/graphql.js | 22 +++++++++++++++++++ 7 files changed, 79 insertions(+), 3 deletions(-) diff --git a/app/components/component-formbuilder/src/components/builderComponents/RadioBox.js b/app/components/component-formbuilder/src/components/builderComponents/RadioBox.js index f0302c9239..cd1e92ece7 100644 --- a/app/components/component-formbuilder/src/components/builderComponents/RadioBox.js +++ b/app/components/component-formbuilder/src/components/builderComponents/RadioBox.js @@ -1,5 +1,10 @@ import React from 'react' -import { RadioGroup } from '@pubsweet/ui' +import styled from 'styled-components' +import { RadioGroup as UnstableRadioGroup } from '@pubsweet/ui' + +const RadioGroup = styled(UnstableRadioGroup)` + position: relative +` const RadioboxFieldBuilder = input => <RadioGroup {...input} /> export default RadioboxFieldBuilder diff --git a/app/components/component-formbuilder/src/components/config/Elements.js b/app/components/component-formbuilder/src/components/config/Elements.js index c46a8e7f27..1729f0c0ba 100644 --- a/app/components/component-formbuilder/src/components/config/Elements.js +++ b/app/components/component-formbuilder/src/components/config/Elements.js @@ -147,6 +147,23 @@ const elements = { ], }, }, + DoiValidation: { + component: 'RadioBox', + props: { + inline: true, + options: [ + { + value: 'true', + label: 'Yes', + }, + { + value: 'false', + label: 'No', + }, + ], + label: 'DOI Validation', + }, + }, }, CheckboxGroup: { id: textfield, diff --git a/app/components/component-submit/src/components/FormTemplate.js b/app/components/component-submit/src/components/FormTemplate.js index 7a80fbf347..84e52cae81 100644 --- a/app/components/component-submit/src/components/FormTemplate.js +++ b/app/components/component-submit/src/components/FormTemplate.js @@ -19,6 +19,8 @@ import LinksInput from './LinksInput' import ValidatedFieldFormik from './ValidatedField' import Confirm from './Confirm' import { articleStatuses } from '../../../../globals' +import { VALIDATE_DOI } from '../../../../queries/index' +import { useApolloClient } from '@apollo/client' const Intro = styled.div` font-style: italic; @@ -110,7 +112,7 @@ const createMarkup = encodedHtml => ({ __html: unescape(encodedHtml), }) -const composeValidate = (vld = [], valueField = {}) => value => { +const composeValidate = (vld = [], valueField = {}, fieldName, client) => value => { const validator = vld || [] if (validator.length === 0) return undefined @@ -129,6 +131,20 @@ const composeValidate = (vld = [], valueField = {}) => value => { return validatorFn }) + + if(errors.length === 0 && fieldName === 'submission.articleURL') { + return client.query({ + query: VALIDATE_DOI, + variables: { + articleURL: value + } + }).then(res => { + if (!res.data.validateDOI.isDOIValid) { + return 'DOI is invalid' + } + return undefined + }) + } return errors.length > 0 ? errors[0] : undefined } @@ -150,6 +166,7 @@ const FormTemplate = ({ validateForm, match, }) => { + const client = useApolloClient() const submitButton = text => ( <div> <Button @@ -256,6 +273,8 @@ const FormTemplate = ({ validate={composeValidate( element.validate, element.validateValue, + element.name, + client )} values={values} /> diff --git a/app/queries/index.js b/app/queries/index.js index d4b8427653..8b36453fbb 100644 --- a/app/queries/index.js +++ b/app/queries/index.js @@ -83,6 +83,18 @@ export const SEARCH_USERS = gql` } ` +export const VALIDATE_DOI = gql` + query Manuscripts( + $articleURL: String + ) { + validateDOI( + articleURL: $articleURL + ){ + isDOIValid + } + } +` + export const GET_MANUSCRIPTS = gql` query Manuscripts( $sort: String diff --git a/app/storage/forms-ncrc/submit.json b/app/storage/forms-ncrc/submit.json index 5a0f0fcb2a..cba65b43a0 100644 --- a/app/storage/forms-ncrc/submit.json +++ b/app/storage/forms-ncrc/submit.json @@ -224,7 +224,7 @@ ] } ], - "id": "NCRC Submission Form", + "id": "submit", "name": "NCRC Submission Form", "description": "<p>NCRC Form</p>", "haspopup": "false" diff --git a/config/permissions.js b/config/permissions.js index 27b697bc67..a2c154abd0 100644 --- a/config/permissions.js +++ b/config/permissions.js @@ -307,6 +307,7 @@ const permissions = { getForm: allow, getForms: allow, user: allow, + validateDOI: allow, }, Mutation: { upload: isAuthenticated, diff --git a/server/model-manuscript/src/graphql.js b/server/model-manuscript/src/graphql.js index 2a1726f014..7435cc6b38 100644 --- a/server/model-manuscript/src/graphql.js +++ b/server/model-manuscript/src/graphql.js @@ -507,6 +507,23 @@ const resolvers = { // return ctx.connectors.User.fetchAll(where, ctx, { eager }) }, + + async validateDOI(_, { articleURL }, ctx) { + const DOI = encodeURI(articleURL.split('.org/')[1]) + try { + await axios.get(`https://api.crossref.org/works/${DOI}/agency`) + + return { + isDOIValid: true + } + } catch(err) { + // eslint-disable-next-line + console.log(err) + return { + isDOIValid: false + } + } + } }, // We want submission into to come out as a stringified JSON, so that we don't have to // change our queries if the submission form changes. We still want to store it as JSONB @@ -522,6 +539,7 @@ const typeDefs = ` manuscripts: [Manuscript]! paginatedManuscripts(sort: String, offset: Int, limit: Int, filter: ManuscriptsFilter): PaginatedManuscripts publishedManuscripts(sort:String, offset: Int, limit: Int): PaginatedManuscripts + validateDOI(articleURL: String): validateDOIResponse } input ManuscriptsFilter { @@ -529,6 +547,10 @@ const typeDefs = ` submission: String } + type validateDOIResponse { + isDOIValid: Boolean + } + type PaginatedManuscripts { totalCount: Int manuscripts: [Manuscript] -- GitLab