Compare commits
5 Commits
9d253c4c86
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b275c4e39 | |||
| 5e2926b9ef | |||
| f1198a8281 | |||
| 7873321be0 | |||
| 443d4b0366 |
@@ -1,175 +1,213 @@
|
||||
import axios from 'axios';
|
||||
import {
|
||||
listLocations,
|
||||
getLocation,
|
||||
addLocation,
|
||||
updateLocation,
|
||||
deleteLocation
|
||||
listLocations,
|
||||
getLocation,
|
||||
addLocation,
|
||||
updateLocation,
|
||||
deleteLocation
|
||||
} from '../api/locations';
|
||||
|
||||
jest.mock('axios');
|
||||
|
||||
describe('Location API Functions', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('listLocations', () => {
|
||||
test('should fetch all locations successfully', async () => {
|
||||
const mockLocations = [
|
||||
{
|
||||
"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",
|
||||
"area": 517.21,
|
||||
"population": 1790658
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
];
|
||||
axios.get.mockResolvedValueOnce({ data: mockLocations });
|
||||
|
||||
const result = await listLocations();
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||
expect(result).toEqual(mockLocations);
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('should handle error when fetching locations fails', async () => {
|
||||
axios.get.mockRejectedValueOnce(new Error('Network error'));
|
||||
describe('listLocations', () => {
|
||||
test('should fetch all locations successfully', async () => {
|
||||
const mockLocations = [
|
||||
{
|
||||
"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
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"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});
|
||||
|
||||
await expect(listLocations()).rejects.toThrow('Network error');
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||
});
|
||||
})
|
||||
const result = await listLocations();
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
axios.get.mockResolvedValueOnce({ data: mockLocation });
|
||||
console.log(result);
|
||||
|
||||
const result = await getLocation(1);
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||
expect(result).toEqual(mockLocations);
|
||||
});
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/1');
|
||||
expect(result).toEqual(mockLocation);
|
||||
test('should handle error when fetching locations fails', async () => {
|
||||
axios.get.mockRejectedValueOnce(new Error('Network error'));
|
||||
|
||||
await expect(listLocations()).rejects.toThrow('Network error');
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||
});
|
||||
})
|
||||
|
||||
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. 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(toCheck);
|
||||
});
|
||||
|
||||
test('should handle error when fetching a location fails', async () => {
|
||||
axios.get.mockRejectedValueOnce(new Error('Location not found'));
|
||||
|
||||
await expect(getLocation(999)).rejects.toThrow('Location not found');
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/999');
|
||||
});
|
||||
});
|
||||
|
||||
test('should handle error when fetching a location fails', async () => {
|
||||
axios.get.mockRejectedValueOnce(new Error('Location not found'));
|
||||
describe('addLocation', () => {
|
||||
test('should add a location successfully', async () => {
|
||||
const newLocation = {
|
||||
"name": "Gdańsk",
|
||||
"description": "Miasto portowe na północy Polski.",
|
||||
"image": "https://example.com/gdansk.jpg",
|
||||
"area": 262.0,
|
||||
"population": 470907
|
||||
};
|
||||
|
||||
await expect(getLocation(999)).rejects.toThrow('Location not found');
|
||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/999');
|
||||
});
|
||||
});
|
||||
const responseLocation = {
|
||||
"id": 3,
|
||||
...newLocation
|
||||
};
|
||||
|
||||
describe('addLocation', () => {
|
||||
test('should add a location successfully', async () => {
|
||||
const newLocation = {
|
||||
"name": "Gdańsk",
|
||||
"description": "Miasto portowe na północy Polski.",
|
||||
"image": "https://example.com/gdansk.jpg",
|
||||
"area": 262.0,
|
||||
"population": 470907
|
||||
};
|
||||
|
||||
const responseLocation = {
|
||||
"id": 3,
|
||||
...newLocation
|
||||
};
|
||||
|
||||
axios.post.mockResolvedValueOnce({ data: responseLocation });
|
||||
axios.post.mockResolvedValueOnce({data: responseLocation});
|
||||
|
||||
const result = await addLocation(newLocation);
|
||||
const result = await addLocation(newLocation);
|
||||
|
||||
expect(axios.post).toHaveBeenCalledWith(
|
||||
'https://hopp.zikor.pl/locations/add',
|
||||
newLocation,
|
||||
{ headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
expect(result).toEqual(responseLocation);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateLocation', () => {
|
||||
test('should update a location successfully', async () => {
|
||||
const locationId = 2;
|
||||
const updatedDetails = {
|
||||
"name": "Kielce",
|
||||
"description": "Updated description for Kielce",
|
||||
"population": 200000
|
||||
};
|
||||
|
||||
const responseLocation = {
|
||||
"id": locationId,
|
||||
"name": "Kielce",
|
||||
"description": "Updated description for Kielce",
|
||||
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||
"area": 109.2,
|
||||
"population": 200000
|
||||
};
|
||||
|
||||
axios.put.mockResolvedValueOnce({ data: responseLocation });
|
||||
|
||||
const result = await updateLocation(locationId, updatedDetails);
|
||||
|
||||
expect(axios.put).toHaveBeenCalledWith(
|
||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||
updatedDetails,
|
||||
{ headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
expect(result).toEqual(responseLocation);
|
||||
expect(axios.post).toHaveBeenCalledWith(
|
||||
'https://hopp.zikor.pl/locations/add',
|
||||
newLocation,
|
||||
{headers: {"Content-Type": "application/json"}}
|
||||
);
|
||||
expect(result).toEqual(responseLocation);
|
||||
});
|
||||
});
|
||||
|
||||
test('should handle error when updating a location fails', async () => {
|
||||
const locationId = 999;
|
||||
const updatedDetails = { population: 300000 };
|
||||
|
||||
axios.put.mockRejectedValueOnce(new Error('Failed to add location'));
|
||||
describe('updateLocation', () => {
|
||||
test('should update a location successfully', async () => {
|
||||
const locationId = 2;
|
||||
const updatedDetails = {
|
||||
"name": "Kielce",
|
||||
"description": "Updated description for Kielce",
|
||||
"population": 200000
|
||||
};
|
||||
|
||||
await expect(updateLocation(locationId, updatedDetails)).rejects.toThrow('Failed to add location');
|
||||
expect(axios.put).toHaveBeenCalledWith(
|
||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||
updatedDetails,
|
||||
{ headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
});
|
||||
});
|
||||
const responseLocation = {
|
||||
"id": locationId,
|
||||
"name": "Kielce",
|
||||
"description": "Updated description for Kielce",
|
||||
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||
"area": 109.2,
|
||||
"population": 200000
|
||||
};
|
||||
|
||||
describe('deleteLocation', () => {
|
||||
test('should delete a location successfully', async () => {
|
||||
const locationId = 2;
|
||||
const responseData = { success: true, message: "Location deleted successfully" };
|
||||
|
||||
axios.delete.mockResolvedValueOnce({ data: responseData });
|
||||
axios.put.mockResolvedValueOnce({data: responseLocation});
|
||||
|
||||
const result = await deleteLocation(locationId);
|
||||
const result = await updateLocation(locationId, updatedDetails);
|
||||
|
||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||
expect(result).toEqual(responseData);
|
||||
expect(axios.put).toHaveBeenCalledWith(
|
||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||
updatedDetails,
|
||||
{headers: {"Content-Type": "application/json"}}
|
||||
);
|
||||
expect(result).toEqual(responseLocation);
|
||||
});
|
||||
|
||||
test('should handle error when updating a location fails', async () => {
|
||||
const locationId = 999;
|
||||
const updatedDetails = {population: 300000};
|
||||
|
||||
axios.put.mockRejectedValueOnce(new Error('Failed to add location'));
|
||||
|
||||
await expect(updateLocation(locationId, updatedDetails)).rejects.toThrow('Failed to add location');
|
||||
expect(axios.put).toHaveBeenCalledWith(
|
||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||
updatedDetails,
|
||||
{headers: {"Content-Type": "application/json"}}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('should handle error when deleting a location fails', async () => {
|
||||
const locationId = 999;
|
||||
|
||||
axios.delete.mockRejectedValueOnce(new Error('Failed to delete location'));
|
||||
describe('deleteLocation', () => {
|
||||
test('should delete a location successfully', async () => {
|
||||
const locationId = 2;
|
||||
const responseData = {success: true, message: "Location deleted successfully"};
|
||||
|
||||
await expect(deleteLocation(locationId)).rejects.toThrow('Failed to delete location');
|
||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||
axios.delete.mockResolvedValueOnce({data: responseData});
|
||||
|
||||
const result = await deleteLocation(locationId);
|
||||
|
||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||
expect(result).toEqual(responseData);
|
||||
});
|
||||
|
||||
test('should handle error when deleting a location fails', async () => {
|
||||
const locationId = 999;
|
||||
|
||||
axios.delete.mockRejectedValueOnce(new Error('Failed to delete location'));
|
||||
|
||||
await expect(deleteLocation(locationId)).rejects.toThrow('Failed to delete location');
|
||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,52 +1,60 @@
|
||||
import {
|
||||
View,
|
||||
StyleSheet,
|
||||
FlatList,
|
||||
View,
|
||||
StyleSheet,
|
||||
FlatList,
|
||||
} from "react-native";
|
||||
import { useTheme, Card, Text, Button } from "react-native-paper";
|
||||
import { Link } from "expo-router";
|
||||
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 theme = useTheme();
|
||||
const {locations, loading} = useLocationStore();
|
||||
|
||||
return (
|
||||
<View
|
||||
style={[styles.container, { backgroundColor: theme.colors.background }]}
|
||||
>
|
||||
<FlatList
|
||||
data={locations}
|
||||
keyExtractor={(item) => item.id.toString()}
|
||||
renderItem={({ item }) => (
|
||||
<Card style={{ margin: 10 }}>
|
||||
<Card.Cover
|
||||
style={{ marginBottom: 10 }}
|
||||
source={item.imageSource}
|
||||
/>
|
||||
<Card.Content style={{ marginBottom: 10 }}>
|
||||
<Text variant="titleLarge">{item.name}</Text>
|
||||
<Text variant="bodyMedium">
|
||||
{item.description && item.description.split(".")[0]}...
|
||||
</Text>
|
||||
</Card.Content>
|
||||
<Card.Actions>
|
||||
<Link href={`/location/${item.id}`} asChild>
|
||||
<Button>Zobacz więcej</Button>
|
||||
</Link>
|
||||
</Card.Actions>
|
||||
</Card>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
if (loading) {
|
||||
return (
|
||||
<View style={[styles.container, {backgroundColor: theme.colors.background}]}>
|
||||
<ActivityIndicator size="large" color={theme.colors.primary}/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View
|
||||
style={[styles.container, {backgroundColor: theme.colors.background}]}
|
||||
>
|
||||
<FlatList
|
||||
data={locations}
|
||||
keyExtractor={(item) => item.id.toString()}
|
||||
renderItem={({item}) => (
|
||||
<Card style={{margin: 10}}>
|
||||
<Card.Cover
|
||||
style={{marginBottom: 10}}
|
||||
source={item.imageSource}
|
||||
/>
|
||||
<Card.Content style={{marginBottom: 10}}>
|
||||
<Text variant="titleLarge">{item.name}</Text>
|
||||
<Text variant="bodyMedium">
|
||||
{item.description && item.description.split(".")[0]}...
|
||||
</Text>
|
||||
</Card.Content>
|
||||
<Card.Actions>
|
||||
<Link href={`/location/${item.id}`} asChild>
|
||||
<Button>Zobacz więcej</Button>
|
||||
</Link>
|
||||
</Card.Actions>
|
||||
</Card>
|
||||
)}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
backgroundColor: "#25292e",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
},
|
||||
container: {
|
||||
flex: 1,
|
||||
backgroundColor: "#25292e",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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,14 +19,23 @@ export default function Location() {
|
||||
}
|
||||
}, [location, loading, fetchLocations]);
|
||||
|
||||
if (loading || !location) {
|
||||
if (loading) {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.text}>Brak lokalizacji - {id}</Text>
|
||||
</View>
|
||||
<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>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
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,
|
||||
}
|
||||
});
|
||||
|
||||
143
locationStore.js
143
locationStore.js
@@ -1,79 +1,88 @@
|
||||
import { create } from "zustand";
|
||||
import {create} from "zustand";
|
||||
import * as api from "@/api/locations";
|
||||
|
||||
const useLocationStore = create((set, get) => ({
|
||||
locations: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
locations: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
fetchLocations: async () => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const data = await api.listLocations();
|
||||
set({ locations: data, loading: false });
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
}
|
||||
},
|
||||
fetchLocations: async () => {
|
||||
set({loading: true, error: null});
|
||||
try {
|
||||
const data = await api.listLocations();
|
||||
set({locations: data, loading: false});
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
}
|
||||
},
|
||||
|
||||
addLocation: async (location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const newLoc = await api.addLocation(location);
|
||||
const normalizedLoc = {
|
||||
...newLoc,
|
||||
imageSource: api.normalizeImageSource(newLoc.image)
|
||||
};
|
||||
set((state) => ({
|
||||
locations: [...state.locations, normalizedLoc],
|
||||
loading: false,
|
||||
}));
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
addLocation: async (location) => {
|
||||
try {
|
||||
const newLoc = await api.addLocation(location);
|
||||
const normalizedLoc = {
|
||||
...newLoc,
|
||||
imageSource: api.normalizeImageSource(newLoc.image)
|
||||
};
|
||||
set((state) => ({
|
||||
locations: [...state.locations, normalizedLoc],
|
||||
}));
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
updateLocation: async (id, location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const updated = await api.updateLocation(id, location);
|
||||
set((state) => ({
|
||||
locations: state.locations.map((loc) =>
|
||||
loc.id === id ? updated : loc
|
||||
),
|
||||
|
||||
loading: false,
|
||||
}));
|
||||
updateLocation: async (id, location) => {
|
||||
try {
|
||||
const updated = await api.updateLocation(id, location);
|
||||
const normalizedLoc = {
|
||||
...updated,
|
||||
imageSource: api.normalizeImageSource(updated.image)
|
||||
};
|
||||
|
||||
return updated;
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
const stringId = String(id);
|
||||
|
||||
deleteLocation: async (id) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const deleted = await api.deleteLocation(id);
|
||||
set((state) => ({
|
||||
locations: state.locations.filter((loc) => loc.id !== id),
|
||||
loading: false,
|
||||
}));
|
||||
if(deleted) {
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return false;
|
||||
}
|
||||
},
|
||||
set((state) => ({
|
||||
locations: state.locations.map((loc) =>
|
||||
String(loc.id) === stringId ? normalizedLoc : loc
|
||||
),
|
||||
}));
|
||||
|
||||
getLocation: (id) => {
|
||||
return get().locations.find((loc) => String(loc.id) === String(id));
|
||||
},
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
deleteLocation: async (id) => {
|
||||
try {
|
||||
const deleted = await api.deleteLocation(id);
|
||||
const stringId = String(id);
|
||||
|
||||
set((state) => ({
|
||||
locations: state.locations.filter((loc) => String(loc.id) !== stringId),
|
||||
loading: false,
|
||||
}));
|
||||
|
||||
return deleted;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
getLocation: (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;
|
||||
},
|
||||
}));
|
||||
|
||||
export default useLocationStore;
|
||||
@@ -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