@@ -13,6 +13,9 @@ import { loadStateFromCookie, saveStateToCookie } from 'utils/cookies'
1313import TopBar from 'components/TopBar'
1414import { fetchUserWithSupertokens , UserWithSupertokens } from 'services/userService'
1515import moment from 'moment-timezone'
16+ import SettingsIcon from '../../assets/settings-slider-icon.png'
17+ import Image from 'next/image'
18+
1619const Chart = dynamic ( async ( ) => await import ( 'components/Chart' ) , {
1720 ssr : false
1821} )
@@ -70,6 +73,9 @@ export default function Dashboard ({ user }: PaybuttonsProps): React.ReactElemen
7073 const [ activePeriod , setActivePeriod ] = useState < PeriodData > ( )
7174 const [ activePeriodString , setActivePeriodString ] = useState < PeriodString > ( '1M' )
7275 const [ totalString , setTotalString ] = useState < string > ( )
76+ const [ selectedButtonIds , setSelectedButtonIds ] = useState < any [ ] > ( [ ] )
77+ const [ showFilters , setShowFilters ] = useState < boolean > ( false )
78+ const [ buttons , setButtons ] = useState < any [ ] > ( [ ] )
7379
7480 const setPeriodFromString = ( data ?: DashboardData , periodString ?: PeriodString ) : void => {
7581 if ( data === undefined ) return
@@ -92,10 +98,30 @@ export default function Dashboard ({ user }: PaybuttonsProps): React.ReactElemen
9298 }
9399 saveStateToCookie ( COOKIE_NAMES . DASHBOARD_FILTER , periodString )
94100 }
101+ const getDataAndSetUpButtons = async ( ) : Promise < void > => {
102+ const paybuttons = await fetchPaybuttons ( )
103+ setButtons ( paybuttons )
104+ }
105+ const fetchPaybuttons = async ( ) : Promise < any > => {
106+ const res = await fetch ( `/api/paybuttons?userId=${ user ?. userProfile . id } ` , {
107+ method : 'GET'
108+ } )
109+ if ( res . status === 200 ) {
110+ return await res . json ( )
111+ }
112+ }
113+
114+ useEffect ( ( ) => {
115+ void getDataAndSetUpButtons ( )
116+ } , [ ] )
95117
96118 useEffect ( ( ) => {
97119 const fetchData = async ( ) : Promise < void > => {
98- const res = await fetch ( 'api/dashboard' , {
120+ let url = 'api/dashboard'
121+ if ( selectedButtonIds . length > 0 ) {
122+ url += `?buttonIds=${ selectedButtonIds . join ( ',' ) } `
123+ }
124+ const res = await fetch ( url , {
99125 headers : {
100126 Timezone : moment . tz . guess ( )
101127 }
@@ -108,7 +134,7 @@ export default function Dashboard ({ user }: PaybuttonsProps): React.ReactElemen
108134 if ( savedActivePeriodString !== undefined ) {
109135 setActivePeriodString ( savedActivePeriodString )
110136 }
111- } , [ ] )
137+ } , [ selectedButtonIds ] )
112138
113139 useEffect ( ( ) => {
114140 setPeriodFromString ( dashboardData , activePeriodString )
@@ -127,6 +153,44 @@ export default function Dashboard ({ user }: PaybuttonsProps): React.ReactElemen
127153 return (
128154 < >
129155 < TopBar title = "Dashboard" user = { user . stUser ?. email } />
156+ < div className = { style . filter_btns } >
157+ < div
158+ onClick = { ( ) => setShowFilters ( ! showFilters ) }
159+ className = { style . show_filters_button }
160+ >
161+ < Image src = { SettingsIcon } alt = "filters" width = { 15 } /> Filters
162+ </ div >
163+ { selectedButtonIds . length > 0 &&
164+ < div
165+ onClick = { ( ) => setSelectedButtonIds ( [ ] ) }
166+ className = { style . show_filters_button }
167+ >
168+ Clear
169+ </ div >
170+ }
171+ </ div >
172+ { showFilters && (
173+ < div className = { style . showfilters_ctn } >
174+ < span > Filter by PayButton</ span >
175+ < div className = { style . filters_ctn } >
176+ { buttons . map ( ( button ) => (
177+ < div
178+ key = { button . id }
179+ onClick = { ( ) => {
180+ setSelectedButtonIds ( prev =>
181+ prev . includes ( button . id )
182+ ? prev . filter ( id => id !== button . id )
183+ : [ ...prev , button . id ]
184+ )
185+ } }
186+ className = { `${ style . filter_button } ${ selectedButtonIds . includes ( button . id ) ? style . active : '' } ` }
187+ >
188+ { button . name }
189+ </ div >
190+ ) ) }
191+ </ div >
192+ </ div >
193+ ) }
130194 < div className = { style . number_ctn } >
131195 < NumberBlock value = { '$' . concat ( formatQuoteValue ( activePeriod . totalRevenue , user . userProfile . preferredCurrencyId ) ) } text = 'Revenue' />
132196 < NumberBlock value = { activePeriod . totalPayments } text = 'Payments' />
0 commit comments