Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gql-api-keys: only setState once when duplicating key #3792

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions web/src/app/admin/admin-api-keys/AdminAPIKeyCreateDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useState } from 'react'
import { gql, useMutation, useQuery } from 'urql'
import CopyText from '../../util/CopyText'
import { fieldErrors, nonFieldErrors } from '../../util/errutil'
Expand Down Expand Up @@ -57,42 +57,46 @@ function nextName(name: string): string {
return `${base} ${parseInt(num) + 1}`
}

function nextExpiration(expiresAt: string, createdAt: string): string {
const created = DateTime.fromISO(createdAt)
const expires = DateTime.fromISO(expiresAt)

const keyLifespan = expires.diff(created, 'days').days

return DateTime.utc().plus({ days: keyLifespan }).toISO()
}

export default function AdminAPIKeyCreateDialog(props: {
onClose: () => void
fromID?: string
}): React.ReactNode {
const [value, setValue] = useState<CreateGQLAPIKeyInput>({
name: '',
description: '',
expiresAt: DateTime.utc().plus({ days: 7 }).toISO(),
query: '',
role: 'user',
})
const [status, createKey] = useMutation(newGQLAPIKeyQuery)
const token = status.data?.createGQLAPIKey?.token || null
const [{ data }] = useQuery({
query: fromExistingQuery,
pause: !props.fromID,
})

useEffect(() => {
if (!data?.gqlAPIKeys?.length) return
const from = data.gqlAPIKeys.find((k: GQLAPIKey) => k.id === props.fromID)
if (!from) return

const created = DateTime.fromISO(from.createdAt)
const expires = DateTime.fromISO(from.expiresAt)

const keyLifespan = expires.diff(created, 'days').days

setValue({
name: nextName(from.name),
description: from.description,
query: from.query,
expiresAt: DateTime.utc().plus({ days: keyLifespan }).toISO(),
role: from.role,
})
}, [data?.gqlAPIKeys])
const oldKey = (data?.gqlAPIKeys || []).find(
(k: GQLAPIKey) => k.id === props.fromID,
)
if (props.fromID && !oldKey) throw new Error('API key not found')
const [value, setValue] = useState<CreateGQLAPIKeyInput>(
oldKey
? {
name: nextName(oldKey.name),
description: oldKey.description,
query: oldKey.query,
role: oldKey.role,
expiresAt: nextExpiration(oldKey.expiresAt, oldKey.createdAt),
}
: {
name: '',
description: '',
expiresAt: DateTime.utc().plus({ days: 7 }).toISO(),
query: '',
role: 'user',
},
)

// handles form on submit event, based on the action type (edit, create) it will send the necessary type of parameter
// token is also being set here when create action is used
Expand Down
Loading