Skip to content

Commit 4fd8b26

Browse files
committed
Made linting changes.
1 parent c62119e commit 4fd8b26

12 files changed

+351
-165
lines changed

packages/deep-economic-analyzer/deep-economic-analyzer.js

Lines changed: 98 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Router} from '@vaadin/router';
77
import {css, html, LitElement} from 'lit';
88
import {i18nMixin, translate} from 'lit-element-i18n';
99

10-
import {getUser} from '@thinkdeep/deep-economic-analyzer/user.js';
10+
import {user, premium} from '@thinkdeep/deep-economic-analyzer/user.js';
1111
import {initApolloClient} from './graphql/client.js';
1212

1313
/**
@@ -20,8 +20,14 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
2020
static get properties() {
2121
return {
2222
companyName: {type: String},
23+
24+
/** Window location */
2325
location: {type: Object},
26+
2427
user: {type: Object},
28+
29+
/** Indicates whether the logged in user is a premium user */
30+
premiumAccount: {type: Boolean},
2531
};
2632
}
2733

@@ -33,6 +39,7 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
3339
this.companyName = '';
3440
this.location = Router.location;
3541
this.user = {};
42+
this.premiumAccount = false;
3643
}
3744

3845
/**
@@ -56,8 +63,9 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
5663

5764
this.companyName = translate('translations:companyName');
5865

59-
this.user = await getUser();
60-
await initApolloClient();
66+
this.user = await user();
67+
this.premiumAccount = premium(this.user);
68+
await initApolloClient(this.user);
6169

6270
const routes = [
6371
{
@@ -81,6 +89,19 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
8189
];
8290

8391
if (this.user.loggedIn) {
92+
if (this.premiumAccount) {
93+
routes.push({
94+
path: `/${translate('translations:summaryPageLabel')}`,
95+
name: translate('translations:summaryPageLabel'),
96+
component: 'deep-analyzer-page-summary',
97+
action: async () => {
98+
await import(
99+
'@thinkdeep/deep-economic-analyzer/deep-analyzer-page-summary.js'
100+
);
101+
},
102+
});
103+
}
104+
84105
routes.push({
85106
path: `/${translate('translations:logoutPageLabel')}`,
86107
name: translate('translations:logoutPageLabel'),
@@ -89,16 +110,6 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
89110
await this.user.logout();
90111
},
91112
});
92-
routes.push({
93-
path: `/${translate('translations:summaryPageLabel')}`,
94-
name: translate('translations:summaryPageLabel'),
95-
component: 'deep-analyzer-page-summary',
96-
action: async () => {
97-
await import(
98-
'@thinkdeep/deep-economic-analyzer/deep-analyzer-page-summary.js'
99-
);
100-
},
101-
});
102113
}
103114

104115
// NOTE: It appears that the vaadin router implements a sort of chain-of-responsibility in that it
@@ -168,51 +179,91 @@ export class DeepEconomicAnalyzer extends i18nMixin(LitElement) {
168179
*/
169180
render() {
170181
return html`
171-
${this.styles}
182+
${DeepEconomicAnalyzer.styles}
172183
173184
<mwc-top-app-bar-fixed>
174185
<div slot="title">${this.companyName}</div>
175186
176-
<mwc-icon-button
177-
@click=${() => Router.go('/')}
178-
icon="home"
179-
aria-label="${translate('translations:homePageLabel')}"
180-
>
181-
</mwc-icon-button>
182-
183-
${this.user.loggedIn
184-
? html`
185-
<mwc-icon-button
186-
@click=${() =>
187-
Router.go('/' + translate('translations:summaryPageLabel'))}
188-
icon="space_dashboard"
189-
aria-label="${translate('translations:summaryPageLabel')}"
190-
>
191-
</mwc-icon-button>
192-
<mwc-icon-button
193-
@click="${() =>
194-
Router.go('/' + translate('translations:logoutPageLabel'))}"
195-
icon="logout"
196-
aria-label="${translate('translations:logoutPageLabel')}"
197-
>
198-
</mwc-icon-button>
199-
`
200-
: html`
201-
<mwc-icon-button
202-
@click="${() =>
203-
Router.go('/' + translate('translations:loginPageLabel'))}"
204-
icon="login"
205-
aria-label="${translate('translations:loginPageLabel')}"
206-
>
207-
</mwc-icon-button>
208-
`}
187+
${this.menuItems(this.user, this.premiumAccount)}
209188
</mwc-top-app-bar-fixed>
210189
211190
<main id="content"></main>
212191
213192
<deep-footer .companyName="${this.companyName}"></deep-footer>
214193
`;
215194
}
195+
196+
/**
197+
* Return the markup needed to render the menu items.
198+
*
199+
* @param {Object} user Current user.
200+
* @param {Boolean} premiumAccount Whether the current user is a premium user.
201+
*
202+
* @return {TemplateResult} Template definining the menu items.
203+
*/
204+
menuItems(user, premiumAccount) {
205+
return html` <mwc-icon-button
206+
@click=${() => Router.go('/')}
207+
icon="home"
208+
aria-label="${translate('translations:homePageLabel')}"
209+
>
210+
</mwc-icon-button>
211+
212+
${user && user.loggedIn
213+
? this.protectedMenuItems(premiumAccount)
214+
: this.standardMenuItems()}`;
215+
}
216+
217+
/**
218+
* Get the markup for protected menu items.
219+
* @param {Boolean} premiumAccount Whether the current user is a premium user.
220+
* @return {TemplateResult} Template of the menu items.
221+
*/
222+
protectedMenuItems(premiumAccount) {
223+
return html`
224+
${premiumAccount ? this.premiumMenuItems() : ''}
225+
226+
<mwc-icon-button
227+
@click="${() =>
228+
Router.go('/' + translate('translations:logoutPageLabel'))}"
229+
icon="logout"
230+
aria-label="${translate('translations:logoutPageLabel')}"
231+
>
232+
</mwc-icon-button>
233+
`;
234+
}
235+
236+
/**
237+
* Get the markup for premium menu items.
238+
* @return {TemplateResult} Template containing menu items.
239+
*/
240+
premiumMenuItems() {
241+
return html`
242+
<mwc-icon-button
243+
@click=${() =>
244+
Router.go('/' + translate('translations:summaryPageLabel'))}
245+
icon="space_dashboard"
246+
aria-label="${translate('translations:summaryPageLabel')}"
247+
>
248+
</mwc-icon-button>
249+
`;
250+
}
251+
252+
/**
253+
* Get the markup for standard menu items.
254+
* @return {TemplateResult} Template containing menu items.
255+
*/
256+
standardMenuItems() {
257+
return html`
258+
<mwc-icon-button
259+
@click="${() =>
260+
Router.go('/' + translate('translations:loginPageLabel'))}"
261+
icon="login"
262+
aria-label="${translate('translations:loginPageLabel')}"
263+
>
264+
</mwc-icon-button>
265+
`;
266+
}
216267
}
217268

