import React from 'react' import { List, Button, FAB, Divider, TextInput, Snackbar, Title, RadioButton, Switch, Portal, Dialog, Chip } from 'react-native-paper'; import { ScrollView, View, FlatList, StyleSheet, Alert, SectionList, SafeAreaView } from 'react-native' const _ = require('lodash') import * as moment from 'moment'; import db from '../../logic/ConfigDB' import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'; import TagsLogic from '../../logic/TagsRelated' import Utils from '../../logic/Utils' export default class Activity extends React.Component { constructor() { super() this.state = { activityName: '', groupId: '', groupName: '', isDailyGoal: true, goalCaption: 'Daily goal', goalHours: '0', goalMins: '0', goalError: false, setGoals: false, setTags: false, showTagsDialog: false, showEditGroupDialog: false, snackBarVisible: false, snackBarMessage: '', tags: [], activityBeingSaved: false, activitySelectedTags: [], activities: [] } this.activitySelectedTags = [] this.DB = new db() this.TagsLogic = new TagsLogic() } onChangeText = (key, value) => { this.setState({ [key]: value }) } showGroupNames = ({item}) => ( } right={props => } /> ); setGoals = () => { this.setState({ setGoals: !this.state.setGoals, }) } componentDidMount = async () => { try { if (this.DB.db == null) this.DB.db = await this.DB.open() this.setTagsInState() this.setActivitiesInState() this.setState({ groupId: this.props.navigation.getParam('group').groupId, groupName: this.props.navigation.getParam('group').groupName, }) } catch( err ) { console.log('database failed to open', err) } } setTagsInState = () => { this.TagsLogic.getTags(this.DB.db).then( result => { for (i in result.tags) { result.tags[i].selected = false } this.setState({ tags: result.tags }) }).catch( err => { this.setState({ snackBarMessage: 'There was an error with retriving of tags', snackBarVisible: true, }) }) } setActivitiesInState = () => { this.DB.run(this.DB.db, 'Select * from "Activity"').then( result => { this.setState({ activities: result.rows.raw() }) }).catch( err => { this.setState({ snackBarMessage: 'There was an error with retriving of activities', snackBarVisible: true, }) }) } // Its used for when selecting/unselecting a tag during setting of tags for new activity selectUnselectTagForActivity = tag => { let allTags = this.state.tags let selectedTagIndex = allTags.indexOf(tag) allTags[selectedTagIndex].selected = !tag.selected if (tag.selected) this.activitySelectedTags.push(tag) else this.activitySelectedTags.splice( this.activitySelectedTags.indexOf(tag) ) this.setState({ tags: allTags, activitySelectedTags: this.activitySelectedTags }) } showTags = ({item}) => ( {item.key} ); renderTagsInDialog = () => { chipTags = [] this.state.tags.map( tag => { chipTags.push( this.selectUnselectTagForActivity(tag)} > {tag.key} ) }) return (chipTags) } editGroupName = () => { this.DB.run(this.DB.db, 'UPDATE "Group" set groupName = ? where groupId = ?', [this.state.groupName, this.state.groupId]).then( result => { this.setState({ snackBarMessage: `Group name successfully updated to "${this.state.groupName}"`, snackBarVisible: true, showEditGroupDialog: false }) }).catch( err => { this.setState({ snackBarMessage: `Error occurred while updating: ${err}`, snackBarVisible: true, showEditGroupDialog: false }) }) } saveActivity = () => { if (Utils.isDuplicateOrEmpty(this.state.activityName, this.state.activities, 'activityName')) { Alert.alert('Activity name cannot be left empty or be a duplicate') return } this.setState({ activityBeingSaved: true }) let insertSQL = 'INSERT INTO "Activity" (activityName, groupId, goalLength, goalType)' insertSQL += ' values (?, ?, ?, ?)' goalLength = moment.duration({ hours: this.state.goalHours, minutes: this.state.goalMins })._milliseconds this.DB.run(this.DB.db, insertSQL, [ this.state.activityName, this.state.groupId, goalLength, this.state.setGoals ? this.state.isDailyGoal ? 'DAILY' : 'WEEKLY' : 'NA' ]).then( result => { if (this.state.setTags) { for (i in this.state.activitySelectedTags) { let insertSQL = 'INSERT INTO "ActivityTag" (activityId, tagId) values (?,?)' let tag = this.state.activitySelectedTags[i] this.DB.run(this.DB.db, insertSQL, [result.insertId, tag.id]) } } this.setState({ activityBeingSaved: false, snackBarMessage: `${this.state.activityName} saved successfully.`, snackBarVisible: true, activityName: '', setGoals: false, goalMins: '0', goalHours: '0', setTags: false, activitySelectedTags: [] }) }).catch( err => { this.setState({ activityBeingSaved: false, snackBarMessage: `Error occured: ${err}`, snackBarVisible: true }) }) } allowValidGoals = (val, maxVal, key) => { if (val === "") { this.setState({ [key]: '0' }) } else { val = parseInt(val) if (this.state.isDailyGoal) { maxVal = key === 'goalHours' ? 17 : maxVal } if (val > maxVal) { this.setState({ goalError: true, [key]: maxVal.toLocaleString(), }) } else { this.setState({ goalError: false, [key]: parseInt(val).toLocaleString() }) } } } render() { return ( this.onChangeText('activityName', val)} style={{ flex: 0.85, marginBottom: 10 }} /> Set goals { this.setState({ setGoals: !this.state.setGoals, }) }} /> {this.state.goalCaption} { this.setState({ isDailyGoal: !this.state.isDailyGoal, goalCaption: !this.state.isDailyGoal ? 'Daily Goal' : 'Weekly Goal' }) }} /> this.allowValidGoals(val, 120, 'goalHours')} style={{ flex: 1 }} /> this.allowValidGoals(val, 59, 'goalMins')} style={{ flex: 1 }} /> Tags { if (!this.state.setTags) { for (i in this.state.tags) this.state.tags[i].selected = false this.setState({ activitySelectedTags: [] }) this.activitySelectedTags = [] } this.setState({ showTagsDialog: !this.state.setTags, setTags: !this.state.setTags, }) }} /> console.log('Pressed star')}, { icon: 'edit', label: 'Edit group', onPress: () => this.setState({ showEditGroupDialog: true }) }, { icon: 'view-list', label: 'View activies', onPress: () => console.log('Pressed notifications') }, ]} onStateChange={({ open }) => this.setState({ open })} style={styles.addGroupFab} onPress={() => { if (this.state.open) { } }} /> this.setState({ showTagsDialog: false, setTags: false })}> Select all applicable tags { this.renderTagsInDialog() } this.setState({ showEditGroupDialog: false })}> Edit group name this.onChangeText('groupName', val)} style={{ flex: 1 }} /> this.setState({ snackBarVisible: false })} > {this.state.snackBarMessage} ); } } const styles = StyleSheet.create({ addGroupFab: { position: 'absolute', marginBottom: 50, right: 0, bottom: 0, }, rowSpaceBetween: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 10 }, displayTags: { flexDirection: 'row', justifyContent: 'space-evenly', flexWrap: 'wrap' }, snackbar: { bottom: 0, position: 'absolute' } })