Skip to content

Commit c7c3d0a

Browse files
authored
feat: subscriptions list (#4189)
* feat: search by name, sort by name and date * feat: change to updated at, add to details card * fix: types, lint * fix: use luxon
1 parent 2fcf6d1 commit c7c3d0a

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

src/shared/components/resource_sort_dropdown/generateSortItems.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,15 @@ export const generateSortItems = (
401401
sortDirection: Sort.Descending,
402402
},
403403
{
404-
label: 'Description (Ascending)',
405-
sortKey: 'description',
406-
sortType: SortTypes.String,
404+
label: 'Updated (Oldest)',
405+
sortKey: 'updatedAt',
406+
sortType: SortTypes.Date,
407407
sortDirection: Sort.Ascending,
408408
},
409409
{
410-
label: 'Description (Descending)',
411-
sortKey: 'description',
412-
sortType: SortTypes.String,
410+
label: 'Updated (Newest)',
411+
sortKey: 'updatedAt',
412+
sortType: SortTypes.Date,
413413
sortDirection: Sort.Descending,
414414
},
415415
]

src/types/subscriptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export interface Subscription {
2222
status?: string
2323
bucket?: string
2424
qos?: number
25-
createdAt?: string
26-
updatedAt?: string
25+
createdAt?: Date
26+
updatedAt?: Date
2727
tokenID?: string
2828
token?: string
2929
}

src/writeData/subscriptions/components/SubscriptionCard.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Libraries
22
import React, {FC, useContext} from 'react'
3+
import {DateTime} from 'luxon'
34

45
// Components
56
import {
@@ -22,6 +23,7 @@ interface Props {
2223

2324
const SubscriptionCard: FC<Props> = ({subscription}) => {
2425
const {deleteSubscription} = useContext(SubscriptionListContext)
26+
const timeSince = new DateTime.fromISO(subscription.updatedAt).toRelative()
2527
return (
2628
<ResourceCard
2729
key={`subscription-card-id--${subscription.id}`}
@@ -42,7 +44,13 @@ const SubscriptionCard: FC<Props> = ({subscription}) => {
4244
}
4345
>
4446
<ResourceCard.Name name={subscription.name} />
45-
<ResourceCard.Description description={subscription.description} />
47+
<ResourceCard.Description
48+
description={`${subscription.brokerHost}:${subscription.brokerPort}/${subscription.topic}`}
49+
/>
50+
<ResourceCard.Meta>
51+
<>{subscription.status}</>
52+
<>Last Modified: {timeSince}</>
53+
</ResourceCard.Meta>
4654
</ResourceCard>
4755
)
4856
}

src/writeData/subscriptions/components/SubscriptionsLanding.tsx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Libraries
2-
import React, {FC, useContext} from 'react'
2+
import React, {FC, useContext, useState} from 'react'
33
import {useHistory} from 'react-router-dom'
44
import {useSelector} from 'react-redux'
55

@@ -35,6 +35,7 @@ import {getOrg} from 'src/organizations/selectors'
3535
// Types
3636
import {ResourceType} from 'src/types'
3737
import {ORGS, SUBSCRIPTIONS} from 'src/shared/constants/routes'
38+
import {Subscription} from 'src/types/subscriptions'
3839

3940
// Styles
4041
import '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

Comments
 (0)