Commit 88bb4caf authored by Giannis Kopanas's avatar Giannis Kopanas Committed by Alexandros Georgantas

feat(tags): add resolvers, graphql schema

parent bd129f51
const BOOK_COMPONENT_ADDED = 'BOOK_COMPONENT_ADDED'
const BOOK_COMPONENT_DELETED = 'BOOK_COMPONENT_DELETED'
const BOOK_COMPONENT_WORKFLOW_UPDATED = 'BOOK_COMPONENT_WORKFLOW_UPDATED'
const BOOK_COMPONENT_PAGINATION_UPDATED = 'BOOK_COMPONENT_PAGINATION_UPDATED'
const BOOK_COMPONENT_TRACK_CHANGES_UPDATED =
'BOOK_COMPONENT_TRACK_CHANGES_UPDATED'
const BOOK_COMPONENT_TITLE_UPDATED = 'BOOK_COMPONENT_TITLE_UPDATED'
const BOOK_COMPONENT_CONTENT_UPDATED = 'BOOK_COMPONENT_CONTENT_UPDATED'
const BOOK_COMPONENT_UPLOADING_UPDATED = 'BOOK_COMPONENT_UPLOADING_UPDATED'
const BOOK_COMPONENT_LOCK_UPDATED = 'BOOK_COMPONENT_LOCK_UPDATED'
const BOOK_COMPONENT_TYPE_UPDATED = 'BOOK_COMPONENT_TYPE_UPDATED'
module.exports = {
BOOK_COMPONENT_ADDED,
BOOK_COMPONENT_DELETED,
BOOK_COMPONENT_WORKFLOW_UPDATED,
BOOK_COMPONENT_PAGINATION_UPDATED,
BOOK_COMPONENT_TRACK_CHANGES_UPDATED,
BOOK_COMPONENT_TITLE_UPDATED,
BOOK_COMPONENT_CONTENT_UPDATED,
BOOK_COMPONENT_UPLOADING_UPDATED,
BOOK_COMPONENT_LOCK_UPDATED,
BOOK_COMPONENT_TYPE_UPDATED,
}
type CustomTag {
id: ID!
label: String!
tagType: String!
}
input CustomTagUpdateInput {
id: ID!
label: String
tagType: String
deleted: Boolean
}
input CustomTagAddInput {
label: String
tagType: String
}
extend type Query {
getCustomTags: [CustomTag]!
}
extend type Mutation {
addCustomTag(input: [CustomTagAddInput]!): [CustomTag]!
updateCustomTag(input: [CustomTagUpdateInput]!): [CustomTag]!
}
const logger = require('@pubsweet/logger')
const { CustomTag } = require('editoria-data-model/src').models
const getCustomTags = async (_, {}, ctx) => {
const customTags = await CustomTag.all()
if (!customTags) {
throw new Error(`CustomTags error: Could not fetch Tags`)
}
return customTags
}
const addCustomTag = async (_, { input }, ctx) => {
try {
Promise.all(
input.map(async tag => {
const { label, tagType } = tag
await new CustomTag({ label, tagType }).save()
}),
)
const customTags = await CustomTag.all()
return customTags
} catch (e) {
logger.error(e)
throw new Error(e)
}
}
const updateCustomTag = async (_, { input }, ctx) => {
try {
Promise.all(
input.map(async tag => {
const { id, deleted, tagType, label } = tag
await CustomTag.query().patchAndFetchById(id, {
label,
deleted,
tagType,
})
}),
)
logger.info(`Custom Tag component with id ${updateCustomTag.id} deleted`)
const customTags = await CustomTag.all()
return customTags
} catch (e) {
logger.error(e)
throw new Error(e)
}
}
module.exports = {
Query: {
getCustomTags,
},
Mutation: {
addCustomTag,
updateCustomTag,
},
}
const models = require('editoria-data-model')
module.exports = {
resolvers: require('./customTag.resolvers'),
typeDefs: require('../graphqlLoaderUtil')('customTag/customTag.graphql'),
model: models.customTag,
}
......@@ -2,6 +2,7 @@ const authorize = require('./authorize')
const book = require('./book')
const bookComponent = require('./bookComponent')
const bookCollection = require('./bookCollection')
const customTag = require('./customTag')
const division = require('./division')
const team = require('./team')
const user = require('./user')
......@@ -18,6 +19,7 @@ module.exports = {
book.typeDefs,
bookComponent.typeDefs,
bookCollection.typeDefs,
customTag.typeDefs,
division.typeDefs,
file.typeDefs,
team.typeDefs,
......@@ -29,6 +31,7 @@ module.exports = {
book.resolvers,
bookComponent.resolvers,
bookCollection.resolvers,
customTag.resolvers,
division.resolvers,
file.resolvers,
team.resolvers,
......
const Base = require('../editoriaBase')
const { string } = require('../helpers').schema
class CustomTag extends Base {
constructor(properties) {
super(properties)
this.type = 'customTag'
}
static get tableName() {
return 'custom_tag'
}
static get schema() {
return {
type: 'object',
required: ['label'],
properties: {
label: string,
tagType: string,
},
}
}
}
module.exports = CustomTag
const model = require('./customTag')
module.exports = {
model,
modelName: 'CustomTag',
}
create table custom_tag (
-- base
id uuid primary key,
type text not null,
created timestamp with time zone not null default current_timestamp,
updated timestamp with time zone,
-- editoria base
deleted boolean default false,
-- own
label text,
tag_type text
);
......@@ -8,6 +8,7 @@ const bookTranslation = require('./bookTranslation')
const file = require('./file')
const fileTranslation = require('./fileTranslation')
const template = require('./template')
const customTag = require('./customTag')
const division = require('./division')
const team = require('./team')
const user = require('./user')
......@@ -18,6 +19,7 @@ const loader = models.reduce((r, c) => Object.assign(r, c), {})
module.exports = {
book,
customTag,
team,
user,
bookCollection,
......@@ -40,6 +42,7 @@ module.exports = {
BookComponentState: bookComponentState.model,
BookComponentTranslation: bookComponentTranslation.model,
BookTranslation: bookTranslation.model,
CustomTag: customTag.model,
Division: division.model,
File: file.model,
FileTranslation: fileTranslation.model,
......
......@@ -6,12 +6,16 @@ import { adopt } from 'react-adopt'
import config from 'config'
import { withRouter } from 'react-router-dom'
import withModal from 'editoria-common/src/withModal'
import WaxPubsweet from './WaxPubsweet'
import statefull from './Statefull'
import {
getBookComponentQuery,
getCustomTagsQuery,
getWaxRulesQuery,
getUserTeamsQuery,
updateCustomTagMutation,
addCustomTagMutation,
updateBookComponentContentMutation,
updateBookComponentTrackChangesMutation,
renameBookComponentMutation,
......@@ -27,11 +31,14 @@ const mapper = {
statefull,
withModal,
getBookComponentQuery,
getCustomTagsQuery,
getWaxRulesQuery,
getUserTeamsQuery,
trackChangeSubscription,
lockChangeSubscription,
orderChangeSubscription,
updateCustomTagMutation,
addCustomTagMutation,
updateBookComponentContentMutation,
updateBookComponentTrackChangesMutation,
lockBookComponentMutation,
......@@ -52,8 +59,11 @@ const mapProps = args => ({
state: args.statefull.state,
setState: args.statefull.setState,
rules: get(args.getWaxRulesQuery, 'data.getWaxRules'),
tags: get(args.getCustomTagsQuery, 'data.getCustomTags'),
bookComponent: get(args.getBookComponentQuery, 'data.getBookComponent'),
teams: get(args.getUserTeamsQuery, 'data.teams'),
updateTags: args.updateCustomTagMutation.updateCustomTag,
addCustomTags: args.addCustomTagMutation.addCustomTag,
updateBookComponentContent:
args.updateBookComponentContentMutation.updateContent,
updateBookComponentTrackChanges:
......@@ -77,6 +87,7 @@ const mapProps = args => ({
loading: args.getBookComponentQuery.networkStatus === 1,
waxLoading: args.getWaxRulesQuery.networkStatus === 1,
teamsLoading: args.getUserTeamsQuery.networkStatus === 1,
tagsLoading: args.getCustomTagsQuery.networkStatus === 1,
refetching:
args.getBookComponentQuery.networkStatus === 4 ||
args.getBookComponentQuery.networkStatus === 2, // possible apollo bug
......@@ -97,10 +108,13 @@ const Connected = props => {
>
{({
bookComponent,
tags,
setState,
onUnlocked,
rules,
teams,
updateTags,
addCustomTags,
updateBookComponentContent,
updateBookComponentTrackChanges,
uploadFile,
......@@ -110,11 +124,13 @@ const Connected = props => {
loading,
waxLoading,
teamsLoading,
tagsLoading,
}) => {
const user = Object.assign({}, currentUser, {
color: getUserWithColor(teams),
})
if (loading || waxLoading || teamsLoading) return null
if (loading || waxLoading || teamsLoading || tagsLoading) return null
let editing
const lock = get(bookComponent, 'lock')
if (lock && lock.userId !== currentUser.id) {
......@@ -143,8 +159,11 @@ const Connected = props => {
renameBookComponent={renameBookComponent}
rules={rules}
setState={setState}
tags={tags}
teamsLoading={teamsLoading}
unlockBookComponent={unlockBookComponent}
updateCustomTags={updateTags}
addCustomTags={addCustomTags}
updateBookComponentContent={updateBookComponentContent}
updateBookComponentTrackChanges={updateBookComponentTrackChanges}
uploadFile={uploadFile}
......
......@@ -5,6 +5,8 @@ import styled from 'styled-components'
import config from 'config'
import Wax from 'wax-editor-react'
import WaxHeader from './WaxHeader'
// import { getFragment } from 'pubsweet-client/src/actions/fragments'
import { th } from '@pubsweet/ui-toolkit'
const Container = styled.div`
display: flex;
......@@ -159,8 +161,29 @@ export class WaxPubsweet extends React.Component {
bookComponent,
updateBookComponentTrackChanges,
renameBookComponent,
updateCustomTags,
addCustomTags,
} = this.props
const { trackChanges, title } = patch
const { trackChanges, title, tags } = patch
if (tags) {
const addTags = tags.filter(tag => !tag.id)
const updateTags = tags.filter(tag => tag.id)
if (addTags.length > 0) {
addCustomTags({
variables: {
input: addTags,
},
})
}
if (updateTags.length > 0) {
updateCustomTags({
variables: {
input: updateTags,
},
})
}
}
if (trackChanges !== undefined) {
return updateBookComponentTrackChanges({
......@@ -339,6 +362,20 @@ export class WaxPubsweet extends React.Component {
if (get(bookComponent, 'componentType') === 'chapter') {
chapterNumber = get(bookComponent, 'componentTypeOrder')
}
let header
if (chapterNumber) {
header = (
<BookTitle>{`${bookComponent.bookTitle} - Chapter ${chapterNumber}. ${
bookComponent.title
}`}</BookTitle>
)
} else {
header = (
<BookTitle>{`${bookComponent.bookTitle} - ${
bookComponent.title
}`}</BookTitle>
)
}
return (
<Container>
......
import React from 'react'
import { Mutation } from 'react-apollo'
import gql from 'graphql-tag'
const ADD_CUSTOM_TAG = gql`
mutation AddCustomTag($input: [CustomTagAddInput]!) {
addCustomTag(input: $input) {
id
label
tagType
}
}
`
const addCustomTagMutation = props => {
const { render } = props
return (
<Mutation mutation={ADD_CUSTOM_TAG}>
{addCustomTag => render({ addCustomTag })}
</Mutation>
)
}
export default addCustomTagMutation
import React from 'react'
import { Query } from 'react-apollo'
import gql from 'graphql-tag'
const GET_CUSTOM_TAGS = gql`
query GetCustomTags {
getCustomTags {
id
label
tagType
}
}
`
const getCustomTagsQuery = props => {
const { render } = props
return (
<Query fetchPolicy="cache-and-network" query={GET_CUSTOM_TAGS}>
{render}
</Query>
)
}
export { GET_CUSTOM_TAGS }
export default getCustomTagsQuery
export { default as getBookComponentQuery } from './getBookComponent'
export { default as getCustomTagsQuery } from './getCustomTags'
export { default as getWaxRulesQuery } from './getWaxRules'
export { default as getUserTeamsQuery } from './getUserTeams'
export { default as updateBookComponentContentMutation } from './updateContent'
export { default as updateCustomTagMutation } from './updateCustomTag'
export { default as addCustomTagMutation } from './addCustomTag'
export {
default as updateBookComponentTrackChangesMutation,
} from './updateTrackChanges'
......
import React from 'react'
import { Mutation } from 'react-apollo'
import gql from 'graphql-tag'
const UPDATE_CUSTOM_TAG = gql`
mutation UpdateCustomTag($input: [CustomTagUpdateInput]!) {
updateCustomTag(input: $input) {
id
label
tagType
}
}
`
const updateCustomTagMutation = props => {
const { render } = props
return (
<Mutation mutation={UPDATE_CUSTOM_TAG}>
{updateCustomTag => render({ updateCustomTag })}
</Mutation>
)
}
export default updateCustomTagMutation
......@@ -12419,7 +12419,19 @@ promise@^7.1.1:
dependencies:
asap "~2.0.3"
prompt@^1.0.0, prompt@flatiron/prompt#1c95d1d8d333b5fbc13fa5f0619f3dcf0d514f87:
prompt@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/prompt/-/prompt-1.0.0.tgz#8e57123c396ab988897fb327fd3aedc3e735e4fe"
integrity sha1-jlcSPDlquYiJf7Mn/Trtw+c15P4=
dependencies:
colors "^1.1.2"
pkginfo "0.x.x"
read "1.0.x"
revalidator "0.1.x"
utile "0.3.x"
winston "2.1.x"
prompt@flatiron/prompt#1c95d1d8d333b5fbc13fa5f0619f3dcf0d514f87:
version "1.0.0"
resolved "https://codeload.github.com/flatiron/prompt/tar.gz/1c95d1d8d333b5fbc13fa5f0619f3dcf0d514f87"
dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment