Add minor webhook edit flow improvements (#178)

This commit is contained in:
Tan Nhu 2023-01-11 09:10:59 -08:00 committed by GitHub
parent abd132fdf6
commit f7962785aa
4 changed files with 69 additions and 22 deletions

View File

@ -123,6 +123,7 @@ export interface StringsMap {
ok: string ok: string
on: string on: string
onDate: string onDate: string
oneMustBeSelected: string
open: string open: string
optional: string optional: string
optionalExtendedDescription: string optionalExtendedDescription: string
@ -207,10 +208,14 @@ export interface StringsMap {
viewCommitDetails: string viewCommitDetails: string
viewed: string viewed: string
webhook: string webhook: string
webhookBranchCreated: string
webhookBranchDeleted: string
webhookBranchUpdated: string
webhookDeleted: string webhookDeleted: string
webhookDetails: string webhookDetails: string
webhookEventsLabel: string webhookEventsLabel: string
webhookListingContent: string webhookListingContent: string
webhookUpdated: string
webhooks: string webhooks: string
write: string write: string
yourBranches: string yourBranches: string

View File

@ -214,3 +214,8 @@ webhookDeleted: Webhook deleted.
failedToDeleteWebhook: Failed to delete Webhook. Please try again. failedToDeleteWebhook: Failed to delete Webhook. Please try again.
webhookDetails: Webhook Details webhookDetails: Webhook Details
updateWebhook: Update Webhook updateWebhook: Update Webhook
webhookUpdated: Webhook updated successfully.
oneMustBeSelected: At least one event must be selected
webhookBranchCreated: Branch created
webhookBranchUpdated: Branch updated
webhookBranchDeleted: Branch deleted

View File

@ -6,10 +6,12 @@ import type { OpenapiWebhookType } from 'services/code'
import { useStrings } from 'framework/strings' import { useStrings } from 'framework/strings'
import { RepositoryPageHeader } from 'components/RepositoryPageHeader/RepositoryPageHeader' import { RepositoryPageHeader } from 'components/RepositoryPageHeader/RepositoryPageHeader'
import { WehookForm } from 'pages/WebhookNew/WehookForm' import { WehookForm } from 'pages/WebhookNew/WehookForm'
import { useAppContext } from 'AppContext'
export default function WebhookDetails() { export default function WebhookDetails() {
const { getString } = useStrings() const { getString } = useStrings()
const { repoMetadata, error, loading, webhookId } = useGetRepositoryMetadata() const { routes } = useAppContext()
const { repoMetadata, error, loading, webhookId, refetch: refreshMetadata } = useGetRepositoryMetadata()
const { const {
data, data,
loading: webhookLoading, loading: webhookLoading,
@ -26,13 +28,19 @@ export default function WebhookDetails() {
repoMetadata={repoMetadata} repoMetadata={repoMetadata}
title={getString('webhookDetails')} title={getString('webhookDetails')}
dataTooltipId="webhookDetails" dataTooltipId="webhookDetails"
extraBreadcrumbLinks={
repoMetadata && [
{
label: getString('webhooks'),
url: routes.toCODEWebhooks({ repoPath: repoMetadata.path as string })
}
]
}
/> />
<PageBody <PageBody
loading={loading || webhookLoading} loading={loading || webhookLoading}
error={error || webhookError} error={error || webhookError}
retryOnError={() => { retryOnError={() => (repoMetadata ? refetchWebhook() : refreshMetadata())}>
refetchWebhook()
}}>
{repoMetadata && data && <WehookForm isEdit webhook={data} repoMetadata={repoMetadata} />} {repoMetadata && data && <WehookForm isEdit webhook={data} repoMetadata={repoMetadata} />}
</PageBody> </PageBody>
</Container> </Container>

View File

@ -30,7 +30,8 @@ enum WebhookEventType {
} }
enum WebhookIndividualEvent { enum WebhookIndividualEvent {
BRANCH_PUSHED = 'branch_pushed', BRANCH_CREATED = 'branch_created',
BRANCH_UPDATED = 'branch_updated',
BRANCH_DELETED = 'branch_deleted' BRANCH_DELETED = 'branch_deleted'
} }
@ -40,8 +41,9 @@ interface FormData {
enabled: boolean enabled: boolean
secure: boolean secure: boolean
events: WebhookEventType events: WebhookEventType
branchPush: boolean branchCreated: boolean
branchDeletion: boolean branchUpdated: boolean
branchDeleted: boolean
} }
interface WebHookFormProps extends Pick<GitInfoProps, 'repoMetadata'> { interface WebHookFormProps extends Pick<GitInfoProps, 'repoMetadata'> {
@ -52,7 +54,7 @@ interface WebHookFormProps extends Pick<GitInfoProps, 'repoMetadata'> {
export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps) { export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps) {
const history = useHistory() const history = useHistory()
const { getString } = useStrings() const { getString } = useStrings()
const { showError } = useToaster() const { showError, showSuccess } = useToaster()
const { routes } = useAppContext() const { routes } = useAppContext()
const { mutate, loading } = useMutate<OpenapiWebhookType>({ const { mutate, loading } = useMutate<OpenapiWebhookType>({
verb: isEdit ? 'PATCH' : 'POST', verb: isEdit ? 'PATCH' : 'POST',
@ -68,8 +70,9 @@ export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps)
secret: isEdit ? '***' : '', secret: isEdit ? '***' : '',
enabled: webhook ? (webhook?.enabled as boolean) : true, enabled: webhook ? (webhook?.enabled as boolean) : true,
secure: webhook?.insecure === false || false, secure: webhook?.insecure === false || false,
branchPush: webhook?.triggers?.includes(WebhookIndividualEvent.BRANCH_PUSHED) || false, branchCreated: webhook?.triggers?.includes(WebhookIndividualEvent.BRANCH_CREATED) || false,
branchDeletion: webhook?.triggers?.includes(WebhookIndividualEvent.BRANCH_DELETED) || false, branchUpdated: webhook?.triggers?.includes(WebhookIndividualEvent.BRANCH_UPDATED) || false,
branchDeleted: webhook?.triggers?.includes(WebhookIndividualEvent.BRANCH_DELETED) || false,
events: WebhookEventType.INDIVIDUAL events: WebhookEventType.INDIVIDUAL
}} }}
formName="create-webhook-form" formName="create-webhook-form"
@ -83,16 +86,20 @@ export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps)
onSubmit={formData => { onSubmit={formData => {
const triggers: OpenapiWebhookTrigger[] = [] const triggers: OpenapiWebhookTrigger[] = []
if (formData.branchPush) { if (formData.branchCreated) {
triggers.push(WebhookIndividualEvent.BRANCH_PUSHED) triggers.push(WebhookIndividualEvent.BRANCH_CREATED)
} }
if (formData.branchDeletion) { if (formData.branchUpdated) {
triggers.push(WebhookIndividualEvent.BRANCH_UPDATED)
}
if (formData.branchDeleted) {
triggers.push(WebhookIndividualEvent.BRANCH_DELETED) triggers.push(WebhookIndividualEvent.BRANCH_DELETED)
} }
if (!triggers.length) { if (!triggers.length) {
return showError('At least one event must be selected') return showError(getString('oneMustBeSelected'))
} }
const data: OpenapiUpdateWebhookRequest = { const data: OpenapiUpdateWebhookRequest = {
@ -105,12 +112,21 @@ export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps)
mutate(data) mutate(data)
.then((response: OpenapiWebhookType) => { .then((response: OpenapiWebhookType) => {
history.push( if (isEdit) {
routes.toCODEWebhookDetails({ showSuccess(getString('webhookUpdated'))
repoPath: repoMetadata.path as string, history.push(
webhookId: String(response.id) routes.toCODEWebhooks({
}) repoPath: repoMetadata.path as string
) })
)
} else {
history.push(
routes.toCODEWebhookDetails({
repoPath: repoMetadata.path as string,
webhookId: String(response.id)
})
)
}
}) })
.catch(exception => { .catch(exception => {
showError(getErrorMessage(exception)) showError(getErrorMessage(exception))
@ -152,8 +168,21 @@ export function WehookForm({ repoMetadata, isEdit, webhook }: WebHookFormProps)
<article <article
style={{ display: 'flex', gap: '6rem', flexWrap: 'wrap', marginLeft: '30px', marginTop: '10px' }}> style={{ display: 'flex', gap: '6rem', flexWrap: 'wrap', marginLeft: '30px', marginTop: '10px' }}>
<section> <section>
<FormInput.CheckBox label={'Branch push'} name="branchPush" className={css.checkbox} /> <FormInput.CheckBox
<FormInput.CheckBox label={'Branch deletion'} name="branchDeletion" className={css.checkbox} /> label={getString('webhookBranchCreated')}
name="branchCreated"
className={css.checkbox}
/>
<FormInput.CheckBox
label={getString('webhookBranchUpdated')}
name="branchUpdated"
className={css.checkbox}
/>
<FormInput.CheckBox
label={getString('webhookBranchDeleted')}
name="branchDeleted"
className={css.checkbox}
/>
</section> </section>
</article> </article>
) : null} ) : null}