1- import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
2- import { TextField , TooltipHost } from 'office-ui-fabric-react' ;
31import * as React from 'react' ;
42import { _t } from '../../../utils/Translator' ;
5- import { faArrowLeft , faRedoAlt , faSearch } from '@fortawesome/free-solid-svg-icons' ;
63import { 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' ;
87import AppContext from '../AppContext' ;
8+ import HelpdeskTicket from '../../../classes/HelpdeskTicket' ;
99import Lead from '../../../classes/Lead' ;
1010import Partner from '../../../classes/Partner' ;
1111import Task from '../../../classes/Task' ;
12- import HelpdeskTicket from '../../../classes/HelpdeskTicket ' ;
12+ import api from '../../api ' ;
1313
1414type SearchRefrechProps = {
1515 title : string ;
1616 partner : Partner ;
1717 searchType : 'lead' | 'task' | 'ticket' ;
1818 setIsSearching : ( isSearching : boolean ) => void ;
1919 setIsLoading : ( isLoading : boolean ) => void ;
20- updateRecords : ( records : Lead [ ] | Task [ ] | HelpdeskTicket [ ] | any | null ) => void ;
20+ updateRecords : ( records : Lead [ ] | Task [ ] | HelpdeskTicket [ ] ) => void ;
2121} ;
2222
2323type SearchRefrechState = {
@@ -34,89 +34,91 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
3434 } ;
3535 }
3636
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 = ( ) => {
4938 this . setState ( { isSearching : ! this . state . isSearching , query : '' } ) ;
5039 this . props . updateRecords ( null ) ;
5140 this . props . setIsSearching ( false ) ;
5241 } ;
5342
54- const onSearchClick = ( ) => {
55- this . setState ( { isSearching : true } ) ;
56- this . props . setIsSearching ( true ) ;
57- } ;
43+ private onRefrechClick = ( ) => {
44+ this . searchData ( ) ;
45+ } ;
5846
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+ } ;
6751
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 ;
7156 }
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 ) ;
7891 if ( this . props . searchType === 'lead' ) {
79- endPoint = api . getLeads ;
92+ data . result = data . result . map ( ( lead : Lead ) => Lead . fromJSON ( lead ) ) ;
8093 } else if ( this . props . searchType === 'task' ) {
81- endPoint = api . getTasks ;
94+ data . result = data . result . map ( ( task : Task ) => Task . fromJSON ( task ) ) ;
8295 } else {
83- endPoint = api . getTickets ;
96+ data . result = data . result . map ( ( ticket : HelpdeskTicket ) => HelpdeskTicket . fromJSON ( ticket ) ) ;
8497 }
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 ) ;
113100 }
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 ,
114116 } ;
115117
116118 let backButton = null ;
117119 if ( this . state . isSearching ) {
118120 backButton = (
119- < div className = "odoo-muted-button" onClick = { onBackClick } style = { { border : 'none' } } >
121+ < div className = "odoo-muted-button" onClick = { this . onBackClick } style = { { border : 'none' } } >
120122 < FontAwesomeIcon icon = { faArrowLeft } />
121123 </ div >
122124 ) ;
@@ -127,7 +129,7 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
127129 searchButton = (
128130 < div style = { { display : 'flex' } } >
129131 < 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' } } >
131133 < FontAwesomeIcon icon = { faSearch } />
132134 </ div >
133135 </ TooltipHost >
@@ -144,26 +146,26 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
144146 style = { { marginLeft : '2px' , marginRight : '2px' } }
145147 placeholder = { _t ( `Search ${ this . props . title . slice ( 0 , this . props . title . indexOf ( ' ' ) ) } ` ) }
146148 value = { this . state . query }
147- onKeyDown = { onKeyDown }
149+ onKeyDown = { this . onKeyDown }
148150 onChange = { ( _ , newValue ) => this . setState ( { query : newValue || '' } ) }
149151 onFocus = { ( e ) => e . target . select ( ) }
150152 autoFocus
151153 />
152154 < div
153155 className = "odoo-muted-button search-icon"
154156 style = { { border : 'none' } }
155- onClick = { ( ) => searchData ( this . state . query ) } >
157+ onClick = { ( ) => this . searchData ( this . state . query ) } >
156158 < FontAwesomeIcon icon = { faSearch } />
157159 </ div >
158160 </ div >
159161 ) ;
160162 }
161163
162164 let refrechButton = null ;
163- if ( this . state . isSearching ) {
165+ if ( ! this . state . isSearching ) {
164166 refrechButton = (
165167 < 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' } } >
167169 < FontAwesomeIcon icon = { faRedoAlt } />
168170 </ div >
169171 </ TooltipHost >
@@ -174,8 +176,8 @@ class SearchRefrech extends React.Component<SearchRefrechProps, SearchRefrechSta
174176 < div style = { broadCampStyle } >
175177 { backButton }
176178 { searchButton }
177- { searchBar }
178179 { refrechButton }
180+ { searchBar }
179181 </ div >
180182 ) ;
181183 }
0 commit comments