1
- import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
2
- import { TextField , TooltipHost } from 'office-ui-fabric-react' ;
3
1
import * as React from 'react' ;
4
2
import { _t } from '../../../utils/Translator' ;
5
- import { faArrowLeft , faRedoAlt , faSearch } from '@fortawesome/free-solid-svg-icons' ;
6
3
import { ContentType , HttpVerb , sendHttpRequest } from '../../../utils/httpRequest' ;
7
- import api from '../../api' ;
4
+ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
5
+ import { TextField , TooltipHost } from 'office-ui-fabric-react' ;
6
+ import { faArrowLeft , faRedoAlt , faSearch } from '@fortawesome/free-solid-svg-icons' ;
8
7
import AppContext from '../AppContext' ;
8
+ import HelpdeskTicket from '../../../classes/HelpdeskTicket' ;
9
9
import Lead from '../../../classes/Lead' ;
10
10
import Partner from '../../../classes/Partner' ;
11
11
import Task from '../../../classes/Task' ;
12
- import HelpdeskTicket from '../../../classes/HelpdeskTicket ' ;
12
+ import api from '../../api ' ;
13
13
14
14
type SearchRefrechProps = {
15
15
title : string ;
16
16
partner : Partner ;
17
17
searchType : 'lead' | 'task' | 'ticket' ;
18
18
setIsSearching : ( isSearching : boolean ) => void ;
19
19
setIsLoading : ( isLoading : boolean ) => void ;
20
- updateRecords : ( records : Lead [ ] | Task [ ] | HelpdeskTicket [ ] | any | null ) => void ;
20
+ updateRecords : ( records : Lead [ ] | Task [ ] | HelpdeskTicket [ ] ) => void ;
21
21
} ;
22
22
23
23
type SearchRefrechState = {
@@ -34,89 +34,91 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
34
34
} ;
35
35
}
36
36
37
- render ( ) {
38
- let broadCampStyle = {
39
- display : 'flex' ,
40
- justifyContent : 'space-between' ,
41
- alignItems : 'center' ,
42
- fontSize : 'medium' ,
43
- color : '#787878' ,
44
- fontWeight : 600 ,
45
- marginRight : '10px' ,
46
- } ;
47
-
48
- const onBackClick = ( ) => {
37
+ private onBackClick = ( ) => {
49
38
this . setState ( { isSearching : ! this . state . isSearching , query : '' } ) ;
50
39
this . props . updateRecords ( null ) ;
51
40
this . props . setIsSearching ( false ) ;
52
41
} ;
53
42
54
- const onSearchClick = ( ) => {
55
- this . setState ( { isSearching : true } ) ;
56
- this . props . setIsSearching ( true ) ;
57
- } ;
43
+ private onRefrechClick = ( ) => {
44
+ this . searchData ( ) ;
45
+ } ;
58
46
59
- const onKeyDown = ( event ) => {
60
- if ( event . key == 'Enter' ) {
61
- if ( ! this . state . query . trim ( ) ) {
62
- return ;
63
- }
64
- searchData ( this . state . query ) ;
65
- }
66
- } ;
47
+ private onSearchClick = ( ) => {
48
+ this . setState ( { isSearching : true } ) ;
49
+ this . props . setIsSearching ( true ) ;
50
+ } ;
67
51
68
- const onRefrechClick = ( ) => {
69
- if ( this . state . query ) {
70
- searchData ( this . state . query ) ;
52
+ private onKeyDown = ( event ) => {
53
+ if ( event . key == 'Enter' ) {
54
+ if ( ! this . state . query . trim ( ) ) {
55
+ return ;
71
56
}
72
- } ;
73
-
74
- const searchData = async ( query : string ) => {
75
- this . props . setIsLoading ( true ) ;
76
-
77
- let endPoint = '' ;
57
+ this . searchData ( this . state . query ) ;
58
+ }
59
+ } ;
60
+
61
+ private getEndPoint = ( params : string ) => {
62
+ let endPoint = '' ;
63
+ if ( this . props . searchType === 'lead' ) {
64
+ endPoint = api . Leads ;
65
+ } else if ( this . props . searchType === 'task' ) {
66
+ endPoint = api . Tasks ;
67
+ } else {
68
+ endPoint = api . Tickets ;
69
+ }
70
+ return endPoint + `/${ params } ` ;
71
+ } ;
72
+
73
+ private searchData = async ( query ?: string ) => {
74
+ this . props . setIsLoading ( true ) ;
75
+ let endPoint = '' ;
76
+ if ( query ) {
77
+ endPoint = this . getEndPoint ( 'search' ) ;
78
+ } else {
79
+ endPoint = this . getEndPoint ( 'refresh' ) ;
80
+ }
81
+ try {
82
+ const res = sendHttpRequest (
83
+ HttpVerb . POST ,
84
+ api . baseURL + endPoint ,
85
+ ContentType . Json ,
86
+ this . context . getConnectionToken ( ) ,
87
+ { query : query , partner : this . props . partner } ,
88
+ true ,
89
+ ) ;
90
+ const data = JSON . parse ( await res . promise ) ;
78
91
if ( this . props . searchType === 'lead' ) {
79
- endPoint = api . getLeads ;
92
+ data . result = data . result . map ( ( lead : Lead ) => Lead . fromJSON ( lead ) ) ;
80
93
} else if ( this . props . searchType === 'task' ) {
81
- endPoint = api . getTasks ;
94
+ data . result = data . result . map ( ( task : Task ) => Task . fromJSON ( task ) ) ;
82
95
} else {
83
- endPoint = api . getTickets ;
96
+ data . result = data . result . map ( ( ticket : HelpdeskTicket ) => HelpdeskTicket . fromJSON ( ticket ) ) ;
84
97
}
85
-
86
- try {
87
- const res = sendHttpRequest (
88
- HttpVerb . POST ,
89
- api . baseURL + endPoint ,
90
- ContentType . Json ,
91
- this . context . getConnectionToken ( ) ,
92
- { query : query , partner_id : this . props . partner . id } ,
93
- true ,
94
- ) ;
95
-
96
- const data = JSON . parse ( await res . promise ) ;
97
-
98
- if ( this . props . searchType === 'lead' ) {
99
- data . result = data . result . map ( ( lead : Lead ) => Lead . fromJSON ( lead ) ) ;
100
- } else if ( this . props . searchType === 'task' ) {
101
- data . result = data . result . map ( ( task : Task ) => Task . fromJSON ( task ) ) ;
102
- } else {
103
- data . result = data . result . map ( ( ticket : HelpdeskTicket ) => HelpdeskTicket . fromJSON ( ticket ) ) ;
104
- }
105
-
106
- if ( data . result ) {
107
- this . props . updateRecords ( data . result ) ;
108
- }
109
- this . props . setIsLoading ( false ) ;
110
- } catch ( error ) {
111
- this . props . setIsLoading ( false ) ;
112
- this . context . showHttpErrorMessage ( error ) ;
98
+ if ( data . result ) {
99
+ this . props . updateRecords ( data . result ) ;
113
100
}
101
+ this . props . setIsLoading ( false ) ;
102
+ } catch ( error ) {
103
+ this . props . setIsLoading ( false ) ;
104
+ this . context . showHttpErrorMessage ( error ) ;
105
+ }
106
+ } ;
107
+
108
+ render ( ) {
109
+ let broadCampStyle = {
110
+ display : 'flex' ,
111
+ justifyContent : 'space-between' ,
112
+ alignItems : 'center' ,
113
+ fontSize : 'medium' ,
114
+ color : '#787878' ,
115
+ fontWeight : 600 ,
114
116
} ;
115
117
116
118
let backButton = null ;
117
119
if ( this . state . isSearching ) {
118
120
backButton = (
119
- < div className = "odoo-muted-button" onClick = { onBackClick } style = { { border : 'none' } } >
121
+ < div className = "odoo-muted-button" onClick = { this . onBackClick } style = { { border : 'none' } } >
120
122
< FontAwesomeIcon icon = { faArrowLeft } />
121
123
</ div >
122
124
) ;
@@ -127,7 +129,7 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
127
129
searchButton = (
128
130
< div style = { { display : 'flex' } } >
129
131
< TooltipHost content = { _t ( `Search ${ this . props . title . slice ( 0 , this . props . title . indexOf ( ' ' ) ) } ` ) } >
130
- < div className = "odoo-muted-button" onClick = { onSearchClick } style = { { border : 'none' } } >
132
+ < div className = "odoo-muted-button" onClick = { this . onSearchClick } style = { { border : 'none' } } >
131
133
< FontAwesomeIcon icon = { faSearch } />
132
134
</ div >
133
135
</ TooltipHost >
@@ -144,26 +146,26 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
144
146
style = { { marginLeft : '2px' , marginRight : '2px' } }
145
147
placeholder = { _t ( `Search ${ this . props . title . slice ( 0 , this . props . title . indexOf ( ' ' ) ) } ` ) }
146
148
value = { this . state . query }
147
- onKeyDown = { onKeyDown }
149
+ onKeyDown = { this . onKeyDown }
148
150
onChange = { ( _ , newValue ) => this . setState ( { query : newValue || '' } ) }
149
151
onFocus = { ( e ) => e . target . select ( ) }
150
152
autoFocus
151
153
/>
152
154
< div
153
155
className = "odoo-muted-button search-icon"
154
156
style = { { border : 'none' } }
155
- onClick = { ( ) => searchData ( this . state . query ) } >
157
+ onClick = { ( ) => this . searchData ( this . state . query ) } >
156
158
< FontAwesomeIcon icon = { faSearch } />
157
159
</ div >
158
160
</ div >
159
161
) ;
160
162
}
161
163
162
164
let refrechButton = null ;
163
- if ( this . state . isSearching ) {
165
+ if ( ! this . state . isSearching ) {
164
166
refrechButton = (
165
167
< TooltipHost content = { _t ( 'Refresh search' ) } >
166
- < div className = "odoo-muted-button" onClick = { onRefrechClick } style = { { border : 'none' } } >
168
+ < div className = "odoo-muted-button" onClick = { this . onRefrechClick } style = { { border : 'none' } } >
167
169
< FontAwesomeIcon icon = { faRedoAlt } />
168
170
</ div >
169
171
</ TooltipHost >
@@ -174,8 +176,8 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
174
176
< div style = { broadCampStyle } >
175
177
{ backButton }
176
178
{ searchButton }
177
- { searchBar }
178
179
{ refrechButton }
180
+ { searchBar }
179
181
</ div >
180
182
) ;
181
183
}
0 commit comments