1
- import React , { useEffect , useRef , useState } from 'react' ;
1
+ import React , { useRef , useState } from 'react' ;
2
2
import { ExpansionCard } from '@navikt/ds-react' ;
3
3
import { BarChartIcon , BriefcaseClockIcon , CalendarIcon , TasklistIcon } from '@navikt/aksel-icons' ;
4
4
import { AnalyticsEvents , logAnalyticsEvent } from 'utils/analytics' ;
5
5
import { usePageContentProps } from 'store/pageContext' ;
6
6
import { getDecoratorParams } from 'utils/decorator-utils' ;
7
7
import { innholdsTypeMap } from 'types/content-props/_content-common' ;
8
8
import { classNames } from 'utils/classnames' ;
9
- import { smoothScrollToTarget , handleStickyScrollOffset } from 'utils/scroll-to' ;
9
+ import { handleStickyScrollOffset } from 'utils/scroll-to' ;
10
10
import { Shortcuts , useShortcuts } from 'utils/useShortcuts' ;
11
11
import { ProductDetailType } from 'types/content-props/product-details' ;
12
-
12
+ import { useCheckAndOpenPanel } from 'store/hooks/useCheckAndOpenPanel' ;
13
13
import style from './Expandable.module.scss' ;
14
14
15
15
type Props = {
@@ -38,17 +38,12 @@ export const Expandable = ({
38
38
const contentProps = usePageContentProps ( ) ;
39
39
const { context } = getDecoratorParams ( contentProps ) ;
40
40
41
- useShortcuts ( {
42
- shortcut : Shortcuts . SEARCH ,
43
- callback : ( ) => {
44
- setIsOpen ( true ) ;
45
- } ,
46
- } ) ;
41
+ useShortcuts ( { shortcut : Shortcuts . SEARCH , callback : ( ) => setIsOpen ( true ) } ) ;
42
+ useCheckAndOpenPanel ( isOpen , setIsOpen , accordionRef , anchorId ) ;
47
43
48
44
const toggleExpandCollapse = ( isOpening : boolean , tittel : string ) => {
49
45
setIsOpen ( isOpening ) ;
50
46
handleStickyScrollOffset ( isOpening , accordionRef . current ) ;
51
-
52
47
logAnalyticsEvent ( isOpening ? AnalyticsEvents . ACC_EXPAND : AnalyticsEvents . ACC_COLLAPSE , {
53
48
tittel,
54
49
opprinnelse : analyticsOriginTag || 'utvidbar tekst' ,
@@ -58,68 +53,21 @@ export const Expandable = ({
58
53
} ) ;
59
54
} ;
60
55
61
- const checkAndOpenPanel = ( ) => {
62
- if ( isOpen ) {
63
- return ;
64
- }
65
-
66
- if ( window . location . toString ( ) . includes ( 'expandall=true' ) ) {
67
- setIsOpen ( true ) ;
68
- return ;
69
- }
70
-
71
- const targetId = window . location . hash . replace ( '#' , '' ) ;
72
- if ( ! targetId ) {
73
- return ;
74
- }
75
-
76
- if ( targetId === anchorId ) {
77
- setIsOpen ( true ) ;
78
- return ;
79
- }
80
-
81
- const elementWithId = document . getElementById ( targetId ) ;
82
- if ( accordionRef . current ?. contains ( elementWithId ) ) {
83
- setIsOpen ( true ) ;
84
- setTimeout ( ( ) => smoothScrollToTarget ( targetId ) , 500 ) ;
85
- }
86
- } ;
87
-
88
- const hashChangeHandler = ( ) => {
89
- checkAndOpenPanel ( ) ;
90
- } ;
91
-
92
56
const getHeaderIcon = ( ) => {
93
- if ( expandableType === ProductDetailType . PROCESSING_TIMES ) {
94
- return < BriefcaseClockIcon aria-hidden className = { style . headerIcon } /> ;
95
- }
96
- if ( expandableType === ProductDetailType . PAYOUT_DATES ) {
97
- return < CalendarIcon aria-hidden className = { style . headerIcon } /> ;
98
- }
99
- if ( expandableType === ProductDetailType . RATES ) {
100
- return < BarChartIcon aria-hidden className = { style . headerIcon } /> ;
57
+ switch ( expandableType ) {
58
+ case ProductDetailType . PROCESSING_TIMES :
59
+ return < BriefcaseClockIcon aria-hidden className = { style . headerIcon } /> ;
60
+ case ProductDetailType . PAYOUT_DATES :
61
+ return < CalendarIcon aria-hidden className = { style . headerIcon } /> ;
62
+ case ProductDetailType . RATES :
63
+ return < BarChartIcon aria-hidden className = { style . headerIcon } /> ;
64
+ case 'documentation_requirements' :
65
+ return < TasklistIcon aria-hidden className = { style . headerIcon } /> ;
66
+ default :
67
+ return null ;
101
68
}
102
- if ( expandableType === 'documentation_requirements' ) {
103
- return < TasklistIcon aria-hidden className = { style . headerIcon } /> ;
104
- }
105
- return null ;
106
69
} ;
107
70
108
- useEffect ( ( ) => {
109
- window . addEventListener ( 'hashchange' , hashChangeHandler ) ;
110
-
111
- return ( ) => {
112
- window . removeEventListener ( 'hashchange' , hashChangeHandler ) ;
113
- } ;
114
-
115
- // eslint-disable-next-line react-hooks/exhaustive-deps
116
- } , [ ] ) ;
117
-
118
- useEffect ( ( ) => {
119
- checkAndOpenPanel ( ) ;
120
- // eslint-disable-next-line react-hooks/exhaustive-deps
121
- } , [ anchorId ] ) ;
122
-
123
71
// Adjust appearande in styling if not type was set for this content
124
72
// This is the wrong use of this component, but some legacy pages have still to
125
73
// be upradet editorial wise.
0 commit comments