Skip to content

Commit

Permalink
Merge pull request #2377 from target/ts-conversion-UserCalendarSubscr…
Browse files Browse the repository at this point in the history
…iptionList

ui/users: convert UserCalendarSubscriptionList file to typescript
  • Loading branch information
KatieMSB authored May 12, 2022
2 parents 216b67b + bcd5547 commit 3307d14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion web/src/app/lists/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const useStyles = makeStyles((theme: Theme) => ({

export interface FlatListSub {
id?: string
subHeader: string
subHeader: JSX.Element | string
}

export interface FlatListNotice extends Notice {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { useState } from 'react'
import { useQuery, gql } from '@apollo/client'
import p from 'prop-types'
import { useParams } from 'react-router-dom'
import { Card, Alert } from '@mui/material'
import FlatList from '../lists/FlatList'
import FlatList, { FlatListListItem } from '../lists/FlatList'
import OtherActions from '../util/OtherActions'
import CreateFAB from '../lists/CreateFAB'
import CalendarSubscribeCreateDialog from '../schedules/calendar-subscribe/CalendarSubscribeCreateDialog'
Expand All @@ -16,6 +15,7 @@ import Spinner from '../loading/components/Spinner'
import { formatTimeSince } from '../util/timeFormat'
import { useConfigValue } from '../util/RequireConfig'
import AppLink from '../util/AppLink'
import { UserCalendarSubscription } from '../../schema'

export const calendarSubscriptionsQuery = gql`
query calendarSubscriptions($id: ID!) {
Expand All @@ -36,15 +36,21 @@ export const calendarSubscriptionsQuery = gql`
}
`

export default function UserCalendarSubscriptionList(props) {
export default function UserCalendarSubscriptionList(props: {
userID: string
}): JSX.Element {
const { userID: _userID } = useParams()
const userID = props.userID || _userID
const [creationDisabled] = useConfigValue(
'General.DisableCalendarSubscriptions',
)
const [showCreateDialog, setShowCreateDialog] = useState(false)
const [showEditDialogByID, setShowEditDialogByID] = useState(null)
const [showDeleteDialogByID, setShowDeleteDialogByID] = useState(null)
const [showEditDialogByID, setShowEditDialogByID] = useState<string | null>(
null,
)
const [showDeleteDialogByID, setShowDeleteDialogByID] = useState<
string | null
>(null)

const { data, loading, error } = useQuery(calendarSubscriptionsQuery, {
variables: {
Expand All @@ -56,18 +62,20 @@ export default function UserCalendarSubscriptionList(props) {
if (!_.get(data, 'user.id')) return loading ? <Spinner /> : <ObjectNotFound />

// sort by schedule names, then subscription names
const subs = data.user.calendarSubscriptions.slice().sort((a, b) => {
if (a.schedule.name < b.schedule.name) return -1
if (a.schedule.name > b.schedule.name) return 1
const subs: UserCalendarSubscription[] = data.user.calendarSubscriptions
.slice()
.sort((a: UserCalendarSubscription, b: UserCalendarSubscription) => {
if ((a?.schedule?.name ?? '') < (b?.schedule?.name ?? '')) return -1
if ((a?.schedule?.name ?? '') > (b?.schedule?.name ?? '')) return 1

if (a.name > b.name) return 1
if (a.name < b.name) return -1
})
if (a.name > b.name) return 1
if (a.name < b.name) return -1
})

const subheaderDict = {}
const items = []
const subheaderDict: { [key: string]: boolean } = {}
const items: FlatListListItem[] = []

function renderOtherActions(id) {
function renderOtherActions(id: string): JSX.Element {
return (
<OtherActions
actions={[
Expand All @@ -85,13 +93,13 @@ export default function UserCalendarSubscriptionList(props) {
}

// push schedule names as subheaders now that the array is sorted
subs.forEach((sub) => {
if (!subheaderDict[sub.schedule.name]) {
subheaderDict[sub.schedule.name] = true
subs.forEach((sub: UserCalendarSubscription) => {
if (!subheaderDict[sub?.schedule?.name ?? '']) {
subheaderDict[sub?.schedule?.name ?? ''] = true
items.push({
subHeader: (
<AppLink to={`/schedules/${sub.scheduleID}`}>
{sub.schedule.name}
{sub.schedule?.name}
</AppLink>
),
})
Expand Down Expand Up @@ -152,7 +160,3 @@ export default function UserCalendarSubscriptionList(props) {
</React.Fragment>
)
}

UserCalendarSubscriptionList.propTypes = {
userID: p.string,
}

0 comments on commit 3307d14

Please sign in to comment.