@@ -3,74 +3,209 @@ import { NavLink, withRouter } from "react-router-dom";
3
3
import Swal from "sweetalert2" ;
4
4
import { Query } from "react-apollo" ;
5
5
import { GET_ALL_CITY_DETAILS } from "../../GraphQL" ;
6
+ import jsonData from "../../geoJsonCountries.json" ;
7
+
6
8
import FriendReadonlyCity from "./subcomponents/FriendReadonlyCity" ;
7
9
import Loader from "../../components/common/Loader/Loader" ;
8
10
import PageNotFound from "../../components/common/PageNotFound/PageNotFound" ;
9
11
10
12
const FriendReadonlyMap = ( ) => {
11
13
const [ loaded , handleLoaded ] = useState ( false ) ;
12
- const [ cityOrCountry , handleMapTypeChange ] = useState ( 1 ) ;
13
- const [ clickedCountryArray , addCountry ] = useState ( [ ] ) ;
14
+ const [ cityOrCountry , ] = useState ( 1 ) ;
15
+ const [ countryArray , addCountry ] = useState ( [ ] ) ;
14
16
const [ tripData , handleTripData ] = useState ( [ ] ) ;
17
+ const [ geoJsonArray , handleGeoJsonArray ] = useState ( [ ] ) ;
18
+ const [ filteredCountryJsonData , handleFilteredCountryJsonData ] = useState ( ) ;
19
+
15
20
const username = window . location . pathname . split ( "/" ) [ 2 ] ;
16
21
17
22
useEffect ( ( ) => {
18
23
localStorage . removeItem ( "token" ) ;
19
24
} , [ ] ) ;
20
25
21
- function handleLoadedCountries ( data ) {
22
- let countryArray = clickedCountryArray ;
23
- let userData = data . user ;
24
- if ( userData != null && userData . Places_visited . length !== 0 ) {
25
- for ( let i = 0 ; i < userData . Places_visited . length ; i ++ ) {
26
- if (
27
- ! countryArray . some ( ( country ) => {
28
- return country . countryId === userData . Places_visited [ i ] . countryId ;
29
- } )
30
- ) {
31
- countryArray . push ( {
32
- username : userData . username ,
33
- countryId : userData . Places_visited [ i ] . countryId ,
26
+ function getJsonData ( data ) {
27
+ let newGeoJsonArray = [ ] ;
28
+
29
+ data . Places_visited . forEach ( ( city ) => {
30
+ let item = {
31
+ type : "Feature" ,
32
+ properties : {
33
+ city : {
34
+ id : city . id ,
35
+ city : city . city ,
36
+ cityId : city . cityId ,
37
+ latitude : city . city_latitude ,
38
+ longitude : city . city_longitude ,
34
39
tripTiming : 0 ,
35
- } ) ;
36
- }
37
- }
38
- }
39
- if ( userData != null && userData . Places_visiting . length !== 0 ) {
40
- for ( let i = 0 ; i < userData . Places_visiting . length ; i ++ ) {
41
- if (
42
- ! countryArray . some ( ( country ) => {
43
- return country . countryId === userData . Places_visiting [ i ] . countryId ;
44
- } )
45
- ) {
46
- countryArray . push ( {
47
- username : userData . username ,
48
- countryId : userData . Places_visiting [ i ] . countryId ,
40
+ } ,
41
+ } ,
42
+ geometry : {
43
+ type : "Point" ,
44
+ coordinates : [ city . city_longitude , city . city_latitude ] ,
45
+ } ,
46
+ } ;
47
+ item . properties . icon = "0" ;
48
+
49
+ newGeoJsonArray . push ( item ) ;
50
+ return ;
51
+ } ) ;
52
+ data . Places_visiting . forEach ( ( city ) => {
53
+ let item = {
54
+ type : "Feature" ,
55
+ properties : {
56
+ city : {
57
+ id : city . id ,
58
+ city : city . city ,
59
+ cityId : city . cityId ,
60
+ latitude : city . city_latitude ,
61
+ longitude : city . city_longitude ,
49
62
tripTiming : 1 ,
50
- } ) ;
51
- }
52
- }
63
+ } ,
64
+ } ,
65
+ geometry : {
66
+ type : "Point" ,
67
+ coordinates : [ city . city_longitude , city . city_latitude ] ,
68
+ } ,
69
+ } ;
70
+ item . properties . icon = "1" ;
71
+
72
+ newGeoJsonArray . push ( item ) ;
73
+ return ;
74
+ } ) ;
75
+ if ( data . Place_living !== undefined ) {
76
+ let item = {
77
+ type : "Feature" ,
78
+ properties : {
79
+ city : {
80
+ id : data . Place_living . id ,
81
+ city : data . Place_living . city ,
82
+ cityId : data . Place_living . cityId ,
83
+ latitude : data . Place_living . city_latitude ,
84
+ longitude : data . Place_living . city_longitude ,
85
+ tripTiming : 2 ,
86
+ } ,
87
+ } ,
88
+ geometry : {
89
+ type : "Point" ,
90
+ coordinates : [ data . Place_living . city_longitude , data . Place_living . city_latitude ] ,
91
+ } ,
92
+ } ;
93
+ item . properties . icon = "2" ;
94
+
95
+ newGeoJsonArray . push ( item ) ;
53
96
}
54
- if ( userData != null && userData . Place_living !== null ) {
97
+ handleGeoJsonArray ( newGeoJsonArray ) ;
98
+ getJsonCountries ( data ) ;
99
+ }
100
+
101
+ function getJsonCountries ( data ) {
102
+ let userCities = [ ] ;
103
+ data . Places_visited . map ( ( city ) => {
104
+ city . tripTiming = 0 ;
105
+ } )
106
+ data . Places_visiting . map ( ( city ) => {
107
+ city . tripTiming = 1 ;
108
+ } )
109
+ data . Place_living . tripTiming = 2 ;
110
+ userCities = [ data . Place_living ] . concat ( data . Places_visited ) . concat ( data . Places_visiting ) ;
111
+ let newCountryArray = [ ] ;
112
+ let newFilteredCountryData = [ ] ;
113
+ for ( let i = 0 ; i < userCities . length ; i ++ ) {
114
+ var newGeoJson = { } ;
55
115
if (
56
- ! countryArray . some ( ( country ) => {
57
- return country . countryId === userData . Place_living . countryId ;
116
+ ! newCountryArray . some ( ( country ) => {
117
+ return (
118
+ country . countryISO === userCities [ i ] . countryISO &&
119
+ country . tripTiming === userCities [ i ] . tripTiming
120
+ ) ;
58
121
} )
59
122
) {
60
- countryArray . push ( {
61
- username : userData . username ,
62
- countryId : userData . Place_living . countryId ,
63
- tripTiming : 2 ,
123
+ newCountryArray . push ( {
124
+ countryId : userCities [ i ] . countryId ,
125
+ country : userCities [ i ] . country ,
126
+ tripTiming : userCities [ i ] . tripTiming ,
127
+ countryISO : userCities [ i ] . countryISO ,
64
128
} ) ;
129
+ let geoJson = jsonData . features . find (
130
+ ( jsonCountry ) => userCities [ i ] . countryISO === jsonCountry . properties . ISO2
131
+ ) ;
132
+ if ( geoJson ) {
133
+ newGeoJson = JSON . parse ( JSON . stringify ( geoJson ) ) ;
134
+ switch ( userCities [ i ] . tripTiming ) {
135
+ case 0 :
136
+ newGeoJson . properties . icon = "0" ;
137
+ break ;
138
+ case 1 :
139
+ newGeoJson . properties . icon = "1" ;
140
+ break ;
141
+ case 2 :
142
+ newGeoJson . properties . icon = "2" ;
143
+ break ;
144
+ default :
145
+ break ;
146
+ }
147
+ newFilteredCountryData . push ( newGeoJson ) ;
148
+ }
65
149
}
66
150
}
67
-
68
- addCountry ( countryArray ) ;
151
+ addCountry ( newCountryArray ) ;
152
+ handleFilteredCountryJsonData ( newFilteredCountryData ) ;
153
+ handleLoaded ( true ) ;
69
154
}
70
155
156
+ // function handleLoadedCountries(data) {
157
+ // let countryArray = clickedCountryArray;
158
+ // let userData = data.user;
159
+ // if (userData != null && userData.Places_visited.length !== 0) {
160
+ // for (let i = 0; i < userData.Places_visited.length; i++) {
161
+ // if (
162
+ // !countryArray.some((country) => {
163
+ // return country.countryId === userData.Places_visited[i].countryId;
164
+ // })
165
+ // ) {
166
+ // countryArray.push({
167
+ // username: userData.username,
168
+ // countryId: userData.Places_visited[i].countryId,
169
+ // tripTiming: 0,
170
+ // });
171
+ // }
172
+ // }
173
+ // }
174
+ // if (userData != null && userData.Places_visiting.length !== 0) {
175
+ // for (let i = 0; i < userData.Places_visiting.length; i++) {
176
+ // if (
177
+ // !countryArray.some((country) => {
178
+ // return country.countryId === userData.Places_visiting[i].countryId;
179
+ // })
180
+ // ) {
181
+ // countryArray.push({
182
+ // username: userData.username,
183
+ // countryId: userData.Places_visiting[i].countryId,
184
+ // tripTiming: 1,
185
+ // });
186
+ // }
187
+ // }
188
+ // }
189
+ // if (userData != null && userData.Place_living !== null) {
190
+ // if (
191
+ // !countryArray.some((country) => {
192
+ // return country.countryId === userData.Place_living.countryId;
193
+ // })
194
+ // ) {
195
+ // countryArray.push({
196
+ // username: userData.username,
197
+ // countryId: userData.Place_living.countryId,
198
+ // tripTiming: 2,
199
+ // });
200
+ // }
201
+ // }
202
+
203
+ // addCountry(countryArray);
204
+ // }
205
+
71
206
function handleTripDataHelper ( data ) {
72
207
handleTripData ( data ) ;
73
- handleLoaded ( true ) ;
208
+ getJsonData ( data ) ;
74
209
}
75
210
function geoScoreSwal ( ) {
76
211
const swalParams = {
@@ -98,20 +233,22 @@ const FriendReadonlyMap = () => {
98
233
{ ( { loading, error, data } ) => {
99
234
if ( loading ) return < Loader /> ;
100
235
if ( error ) return `Error! ${ error } ` ;
101
- handleLoadedCountries ( data ) ;
236
+ // handleLoadedCountries(data);
102
237
if ( ! loaded ) return < Loader /> ;
103
238
return (
104
239
< div className = "map-container" id = "map-readonly" >
105
240
< span className = "user-map-name" > { username + "'s Map" } </ span >
106
241
{ cityOrCountry ? (
107
- < NavLink to = { `/new ` } >
242
+ < NavLink to = { `/` } >
108
243
< button className = "create-map" > CREATE MY MAP</ button >
109
244
</ NavLink >
110
245
) : null }
111
246
< div className = { cityOrCountry ? "map city-map" : "map country-map" } >
112
247
< FriendReadonlyCity
113
248
tripData = { tripData }
114
- handleMapTypeChange = { handleMapTypeChange }
249
+ geoJsonArray = { geoJsonArray }
250
+ filteredCountryJsonData = { filteredCountryJsonData }
251
+ countryArray = { countryArray }
115
252
/>
116
253
</ div >
117
254
< span className = "georney-score" onClick = { ( ) => geoScoreSwal ( ) } >
0 commit comments