Skip to content

Commit c11ba5b

Browse files
committed
CORE-829 Add login gate
[CORE-829]
1 parent a3dd806 commit c11ba5b

11 files changed

+93
-5
lines changed

src/app/components/NavBar/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ const LoggedOutState: FunctionComponent<{currentPath: string}> = ({currentPath})
151151
</Styled.Link>}
152152
</FormattedMessage>;
153153

154+
// tslint:disable-next-line:variable-name
155+
export const ConnectedLoginButton = connect(
156+
(state: AppState) => ({
157+
currentPath: selectNavigation.pathname(state),
158+
})
159+
)(LoggedOutState);
160+
154161
interface NavigationBarProps {
155162
user?: User;
156163
loggedOut: boolean;

src/app/content/components/Content.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from './constants';
2929
import ContentPane from './ContentPane';
3030
import ContentWarning from './ContentWarning';
31+
import LoginGate from './LoginGate';
3132
import LabsCTA from './LabsCall';
3233
import NudgeStudyTools from './NudgeStudyTools';
3334
import Page from './Page';
@@ -103,11 +104,13 @@ const Content = ({mobileExpanded, book}: {mobileExpanded: boolean; book: Book})
103104
<Topbar />
104105
<ContentNotifications mobileExpanded={mobileExpanded} />
105106
<ContentWarning book={book} />
106-
<Page>
107-
<PrevNextBar />
108-
<LabsCTA />
109-
<BuyBook book={book} />
110-
</Page>
107+
<LoginGate book={book}>
108+
<Page>
109+
<PrevNextBar />
110+
<LabsCTA />
111+
<BuyBook book={book} />
112+
</Page>
113+
</LoginGate>
111114
<Attribution />
112115
<Footer />
113116
</ContentPane>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import LoginGate from './LoginGate';
3+
import renderer from 'react-test-renderer';
4+
import { book as archiveBook } from '../../../test/mocks/archiveLoader';
5+
import { mockCmsBook } from '../../../test/mocks/osWebLoader';
6+
import { formatBookData } from '../utils';
7+
import TestContainer from '../../../test/TestContainer';
8+
9+
const dummyBook = {
10+
...formatBookData(archiveBook, mockCmsBook),
11+
require_login_message_text: 'some warning text',
12+
};
13+
14+
describe('LoginGate', () => {
15+
it('renders when not authenticated', () => {
16+
const component = renderer.create(<TestContainer>
17+
<LoginGate book={dummyBook}>
18+
</LoginGate>
19+
</TestContainer>);
20+
21+
expect(component.root.findByType('a').props.href).toBe('/accounts/login?r=/');
22+
});
23+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import { Book } from '../types';
3+
import { hasOSWebData } from '../guards';
4+
import { ConnectedLoginButton } from '../../components/NavBar';
5+
import { user } from '../../auth/selectors';
6+
import { useSelector } from 'react-redux';
7+
import styled from 'styled-components/macro';
8+
9+
// tslint:disable-next-line:variable-name
10+
const WarningDiv = styled.div`
11+
display: flex;
12+
flex-direction: column;
13+
font-size: 1.8rem;
14+
padding: 8rem 1.5rem;
15+
16+
> div {
17+
margin: 0 auto;
18+
max-width: 60rem;
19+
20+
a {
21+
justify-self: center;
22+
}
23+
}
24+
`;
25+
26+
export default function LoginGate({
27+
book,
28+
children,
29+
}: React.PropsWithChildren<{ book: Book }>) {
30+
const authenticated = !!useSelector(user);
31+
32+
if (
33+
authenticated ||
34+
!hasOSWebData(book) ||
35+
!book.require_login_message_text
36+
) {
37+
return <>{children}</>;
38+
}
39+
return (
40+
<WarningDiv>
41+
<div>
42+
{book.require_login_message_text}
43+
<ConnectedLoginButton />
44+
</div>
45+
</WarningDiv>
46+
);
47+
}

src/app/content/hooks/locationChange.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ describe('contentRouteHookBody', () => {
215215
promote_image: null,
216216
publish_date: '2012-06-21',
217217
content_warning_text: '',
218+
require_login_message_text: '',
218219
id: 72,
219220
};
220221

src/app/content/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export interface BookWithOSWebData extends VersionedArchiveBookWithConfig {
8686
}
8787
};
8888
content_warning_text: string | null;
89+
require_login_message_text: string | null;
8990
publish_date: string;
9091
amazon_link: string;
9192
polish_site_link: string;

src/app/content/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const formatBookData = <O extends OSWebBook | undefined>(
106106
return {
107107
...pickArchiveFields(archiveBook),
108108
content_warning_text: osWebBook.content_warning_text,
109+
require_login_message_text: osWebBook.require_login_message_text,
109110
amazon_link: osWebBook.amazon_link,
110111
polish_site_link: osWebBook.polish_site_link,
111112
authors: osWebBook.authors,

src/app/content/utils/seoUtils.spec.data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const mockOsWebBook: OSWebBook = {
7676
amazon_link: '',
7777
polish_site_link: '',
7878
content_warning_text: '',
79+
require_login_message_text: '',
7980
id: 72,
8081
};
8182

src/gateways/createOSWebLoader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface OSWebBook {
2222
amazon_link: string;
2323
polish_site_link: string;
2424
content_warning_text: string | null;
25+
require_login_message_text: string | null;
2526
id: number;
2627
}
2728

@@ -44,6 +45,7 @@ export const fields = [
4445
'book_subjects',
4546
'book_categories',
4647
'content_warning_text',
48+
'require_login_message_text',
4749
'id',
4850
].join(',');
4951

src/test/fixtures/apps/cms/api/v2/pages/%3Ftype%3Dbooks.Book%26fields%3Dcnx_id%2Cauthors%2Cpublish_date%2Ccover_color%2Camazon_link%2Cbook_state%2Cpromote_image%2Cbook_subjects%2Cbook_categories%2Ccontent_warning_text%2Cid%26slug%3Dbook-slug-1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"publish_date": "2012-06-21",
1818
"book_state": "live",
1919
"content_warning_text": "sensitive material is covered",
20+
"require_login_message_text": "you have to log in",
2021
"id": 72
2122
}
2223
]

0 commit comments

Comments
 (0)