Commit 632e4714 authored by Giannis Kopanas's avatar Giannis Kopanas Committed by Alexandros Georgantas

feat(applicationmanager): get live updates subscriptions

parent 8f9932c7
......@@ -22,3 +22,6 @@ extend type Mutation {
updateApplicationParameters(input: updateParametersInput!): applicationParameter!
}
extend type Subscription {
updateApplicationParameters: [applicationParameter!]!
}
const logger = require('@pubsweet/logger')
const { ApplicationParameter } = require('editoria-data-model/src').models
const pubsweetServer = require('pubsweet-server')
const { UPDATE_APPLICATION_PARAMETERS } = require('./consts')
const { pubsubManager } = pubsweetServer
const getApplicationParameters = async (_, args, ctx) => {
const { context, area } = args
const parameters = await ApplicationParameter.query()
......@@ -21,14 +28,25 @@ const getApplicationParameters = async (_, args, ctx) => {
}
const updateApplicationParameters = async (_, { input }, ctx) => {
const { context, area, config } = input
try {
const pubsub = await pubsubManager.getPubsub()
const parameter = await ApplicationParameter.query().findOne({
context,
area,
})
const parameter = await ApplicationParameter.query().findOne({
context,
area,
})
const updatedParameter = await parameter.$query().updateAndFetch({ config })
const updatedParameter = await parameter.$query().updateAndFetch({ config })
return updatedParameter
const applicationParameters = await ApplicationParameter.query()
pubsub.publish(UPDATE_APPLICATION_PARAMETERS, {
updateApplicationParameters: applicationParameters,
})
return updatedParameter
} catch (e) {
logger.error(e)
throw new Error(e)
}
}
module.exports = {
......@@ -38,4 +56,12 @@ module.exports = {
Mutation: {
updateApplicationParameters,
},
Subscription: {
updateApplicationParameters: {
subscribe: async () => {
const pubsub = await pubsubManager.getPubsub()
return pubsub.asyncIterator(UPDATE_APPLICATION_PARAMETERS)
},
},
},
}
const UPDATE_APPLICATION_PARAMETERS = 'UPDATE_APPLICATION_PARAMETERS'
module.exports = {
UPDATE_APPLICATION_PARAMETERS,
}
......@@ -4,8 +4,8 @@ import React from 'react'
import { get, findIndex, map } from 'lodash'
import { adopt } from 'react-adopt'
import { withRouter } from 'react-router-dom'
import BookBuilder from './BookBuilder'
import withModal from 'editoria-common/src/withModal'
import BookBuilder from './BookBuilder'
import statefull from './Statefull'
import {
getBookQuery,
......@@ -298,10 +298,10 @@ const Connected = props => {
return (
<BookBuilder
addBookComponent={addBookComponent}
addBookComponents={addBookComponents}
applicationParameter={applicationParameter}
state={state}
setState={setState}
addBookComponents={addBookComponents}
onTeamManager={onTeamManager}
onError={onError}
onWarning={onWarning}
......
......@@ -4,8 +4,13 @@ import { adopt } from 'react-adopt'
import CurrentUserQuery from './queries/currentUser'
import ApplicationParameterQuery from './queries/applicationParameter'
import { updateApplicationParametersSubscription } from './queries/applicationParameterSubscriptions'
const mapper = { CurrentUserQuery, ApplicationParameterQuery }
const mapper = {
CurrentUserQuery,
ApplicationParameterQuery,
updateApplicationParametersSubscription,
}
const mapProps = args => ({
currentUser: get(args.CurrentUserQuery, 'data.currentUser'),
......
import React from 'react'
import { Subscription } from 'react-apollo'
import gql from 'graphql-tag'
const UPDATE_APPLICATION_PARAMETERS_SUBSCRIPTION = gql`
subscription UpdateApplicationParameters {
updateApplicationParameters {
id
context
area
config
}
}
`
const updateApplicationParametersSubscription = props => {
const { render, ApplicationParameterQuery } = props
const triggerRefetch = () => {
ApplicationParameterQuery.refetch()
}
return (
<Subscription
onSubscriptionData={triggerRefetch}
subscription={UPDATE_APPLICATION_PARAMETERS_SUBSCRIPTION}
>
{render}
</Subscription>
)
}
export { updateApplicationParametersSubscription }
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