Compare commits
5 Commits
9d253c4c86
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b275c4e39 | |||
| 5e2926b9ef | |||
| f1198a8281 | |||
| 7873321be0 | |||
| 443d4b0366 |
@@ -22,22 +22,34 @@ describe('Location API Functions', () => {
|
||||
"name": "Warszawa",
|
||||
"description": "Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.",
|
||||
"image": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
||||
"area": 517.21,
|
||||
"population": 1790658
|
||||
"imageSource": {
|
||||
"uri": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
||||
},
|
||||
"area": 517.24,
|
||||
"population": 1790658,
|
||||
"longitude": 52.232887,
|
||||
"latitude": 20.896273
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Kielce",
|
||||
"description": "Stolica województwa świętokrzyskiego, położona w centralnej Polsce. Kielce to miasto w centralnej Polsce, znane z pięknych krajobrazów i bogatej historii. Warto odwiedzić Kielecki Park Etnograficzny, Muzeum Zabawek oraz Katedrę Wniebowzięcia Najświętszej Maryi Panny. Kielce są również znane z licznych festiwali i wydarzeń kulturalnych. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry.",
|
||||
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||
"area": 109.2,
|
||||
"population": 196000
|
||||
"imageSource": {
|
||||
"uri": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||
},
|
||||
"area": 109.4,
|
||||
"population": 196000,
|
||||
"longitude": 50.85416,
|
||||
"latitude": 20.533003
|
||||
}
|
||||
];
|
||||
axios.get.mockResolvedValueOnce({data: mockLocations});
|
||||
|
||||
const result = await listLocations();
|
||||
|
||||
console.log(result);
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||
expect(result).toEqual(mockLocations);
|
||||
});
|
||||
@@ -52,21 +64,47 @@ describe('Location API Functions', () => {
|
||||
|
||||
describe('getLocation', () => {
|
||||
test('should fetch a location by id successfully', async () => {
|
||||
const mockLocation = {
|
||||
"id": 1,
|
||||
"name": "Warszawa",
|
||||
"description": "Stolica Polski, położona w centralnej części kraju.",
|
||||
"image": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
||||
"area": 517.21,
|
||||
"population": 1790658
|
||||
};
|
||||
const mockLocation = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Warszawa',
|
||||
description: 'Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.',
|
||||
image: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg',
|
||||
imageSource: {
|
||||
uri: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg'
|
||||
},
|
||||
area: 517.24,
|
||||
population: 1790658,
|
||||
longitude: 52.232887,
|
||||
latitude: 20.896273
|
||||
}
|
||||
];
|
||||
|
||||
const toCheck = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Warszawa',
|
||||
description: 'Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.',
|
||||
image: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg',
|
||||
imageSource: {
|
||||
uri: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg'
|
||||
},
|
||||
area: 517.24,
|
||||
population: 1790658,
|
||||
longitude: 52.232887,
|
||||
latitude: 20.896273
|
||||
},
|
||||
{"image" : null}
|
||||
];
|
||||
|
||||
axios.get.mockResolvedValueOnce({data: mockLocation});
|
||||
|
||||
const result = await getLocation(1);
|
||||
|
||||
console.log(result);
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/1');
|
||||
expect(result).toEqual(mockLocation);
|
||||
expect(result).toEqual(toCheck);
|
||||
});
|
||||
|
||||
test('should handle error when fetching a location fails', async () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import axios from "axios";
|
||||
|
||||
const API_URL = "http://192.168.0.118:9000";
|
||||
const API_URL = "http://192.168.0.130:9010";
|
||||
|
||||
export async function listLocations() {
|
||||
try {
|
||||
@@ -9,6 +9,8 @@ export async function listLocations() {
|
||||
return "No locations found";
|
||||
}
|
||||
|
||||
console.log("Locations fetched successfully");
|
||||
|
||||
return response.data.map(location => ({
|
||||
...location,
|
||||
imageSource: normalizeImageSource(location.image)
|
||||
@@ -63,6 +65,7 @@ export async function addLocation(location) {
|
||||
console.log("Error adding location");
|
||||
return("Failed to add location");
|
||||
}
|
||||
console.log("Location added successfully:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error adding location:", error);
|
||||
@@ -79,6 +82,7 @@ export async function updateLocation(id, location) {
|
||||
console.log("Failed to update location");
|
||||
return("Failed to update location");
|
||||
}
|
||||
console.log("Location updated successfully:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error while updating location:", error);
|
||||
@@ -93,6 +97,7 @@ export async function deleteLocation(id) {
|
||||
console.log("Error deleting location");
|
||||
return("Failed to delete location");
|
||||
}
|
||||
console.log("Location deleted successfully:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error while deleting location:", error);
|
||||
|
||||
@@ -90,6 +90,7 @@ export default function FormScreen() {
|
||||
const result = await launchCameraAsync({
|
||||
allowsEditing: false,
|
||||
base64: true,
|
||||
quality: 0.2,
|
||||
});
|
||||
|
||||
if (!result.canceled && result.assets && result.assets.length > 0) {
|
||||
|
||||
@@ -3,13 +3,21 @@ import {
|
||||
StyleSheet,
|
||||
FlatList,
|
||||
} from "react-native";
|
||||
import { useTheme, Card, Text, Button } from "react-native-paper";
|
||||
import {useTheme, Card, Text, Button, ActivityIndicator} from "react-native-paper";
|
||||
import {Link} from "expo-router";
|
||||
import useLocationStore from "@/locationStore";
|
||||
|
||||
export default function Index() {
|
||||
const theme = useTheme();
|
||||
const { locations } = useLocationStore();
|
||||
const {locations, loading} = useLocationStore();
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<View style={[styles.container, {backgroundColor: theme.colors.background}]}>
|
||||
<ActivityIndicator size="large" color={theme.colors.primary}/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View
|
||||
|
||||
@@ -16,7 +16,6 @@ export default function RootLayout() {
|
||||
fetchLocations();
|
||||
}, []);
|
||||
|
||||
console.log();
|
||||
const deleteLocation = useLocationStore((state) => state.deleteLocation);
|
||||
|
||||
const handleDelete = async (id) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { View, ScrollView, StyleSheet } from "react-native";
|
||||
import { useEffect } from "react";
|
||||
import { useTheme, Text, Card } from "react-native-paper";
|
||||
import {useTheme, Text, Card, ActivityIndicator} from "react-native-paper";
|
||||
import { useLocalSearchParams } from "expo-router";
|
||||
import useLocationStore from "@/locationStore";
|
||||
|
||||
@@ -19,7 +19,15 @@ export default function Location() {
|
||||
}
|
||||
}, [location, loading, fetchLocations]);
|
||||
|
||||
if (loading || !location) {
|
||||
if (loading) {
|
||||
return (
|
||||
<View style={[styles.container, {backgroundColor: theme.colors.background}]}>
|
||||
<ActivityIndicator size="large" color={theme.colors.primary}/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
if (!location) {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.text}>Brak lokalizacji - {id}</Text>
|
||||
@@ -27,6 +35,7 @@ export default function Location() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<View style={[styles.container, {backgroundColor: theme.colors.background}]}>
|
||||
<ScrollView>
|
||||
|
||||
@@ -6,10 +6,12 @@ import {
|
||||
KeyboardAvoidingView,
|
||||
View,
|
||||
ActivityIndicator,
|
||||
Image,
|
||||
} from "react-native";
|
||||
import {TextInput, Button, Snackbar, useTheme} from "react-native-paper";
|
||||
import {TextInput, Button, Snackbar, useTheme, Text} from "react-native-paper";
|
||||
import {useLocalSearchParams, useRouter} from "expo-router";
|
||||
import useLocationStore from "@/locationStore";
|
||||
import {normalizeImageSource} from "@/api/locations";
|
||||
|
||||
export default function EditLocation() {
|
||||
const theme = useTheme();
|
||||
@@ -89,6 +91,16 @@ export default function EditLocation() {
|
||||
}
|
||||
};
|
||||
|
||||
const isBase64Image = (str) => {
|
||||
let image = normalizeImageSource(str);
|
||||
return image && (
|
||||
image.uri.startsWith('data:image/jpeg;base64,') ||
|
||||
image.uri.startsWith('data:image/png;base64,') ||
|
||||
image.uri.startsWith('data:image/gif;base64,') ||
|
||||
image.uri.startsWith('data:image/webp;base64,')
|
||||
);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<View
|
||||
@@ -99,6 +111,14 @@ export default function EditLocation() {
|
||||
);
|
||||
}
|
||||
|
||||
if (!formData) {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.text}>Brak lokalizacji - {id}</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<KeyboardAvoidingView
|
||||
style={{flex: 1}}
|
||||
@@ -112,6 +132,30 @@ export default function EditLocation() {
|
||||
>
|
||||
{message}
|
||||
</Snackbar>
|
||||
|
||||
{isBase64Image(formData.image) ? (
|
||||
<View style={styles.imageContainer}>
|
||||
<Text style={{color: theme.colors.onBackground, marginBottom: 5}}>
|
||||
Aktualne zdjęcie:
|
||||
</Text>
|
||||
<Image
|
||||
source={normalizeImageSource(formData.image)}
|
||||
style={styles.imagePreview}
|
||||
resizeMode="contain"
|
||||
/>
|
||||
</View>
|
||||
) : (
|
||||
<TextInput
|
||||
mode="outlined"
|
||||
label="Link do zdjęcia"
|
||||
multiline={true}
|
||||
placeholder="Wpisz link do zdjęcia"
|
||||
style={{margin: 10, width: "100%"}}
|
||||
value={formData.image}
|
||||
onChangeText={(e) => setFormData({...formData, image: e})}
|
||||
/>
|
||||
)}
|
||||
|
||||
<TextInput
|
||||
mode="outlined"
|
||||
label="Nazwa"
|
||||
@@ -129,15 +173,6 @@ export default function EditLocation() {
|
||||
value={formData.description}
|
||||
onChangeText={(e) => setFormData({...formData, description: e})}
|
||||
/>
|
||||
<TextInput
|
||||
mode="outlined"
|
||||
label="Link do zdjęcia"
|
||||
multiline={true}
|
||||
placeholder="Wpisz link do zdjęcia"
|
||||
style={{margin: 10, width: "100%"}}
|
||||
value={formData.image}
|
||||
onChangeText={(e) => setFormData({...formData, image: e})}
|
||||
/>
|
||||
<TextInput
|
||||
mode="outlined"
|
||||
label="Powierzchnia"
|
||||
@@ -194,4 +229,15 @@ const styles = StyleSheet.create({
|
||||
alignItems: "center",
|
||||
flexGrow: 1,
|
||||
},
|
||||
imageContainer: {
|
||||
margin: 10,
|
||||
width: "100%",
|
||||
alignItems: "center",
|
||||
padding: 10,
|
||||
},
|
||||
imagePreview: {
|
||||
width: "100%",
|
||||
height: 200,
|
||||
borderRadius: 5,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -17,7 +17,6 @@ const useLocationStore = create((set, get) => ({
|
||||
},
|
||||
|
||||
addLocation: async (location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const newLoc = await api.addLocation(location);
|
||||
const normalizedLoc = {
|
||||
@@ -26,7 +25,6 @@ addLocation: async (location) => {
|
||||
};
|
||||
set((state) => ({
|
||||
locations: [...state.locations, normalizedLoc],
|
||||
loading: false,
|
||||
}));
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
@@ -36,18 +34,22 @@ addLocation: async (location) => {
|
||||
},
|
||||
|
||||
updateLocation: async (id, location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const updated = await api.updateLocation(id, location);
|
||||
const normalizedLoc = {
|
||||
...updated,
|
||||
imageSource: api.normalizeImageSource(updated.image)
|
||||
};
|
||||
|
||||
const stringId = String(id);
|
||||
|
||||
set((state) => ({
|
||||
locations: state.locations.map((loc) =>
|
||||
loc.id === id ? updated : loc
|
||||
String(loc.id) === stringId ? normalizedLoc : loc
|
||||
),
|
||||
|
||||
loading: false,
|
||||
}));
|
||||
|
||||
return updated;
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return null;
|
||||
@@ -55,16 +57,16 @@ addLocation: async (location) => {
|
||||
},
|
||||
|
||||
deleteLocation: async (id) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const deleted = await api.deleteLocation(id);
|
||||
const stringId = String(id);
|
||||
|
||||
set((state) => ({
|
||||
locations: state.locations.filter((loc) => loc.id !== id),
|
||||
locations: state.locations.filter((loc) => String(loc.id) !== stringId),
|
||||
loading: false,
|
||||
}));
|
||||
if(deleted) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return deleted;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return false;
|
||||
@@ -72,7 +74,14 @@ addLocation: async (location) => {
|
||||
},
|
||||
|
||||
getLocation: (id) => {
|
||||
return get().locations.find((loc) => String(loc.id) === String(id));
|
||||
const location = get().locations.find((loc) => String(loc.id) === String(id));
|
||||
if (location && !location.imageSource) {
|
||||
return {
|
||||
...location,
|
||||
imageSource: api.normalizeImageSource(location.image)
|
||||
};
|
||||
}
|
||||
return location;
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -15,16 +15,16 @@
|
||||
"expo": "^53.0.0",
|
||||
"expo-constants": "~17.1.6",
|
||||
"expo-linking": "~7.1.4",
|
||||
"expo-router": "~5.0.7",
|
||||
"expo-router": "~5.1.0",
|
||||
"expo-status-bar": "~2.2.3",
|
||||
"expo-image-picker": "~16.1.4",
|
||||
"expo-location": "~18.1.5",
|
||||
"react": "19.0.0",
|
||||
"react-dom": "19.0.0",
|
||||
"react-native": "0.79.2",
|
||||
"react-native": "0.79.3",
|
||||
"react-native-paper": "^5.13.1",
|
||||
"react-native-safe-area-context": "5.4.0",
|
||||
"react-native-screens": "~4.10.0",
|
||||
"react-native-screens": "~4.11.1",
|
||||
"react-native-web": "^0.20.0",
|
||||
"zustand": "^5.0.3"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user