Skip to content

Commit

Permalink
study spaces are added to the workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLV committed Dec 4, 2019
1 parent a564873 commit 697b083
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .expo/packager-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"expoServerPort": 19000,
"expoServerNgrokUrl": "https://p5-k5p.anonymous.final-project-4-locos-1.exp.direct",
"packagerNgrokUrl": "https://packager.p5-k5p.anonymous.final-project-4-locos-1.exp.direct",
"ngrokPid": 17441,
"ngrokPid": 21183,
"devToolsPort": 19002,
"packagerPort": 19001,
"packagerPid": 17396
"packagerPid": 21153
}
7 changes: 4 additions & 3 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import AuthMap from '../components/AuthMap'

import UserWorkspacesScreen from '../Screens/WorkSpace/Member/UserWorkspacesScreen'
import UserJoinWorkspacesScreen from '../Screens/WorkSpace/Member/UserJoinWorkspacesScreen'
import UserWorkspaceMap from '../components/WorkSpaceMap'
// import UserWorkspaceMap from '../components/WorkSpaceMap'



Expand All @@ -35,7 +35,7 @@ const AuthWorkSpacesNavigator = createStackNavigator(
AuthWorkspacesScreen: AuthWorkspacesScreen,
AuthEditWorkspaceScreen :AuthEditWorkspaceScreen,
AuthMap:AuthMap,
UserWorkspaceMap:UserWorkspaceMap
// UserWorkspaceMap:UserWorkspaceMap
}, {
defaultNavigationOptions: {
headerStyle: {
Expand Down
14 changes: 7 additions & 7 deletions src/components/AuthMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export default class myMap extends React.Component {
latitude: null,
longitude: null,
},

polygons: [],
editing: null,
creating: false,
currentUser : null,
userId : null,
rendered : false,
wsID: this.props.navigation.getParam('workspaceId')
}
}

Expand All @@ -59,16 +59,16 @@ export default class myMap extends React.Component {
owner: this.state.userId,
point1: this.state.editing.points[0],
point2: this.state.editing.points[1],
wsID: this.props.navigation.getParam('workspaceId')
wsID: this.state.wsID,
}

//push new study space
firebase.database().ref("/StudySpaces/" + this.state.editing.studySpaceKey).update(payload)

//push ref key to users
//push ref key to workspaces
var self = this;
var newKey = this.state.editing.studySpaceKey
firebase.database().ref("/Users/" + this.state.userId + "/").once('value').then(function(snapshot){
firebase.database().ref("/workspaces/" + this.state.wsID + "/").once('value').then(function(snapshot){

var schema = snapshot.val()

Expand All @@ -84,7 +84,7 @@ export default class myMap extends React.Component {
var points = {}
points[newKey] = newKey
}
firebase.database().ref("/Users/" + self.state.userId + "/StudySpaces").update(points)
firebase.database().ref("/workspaces/" + self.state.wsID + "/StudySpaces").update(points)
})

this.setState({
Expand Down Expand Up @@ -118,7 +118,7 @@ export default class myMap extends React.Component {
firebase.database().ref('/StudySpaces/'+polygon.studySpaceKey + "/").remove()

//remove from Users/id/studyspaces
firebase.database().ref('/Users/'+this.state.userId + '/StudySpaces/'+ polygon.studySpaceKey).remove()
firebase.database().ref('/workspaces/'+this.state.wsID + '/StudySpaces/'+ polygon.studySpaceKey).remove()

this.state.polygons.splice(polygon.id, 1)
for(i = polygon.id; i < this.state.polygons.length; i++){
Expand Down Expand Up @@ -203,7 +203,7 @@ export default class myMap extends React.Component {
await firebase.auth().onAuthStateChanged(function(user){
if(user){
//get user studyspaces
firebase.database().ref('/Users/'+ user.uid + "/StudySpaces/").once('value').then(function(snapshot){
firebase.database().ref('/workspaces/'+ self.state.wsID + "/StudySpaces/").once('value').then(function(snapshot){

var spaces = snapshot.val()

Expand Down
183 changes: 181 additions & 2 deletions src/components/myMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ export default class myMap extends React.Component {
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},

coord:{
latitude: null,
longitude: null,
},

polygons: [],
editing: null,
creating: false,
currentUser : null,
userId : null,
rendered : false,
}
}

Expand All @@ -40,6 +49,129 @@ export default class myMap extends React.Component {
};
};

finish(){
const{polygons, editing} = this.state;
if(editing.coordinates.length < 4){
Alert.alert('Your study space is incomplete!');
} else {
//make a payload to push in /StudySpaces
var payload = {
owner: this.state.userId,
point1: this.state.editing.points[0],
point2: this.state.editing.points[1],
}

//push new study space
firebase.database().ref("/StudySpaces/" + this.state.editing.studySpaceKey).update(payload)

//push ref key to users
var self = this;
var newKey = this.state.editing.studySpaceKey
firebase.database().ref("/Users/" + this.state.userId + "/").once('value').then(function(snapshot){

var schema = snapshot.val()

//if already have studyspaces
if (snapshot.hasChild("StudySpaces")){
// console.log("has child")
var points = schema["StudySpaces"]
points[newKey] = newKey
}
//if first study space
else{
// console.log("no children")
var points = {}
points[newKey] = newKey
}
firebase.database().ref("/Users/" + self.state.userId + "/StudySpaces").update(points)
})

this.setState({
polygons: [...polygons, editing],
editing: null,
creating: false,
});
}
}

cancel(){
const{polygons} = this.state;
id = id - 1
this.setState({
polygons: [...polygons],
editing: null,
creating: false,
})
}

create(){
this.setState({
creating: true,
})
}

delete(polygon){
const{polygons} = this.state;
//remove from database
//remove from /StudySpaces
firebase.database().ref('/StudySpaces/'+polygon.studySpaceKey + "/").remove()

//remove from Users/id/studyspaces
firebase.database().ref('/Users/'+this.state.userId + '/StudySpaces/'+ polygon.studySpaceKey).remove()

this.state.polygons.splice(polygon.id, 1)
for(i = polygon.id; i < this.state.polygons.length; i++){
this.state.polygons[i].id = this.state.polygons[i].id - 1
}
id = id - 1
this.setState({
polygons: [...polygons]
})
}

onPress(e){
const{editing, coord, creating} = this.state;
if(creating == true){
if(!editing){

this.setState({
editing: {
id: id++,
coordinates: [e.nativeEvent.coordinate],
points: [e.nativeEvent.coordinate],
},
coord: {
latitude: e.nativeEvent.coordinate.latitude,
longitude: e.nativeEvent.coordinate.longitude,
}
});
} else if(editing.coordinates.length < 4){
var newKey = firebase.database().ref("/StudySpaces/").push().key

this.setState({
editing:{
studySpaceKey: newKey,
...editing,
coordinates: [...editing.coordinates, {latitude: e.nativeEvent.coordinate.latitude, longitude: coord.longitude },
e.nativeEvent.coordinate, {latitude: coord.latitude, longitude: e.nativeEvent.coordinate.longitude}],
points: [...editing.coordinates, e.nativeEvent.coordinate],
},
});
} else{}
}
}

onPolygonPress(polygon){
Alert.alert(
'Do you wish to delete this polygon?',
'',
[
{text: 'No'},
{text: 'Yes', onPress: () => this.delete(polygon)},
],
);
}

makeCoordinates(point1, point2){
coordinates = []

Expand Down Expand Up @@ -98,7 +230,6 @@ export default class myMap extends React.Component {
renderingPolygons.push(polygon)
id+=1
}

self.setState({
polygons : renderingPolygons
})
Expand All @@ -124,7 +255,6 @@ export default class myMap extends React.Component {
console.log("no user logged on")
}
})

navigator.geolocation.getCurrentPosition(
position =>{
this.setState({
Expand Down Expand Up @@ -157,18 +287,55 @@ export default class myMap extends React.Component {
showsUserLocation={true}
showsMyLocationButton={true}
initialRegion={this.state.initialRegion}
onPress={e => this.onPress(e)}
>
{this.state.polygons.map(polygon => (
<Polygon
key={polygon.id}
tappable = {true}
coordinates={polygon.coordinates}
strokeColor={'red'}
fillColor={'hsla(240, 100%, 50%, 0.5)'}
strokeColor={1}
onPress={() => this.onPolygonPress(polygon)}
/>
))}
{this.state.editing && (
<Polygon
key={this.state.editing.id}
coordinates={this.state.editing.coordinates}
strokeColor={'red'}
fillColor={'hsla(240, 100%, 50%, 0.5)'}
strokeColor={1}
/>
)}
</MapView>
<View style={styles.buttonContainerStyle}>
{!this.state.creating && (
<TouchableOpacity
onPress={() => this.create()}
style={styles.buttonStyle}
>
<Text>Create Study Space</Text>
</TouchableOpacity>
)}
{this.state.editing && (
<TouchableOpacity
onPress={() => this.finish()}
style={styles.buttonStyle}
>
<Text>Finish</Text>
</TouchableOpacity>
)}
{this.state.editing && (
<TouchableOpacity
onPress={() => this.cancel()}
style={styles.buttonStyle}
>
<Text>Cancel</Text>
</TouchableOpacity>
)}
</View>
</View>
);
}
Expand All @@ -186,4 +353,16 @@ const styles = StyleSheet.create({
mapStyle: {
...StyleSheet.absoluteFillObject,
},
buttonContainerStyle:{
flexDirection: 'row',
justifyContent: 'space-around',
bottom: 30,
marginHorizontal: 80
},
buttonStyle:{
backgroundColor: '#hsla(60, 100%, 50%, 0.5)',
paddingHorizontal: 10,
paddingVertical: 10,
borderRadius: 10,
},
});

0 comments on commit 697b083

Please sign in to comment.