Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. import React from "react";
  2. import {
  3. RefreshControl,
  4. Platform,
  5. SectionList,
  6. StyleSheet,
  7. Text,
  8. View
  9. } from "react-native";
  10. import { Constants } from "expo";
  11. import Swipeout from "react-native-swipeout";
  12. import moment from "moment/min/moment-with-locales";
  13. import { AntDesign, Ionicons } from "@expo/vector-icons";
  14. import { Stitch, RemoteMongoClient } from "mongodb-stitch-react-native-sdk";
  15.  
  16. export default class SettingsScreen extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. currentUserId: undefined,
  21. client: undefined,
  22. tasks: undefined,
  23. refreshing: false
  24. };
  25. this._loadClient = this._loadClient.bind(this);
  26. }
  27.  
  28. componentDidMount() {
  29. this._loadClient();
  30. }
  31.  
  32. _onRefresh = () => {
  33. this.setState({ refreshing: true });
  34. const stitchAppClient = Stitch.defaultAppClient;
  35. const mongoClient = stitchAppClient.getServiceClient(
  36. RemoteMongoClient.factory,
  37. "mongodb-atlas"
  38. );
  39. const db = mongoClient.db("taskmanager");
  40. const tasks = db.collection("tasks");
  41. tasks
  42. .find(
  43. {
  44. status: "completed"
  45. },
  46. { sort: { date: -1 } }
  47. )
  48. .asArray()
  49. .then(docs => {
  50. this.setState({ tasks: docs });
  51. this.setState({ refreshing: false });
  52. })
  53. .catch(err => {
  54. console.warn(err);
  55. });
  56. };
  57.  
  58. render() {
  59. const { manifest } = Constants;
  60. const sections =
  61. this.state.tasks == undefined
  62. ? [{ data: [{ title: "Loading..." }], title: "Loading..." }]
  63. : this.state.tasks.length > 0
  64. ? [{ data: this.state.tasks, title: "Completed tasks" }]
  65. : [
  66. {
  67. data: [{ title: "No completed tasks" }],
  68. title: "No completed tasks"
  69. }
  70. ];
  71.  
  72. return (
  73. <SectionList
  74. style={{ ...styles.container }}
  75. renderItem={this._renderItem}
  76. renderSectionHeader={this._renderSectionHeader}
  77. stickySectionHeadersEnabled={true}
  78. keyExtractor={(item, index) => index}
  79. sections={sections}
  80. refreshControl={
  81. <RefreshControl
  82. refreshing={this.state.refreshing}
  83. onRefresh={this._onRefresh}
  84. />
  85. }
  86. />
  87. );
  88. }
  89.  
  90. _renderSectionHeader = ({ section }) => {
  91. return <SectionHeader title={section.title} />;
  92. };
  93.  
  94. _renderItem = ({ item }) => {
  95. return (
  96. <SectionContent>
  97. <Swipeout
  98. autoClose={true}
  99. backgroundColor="none"
  100. right={[
  101. {
  102. component: (
  103. <View
  104. style={{
  105. flex: 1,
  106. alignItems: "center",
  107. justifyContent: "center",
  108. flexDirection: "column"
  109. }}
  110. >
  111. <Ionicons
  112. name={Platform.OS == "ios" ? "ios-archive" : "md-archive"}
  113. size={30}
  114. style={{ textAlign: "center", color: "white" }}
  115. />
  116. </View>
  117. ),
  118. backgroundColor: "#2e78b7",
  119. onPress: () => this._onPressArchive(item._id)
  120. }
  121. ]}
  122. >
  123. <View style={styles.taskListTextTime}>
  124. {item.title != "No completed tasks" &&
  125. item.title != "Loading..." ? (
  126. <Text style={styles.taskListTextTime}>
  127. created {moment(item.date).fromNow()}
  128. </Text>
  129. ) : item.title == "No completed tasks" ? (
  130. <AntDesign
  131. name={Platform.OS == "ios" ? "smileo" : "smileo"}
  132. size={30}
  133. style={{
  134. textAlign: "center",
  135. color: "lightgray",
  136. marginTop: 25
  137. }}
  138. />
  139. ) : (
  140. <Text />
  141. )}
  142. </View>
  143. <Text style={styles.sectionContentText}>
  144. {item.title != "No completed tasks" ? item.description : ""}
  145. </Text>
  146. <Text style={styles.taskListTextTimeComplete}>
  147. {item.title != "No completed tasks" && item.title != "Loading..."
  148. ? "completed " + moment(item.completedDate).fromNow()
  149. : null}
  150. </Text>
  151. </Swipeout>
  152. </SectionContent>
  153. );
  154. };
  155.  
  156. _loadClient() {
  157. const stitchAppClient = Stitch.defaultAppClient;
  158. const mongoClient = stitchAppClient.getServiceClient(
  159. RemoteMongoClient.factory,
  160. "mongodb-atlas"
  161. );
  162. const db = mongoClient.db("taskmanager");
  163. const tasks = db.collection("tasks");
  164. tasks
  165. .find(
  166. {
  167. status: "completed"
  168. },
  169. { sort: { date: -1 } }
  170. )
  171. .asArray()
  172. .then(docs => {
  173. this.setState({ tasks: docs });
  174. })
  175. .catch(err => {
  176. console.warn(err);
  177. });
  178. }
  179. _onPressArchive(itemID) {
  180. const stitchAppClient = Stitch.defaultAppClient;
  181. const mongoClient = stitchAppClient.getServiceClient(
  182. RemoteMongoClient.factory,
  183. "mongodb-atlas"
  184. );
  185. const db = mongoClient.db("taskmanager");
  186. const tasks = db.collection("tasks");
  187. tasks
  188. .updateOne(
  189. { _id: itemID },
  190. { $set: { status: "archived", archivedDate: new Date() } },
  191. { upsert: true }
  192. )
  193. .then(() => {
  194. tasks
  195. .find({ status: "completed" }, { sort: { date: -1 } })
  196. .asArray()
  197. .then(docs => {
  198. this.setState({ tasks: docs });
  199. })
  200. .catch(err => {
  201. console.warn(err);
  202. });
  203. })
  204. .catch(err => {
  205. console.warn(err);
  206. });
  207. }
  208. }
  209.  
  210. const SectionHeader = ({ title }) => {
  211. return (
  212. <View style={styles.sectionHeaderContainer}>
  213. <Text style={styles.sectionHeaderText}>{title}</Text>
  214. </View>
  215. );
  216. };
  217.  
  218. const SectionContent = props => {
  219. return <View style={styles.sectionContentContainer}>{props.children}</View>;
  220. };
  221.  
  222. SettingsScreen.navigationOptions = {
  223. headerTitle: (
  224. <Ionicons
  225. name={
  226. Platform.OS == "ios"
  227. ? "ios-checkmark-circle-outline"
  228. : "md-checkmark-circle-outline"
  229. }
  230. size={23}
  231. style={{
  232. color: "#2e78b7",
  233. flex: 1,
  234. textAlign: "center"
  235. }}
  236. resizeMode="contain"
  237. />
  238. )
  239. };
  240.  
  241. const styles = StyleSheet.create({
  242. container: {
  243. flex: 1,
  244. backgroundColor: "#fff"
  245. },
  246. sectionHeaderContainer: {
  247. backgroundColor: "#fbfbfb",
  248. paddingVertical: 8,
  249. paddingHorizontal: 15,
  250. borderWidth: StyleSheet.hairlineWidth,
  251. borderColor: "#ededed",
  252. alignItems: "center"
  253. },
  254. sectionHeaderText: {
  255. fontSize: 14,
  256. fontWeight: "bold"
  257. },
  258. sectionContentContainer: {
  259. paddingHorizontal: 15,
  260. borderBottomWidth: StyleSheet.hairlineWidth,
  261. borderBottomColor: "lightgray"
  262. },
  263. sectionContentText: {
  264. color: "black",
  265. fontSize: 15,
  266. paddingBottom: 10,
  267. paddingHorizontal: 10,
  268. textAlign: "left"
  269. },
  270. taskListTextTime: {
  271. paddingHorizontal: 15,
  272. paddingVertical: 3,
  273. textAlign: "center",
  274. color: "lightgray"
  275. },
  276. taskListTextTimeComplete: {
  277. paddingVertical: 3,
  278. paddingHorizontal: 10,
  279. textAlign: "left",
  280. color: "green",
  281. fontSize: 13
  282. }
  283. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement