1- import { ApolloMutationController } from '@apollo-elements/core' ;
2- import { ApolloQuery , css , html } from '@apollo-elements/lit-apollo' ;
1+ import {
2+ ApolloMutationController ,
3+ ApolloQueryController ,
4+ } from '@apollo-elements/core' ;
5+ import { LitElement , css , html } from '@apollo-elements/lit-apollo' ;
36import '@google-web-components/google-chart' ;
47import '@material/mwc-button' ;
8+ import '@material/mwc-list/mwc-list-item' ;
9+ import '@material/mwc-select' ;
510import '@material/mwc-textfield' ;
611import '@thinkdeep/deep-button/deep-button.mjs' ;
712import '@thinkdeep/deep-card/deep-card.mjs' ;
@@ -11,9 +16,10 @@ import {translate} from 'lit-element-i18n';
1116import CollectEconomicData from './graphql/CollectEconomicData.mutation.graphql' ;
1217import GetSentiment from './graphql/GetSentiment.query.graphql' ;
1318
14- export default class DeepAnalyzerPageSummary extends ApolloQuery {
19+ export default class DeepAnalyzerPageSummary extends LitElement {
1520 static get properties ( ) {
1621 return {
22+ query : { type : Object } ,
1723 mutation : { type : Object } ,
1824 selectedSentiments : { type : Array } ,
1925 } ;
@@ -22,12 +28,13 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
2228 constructor ( ) {
2329 super ( ) ;
2430
25- this . query = GetSentiment ;
26-
27- this . variables = {
28- economicEntityName : 'Google' ,
29- economicEntityType : 'BUSINESS' ,
30- } ;
31+ this . query = new ApolloQueryController ( this , GetSentiment , {
32+ variables : {
33+ economicEntityName : 'Google' ,
34+ economicEntityType : 'BUSINESS' ,
35+ } ,
36+ onData : this . _triggerUpdate . bind ( this ) ,
37+ } ) ;
3138
3239 this . mutation = new ApolloMutationController ( this , CollectEconomicData ) ;
3340
@@ -38,12 +45,14 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
3845 super . connectedCallback ( ) ;
3946
4047 this . addEventListener ( 'google-chart-select' , this . _handleChartSelection ) ;
48+ this . addEventListener ( 'selected' , this . _onSelect ) ;
4149 }
4250
4351 disconnectedCallback ( ) {
4452 super . disconnectedCallback ( ) ;
4553
4654 this . removeEventListener ( 'google-chart-select' , this . _handleChartSelection ) ;
55+ this . removeEventListener ( 'selected' , this . _onSelect ) ;
4756 }
4857
4958 static get styles ( ) {
@@ -60,7 +69,7 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
6069 ${ translate ( 'translations:startCollectingLabel' ) }
6170 < mwc-textfield
6271 label ="Business name "
63- @input ="${ debounce ( this . _setCompanyName . bind ( this ) , 350 ) } "
72+ @input ="${ debounce ( this . _onInput . bind ( this ) , 350 ) } "
6473 > </ mwc-textfield >
6574 < mwc-button
6675 raised
@@ -69,18 +78,24 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
6978 icon ="input "
7079 > </ mwc-button >
7180
72- < div >
73- < google-chart
74- type ="line "
75- options ="{"title": "Sentiment as a function of time" } "
76- cols ="[{"label": "Timestamp", "type": "number"}, {"label": "Sentiment", "type": "number"}] "
77- rows ="[${
78- this . data ?. sentiments ?. map ( ( sentiment ) =>
79- JSON . stringify ( [ sentiment . timestamp , sentiment . score ] )
80- )
81- } ] "
82- > </ google-chart >
83- </ div >
81+ < mwc-select label ="Select a business ">
82+ < mwc-list-item value ="Google "> Google</ mwc-list-item >
83+ < mwc-list-item value ="Amazon "> Amazon</ mwc-list-item >
84+ < mwc-list-item value ="PetCo "> PetCo</ mwc-list-item >
85+ < mwc-list-item value ="Tesla "> Tesla</ mwc-list-item >
86+ < mwc-list-item value ="Ford "> Ford</ mwc-list-item >
87+ </ mwc-select >
88+
89+ < google-chart
90+ type ="line "
91+ options ="{"title": "Sentiment as a function of time" } "
92+ cols ="[{"label": "Timestamp", "type": "number"}, {"label": "Sentiment", "type": "number"}] "
93+ rows ="[${
94+ this . query ?. data ?. sentiments ?. map ( ( sentiment ) =>
95+ JSON . stringify ( [ sentiment . timestamp , sentiment . score ] )
96+ )
97+ } ] "
98+ > </ google-chart >
8499
85100 ${
86101 this . selectedSentiments . map ( ( sentiment ) =>
@@ -111,7 +126,7 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
111126
112127 const selectedPoint = googleChart . rows [ selectedRow ] ;
113128
114- this . data ?. sentiments ?. forEach ( ( sentiment ) => {
129+ this . query ?. data ?. sentiments ?. forEach ( ( sentiment ) => {
115130 if ( this . _hasMatchingData ( sentiment , selectedPoint ) ) {
116131 this . selectedSentiments . push ( sentiment ) ;
117132 }
@@ -132,13 +147,39 @@ export default class DeepAnalyzerPageSummary extends ApolloQuery {
132147 ) ;
133148 }
134149
135- _setCompanyName ( { explicitOriginalTarget} ) {
150+ /**
151+ * Handle input.
152+ * @param {HTMLElement } explicitOriginalTarget - The input element containing the business for which data will be collected.
153+ */
154+ _onInput ( { explicitOriginalTarget} ) {
136155 const companyName = explicitOriginalTarget . value ;
137156 this . mutation . variables = {
138157 economicEntityName : companyName ,
139158 economicEntityType : 'BUSINESS' ,
140159 } ;
141160 }
161+
162+ /**
163+ * Find the selected business and add it to the query variables.
164+ */
165+ _onSelect ( ) {
166+ const businessName = this . shadowRoot . querySelector ( '[aria-selected="true"]' )
167+ . value ;
168+ this . query . variables = {
169+ economicEntityName : businessName ,
170+ economicEntityType : 'BUSINESS' ,
171+ } ;
172+ }
173+
174+ /**
175+ * Trigger an update to the DOM.
176+ *
177+ * NOTE: This is used because the google-chart wasn't dynamically updating as a result of data fetch.
178+ * This happened to solve the issue.
179+ */
180+ _triggerUpdate ( ) {
181+ this . requestUpdate ( ) ;
182+ }
142183}
143184
144185customElements . define ( 'deep-analyzer-page-summary' , DeepAnalyzerPageSummary ) ;
0 commit comments