From 76954765ec4cfa4e57c6f2b05b092accc1caadf1 Mon Sep 17 00:00:00 2001 From: M10T Date: Sat, 15 Aug 2020 17:04:43 -0400 Subject: [PATCH] Added favorites for announcements from teachers --- app/Announcements.js | 59 +++++++++++++++++++++++++++++++++----------- app/Clubs.js | 2 -- package-lock.json | 20 +++++++++++++++ package.json | 1 + 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/app/Announcements.js b/app/Announcements.js index 4b5cabd..75fd103 100644 --- a/app/Announcements.js +++ b/app/Announcements.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, {useState} from 'react'; import { SafeAreaView, StyleSheet, @@ -20,12 +20,16 @@ import { import styles from './styles/liststyles' import { url } from './resources/fetchInfo.json' +import { SearchBar } from 'react-native-elements'; +import Icon from 'react-native-vector-icons/AntDesign' +import AsyncStorage from '@react-native-community/async-storage' + +const STORAGE_KEY = "teacherAnnouncements" const Announcement = ({item}) => { const date = new Date const dateStr = `${date.getMonth()+1}/${date.getDate()}/${date.getFullYear()}` const dateInfo = dateStr===item.item.date&&item.item.time!==undefined?item.item.time:item.item.date; - console.log(dateStr) return ( {dateInfo!==undefined?{dateInfo}:<>} @@ -35,21 +39,25 @@ const Announcement = ({item}) => { } export const TeacherList = ({route}) => { - return - } - keyExtractor={item=>JSON.stringify(item)} - /> - + return ( + + } + keyExtractor={item=>JSON.stringify(item)} + /> + + ) } function TeacherButton(props) { + const [color, setColor] = useState(props.color?props.color:'white') return ( - - props.navigation.navigate('TeacherList',{data:props.data,name:props.name})} activeOpacity={0.8}> + + props.navigation.navigate('TeacherList',{data:props.data,name:props.name})} activeOpacity={0.8}> {props.name} + {props.icon?{setColor(color=='yellow'?'white':'yellow');props.addFavorite(props.name)}}/>:<>} ) } @@ -61,10 +69,25 @@ class Announcements extends React.Component { this.state = { data: [], teacherNames: [], + favoriteNames: [], isLoading:true } } + addFavorite = (name) => { + const favoriteNames = this.state.favoriteNames.slice().map(({name})=>name) + const index = favoriteNames.indexOf(name) + if (index < 0) { + favoriteNames.push(name) + } + else { + favoriteNames.splice(index,1) + } + favoriteNames.sort() + this.setState({favoriteNames:favoriteNames.map(name=>({name:name}))}) + AsyncStorage.setItem(STORAGE_KEY,JSON.stringify(favoriteNames)).catch(console.log).done() + } + componentDidMount() { fetch(`${url}/api/en/announcements`,{ headers: { @@ -82,16 +105,22 @@ class Announcements extends React.Component { this.setState({data: data, teacherNames: teacherNames.map(x=>({name:x})),isLoading:false}); }) .catch((error) => console.error(error)) + + AsyncStorage.getItem(STORAGE_KEY) + .then(value=>value==null?[]:JSON.parse(value).map(x=>({name:x}))) + .then(names=>this.setState({favoriteNames:names})) + .catch(console.log) + .done() } render() { if (this.state.isLoading) return <> return ( - - x.teacher==null||x.teacher.trim()==='')} name="No Teacher" navigation={this.props.navigation}/> + + x.teacher==null||x.teacher.trim()==='')} name="No Teacher" navigation={this.props.navigation} /> x.teacher===item.name)} name={item.name} navigation={this.props.navigation}/>} + data={this.state.favoriteNames.concat(this.state.teacherNames.filter(x=>this.state.favoriteNames.map(({name})=>name).indexOf(x.name) < 0))} + renderItem={({item})=>= 0?'yellow':'white'} item={item} data={this.state.data.filter(x=>x.teacher===item.name)} name={item.name} navigation={this.props.navigation} icon={true} addFavorite={this.addFavorite}/>} keyExtractor={(item,index)=>item.name+index} /> diff --git a/app/Clubs.js b/app/Clubs.js index c6816c1..2dd6b63 100644 --- a/app/Clubs.js +++ b/app/Clubs.js @@ -81,8 +81,6 @@ class Clubs extends React.Component { const { data , dataSearch, isLoading,search} = this.state; return ( - -