-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory-list.js
118 lines (103 loc) · 3.48 KB
/
category-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react';
import { Avatar, Button, ButtonGroup, Icon, Layout, ListItem, MenuItem, OverflowMenu } from '@ui-kitten/components';
import { StyleSheet, ToastAndroid, View } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Reactotron from 'reactotron-react-native'
import axios from 'axios';
import { ScrollView } from 'react-native-gesture-handler';
var baseUrl = "https://standtogetherforchange.org";
const PlusIcon = (props) => (
<Icon {...props} name='plus' />
)
const EditIcon = (props) => (
<Icon {...props} name='edit' />
);
const TrashIcon = (props) => (
<Icon {...props} name='trash' />
);
export const ViewCategoriesScreen = ({ navigation }) => {
const [categories, setCategories] = React.useState([]);
const fetchData = async () => {
const params = {
target: 'categories',
};
try {
// get the data from the api
const response = await axios({
method: 'get',
url: `${baseUrl}/api.php`,
params: params,
});
Reactotron.log({
response, params
});
// convert the data to json
if (response.status === 200) {
// set state with the result
setCategories(response.data);
} else {
// Reactotron.log(response);
throw new Error("An error has occurred");
}
} catch (error) {
Reactotron.log({ error });
console.error(error);
}
}
React.useEffect(() => {
fetchData();
}, []);
const onNewButton = () => {
navigation && navigation.navigate('CreateCategory', {
context: 'create'
});
};
const Buttons = ({ category }) => {
const onEditButtonPress = () => {
navigation && navigation.navigate('CreateCategory', {
context: 'edit',
category: category
});
}
const onDeleteButtonPress = () => {
axios.get(baseUrl + '/api.php?target=delete-category&id=' + category.CategoryId)
.then(function (response) {
fetchData();
ToastAndroid.show(response.data.message, ToastAndroid.SHORT);
})
.catch(function (error) {
ToastAndroid.show('Error deleting category', ToastAndroid.SHORT);
});
}
return (
<ButtonGroup style={styles.buttonGroup} size="small">
<Button accessoryLeft={EditIcon} onPress={onEditButtonPress} />
<Button accessoryLeft={TrashIcon} status="danger" onPress={onDeleteButtonPress} />
</ButtonGroup>
)
};
return (
<>
<View>
<Button status='primary' accessoryLeft={PlusIcon} onPress={onNewButton}>
NEW
</Button>
</View>
<ScrollView>
{Array.isArray(categories) && categories.map((category, index) => (
<ListItem
key={index}
title={`${category.CategoryName}`}
accessoryRight={() => <Buttons category={category} />}
/>
))}
</ScrollView>
</>
);
}
const styles = StyleSheet.create({
icon: {
width: 32,
height: 32,
},
});