2323 */
2424
2525import React , { useEffect , useState , useContext , useCallback } from 'react'
26- import { ComponentsProvider , Card , CardContent , Flex , FlexItem , Tree , TreeItem , Space , SpaceVertical , Heading } from '@looker/components'
26+ import {
27+ ComponentsProvider ,
28+ Card , CardContent ,
29+ Flex , FlexItem ,
30+ Tree , TreeItem ,
31+ Space , SpaceVertical ,
32+ Heading , ToggleSwitch
33+ } from '@looker/components'
2734import { ExtensionContext } from '@looker/extension-sdk-react'
28- import { LookerEmbedSDK , LookerEmbedLook } from '@looker/embed-sdk'
35+ import { LookerEmbedSDK } from '@looker/embed-sdk'
2936import styled from "styled-components"
37+ import { paddingBottom } from 'styled-system'
3038
3139const EmbedContainer = styled . div `
3240 width: 100%;
@@ -40,8 +48,12 @@ export const HelloWorld = () => {
4048 const { core40SDK, extensionSDK } = useContext ( ExtensionContext )
4149 const [ qid , setQid ] = useState ( ) ;
4250 const [ geos , setGeos ] = useState ( ) ;
51+ const [ state , setState ] = useState ( ) ;
52+ const [ explore , setExplore ] = useState ( ) ;
53+ const [ on , setOn ] = React . useState ( false )
54+
4355
44- useEffect ( ( ) => {
56+ useEffect ( ( ) => {
4557 const initialize = async ( ) => {
4658 try {
4759 const states = await core40SDK . ok ( core40SDK . run_inline_query (
@@ -60,7 +72,7 @@ export const HelloWorld = () => {
6072 ) )
6173 setGeos (
6274 states . map ( ( state ) => {
63- return < TreeItem onClick = { async ( ) => { getQid ( state [ 'users.state' ] ) } } icon = "FieldLocation" >
75+ return < TreeItem onClick = { async ( ) => { setState ( state [ 'users.state' ] ) } } icon = "FieldLocation" >
6476 { state [ 'users.state' ] }
6577 </ TreeItem >
6678 } )
@@ -72,18 +84,30 @@ export const HelloWorld = () => {
7284 getQid ( 'California' )
7385 initialize ( )
7486 } , [ ] )
75- const getQid = async ( state ) => {
87+
88+ //Listens for changes in the state variable (which changes onclick of the tree) and updates the embed query filters
89+ useEffect ( ( ) => {
90+ if ( state && explore ) {
91+ explore . updateFilters ( { 'users.state' : state } )
92+ explore . run ( )
93+ }
94+ }
95+ , [ state ] )
96+
97+ /*
98+ Uses the state variable to call Looker's API and get a new qid.
99+ Also internally uses the `on` variable to determine the measure field visualized
100+ */
101+ const getQid = async ( state ) => {
76102 let visconfig = {
77- "series_types" : { } ,
78- "type" : "looker_map" ,
79103 "map_plot_mode" : "points" ,
80104 "heatmap_gridlines" : false ,
81105 "heatmap_gridlines_empty" : false ,
82106 "heatmap_opacity" : 0.5 ,
83107 "show_region_field" : true ,
84108 "draw_map_labels_above_data" : true ,
85109 "map_tile_provider" : "light" ,
86- "map_position " : "fit_data" ,
110+ "map_zoom " : 6 ,
87111 "map_scale_indicator" : "off" ,
88112 "map_pannable" : true ,
89113 "map_zoomable" : true ,
@@ -92,56 +116,114 @@ export const HelloWorld = () => {
92116 "map_marker_radius_mode" : "proportional_value" ,
93117 "map_marker_units" : "meters" ,
94118 "map_marker_proportional_scale_type" : "linear" ,
95- "map_marker_color_mode" : "fixed " ,
119+ "map_marker_color_mode" : "value " ,
96120 "show_view_names" : false ,
97121 "show_legend" : true ,
98122 "quantize_map_value_colors" : false ,
99123 "reverse_map_value_colors" : false ,
100- "defaults_version" : 1
124+ "type" : "looker_map" ,
125+ "x_axis_gridlines" : false ,
126+ "y_axis_gridlines" : true ,
127+ "show_y_axis_labels" : true ,
128+ "show_y_axis_ticks" : true ,
129+ "y_axis_tick_density" : "default" ,
130+ "y_axis_tick_density_custom" : 5 ,
131+ "show_x_axis_label" : true ,
132+ "show_x_axis_ticks" : true ,
133+ "y_axis_scale_mode" : "linear" ,
134+ "x_axis_reversed" : false ,
135+ "y_axis_reversed" : false ,
136+ "plot_size_by_field" : false ,
137+ "trellis" : "" ,
138+ "stacking" : "" ,
139+ "limit_displayed_rows" : false ,
140+ "legend_position" : "center" ,
141+ "point_style" : "none" ,
142+ "show_value_labels" : false ,
143+ "label_density" : 25 ,
144+ "x_axis_scale" : "auto" ,
145+ "y_axis_combined" : true ,
146+ "ordering" : "none" ,
147+ "show_null_labels" : false ,
148+ "show_totals_labels" : false ,
149+ "show_silhouette" : false ,
150+ "totals_color" : "#808080" ,
151+ "defaults_version" : 1 ,
152+ "hidden_fields" : [
153+ "users.id"
154+ ] ,
155+ "series_types" : { }
101156 }
102157 const response = await core40SDK . ok (
103158 core40SDK . create_query ( {
104159 view :'order_items' ,
105160 model :'join20' ,
106- fields :[ 'users.location' , 'order_items.count' ] ,
161+ fields : [
162+ on ? "order_items.total_profit" : "order_items.count" ,
163+ "users.location" ,
164+ "users.id"
165+ ] ,
107166 filters :{ 'users.state' :state } ,
167+ sorts :[ 'order_items.total_profit desc' ] ,
108168 vis_config : visconfig
109169 } )
110170 )
111171 setQid ( response . client_id )
112172 }
113- const embedCtrRef = useCallback ( ( el ) => {
114- const hostUrl = extensionSDK . lookerHostData . hostUrl
115- if ( el && hostUrl && qid ) {
116- el . innerHTML = ''
117- LookerEmbedSDK . init ( hostUrl )
118- LookerEmbedSDK . createExploreWithUrl ( `${ hostUrl } /embed/query/join20/order_items?qid=${ qid } &sdk=2&embed_domain=${ hostUrl } &sandboxed_host=true` )
119- . appendTo ( el )
120- . on ( 'drillmenu:click' , ( e ) => { console . log ( e ) } )
121- . build ( )
122- . connect ( )
123- . then ( )
124- . catch ( ( error ) => {
125- console . error ( 'Connection error' , error )
126- } )
173+
174+ //Listens to the toggle switch `on` state variable and cyles the qid with the other measure
175+ useEffect ( ( ) => {
176+ const cycleQid = async ( ) => {
177+ if ( state ) {
178+ getQid ( state )
127179 }
128- } , [ qid ] )
180+ }
181+ cycleQid ( )
182+ }
183+ , [ on ] )
184+
185+ // Embed iframe builder, listens to changes in the qid (Which currently only changes on pageload)
186+ const embedCtrRef = useCallback ( ( el ) => {
187+ const hostUrl = extensionSDK . lookerHostData . hostUrl
188+ if ( el && hostUrl && qid ) {
189+ el . innerHTML = ''
190+ LookerEmbedSDK . init ( hostUrl )
191+ LookerEmbedSDK . createExploreWithUrl ( `${ hostUrl } /embed/query/join20/order_items?qid=${ qid } &sdk=2&embed_domain=${ hostUrl } &sandboxed_host=true` )
192+ . appendTo ( el )
193+ . on ( 'drillmenu:click' , ( e ) => { console . log ( e ) } )
194+ . build ( )
195+ . connect ( )
196+ . then ( setExplore )
197+ . catch ( ( error ) => {
198+ console . error ( 'Connection error' , error )
199+ } )
200+ }
201+ } , [ qid ] )
129202
130203 return (
131204 < >
132205 < ComponentsProvider >
133- < div style = { { backgroundColor :'#A9AAC7' , padding :'10px' , height :'100%' } } >
134- < SpaceVertical m = { 5 } > < Heading style = { { color :'#404263' , fontWeight :'bold' , fontFamily :'Arial' } } > State Sales Explorer</ Heading > </ SpaceVertical >
135-
206+ < div style = { { backgroundColor :'#262D33' , padding :'10px' , height :'100%' } } >
207+ < Flex flexDirection = "row" mr = "small" >
208+ < FlexItem >
209+ < SpaceVertical m = { 2 } >
210+ < Heading style = { { color :'#fff' , fontWeight :'bold' , fontFamily :'Arial' } } > Best Customers by Region</ Heading >
211+ </ SpaceVertical >
212+ </ FlexItem >
213+
214+ </ Flex >
136215 < Flex flexDirection = "row" mr = "large" >
137216 < FlexItem maxWidth = "200" maxHeight = "800" >
138217 < Card raised >
139218 < CardContent style = { { overflow :'scroll' } } >
140- < Tree label = "Regions" icon = "Hamburger" defaultOpen >
219+ < Tree label = "Regions" icon = "Hamburger" defaultOpen style = { { overflow : 'hidden' } } >
141220 { geos }
142221 </ Tree >
143222 </ CardContent >
144223 </ Card >
224+ < div style = { { color :'#fff' , paddingTop :'5px' , paddingBottom :'0px' } } >
225+ By Volume < ToggleSwitch onChange = { ( event ) => setOn ( event . target . checked ) } on = { on } id = "switch" /> By Profit
226+ </ div >
145227 </ FlexItem >
146228 < Space />
147229 < FlexItem minWidth = "80%" maxWidth = "100%" maxHeight = "800" >
0 commit comments