Skip to content

Commit

Permalink
feat: set pending state on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
drewlyton committed Sep 30, 2024
1 parent eee7cb3 commit e3f4a0d
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ReadOnlyIcon} from '@sanity/icons'
import {Text} from '@sanity/ui'
import {useState} from 'react'
import {useMemo, useState} from 'react'
import {Translate, useCurrentUser, useListFormat, useTranslation} from 'sanity'

import {
Expand All @@ -23,6 +23,11 @@ export function PermissionCheckBanner({granted, requiredPermission}: PermissionC
loading: requestStatusLoading,
error: requestStatusError,
} = useRoleRequestsStatus()
const [requestSent, setRequestSent] = useState(false)
const requestPending = useMemo(
() => roleRequestStatus === 'pending' || requestSent,
[roleRequestStatus, requestSent],
)
const currentUserRoles = currentUser?.roles || []
const isOnlyViewer = currentUserRoles.length === 1 && currentUserRoles[0].name === 'viewer'
const [showRequestPermissionDialog, setShowRequestPermissionDialog] = useState(false)
Expand Down Expand Up @@ -57,17 +62,13 @@ export function PermissionCheckBanner({granted, requiredPermission}: PermissionC
action={
isOnlyViewer && roleRequestStatus && !requestStatusError && !requestStatusLoading
? {
onClick:
roleRequestStatus === 'pending'
? undefined
: () => setShowRequestPermissionDialog(true),
text:
roleRequestStatus === 'pending'
? t('banners.permission-check-banner.request-permission-button.sent')
: t('banners.permission-check-banner.request-permission-button.text'),
tone: roleRequestStatus === 'pending' ? 'default' : 'primary',
disabled: roleRequestStatus === 'pending',
mode: roleRequestStatus === 'pending' ? 'bleed' : undefined,
onClick: requestPending ? undefined : () => setShowRequestPermissionDialog(true),
text: requestPending
? t('banners.permission-check-banner.request-permission-button.sent')
: t('banners.permission-check-banner.request-permission-button.text'),
tone: requestPending ? 'default' : 'primary',
disabled: requestPending,
mode: requestPending ? 'bleed' : undefined,
}
: undefined
}
Expand All @@ -77,7 +78,10 @@ export function PermissionCheckBanner({granted, requiredPermission}: PermissionC
{showRequestPermissionDialog && (
<RequestPermissionDialog
onClose={() => setShowRequestPermissionDialog(false)}
onRequestSubmitted={() => setShowRequestPermissionDialog(false)}
onRequestSubmitted={() => {
setRequestSent(true)
setShowRequestPermissionDialog(false)
}}
/>
)}
</>
Expand Down

0 comments on commit e3f4a0d

Please sign in to comment.