All files / modules/10-common/modals/ExtendTrial ExtendTrialOrFeedbackForm.tsx

97.3% Statements 36/37
100% Branches 8/8
90% Functions 9/10
97.14% Lines 34/35

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188              16x 16x 16x 16x 16x 16x 16x 16x                                           16x 16x 16x 16x     16x 18x 18x                                                           16x                 18x 18x                         16x 1x 1x   1x 1x       1x                         6x 6x                           16x             7x 7x   7x 4x       7x                           12x 12x                                              
/*
 * Copyright 2021 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, { useEffect } from 'react'
import { Formik, FormikForm, Heading, Container, Text, Layout, Button, FormInput, Tag } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import * as Yup from 'yup'
import { useStrings } from 'framework/strings'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, LicenseActions, FeedbackActions } from '@common/constants/TrackingConstants'
import css from './useExtendTrialOrFeedbackModal.module.scss'
 
export interface FeedbackFormValues {
  experience?: string
  suggestion?: string
}
 
interface FeedBackFormProps {
  moduleDescription: string
  onCloseModal: () => void
  onSubmit: (values: FeedbackFormValues) => void
  loading: boolean
}
 
interface ExtendTrialFormProps {
  expiryDateStr: string
  moduleDescription: string
  onCloseModal: () => void
  onSubmit: (values: FeedbackFormValues) => void
  loading: boolean
}
 
const enum EXPERIENCE {
  USEFUL = '10',
  NEEDMORE = '5',
  IMPROVE = '0'
}
 
const Feedback = ({ moduleDescription }: { moduleDescription: string }): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <Container padding={{ bottom: 'large' }}>
      <FormInput.RadioGroup
        className={css.experience}
        name="experience"
        label={getString('common.extendTrial.feedback.ifUseful', { moduleDescription: moduleDescription })}
        items={[
          {
            label: getString('common.extendTrial.feedback.answers.useful'),
            value: EXPERIENCE.USEFUL
          },
          {
            label: getString('common.extendTrial.feedback.answers.needMore'),
            value: EXPERIENCE.NEEDMORE
          },
          {
            label: getString('common.extendTrial.feedback.answers.improve'),
            value: EXPERIENCE.IMPROVE
          }
        ]}
      />
      <FormInput.TextArea
        className={css.suggestion}
        name="suggestion"
        label={getString('common.extendTrial.feedback.suggestion', { moduleDescription: moduleDescription })}
      />
    </Container>
  )
}
 
const Footer = ({
  onCloseModal,
  loading,
  hasInput
}: {
  onCloseModal: () => void
  loading: boolean
  hasInput: boolean
}): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <Layout.Horizontal spacing="medium">
      <Button intent="primary" text={getString('submit')} type="submit" disabled={loading || !hasInput} />
      <Button
        intent="none"
        text={getString('common.extendTrial.doItLater')}
        type="reset"
        onClick={() => onCloseModal()}
      />
    </Layout.Horizontal>
  )
}
 
export const FeedBackForm: React.FC<FeedBackFormProps> = ({ onCloseModal, onSubmit, moduleDescription, loading }) => {
  const { getString } = useStrings()
  const { trackEvent } = useTelemetry()
 
  useEffect(() => {
    trackEvent(FeedbackActions.LoadFeedback, {
      category: Category.FEEDBACK
    })
  }, [])
  return (
    <Formik
      initialValues={{
        experience: undefined,
        suggestion: ''
      }}
      onSubmit={onSubmit}
      formName="feedbackForm"
      validationSchema={Yup.object().shape({
        experience: Yup.string().trim().required(getString('validation.thisIsARequiredField'))
      })}
    >
      {formikProps => {
        const hasInput = formikProps.values.experience || formikProps.values.suggestion.trim() !== ''
        return (
          <FormikForm>
            <Heading color={Color.BLACK} font={{ size: 'large', weight: 'bold' }} padding={{ bottom: 'large' }}>
              {`${moduleDescription} ${getString('common.extendTrial.feedback.title')}`}
            </Heading>
            <Feedback moduleDescription={moduleDescription} />
            <Footer onCloseModal={onCloseModal} loading={loading} hasInput={hasInput} />
          </FormikForm>
        )
      }}
    </Formik>
  )
}
 
export const ExtendTrialForm: React.FC<ExtendTrialFormProps> = ({
  expiryDateStr,
  onCloseModal,
  onSubmit,
  moduleDescription,
  loading
}) => {
  const { getString } = useStrings()
  const { trackEvent } = useTelemetry()
 
  useEffect(() => {
    trackEvent(LicenseActions.LoadExtendedTrial, {
      category: Category.LICENSE
    })
  }, [])
  return (
    <Formik
      initialValues={{
        experience: undefined,
        suggestion: ''
      }}
      onSubmit={onSubmit}
      enableReinitialize={true}
      formName="extendTrialForm"
      validationSchema={Yup.object().shape({
        experience: Yup.string().trim().required(getString('validation.thisIsARequiredField'))
      })}
    >
      {formikProps => {
        const hasInput = formikProps.values.experience || formikProps.values.suggestion.trim() !== ''
        return (
          <FormikForm className={css.extendTrial}>
            <Heading color={Color.BLACK} font={{ size: 'large', weight: 'bold' }} padding={{ bottom: 'large' }}>
              {getString('common.extendTrial.heading')}
            </Heading>
            <Tag round className={css.tag}>
              {getString('common.trialInProgress')}
            </Tag>
            <Text padding={{ top: 'small', bottom: 'large' }} className={css.description}>
              {getString('common.extendTrial.description')}
            </Text>
            <Layout.Horizontal padding={{ bottom: 'large' }}>
              <Text padding={{ right: 'small' }}>{`${getString('common.extendTrial.expiryDate')}:`}</Text>
              <Text color={Color.BLACK}>{expiryDateStr}</Text>
            </Layout.Horizontal>
            <Feedback moduleDescription={moduleDescription} />
            <Footer onCloseModal={onCloseModal} loading={loading} hasInput={hasInput} />
          </FormikForm>
        )
      }}
    </Formik>
  )
}