import React, { PureComponent, useRef, useState } from 'react'; import { ServiceAccountDTO } from 'app/types'; import { css, cx } from '@emotion/css'; import { config } from 'app/core/config'; import { GrafanaTheme } from '@grafana/data'; import { Button, ConfirmButton, ConfirmModal, Input, LegacyInputStatus, stylesFactory } from '@grafana/ui'; interface Props { serviceaccount: ServiceAccountDTO; onServiceAccountUpdate: (serviceaccount: ServiceAccountDTO) => void; onServiceAccountDelete: (serviceaccountId: number) => void; onServiceAccountDisable: (serviceaccountId: number) => void; onServiceAccountEnable: (serviceaccountId: number) => void; } export function ServiceAccountProfile({ serviceaccount, onServiceAccountUpdate, onServiceAccountDelete, onServiceAccountDisable, onServiceAccountEnable, }: Props) { const [showDeleteModal, setShowDeleteModal] = useState(false); const [showDisableModal, setShowDisableModal] = useState(false); const deleteServiceAccountRef = useRef(null); const showDeleteServiceAccountModal = (show: boolean) => () => { setShowDeleteModal(show); if (!show && deleteServiceAccountRef.current) { deleteServiceAccountRef.current.focus(); } }; const disableServiceAccountRef = useRef(null); const showDisableServiceAccountModal = (show: boolean) => () => { setShowDisableModal(show); if (!show && disableServiceAccountRef.current) { disableServiceAccountRef.current.focus(); } }; const handleServiceAccountDelete = () => onServiceAccountDelete(serviceaccount.id); const handleServiceAccountDisable = () => onServiceAccountDisable(serviceaccount.id); const handleServiceAccountEnable = () => onServiceAccountEnable(serviceaccount.id); const onServiceAccountNameChange = (newValue: string) => { onServiceAccountUpdate({ ...serviceaccount, name: newValue, }); }; const styles = getStyles(config.theme); return ( <>

Information

<> ); } const getStyles = stylesFactory((theme: GrafanaTheme) => { return { buttonRow: css` margin-top: 0.8rem; > * { margin-right: 16px; } `, }; }); interface ServiceAccountProfileRowProps { label: string; value?: string; inputType?: string; onChange?: (value: string) => void; } interface ServiceAccountProfileRowState { value: string; editing: boolean; } export class ServiceAccountProfileRow extends PureComponent< ServiceAccountProfileRowProps, ServiceAccountProfileRowState > { inputElem?: HTMLInputElement; static defaultProps: Partial = { value: '', inputType: 'text', }; state = { editing: false, value: this.props.value || '', }; setInputElem = (elem: any) => { this.inputElem = elem; }; onEditClick = () => { this.setState({ editing: true }, this.focusInput); }; onCancelClick = () => { this.setState({ editing: false, value: this.props.value || '' }); }; onInputChange = (event: React.ChangeEvent, status?: LegacyInputStatus) => { if (status === LegacyInputStatus.Invalid) { return; } this.setState({ value: event.target.value }); }; onInputBlur = (event: React.FocusEvent, status?: LegacyInputStatus) => { if (status === LegacyInputStatus.Invalid) { return; } this.setState({ value: event.target.value }); }; focusInput = () => { if (this.inputElem && this.inputElem.focus) { this.inputElem.focus(); } }; onSave = () => { if (this.props.onChange) { this.props.onChange(this.state.value); } }; render() { const { label, inputType } = this.props; const { value } = this.state; const labelClass = cx( 'width-16', css` font-weight: 500; ` ); const inputId = `${label}-input`; return ( {this.state.editing ? ( ) : ( {this.props.value} )} Edit ); } }