All files / modules/33-auth-settings/pages/AccountOverview/views AccountNameForm.tsx

89.29% Statements 25/28
50% Branches 4/8
80% Functions 4/5
89.29% Lines 25/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97              2x 2x 2x 2x 2x 2x   2x 2x 2x                       2x         2x 2x 2x         2x       2x 2x 2x     1x   1x   2x 2x     2x         2x             10x                                                   2x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import * as Yup from 'yup'
import { Button, FormInput, Formik, FormikForm, Layout, ButtonVariation } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/components'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useUpdateAccountNameNG } from 'services/cd-ng'
import { NameSchema } from '@common/utils/Validation'
import css from '../AccountOverview.module.scss'
 
interface FormValues {
  name: string
}
 
interface AccountNameFormProps {
  name: string
  setUpdateAccountName: (value: boolean) => void
  refetchAcct: () => void
}
 
const AccountNameForm: React.FC<AccountNameFormProps> = ({
  name,
  setUpdateAccountName,
  refetchAcct
}: AccountNameFormProps) => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const { showError } = useToaster()
  const {
    mutate: updateAcctName,
    loading: updatingAcctName,
    cancel
  } = useUpdateAccountNameNG({
    accountIdentifier: accountId
  })
 
  const handleSubmit = async (values: FormValues): Promise<FormValues> => {
    try {
      await updateAcctName({
        name: values.name.trim()
      })
      refetchAcct()
    } catch (err) {
      showError(err.data?.message || getString('somethingWentWrong'))
    }
    setUpdateAccountName(false)
    return values
  }
 
  return (
    <Formik<FormValues>
      initialValues={{
        name: name
      }}
      onSubmit={(value: FormValues) => handleSubmit(value)}
      formName="accountNameForm"
      validationSchema={Yup.object().shape({
        name: NameSchema()
      })}
    >
      {() => (
        <FormikForm>
          <Layout.Horizontal spacing="medium">
            <FormInput.Text name="name" className={css.nameInput} />
            <Button
              variation={ButtonVariation.PRIMARY}
              text={getString('save')}
              type="submit"
              disabled={updatingAcctName}
            />
            <Button
              text={getString('cancel')}
              onClick={() => {
                if (updatingAcctName) {
                  cancel()
                }
                setUpdateAccountName(false)
              }}
              variation={ButtonVariation.TERTIARY}
            />
          </Layout.Horizontal>
        </FormikForm>
      )}
    </Formik>
  )
}
 
export default AccountNameForm