Skip to content

Commit

Permalink
feat(plan) open combobox on "add charge" click
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Jul 17, 2023
1 parent 8c56a3a commit 237588c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ditto/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@
"text_648c2be974f70300748a4cd4": "Add a recurring charge",
"text_649c54823c90890062476255": "Prorate charge amount",
"text_649c47c0a6c1f200de8ff48d": "Prorated charge",
"text_649c54823c9089006247625a": "Can’t be prorated due to {{chargeModel}} pricing model defined in above.",
"text_649c54823c9089006247625a": "Can’t be prorated due to {{chargeModel}} pricing model defined above.",
"text_649c49bcebd91c0082d84446": "Full charge",
"text_649c54823c90890062476259": "Based between the event reception date and the end date of the period",
"text_645bb193927b375079d28a8f": "Taxes",
Expand Down
21 changes: 8 additions & 13 deletions src/components/plans/ChargeAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ export const ChargeAccordion = memo(

const isProratedOptionDisabled = useMemo(() => {
return (
localCharge.chargeModel === ChargeModelEnum.Volume ||
(localCharge.billableMetric.recurring &&
localCharge.chargeModel === ChargeModelEnum.Graduated) ||
localCharge.chargeModel === ChargeModelEnum.Package ||
localCharge.chargeModel === ChargeModelEnum.Percentage
)
}, [localCharge.chargeModel])
}, [localCharge.billableMetric.recurring, localCharge.chargeModel])

