@@ -2,15 +2,22 @@ import React, { useState, useEffect } from 'react'
2
2
import './Header.css'
3
3
import logo from '../logo.svg'
4
4
import { NavLink , RouteComponentProps , withRouter } from 'react-router-dom' ;
5
+ import { BasketSummary } from './BasketSummary'
6
+ import { IApplicationState } from './Store' ;
7
+ import { connect } from 'react-redux' ;
8
+ interface IHeader extends RouteComponentProps {
9
+ basketCount : number
10
+ }
5
11
6
- const Header_ : React . FC < RouteComponentProps > = ( { location : { search : urlSearch } , history} ) => {
12
+ const Header_ : React . FC < IHeader > = ( { location : { search : urlSearch } , history, basketCount } ) => {
7
13
const [ search , setSearch ] = useState < string > ( urlSearch || '' )
14
+
8
15
useEffect ( ( ) => {
9
16
const searchParams = new window . URLSearchParams ( urlSearch )
10
17
setSearch ( s => searchParams . get ( 'search' ) || '' )
11
18
} , [ urlSearch ] )
12
19
13
- const handleSearchOnChange : React . ChangeEventHandler < HTMLInputElement > = ( { currentTarget : { value } } ) => {
20
+ const handleSearchOnChange : React . ChangeEventHandler < HTMLInputElement > = ( { currentTarget : { value } } ) => {
14
21
setSearch ( value )
15
22
}
16
23
@@ -28,6 +35,7 @@ const Header_: React.FC<RouteComponentProps> = ({location: { search: urlSearch }
28
35
value = { search }
29
36
onChange = { handleSearchOnChange }
30
37
onKeyDown = { handleSearchOnKeyDown } />
38
+ < BasketSummary count = { basketCount } />
31
39
</ form >
32
40
< img src = { logo } alt = "logo" className = "header-logo" />
33
41
< h1 className = "header-title" > React Twinkle Shop</ h1 >
@@ -40,4 +48,8 @@ const Header_: React.FC<RouteComponentProps> = ({location: { search: urlSearch }
40
48
)
41
49
}
42
50
43
- export const Header = withRouter ( Header_ )
51
+ const mapStateToProps = ( { basket : { products } } : IApplicationState ) => ( {
52
+ basketCount : products . length
53
+ } )
54
+
55
+ export const Header = connect ( mapStateToProps ) ( withRouter ( Header_ ) )
0 commit comments