11// Libraries
2- import React , { FC , useContext } from 'react'
2+ import React , { FC , useContext , useState } from 'react'
33import { useHistory } from 'react-router-dom'
44import { useSelector } from 'react-redux'
55
@@ -35,6 +35,7 @@ import {getOrg} from 'src/organizations/selectors'
3535// Types
3636import { ResourceType } from 'src/types'
3737import { ORGS , SUBSCRIPTIONS } from 'src/shared/constants/routes'
38+ import { Subscription } from 'src/types/subscriptions'
3839
3940// Styles
4041import 'src/writeData/subscriptions/components/SubscriptionsLanding.scss'
@@ -43,6 +44,48 @@ const SubscriptionsLanding: FC = () => {
4344 const { subscriptions, loading} = useContext ( SubscriptionListContext )
4445 const history = useHistory ( )
4546 const org = useSelector ( getOrg )
47+ const [ search , setSearch ] = useState ( '' )
48+ const [ sortOptions , setSortOptions ] = useState ( {
49+ sortKey : 'name' as keyof Subscription ,
50+ sortType : SortTypes . String ,
51+ sortDirection : Sort . Ascending ,
52+ } )
53+ const filteredSubscriptions =
54+ subscriptions && search
55+ ? subscriptions . filter (
56+ s => s . name . toLowerCase ( ) === search . toLowerCase ( ) . trim ( )
57+ )
58+ : subscriptions
59+ const handleSort = ( subscriptions : Subscription [ ] ) : Subscription [ ] => {
60+ let sortedSubscriptions
61+ if ( sortOptions . sortDirection === Sort . Ascending ) {
62+ sortedSubscriptions = subscriptions . sort ( ( a , b ) =>
63+ ( a [ sortOptions . sortKey ] as string )
64+ . toLowerCase ( )
65+ . localeCompare ( ( b [ sortOptions . sortKey ] as string ) . toLowerCase ( ) )
66+ )
67+ } else if ( sortOptions . sortDirection === Sort . Descending ) {
68+ sortedSubscriptions = subscriptions . sort ( ( a , b ) =>
69+ ( b [ sortOptions . sortKey ] as string )
70+ . toLowerCase ( )
71+ . localeCompare ( ( a [ sortOptions . sortKey ] as string ) . toLowerCase ( ) )
72+ )
73+ }
74+ return sortedSubscriptions
75+ }
76+ const setSort = ( sortKey , sortDirection , sortType ) => {
77+ if (
78+ sortKey === sortOptions . sortKey &&
79+ sortDirection === sortOptions . sortDirection
80+ ) {
81+ return
82+ }
83+ setSortOptions ( {
84+ sortKey,
85+ sortType,
86+ sortDirection,
87+ } )
88+ }
4689 return (
4790 < Page
4891 className = "subscriptions-landing"
@@ -55,15 +98,15 @@ const SubscriptionsLanding: FC = () => {
5598 < Page . ControlBarLeft >
5699 < SearchWidget
57100 placeholderText = "Filter subscriptions..."
58- searchTerm = ""
59- onSearch = { ( ) => { } }
101+ searchTerm = { search }
102+ onSearch = { setSearch }
60103 />
61104 < ResourceSortDropdown
62105 resourceType = { ResourceType . Subscriptions }
63- sortDirection = { Sort . Ascending }
64- sortKey = "name"
65- sortType = { SortTypes . String }
66- onSelect = { ( ) => { } }
106+ sortDirection = { sortOptions . sortDirection }
107+ sortKey = { sortOptions . sortKey }
108+ sortType = { sortOptions . sortType }
109+ onSelect = { setSort }
67110 />
68111 </ Page . ControlBarLeft >
69112 < Page . ControlBarRight >
@@ -83,7 +126,9 @@ const SubscriptionsLanding: FC = () => {
83126 </ Page . ControlBarRight >
84127 </ Page . ControlBar >
85128 { subscriptions && subscriptions . length ? (
86- < SubscriptionsList subscriptions = { subscriptions } />
129+ < SubscriptionsList
130+ subscriptions = { handleSort ( filteredSubscriptions ) }
131+ />
87132 ) : (
88133 < EmptySubscriptionState />
89134 ) }
0 commit comments