218269
customElements.define('deep-economic-analyzer', DeepEconomicAnalyzer);

packages/deep-economic-analyzer/deep-site-configuration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {ApolloMutationController} from '@apollo-elements/core';
22
import {GetOrCreateSiteConfiguration} from './graphql/GetOrCreateSiteConfiguration.mutation.graphql';
33
import {UpdateConfiguration} from './graphql/UpdateConfiguration.mutation.graphql';
44
import {LitElement} from 'lit';
5-
import {getUser} from './user.js';
5+
import {user} from './user.js';
66

77
/**
88
* Lit component providing interaction with user site configurations.
@@ -47,7 +47,7 @@ class DeepSiteConfiguration extends LitElement {
4747

4848
this.configuration = {observedEconomicEntities: []};
4949

50-
getUser().then(
50+
user().then(
5151
(user) => {
5252
this.user = user;
5353

packages/deep-economic-analyzer/graphql/client.js

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,66 @@ import {setContext} from '@apollo/client/link/context';
88
import {getMainDefinition} from '@apollo/client/utilities';
99
import {WebSocketLink} from './web-socket-link.js';
1010

11-
import {getUser} from '../user.js';
12-
13-
let client = globalThis.__APOLLO_CLIENT__ || null;
1411
/**
1512
* Initialize the apollo client for use with the application.
13+
*
14+
* @param {Object} user Currently logged in user.
1615
*/
17-
const initApolloClient = async () => {
18-
if (!client) {
19-
const authHeaders = (_user) => {
20-
return {
21-
authorization: _user?.accessToken ? `Bearer ${_user.accessToken}` : '',
22-
me: _user?.idToken ? _user.idToken : '',
23-
};
16+
const initApolloClient = async (user) => {
17+
const authHeaders = (_user) => {
18+
return {
19+
authorization: _user?.accessToken ? `Bearer ${_user.accessToken}` : '',
20+
me: _user?.idToken ? _user.idToken : '',
2421
};
22+
};
2523

26-
const user = await getUser();
27-
28-
const cache = new InMemoryCache({addTypename: false});
24+
const cache = new InMemoryCache({addTypename: false});
2925

30-
const wsLink = new WebSocketLink({
31-
url: process.env.PREDECOS_MICROSERVICE_SUBSCRIPTION_URL,
32-
connectionParams: () => {
33-
return authHeaders(user);
34-
},
35-
});
26+
const wsLink = new WebSocketLink({
27+
url: process.env.PREDECOS_MICROSERVICE_SUBSCRIPTION_URL,
28+
connectionParams: () => {
29+
return authHeaders(user);
30+
},
31+
});
3632

37-
const httpLink = new HttpLink({
38-
uri: process.env.PREDECOS_MICROSERVICE_GATEWAY_URL,
39-
credentials: 'include',
40-
});
33+
const httpLink = new HttpLink({
34+
uri: process.env.PREDECOS_MICROSERVICE_GATEWAY_URL,
35+
credentials: 'include',
36+
});
4137

42-
const backendLink = split(
43-
({query}) => {
44-
const definition = getMainDefinition(query);
45-
return (
46-
definition.kind === 'OperationDefinition' &&
47-
definition.operation === 'subscription'
48-
);
49-
},
50-
wsLink,
51-
httpLink
52-
);
38+
const backendLink = split(
39+
({query}) => {
40+
const definition = getMainDefinition(query);
41+
return (
42+
definition.kind === 'OperationDefinition' &&
43+
definition.operation === 'subscription'
44+
);
45+
},
46+
wsLink,
47+
httpLink
48+
);
5349

54-
const authLink = setContext((_, {headers}) => {
55-
return {
56-
headers: {...headers, ...authHeaders(user)},
57-
};
58-
});
50+
const authLink = setContext((_, {headers}) => {
51+
return {
52+
headers: {...headers, ...authHeaders(user)},
53+
};
54+
});
5955

60-
client = new ApolloClient({
61-
cache,
62-
link: authLink.concat(backendLink),
63-
defaultOptions: {
64-
watchQuery: {
65-
fetchPolicy: 'no-cache',
66-
errorPolicy: 'ignore',
67-
},
68-
query: {
69-
fetchPolicy: 'no-cache',
70-
errorPolicy: 'all',
71-
},
56+
// NOTE: Assignment to the global __APOLLO_CLIENT__ ensures all application graphql requests are made using the specified client.
57+
globalThis.__APOLLO_CLIENT__ = new ApolloClient({
58+
cache,
59+
link: authLink.concat(backendLink),
60+
defaultOptions: {
61+
watchQuery: {
62+
fetchPolicy: 'no-cache',
63+
errorPolicy: 'ignore',
7264
},
73-
});
74-
}
75-
76-
// NOTE: This ensures all application graphql requests are made using the specified client.
77-
globalThis.__APOLLO_CLIENT__ = client;
65+
query: {
66+
fetchPolicy: 'no-cache',
67+
errorPolicy: 'all',
68+
},
69+
},
70+
});
7871
};
7972

8073
const setApolloClientForTesting = (mockClient) => {

packages/deep-economic-analyzer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@web/test-runner-playwright": "^0.8.6",
3131
"@web/test-runner-puppeteer": "^0.10.0",
3232
"chai": "^4.3.4",
33+
"jwt-decode": "^3.1.2",
3334
"mocha": "^9.0.2",
3435
"rimraf": "^3.0.2",
3536
"rollup": "^2.72.1",

packages/deep-economic-analyzer/test/deep-analyzer-page-summary.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ const tweetList = (element) => {
109109

110110
describe('deep-analyzer-page-summary', () => {
111111
beforeEach(async () => {
112-
await initializeE2e();
112+
await initializeE2e(
113+
process.env.PREDECOS_TEST_AUTH_PREMIUM_USERNAME,
114+
process.env.PREDECOS_TEST_AUTH_PREMIUM_PASSWORD
115+
);
113116
});
114117

115118
it('should allow users to collect data for a desired business', async () => {
@@ -153,7 +156,7 @@ describe('deep-analyzer-page-summary', () => {
153156
expect(chart.rows.length).to.be.greaterThan(0);
154157
});
155158

156-
it('should allow users to select a business to alanyze', async () => {
159+
it('should allow users to select a business to analyze', async () => {
157160
const businessName = 'Moosehead';
158161

159162
const element = await fixtureSync(

0 commit comments

Comments
 (0)