Skip to content

Commit

Permalink
Add TOC modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Joozty committed Oct 27, 2020
1 parent 657c15b commit 3b1399b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 17 deletions.
3 changes: 2 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ const nextConfig = {
'store': path.join(__dirname, 'store'),
'api': path.join(__dirname, 'api'),
'config': path.join(__dirname, 'config.js'),
'@sentry/node': config.isServer ? '@sentry/node' : '@sentry/browser',
'pages': path.join(__dirname, 'pages'),

'@sentry/node': config.isServer ? '@sentry/node' : '@sentry/browser',
'react': path.join(__dirname, 'node_modules', 'react'),
'react-dom': path.join(__dirname, 'node_modules', 'react-dom'),
})
Expand Down
29 changes: 16 additions & 13 deletions pages/_app/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react'
import NextApp from 'next/app'
import { withRouter } from 'next/router'

import '@oacore/design/lib/index.css'
import './global.css'

import TermsModal from 'templates/terms-modal'
import { UnauthorizedError } from 'api/errors'
import { AuthorizationError, AccessError, NotFoundError } from 'store/errors'
import { logPageView } from 'utils/analytics'
Expand Down Expand Up @@ -149,30 +150,32 @@ class App extends NextApp {
const { Component, pageProps, router } = this.props
const { isAuthorized } = this.state
const pathname = router.asPath
const variant =
isAuthorized && !isRouteWithoutStore(pathname) ? 'internal' : 'public'
const isPublicRoute = isRouteWithoutStore(pathname)
const variant = isAuthorized && !isPublicRoute ? 'internal' : 'public'

if (!isAuthorized) {
if (!isAuthorized || isPublicRoute) {
return (
<Application
dataProvider={undefined}
pathname={pathname}
variant={variant}
>
{isRouteWithoutStore(pathname) ? <Component {...pageProps} /> : null}
{isPublicRoute ? <Component {...pageProps} /> : null}
</Application>
)
}

return (
<Application
dataProvider={store.dataProvider}
pathname={pathname}
variant={variant}
isAuthenticated
>
<Component {...pageProps} />
</Application>
<TermsModal>
<Application
dataProvider={store.dataProvider}
pathname={pathname}
variant={variant}
isAuthenticated
>
<Component {...pageProps} />
</Application>
</TermsModal>
)
}

Expand Down
38 changes: 35 additions & 3 deletions store/data-provider/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { action, observable } from 'mobx'
import { action, observable, computed } from 'mobx'

import Resource from '../resource'
import Works from './works'
import DepositDates from './deposit-dates'
import DOI from './doi'
import Issues from '../issues'
import apiRequest from '../../api'
import { NotFoundError as NetworkNotFoundError } from '../../api/errors'
import { NotFoundError } from '../errors'

import { NotFoundError as NetworkNotFoundError } from 'api/errors'
import apiRequest from 'api'

class DataProvider extends Resource {
@observable id = ''

@observable name = ''

@observable acceptedTerms = false

@observable irus = null

@observable rioxx = null
Expand All @@ -38,10 +41,20 @@ class DataProvider extends Resource {

@observable location = {}

@observable revalidatingPolicies = false

@observable policies = null

@computed
get areTermsAccepted() {
return this.policies?.some((p) => p?.type === 'terms' && p?.acceptedDate)
}

@action retrieve() {
super.retrieve().then(
() => {
this.reset()
this.retrievePolicies()
this.retrieveStatistics()
this.retrievePluginConfig()
this.retrieveIrusStats()
Expand Down Expand Up @@ -115,6 +128,25 @@ class DataProvider extends Resource {
}
}

@action async retrievePolicies() {
const { data } = await this.options.request(
`/data-providers/${this.id}/policies`
)
this.policies = data
}

@action async acceptTerms() {
this.revalidatingPolicies = true
try {
await this.options.request(`/data-providers/${this.id}/policies/terms`, {
method: 'POST',
})
await this.retrievePolicies()
} catch (error) {
this.revalidatingPolicies = true
}
}

@action
reset() {
this.irus = null
Expand Down
41 changes: 41 additions & 0 deletions templates/terms-modal/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'
import { Button, Modal } from '@oacore/design'

import styles from './styles.module.css'

import TermsPage from 'pages/terms'
import { withGlobalStore } from 'store'

const TermsModal = ({ store, children }) => (
<>
{children}
{!store.user.superAdmin &&
store.dataProvider.policies !== null &&
!store.dataProvider.areTermsAccepted && (
<Modal
aria-label="Terms and Conditions"
hideManually
className={styles.modal}
>
<Modal.Content className={styles.content}>
<TermsPage />
</Modal.Content>
<Modal.Footer className={styles.footer}>
<span className={styles.nameText}>
Agreeing as {store.user.email}
</span>
<Button
disabled={store.dataProvider.revalidatingPolicies}
variant="contained"
onClick={() => {
store.dataProvider.acceptTerms()
}}
>
Accept and continue
</Button>
</Modal.Footer>
</Modal>
)}
</>
)
export default withGlobalStore(TermsModal)
19 changes: 19 additions & 0 deletions templates/terms-modal/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.modal {
max-width: 50rem;
}

.content {
display: grid;
grid-template-columns: auto fit-content(80ch) auto;
}

.footer {
display: flex;
align-items: center;
justify-content: flex-end;
}

.name-text {
margin-right: 1rem;
font-weight: 600;
}

0 comments on commit 3b1399b

Please sign in to comment.