Skip to content

Commit

Permalink
create a state to update when a city is added to the list of cities
Browse files Browse the repository at this point in the history
  • Loading branch information
aline-borges committed Nov 12, 2020
1 parent acdea03 commit 0df1312
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/screens/Location/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';

const Location = ({ route }) => {
const Location = ({ route, navigation }) => {
const { cityData } = route.params;

const timezone = cityData.data2.timezone;
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Locations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const Locations = ({ navigation }) => {
setSavedCities(newCities);
setLocations(newLocations);
AsyncStorage.setItem("locationData", JSON.stringify(newCities));

console.log(newCities);
console.log(newLocations);
};
Expand Down Expand Up @@ -101,7 +101,7 @@ const Locations = ({ navigation }) => {
) : (
locations.map(location => renderLocation(location))
)}
<TouchableOpacity onPress={() => navigation.navigate('Search')}>
<TouchableOpacity onPress={() => navigation.navigate('Search', { currentLocations: locations, updateLocations: setLocations })}>
<Feather name="plus-circle" style={styles.icon} />
</TouchableOpacity>
</SafeAreaView>
Expand Down
19 changes: 17 additions & 2 deletions src/screens/Search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { LinearGradient } from 'expo-linear-gradient';
import { Feather } from '@expo/vector-icons';
import { SafeAreaView } from 'react-native-safe-area-context';
import AsyncStorage from '@react-native-community/async-storage';
import { StackActions } from '@react-navigation/native';

import OpenWeather, { getOneCall } from '../../services/apis/openWeather';

const Search = ({ navigation }) => {
const Search = ({ navigation, route }) => {
const [query, setQuery] = useState('');
const { currentLocations, updateLocations } = route.params;

const saveData = async (name) => {
const cities = JSON.parse(await AsyncStorage.getItem("locationData")) || [];
Expand All @@ -32,7 +34,20 @@ const Search = ({ navigation }) => {
const data2 = await getOneCall(data.coord.lat, data.coord.lon);
const datas = {data: data, data2: data2}
saveData(data.name);
navigation.navigate('Locations', { cityData: datas });
const timezone = data2.timezone;
const time = new Date().toLocaleTimeString("pt-BR", {timeZone: timezone}).split(':');
const hour = `${time[0]}:${time[1]}`;
const location = {
data,
data2,
hour
}
updateLocations([...currentLocations, location]);
navigation.dispatch(
StackActions.replace('Location', {
cityData: datas,
})
);
}
};

Expand Down

0 comments on commit 0df1312

Please sign in to comment.