Commit 9e675afe authored by Giannis Kopanas's avatar Giannis Kopanas Committed by Alexandros Georgantas

feat(header): easier navigation between bookcomponents

parent 3eb1addd
......@@ -45,6 +45,8 @@ type BookComponent {
divisionId: ID!
divisionType: String
lock: Lock
nextBookComponent: BookComponent
prevBookComponent: BookComponent
pagination: Pagination!
archived: Boolean!
workflowStages: [WorkFlowItem]
......@@ -108,7 +110,7 @@ extend type Subscription {
bookComponentWorkflowUpdated: BookComponent!
bookComponentPaginationUpdated: BookComponent!
bookComponentTrackChangesUpdated: BookComponent!
bookComponentLockUpdated: BookComponent!
bookComponentLockUpdated(bookComponentIds: [ID]): BookComponent!
bookComponentTitleUpdated: BookComponent!
bookComponentContentUpdated: BookComponent!
bookComponentTypeUpdated: BookComponent!
......
const findIndex = require('lodash/findIndex')
const find = require('lodash/find')
const concat = require('lodash/concat')
const flattenDeep = require('lodash/flattenDeep')
const groupBy = require('lodash/groupBy')
const pullAll = require('lodash/pullAll')
// const map = require('lodash/flatMapDepth')
......@@ -12,6 +14,7 @@ const assign = require('lodash/assign')
const config = require('config')
const logger = require('@pubsweet/logger')
const pubsweetServer = require('pubsweet-server')
const { withFilter } = require('graphql-subscriptions')
const {
BookComponentState,
......@@ -38,6 +41,20 @@ const {
const { pubsubManager } = pubsweetServer
const getOrderedBookComponents = async bookComponent => {
const divisions = await Division.findByField(
'bookId',
bookComponent.bookId,
).orderByRaw(
`label='${'Frontmatter'}' desc, label='${'Body'}' desc, label='${'Backmatter'}' desc`,
)
const orderedComponent = flattenDeep(
concat([...map(divisions, division => division.bookComponents)]),
)
return orderedComponent
}
const getBookComponent = async (_, { id }, ctx) => {
const bookComponent = await BookComponent.findById(id)
if (!bookComponent) {
......@@ -231,7 +248,7 @@ const renameBookComponent = async (_, { input }, ctx) => {
pubsub.publish(BOOK_COMPONENT_TITLE_UPDATED, {
bookComponentTitleUpdated: updatedBookComponent,
})
console.log('hello rename',updatedBookComponent)
return updatedBookComponent
}
......@@ -599,17 +616,44 @@ module.exports = {
},
async bookTitle(bookComponent, _, ctx) {
const book = await Book.findById(bookComponent.bookId)
console.log('book', book, bookComponent)
// console.log('book', book, bookComponent)
const bookTranslation = await BookTranslation.query()
.where('bookId', book.id)
.andWhere('languageIso', 'en')
console.log('booktit', bookTranslation)
// console.log('booktit', bookTranslation)
return bookTranslation[0].title
},
async nextBookComponent(bookComponent, _, ctx) {
const orderedComponent = await getOrderedBookComponents(bookComponent)
const current = orderedComponent.findIndex(
comp => comp === bookComponent.id,
)
try {
const next = orderedComponent[current + 1]
const nextBookComponent = await BookComponent.findById(next)
return nextBookComponent
} catch (e) {
return null
}
},
async prevBookComponent(bookComponent, _, ctx) {
const orderedComponent = await getOrderedBookComponents(bookComponent)
const current = orderedComponent.findIndex(
comp => comp === bookComponent.id,
)
try {
const prev = orderedComponent[current - 1]
const prevBookComponent = await BookComponent.findById(prev)
return prevBookComponent
} catch (e) {
return null
}
},
async divisionType(bookComponent, _, ctx) {
console.log('in here')
// console.log('in here')
const division = await Division.findById(bookComponent.divisionId)
console.log('division', division)
// console.log('division', division)
return division.label
},
async divisionId(bookComponent, _, ctx) {
......@@ -753,9 +797,15 @@ module.exports = {
},
},
bookComponentLockUpdated: {
subscribe: async () => {
async subscribe(rootValue, args, context) {
const pubsub = await pubsubManager.getPubsub()
return pubsub.asyncIterator(BOOK_COMPONENT_LOCK_UPDATED)
return withFilter(
() => pubsub.asyncIterator(BOOK_COMPONENT_LOCK_UPDATED),
(payload, variables) =>
variables.bookComponentIds.includes(
payload.bookComponentLockUpdated.id,
),
)(rootValue, args, context)
},
},
bookComponentTypeUpdated: {
......
......@@ -13,6 +13,7 @@
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"editoria-data-model": "^0.1.2",
"apollo-server": "*",
"highlight.js": "^9.13.1",
"html-epub": "git+https://git@gitlab.coko.foundation/pubsweet/html-epub#updated-libtidy",
"mkdirp": "^0.5.1",
......
......@@ -40,8 +40,8 @@ const BOOK_COMPONENT_WORKFLOW_UPDATED_SUBSCRIPTION = gql`
`
const BOOK_COMPONENT_LOCK_UPDATED_SUBSCRIPTION = gql`
subscription BookComponentLockUpdated {
bookComponentLockUpdated {
subscription BookComponentLockUpdated($bookComponentIds: [ID]!) {
bookComponentLockUpdated(bookComponentIds: $bookComponentIds) {
id
}
}
......@@ -239,10 +239,22 @@ const lockChangeSubscription = props => {
refetch()
}
if (!getBookQuery.data.getBook) {
return null
}
const { divisions } = getBookQuery.data.getBook
const subscribeToBookComponents = []
divisions.forEach(division => {
division.bookComponents.forEach(item => {
subscribeToBookComponents.push(item.id)
})
})
return (
<Subscription
onSubscriptionData={triggerRefetch}
subscription={BOOK_COMPONENT_LOCK_UPDATED_SUBSCRIPTION}
variables={{ bookComponentIds: subscribeToBookComponents }}
>
{render}
</Subscription>
......
......@@ -5,8 +5,8 @@ import { get, sortBy } from 'lodash'
import { adopt } from 'react-adopt'
import config from 'config'
import { withRouter } from 'react-router-dom'
import WaxPubsweet from './WaxPubsweet'
import withModal from 'editoria-common/src/withModal'
import WaxPubsweet from './WaxPubsweet'
import statefull from './Statefull'
import {
getBookComponentQuery,
......@@ -20,6 +20,7 @@ import {
uploadFileMutation,
trackChangeSubscription,
lockChangeSubscription,
orderChangeSubscription,
} from './queries'
const mapper = {
......@@ -36,6 +37,7 @@ const mapper = {
renameBookComponentMutation,
trackChangeSubscription,
lockChangeSubscription,
orderChangeSubscription,
}
const getUserWithColor = (teams = []) => {
......@@ -127,16 +129,17 @@ const Connected = props => {
return (
<WaxPubsweet
bookComponent={bookComponent}
setState={setState}
onUnlocked={onUnlocked}
editing={editing}
bookComponentId={bookComponentId}
config={config}
editing={editing}
history={history}
key={bookComponent.id}
loading={loading}
lockBookComponent={lockBookComponent}
onUnlocked={onUnlocked}
renameBookComponent={renameBookComponent}
rules={rules}
setState={setState}
teamsLoading={teamsLoading}
unlockBookComponent={unlockBookComponent}
updateBookComponentContent={updateBookComponentContent}
......
This diff is collapsed.
......@@ -28,6 +28,16 @@ const GET_BOOK_COMPONENT = gql`
isAdmin
surname
}
nextBookComponent {
id
title
bookId
}
prevBookComponent {
id
title
bookId
}
content
}
}
......
......@@ -14,4 +14,5 @@ export {
trackChangeSubscription,
titleChangeSubscription,
lockChangeSubscription,
orderChangeSubscription,
} from './waxPubsweetSubscriptions'
......@@ -12,12 +12,21 @@ const BOOK_COMPONENT_TRACK_CHANGES_UPDATED_SUBSCRIPTION = gql`
`
const BOOK_COMPONENT_LOCK_UPDATED_SUBSCRIPTION = gql`
subscription BookComponentLockUpdated {
bookComponentLockUpdated {
subscription BookComponentLockUpdated($bookComponentIds: [ID]!) {
bookComponentLockUpdated(bookComponentIds: $bookComponentIds) {
id
}
}
`
const BOOK_COMPONENT_ORDER_UPDATED_SUBSCRIPTION = gql`
subscription BookComponentOrderUpdated {
bookComponentOrderUpdated {
id
}
}
`
const BOOK_COMPONENT_TITLE_UPDATED_SUBSCRIPTION = gql`
subscription BookComponentTitleUpdated {
bookComponentTitleUpdated {
......@@ -48,6 +57,25 @@ const trackChangeSubscription = props => {
)
}
const orderChangeSubscription = props => {
const { render, getBookComponentQuery, statefull } = props
const { pauseUpdates } = statefull
const { refetch } = getBookComponentQuery
const triggerRefetch = () => {
if (pauseUpdates) return
refetch()
}
return (
<Subscription
onSubscriptionData={triggerRefetch}
subscription={BOOK_COMPONENT_ORDER_UPDATED_SUBSCRIPTION}
>
{render}
</Subscription>
)
}
const titleChangeSubscription = props => {
const { render, getBookComponentQuery, statefull } = props
const { pauseUpdates } = statefull
......@@ -75,11 +103,16 @@ const lockChangeSubscription = props => {
if (pauseUpdates) return
refetch()
}
if (!getBookComponentQuery.data.getBookComponent) {
return null
}
const { id } = getBookComponentQuery.data.getBookComponent
return (
<Subscription
onSubscriptionData={triggerRefetch}
subscription={BOOK_COMPONENT_LOCK_UPDATED_SUBSCRIPTION}
variables={{ bookComponentIds: [id] }}
>
{render}
</Subscription>
......@@ -89,4 +122,5 @@ export {
trackChangeSubscription,
titleChangeSubscription,
lockChangeSubscription,
orderChangeSubscription,
}
This diff is collapsed.
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