Skip to content

Commit

Permalink
ui: convert UserContactMethodSelect to typescript (#3438)
Browse files Browse the repository at this point in the history
* use urql useQuery

* convert to ts
  • Loading branch information
KatieMSB authored Nov 16, 2023
1 parent 0ae430d commit 8f045f4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 70 deletions.
70 changes: 0 additions & 70 deletions web/src/app/users/UserContactMethodSelect.js

This file was deleted.

63 changes: 63 additions & 0 deletions web/src/app/users/UserContactMethodSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { ReactNode } from 'react'
import { useQuery, gql } from 'urql'
import MenuItem from '@mui/material/MenuItem'
import TextField from '@mui/material/TextField'
import { sortContactMethods } from './util'

const query = gql`
query userCMSelect($id: ID!) {
user(id: $id) {
id
contactMethods {
id
name
type
value
}
}
}
`

type CMExtraItems = {
label?: ReactNode
value: string
}

interface UserContactMethodSelectProps {
userID: string
extraItems: CMExtraItems[]
}

export default function UserContactMethodSelect({
userID,
extraItems = [] as CMExtraItems[],
...rest
}: UserContactMethodSelectProps): ReactNode {
const [{ data }] = useQuery({
query,
requestPolicy: 'network-only',
variables: {
id: userID,
},
})

const cms = data?.user ? data?.user?.contactMethods : []

return (
<TextField select {...rest}>
{sortContactMethods(cms)
.map((cm) => (
<MenuItem key={cm.id} value={cm.id}>
{cm.name} ({cm.type})
</MenuItem>
))
.concat(
extraItems.map((item) => (
<MenuItem key={item.value} value={item.value}>
{item.label || item.value}
</MenuItem>
)),
)}
</TextField>
)
}

0 comments on commit 8f045f4

Please sign in to comment.