|
1 | | -import React, {useState} from 'react'; |
2 | | -import {OutdialCallComponentProps} from '../task.types'; |
| 1 | +import React, {useEffect, useMemo, useState} from 'react'; |
| 2 | +import {OutdialAniEntry, OutdialCallComponentProps} from '../task.types'; |
3 | 3 | import './outdial-call.style.scss'; |
4 | 4 | import {withMetrics} from '@webex/cc-ui-logging'; |
| 5 | +import {Input, Button, Option, Select} from '@momentum-design/components/dist/react'; |
| 6 | +import {OutdialStrings, KEY_LIST} from './constants'; |
5 | 7 |
|
| 8 | +/** |
| 9 | + * OutdialCallComponent renders a dialpad UI for agents to initiate outbound calls. |
| 10 | + * It allows input of a destination number, selection of an ANI, and validates input. |
| 11 | + * |
| 12 | + * This component provides a keypad interface for entering a destination number, validates the input, |
| 13 | + * allows selection of an ANI (Automatic Number Identification), and triggers an outbound call action. |
| 14 | + * |
| 15 | + * @param props - Properties for the OutdialCallComponent. |
| 16 | + * @property startOutdial - Function to initiate the outdial call with the entered destination number. |
| 17 | + */ |
6 | 18 | const OutdialCallComponent: React.FunctionComponent<OutdialCallComponentProps> = (props) => { |
7 | | - const {startOutdial} = props; |
| 19 | + const {logger, startOutdial, getOutdialANIEntries} = props; |
| 20 | + |
| 21 | + // State Hooks |
8 | 22 | const [destination, setDestination] = useState(''); |
| 23 | + const [isValidNumber, setIsValidNumber] = useState(''); |
| 24 | + const [selectedANI, setSelectedANI] = useState(undefined); |
| 25 | + const [outdialANIList, setOutdialANIList] = useState<OutdialAniEntry[]>([]); |
| 26 | + |
| 27 | + // Validate the input format using regex from agent desktop |
| 28 | + const regExForDnSpecialChars = useMemo( |
| 29 | + () => new RegExp('^[+1][0-9]{3,18}$|^[*#][+1][0-9*#:]{3,18}$|^[0-9*#]{3,18}$'), |
| 30 | + [] |
| 31 | + ); |
| 32 | + |
| 33 | + // useEffect and useState to allow for async fetching of outdial ANI entries |
| 34 | + useEffect(() => { |
| 35 | + // Give Select an empty list if outdial ANI entries are not provided |
| 36 | + const updateOutdialANIList = async () => { |
| 37 | + try { |
| 38 | + const result = await getOutdialANIEntries(); |
| 39 | + setOutdialANIList(result); |
| 40 | + } catch (error) { |
| 41 | + logger?.error(`CC-Widgets: Task: Error fetching outdial ANI entries: ${error}`, { |
| 42 | + module: 'OutdialCallComponent', |
| 43 | + method: 'updateOutdialANIList', |
| 44 | + }); |
| 45 | + setOutdialANIList([]); |
| 46 | + } |
| 47 | + }; |
| 48 | + updateOutdialANIList(); |
| 49 | + }, []); |
9 | 50 |
|
10 | | - const updateOutboundNumber = (e: React.ChangeEvent<HTMLInputElement>) => { |
11 | | - // Allow only valid input that is digits, #, *, and + |
12 | | - const VALID_KEYPAD_CHARS = /[\d#*+]/g; |
13 | | - const filteredValue = e.target.value.match(VALID_KEYPAD_CHARS)?.join('') || ''; |
14 | | - setDestination(filteredValue); |
| 51 | + /** |
| 52 | + * validateOutboundNumber |
| 53 | + * @param value the dial number to validate |
| 54 | + * If the input is invalid, sets an error message on dial number input |
| 55 | + */ |
| 56 | + const validateOutboundNumber = (value: string) => { |
| 57 | + if (value && !regExForDnSpecialChars.test(value)) { |
| 58 | + setIsValidNumber(OutdialStrings.INCORRECT_DN_FORMAT); |
| 59 | + } else { |
| 60 | + setIsValidNumber(''); |
| 61 | + } |
15 | 62 | }; |
16 | 63 |
|
17 | | - // Function to press a key on the outdial keypad. |
18 | | - const handelKeyPress = (value: string) => { |
19 | | - setDestination((prev) => prev + value); |
| 64 | + /** |
| 65 | + * handleOnClick |
| 66 | + * @param value The key value pressed |
| 67 | + * Appends the pressed key to the destination input field |
| 68 | + */ |
| 69 | + const handleOnClick = (value: string) => { |
| 70 | + setDestination(destination + value); |
| 71 | + validateOutboundNumber(destination + value); |
20 | 72 | }; |
21 | 73 |
|
22 | 74 | return ( |
23 | | - <div className="out-dial-call-box"> |
24 | | - <section className="out-dial-call-section-box"> |
25 | | - <fieldset className="out-dial-call-fieldset"> |
26 | | - <legend className="out-dial-call-legend-box">Outdial Call</legend> |
27 | | - <div className="keypad"> |
28 | | - <input |
29 | | - onChange={updateOutboundNumber} |
30 | | - id="outBoundDialNumber" |
31 | | - placeholder="Enter number to dial" |
32 | | - value={destination} |
33 | | - /> |
34 | | - <div className="keys"> |
35 | | - {['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#'].map((key) => ( |
36 | | - <div key={key} className="key" onClick={() => handelKeyPress(key)}> |
37 | | - {key} |
38 | | - </div> |
39 | | - ))} |
40 | | - </div> |
41 | | - <button className="out-dial-call-btn" onClick={() => startOutdial(destination)}> |
42 | | - <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"> |
43 | | - <path d="M6.62 10.79a15.053 15.053 0 006.59 6.59l2.2-2.2a1 1 0 011.11-.27c1.12.45 2.33.69 3.58.69a1 1 0 011 1v3.5a1 1 0 01-1 1C10.29 21 3 13.71 3 4.5a1 1 0 011-1h3.5a1 1 0 011 1c0 1.25.24 2.46.69 3.58a1 1 0 01-.27 1.11l-2.2 2.2z" /> |
44 | | - </svg> |
45 | | - </button> |
46 | | - </div> |
47 | | - </fieldset> |
48 | | - </section> |
49 | | - </div> |
| 75 | + <article className="keypad" data-testid="outdial-call-container"> |
| 76 | + <Input |
| 77 | + className="outdial-input" |
| 78 | + id="outdial-number-input" |
| 79 | + name="outdial-number-input" |
| 80 | + data-testid="outdial-number-input" |
| 81 | + helpText={isValidNumber} |
| 82 | + helpTextType={isValidNumber ? 'error' : 'default'} |
| 83 | + placeholder={OutdialStrings.DN_PLACEHOLDER} |
| 84 | + value={destination} |
| 85 | + onChange={(e: unknown) => { |
| 86 | + const inputValue = (e as React.ChangeEvent<HTMLInputElement>).target.value; |
| 87 | + setDestination(inputValue); |
| 88 | + validateOutboundNumber(inputValue); |
| 89 | + }} |
| 90 | + /> |
| 91 | + <ul className="keys" data-testid="outdial-keypad-keys"> |
| 92 | + {KEY_LIST.map((key) => ( |
| 93 | + <li key={key}> |
| 94 | + <Button className="key button" onClick={() => handleOnClick(key)}> |
| 95 | + {key} |
| 96 | + </Button> |
| 97 | + </li> |
| 98 | + ))} |
| 99 | + </ul> |
| 100 | + <Select |
| 101 | + className="outdial-input" |
| 102 | + label={OutdialStrings.ANI_SELECT_LABEL} |
| 103 | + id="outdial-ani-option-select" |
| 104 | + name="outdial-ani-option-select" |
| 105 | + data-testid="outdial-ani-option-select" |
| 106 | + placeholder={OutdialStrings.ANI_SELECT_PLACEHOLDER} |
| 107 | + onChange={(event: CustomEvent) => { |
| 108 | + setSelectedANI(event.detail.value); |
| 109 | + }} |
| 110 | + > |
| 111 | + {outdialANIList.map((option: OutdialAniEntry, index: number) => { |
| 112 | + return ( |
| 113 | + <Option |
| 114 | + selected={option.number === selectedANI} |
| 115 | + key={index} |
| 116 | + value={option.number} |
| 117 | + name={`outdial-ani-option-${index}`} |
| 118 | + data-testid={`outdial-ani-option-${index}`} |
| 119 | + > |
| 120 | + {option.name} |
| 121 | + </Option> |
| 122 | + ); |
| 123 | + })} |
| 124 | + </Select> |
| 125 | + <Button |
| 126 | + data-testid="outdial-call-button" |
| 127 | + prefixIcon={'handset-regular'} |
| 128 | + onClick={() => startOutdial(destination, selectedANI)} |
| 129 | + disabled={!!isValidNumber || !destination} |
| 130 | + size={40} |
| 131 | + /> |
| 132 | + </article> |
50 | 133 | ); |
51 | 134 | }; |
52 | 135 |
|
|
0 commit comments