@@ -19,9 +19,8 @@ export default {
1919 state . items = payload ;
2020 state . searchLoading = false ;
2121 } ,
22- UNSET_CITY ( state ) {
23- state . cities = [ ] ;
24- state . items = [ ] ;
22+ UNSET_CITY ( state , payload ) {
23+ state . cities = payload ;
2524 } ,
2625 SET_LOADING ( state , payload ) {
2726 state . isLoading = payload ;
@@ -48,7 +47,7 @@ export default {
4847 let longitude = position . coords . longitude ;
4948 let url = `https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=${
5049 key . weather
51- } &q=${ latitude } ,${ longitude } &language=ru-ru`;
50+ } &q=${ latitude } ,${ longitude } &language=ru-ru`;
5251 axios
5352 . get ( url )
5453 . then ( response => {
@@ -78,12 +77,11 @@ export default {
7877 }
7978 } ,
8079 async GET_WEATHER_CITY ( { commit } , data ) {
81- console . log ( data )
8280 let queryKey = data . Key ? data . Key : data . selectCity . Key ;
8381 let city = { } ;
8482 let url = `https://dataservice.accuweather.com/currentconditions/v1/${ queryKey } ?apikey=${
8583 key . weather
86- } &language=ru-ru&details=true`;
84+ } &language=ru-ru&details=true`;
8785 await axios
8886 . get ( url )
8987 . then ( result => {
@@ -108,18 +106,18 @@ export default {
108106 : data . selectCity . country ,
109107 temp : `${ res . Temperature . Metric . Value . toFixed ( ) } ° ${
110108 res . Temperature . Metric . Unit
111- } `,
109+ } `,
112110 windDirect : res . Wind . Direction . Localized ,
113111 windSpeed : `${ res . Wind . Speed . Metric . Value } ${
114112 res . Wind . Speed . Metric . Unit
115- } `,
113+ } `,
116114 weatherText : res . WeatherText ,
117115 realFeelTemperature : `${ res . RealFeelTemperature . Metric . Value . toFixed ( ) } ° ${
118116 res . RealFeelTemperature . Metric . Unit
119- } `,
117+ } `,
120118 visibility : `${ res . Visibility . Metric . Value } ${
121119 res . Visibility . Metric . Unit
122- } `,
120+ } `,
123121 WeatherIcon : res . WeatherIcon ,
124122 IsDayTime : res . IsDayTime ,
125123 time : time ,
@@ -134,7 +132,7 @@ export default {
134132 commit ( "SET_SEARCHLOADING" , true ) ;
135133 let url = `https://dataservice.accuweather.com/locations/v1/cities/autocomplete?apikey=${
136134 key . weather
137- } &q=${ payload . searchText } &language=ru-ru`;
135+ } &q=${ payload . searchText } &language=ru-ru`;
138136 let items = [ ] ;
139137 let cities = { } ;
140138 axios
@@ -164,7 +162,7 @@ export default {
164162 let arr = [ ] ;
165163 let url = `https://dataservice.accuweather.com/forecasts/v1/daily/5day/${
166164 payload . city . key
167- } ?apikey=${ key . weather } &language=ru-ru&metric=true`;
165+ } ?apikey=${ key . weather } &language=ru-ru&metric=true`;
168166 await axios
169167 . get ( url )
170168 . then ( result => {
@@ -187,14 +185,15 @@ export default {
187185 . catch ( error => console . error ( error . message ) ) ;
188186 return arr ;
189187 } ,
190- SAVE_TO_LS ( { commit } , payload ) {
188+ SAVE_TO_LS ( { commit, state } , payload ) {
191189 let arr = [ ] ;
192190 let exist = false ;
193191 let city = { } ;
194192 if ( localStorage . getItem ( "city" ) != null ) {
195193 try {
196194 arr = JSON . parse ( localStorage . getItem ( "city" ) ) ;
197195 } catch ( e ) {
196+ // eslint-disable-next-line
198197 return console . error ( e ) ;
199198 }
200199 if ( arr . some ( e => e . Key === payload . city . key ) ) {
@@ -212,6 +211,11 @@ export default {
212211 } ;
213212 arr . push ( city ) ;
214213 localStorage . setItem ( "city" , JSON . stringify ( arr ) ) ;
214+ // change state
215+ let data = state . cities . map ( el =>
216+ el . key === payload . city . key ? { ...el , fromLS : true } : el
217+ )
218+ commit ( "UNSET_CITY" , data ) ;
215219 }
216220 } ,
217221 DELETE_TO_LS ( { commit, state } , payload ) {
@@ -220,12 +224,17 @@ export default {
220224 try {
221225 arr = JSON . parse ( localStorage . getItem ( "city" ) ) ;
222226 } catch ( e ) {
227+ // eslint-disable-next-line
223228 return console . error ( e ) ;
224229 }
225230 let filteredArr = arr . filter ( el => el . Key != payload . city . key ) ;
226231 localStorage . setItem ( "city" , JSON . stringify ( filteredArr ) ) ;
227- // let data = state.cities.filter(el => el.Key != payload.city.key);
228- // commit("SET_CITY", data);
232+ // let data = state.cities.map(el =>
233+ // el.Key === payload.city.key ? el.fromLS = false : el
234+ // )
235+ // change state
236+ let data = state . cities . filter ( el => el . key != payload . city . key )
237+ commit ( "UNSET_CITY" , data ) ;
229238 }
230239 }
231240 } ,
0 commit comments