const proratedOptionHelperText = useMemo(() => {
if (isProratedOptionDisabled)
Expand Down Expand Up @@ -300,12 +301,12 @@ export const ChargeAccordion = memo(
label: translate('text_624aa732d6af4e0103d40e6f'),
value: ChargeModelEnum.Standard,
},
{
label: translate('text_62793bbb599f1c01522e919f'),
value: ChargeModelEnum.Graduated,
},
...(!localCharge.billableMetric.recurring
? [
{
label: translate('text_62793bbb599f1c01522e919f'),
value: ChargeModelEnum.Graduated,
},
{
label: translate('text_62a0b7107afa2700a65ef6e2'),
value: ChargeModelEnum.Percentage,
Expand Down Expand Up @@ -435,13 +436,7 @@ export const ChargeAccordion = memo(
<Switch
name={`charge-${localCharge.id}-prorated`}
label={translate('text_649c54823c90890062476255')}
disabled={
disabled ||
(localCharge.billableMetric.recurring &&
localCharge.chargeModel === ChargeModelEnum.Graduated) ||
localCharge.chargeModel === ChargeModelEnum.Package ||
localCharge.chargeModel === ChargeModelEnum.Percentage
}
disabled={disabled || isProratedOptionDisabled}
subLabel={proratedOptionHelperText}
checked={!!localCharge.prorated}
onChange={(value) => handleUpdate('prorated', Boolean(value))}
Expand Down
52 changes: 35 additions & 17 deletions src/components/plans/ChargesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
useGetRecurringBillableMetricsLazyQuery,
} from '~/generated/graphql'
import { Item } from '~/components/form/ComboBox/ComboBoxItem'
import {
MUI_INPUT_BASE_ROOT_CLASSNAME,
SEARCH_METERED_CHARGE_INPUT_CLASSNAME,
SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME,
} from '~/core/constants/form'

import { LocalChargeInput, PlanFormInput } from './types'
import { ChargeAccordion } from './ChargeAccordion'
Expand Down Expand Up @@ -76,9 +81,6 @@ interface ChargesSectionProps {

const getNewChargeId = (id: string, index: number) => `plan-charge-${id}-${index}`

const SEARCH_METERED_CHARGE_INPUT_NAME = 'searchMeteredChargeInput'
const SEARCH_RECURRING_CHARGE_INPUT_NAME = 'searchRecurringChargeInput'

export const ChargesSection = memo(
({
canBeEdited,
Expand Down Expand Up @@ -208,14 +210,14 @@ export const ChargesSection = memo(
if (!showAddMeteredCharge) setShowAddMeteredCharge(true)

setTimeout(() => {
const element = document.getElementsByName(
SEARCH_METERED_CHARGE_INPUT_NAME
)[0]
const element = document.querySelector(
`.${SEARCH_METERED_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement

if (!element) return

element.scrollIntoView({ behavior: 'smooth' })
element.focus()
element.click()

closePopper()
}, 0)
Expand All @@ -230,14 +232,14 @@ export const ChargesSection = memo(
if (!showAddRecurringCharge) setShowAddRecurringCharge(true)

setTimeout(() => {
const element = document.getElementsByName(
SEARCH_RECURRING_CHARGE_INPUT_NAME
)[0]
const element = document.querySelector(
`.${SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement

if (!element) return

element.scrollIntoView({ behavior: 'smooth' })
element.focus()
element.click()

closePopper()
}, 0)
Expand Down Expand Up @@ -305,7 +307,7 @@ export const ChargesSection = memo(
{!!showAddMeteredCharge && (
<AddChargeInlineWrapper>
<ComboBox
name={SEARCH_METERED_CHARGE_INPUT_NAME}
className={SEARCH_METERED_CHARGE_INPUT_CLASSNAME}
data={meteredBillableMetrics}
searchQuery={getMeteredBillableMetrics}
loading={meteredBillableMetricsLoading}
Expand Down Expand Up @@ -369,7 +371,11 @@ export const ChargesSection = memo(
onClick={() => {
setShowAddMeteredCharge(true)
setTimeout(() => {
document.getElementsByName(SEARCH_METERED_CHARGE_INPUT_NAME)[0].focus()
;(
document.querySelector(
`.${SEARCH_METERED_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement
)?.click()
}, 0)
}}
>
Expand All @@ -384,7 +390,11 @@ export const ChargesSection = memo(
onClick={() => {
setShowAddRecurringCharge(true)
setTimeout(() => {
document.getElementsByName(SEARCH_RECURRING_CHARGE_INPUT_NAME)[0].focus()
;(
document.querySelector(
`.${SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement
)?.click()
}, 0)
}}
>
Expand Down Expand Up @@ -441,7 +451,7 @@ export const ChargesSection = memo(
{!!showAddRecurringCharge && (
<AddChargeInlineWrapper>
<ComboBox
name={SEARCH_RECURRING_CHARGE_INPUT_NAME}
className={SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME}
data={recurringBillableMetrics}
searchQuery={getRecurringBillableMetrics}
loading={recurringBillableMetricsLoading}
Expand Down Expand Up @@ -502,7 +512,11 @@ export const ChargesSection = memo(
onClick={() => {
setShowAddMeteredCharge(true)
setTimeout(() => {
document.getElementsByName(SEARCH_METERED_CHARGE_INPUT_NAME)[0].focus()
;(
document.querySelector(
`.${SEARCH_METERED_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement
)?.click()
}, 0)
}}
>
Expand All @@ -517,7 +531,11 @@ export const ChargesSection = memo(
onClick={() => {
setShowAddRecurringCharge(true)
setTimeout(() => {
document.getElementsByName(SEARCH_RECURRING_CHARGE_INPUT_NAME)[0].focus()
;(
document.querySelector(
`.${SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME} .${MUI_INPUT_BASE_ROOT_CLASSNAME}`
) as HTMLElement
)?.click()
}, 0)
}}
>
Expand Down
3 changes: 2 additions & 1 deletion src/core/constants/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export enum FORM_ERRORS_ENUM {
}

// Selectors
export const SEARCH_CHARGE_INPUT_NAME = 'searchChargeInput'
export const SEARCH_METERED_CHARGE_INPUT_CLASSNAME = 'searchMeteredChargeInput'
export const SEARCH_RECURRING_CHARGE_INPUT_CLASSNAME = 'searchRecurringChargeInput'
export const MUI_INPUT_BASE_ROOT_CLASSNAME = 'MuiInputBase-root'

0 comments on commit 237588c

Please sign in to comment.