Commit 662d9bb7 authored by Hedda's avatar Hedda
Browse files

#13 backend up and running

parent d2d6ca60
......@@ -15,7 +15,7 @@ router.post('/updateData', (req, res) => {
// this is our get method to get data by id for destination page
router.get('/getDataFrom/:id', (req, res, next) => {
router.get('/search/:id', (req, res, next) => {
var destinatoinID = req.params.id;
Destinations.findById(destinatoinID, (err, data) => {
if (err) return res.json({ success: false, error: err });
......@@ -28,7 +28,7 @@ router.get('/getData', (req, res) => {
Destinations.find((err, data) => {
if (err) return res.json({ success: false, error: err });
else{
var sorted = this.sortData(data)
var sorted = sortData(data, req)
return res.json({ success: true, data: sorted });
}
});
......@@ -36,8 +36,8 @@ Destinations.find((err, data) => {
//get the three most popular destinations to show at the main page
router.get('/threeMostPopular', (req, res) => {
Destinations.find().sort({popularity: -1}).limit(3).exec(function(err, data){
router.get('/fiveMostPopular', (req, res) => {
Destinations.find().sort({popularity: -1}).limit(5).exec(function(err, data){
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
})
......@@ -48,7 +48,7 @@ const word = req.params.word.toLowerCase()
Destinations.find({$or: [{'name': word}, {'country': word}, {'continent': word}]}, function (err, data){
if (err) return res.json({ success: false, error: err });
else{
var sorted = this.sortData(data)
var sorted = sortData(data, req)
return res.json({ success: true, data: sorted });
}
})})
......@@ -65,15 +65,7 @@ router.get('/search/:continent/:word', (req, res) => {
})})
//get the three most popular destinations to show at the main page
router.get('/wordcloudPopularity', (req, res) => {
Destinations.find().select('name popularity -_id').exec(function(err, data){
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
})
})
sortData = (data) => {
sortData = (data, req) => {
const sorting = req.params.sort;
if(sorting === "A-Z"){
const sorted = data.sort((a,b) => (a.name > b.name) ? 1:-1)
......@@ -88,4 +80,4 @@ sortData = (data) => {
// append /api for our http requests
app.use('/api', router);
\ No newline at end of file
module.exports = router;
\ No newline at end of file
......@@ -3,6 +3,7 @@ const express = require('express');
var cors = require('cors');
const bodyParser = require('body-parser');
const logger = require('morgan');
const router = require('./routers/api')
const API_PORT = 3001;
......@@ -31,5 +32,8 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger('dev'));
app.use('/api', router)
// launch our backend into a port
app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));
......@@ -8,6 +8,7 @@ import { createStore, applyMiddleware } from 'redux';
import rootReducer from './reducers/index'
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { View } from 'react-native'
const store = createStore(rootReducer, applyMiddleware(thunk))
......@@ -30,4 +31,4 @@ const TabNavigator = createBottomTabNavigator(
const AppContainer = createAppContainer(TabNavigator)
export default () => (<Provider store = {store}><View style = {{flex: 1}}><AppContainer /></View></Provider>);
\ No newline at end of file
export default () => (<Provider store = {store}><View style = {{flex: 1}}><AppContainer style={{margin: 100}}/></View></Provider>);
\ No newline at end of file
export const setData = (data) => {
export const setPage = (page) => {
return (dispatch, getState) => {
console.log("INNI ACTION")
console.log("INNI ACTION med page: ", page)
dispatch({
type: 'SET_DATA', data
type: 'SET_PAGE', page
})
}
......
......@@ -2,28 +2,25 @@ import Axios from "axios"
const API_URL = "http://it2810-10.idi.ntnu.no:3001/api/"
export const GetAllData = () => {
console.log("we're in")
fetch(API_URL + "getData")
.then((data) => data.json())
.then((res) => {console.log("Dette er dataen: \n", res.data);return res.data})
}
/*
const GetDataFromSearch = (search) => {
fetch(API_URL + "search/" + search)
.then((data) => data.json())
.then((res) => {this.props.setData(res.data)})
.then(console.log("Satt data i redux"))
}
const createURL = (input) => {
const API_URL = "http://it2810-10.idi.ntnu.no:3001/api/"
if(input == undefined){
return API_URL +"getData"
}
if(typeof input == 'string'){
return API_URL + "search/" + input
}
if(typeof input == 'number'){
return API_URL + "fiveMostPopular"
}
}
const GetDataFromId = (destinationID) => {
fetch(API_URL + "getDataFrom/" + destinationID)
.then((data) => data.json())
.then((res) => {this.props.setData(res.data)})
.then(console.log("Satt data i redux"))
export const GetData = (input) => {
const url = createURL(input)
console.log("Her er url: ",url)
return Axios.get(url);
}
/*
......@@ -33,15 +30,4 @@ const UpdatePopulatiry =(destinationID, newPopularity) => {
update: { popularity: newPopularity },
})
}
const mapDispatchToProps = (dispatch) => {
return {
setData: (data) => dispatch(setData(data)),
showDestination: (destinationID) => dispatch(showDestination(destinationID)),
}
};
//export default connect(null, mapDispatchToProps)({GetAllData, GetDataFromSearch, GetDataFromId, UpdatePopulatiry})
//export default connect(null, mapDispatchToProps)(GetAllData)
*/
\ No newline at end of file
import React, { Component } from 'react';
import { StyleSheet, Text, View, Alert, TouchableHighlight } from 'react-native';
import { StyleSheet, Text, View, Alert, TouchableHighlight, Image } from 'react-native';
import { connect } from 'react-redux';
import { GetData } from '../api/fetchers'
class Card extends Component {
state ={
data: [],
}
componentDidMount(){
this.checkPage()
}
checkPage(){
if(this.props.page === "Home"){
var locations = GetData(5)
locations.then((data) => {console.log(data);this.setState({data: data})})
}
}
render(){
const styles = StyleSheet.create({
......@@ -18,15 +38,30 @@ class Card extends Component {
alignItems: "center"
}
})
const { data } = this.state
return(
<TouchableHighlight underlayColor = 'transparent' onPress = {() => Alert.alert("HEI")}>
<View style = {styles.container}>
<View><Text> Bildet her </Text></View>
<View><Text> Bynavn her </Text></View>
<View>
{data.map(dat => {
<TouchableHighlight underlayColor = 'transparent' onPress = {() => Alert.alert("HEI")}>
<View style = {styles.container}>
<View><Image source={{uri: dat.img}}/></View>
<View><Text> {dat.name} </Text></View>
</View>
</TouchableHighlight>
})}
</View>
</TouchableHighlight>
)
}
)}
}
export default Card;
const mapStateToProps = (state) => { //give us accsess to the data in store
return {
page: state.page.page,
}
};
export default connect(mapStateToProps)(Card);
......@@ -47,7 +47,7 @@ class SearchBox extends Component {
return(
<View style = {{flexDirection: "row", margin: 20}}>
<TextInput style = {inputfield} autoCorrect = "false" onKeyPress = {(e) => this.handleSearchWord(e)} onSubmitEditing = {() => this.search()} />
<TextInput style = {inputfield} autoCorrect = {false} onKeyPress = {(e) => this.handleSearchWord(e)} onSubmitEditing = {() => this.search()} />
<Icon name = "search" onPress = {() => this.search()}/>
<Text> {this.props.word} </Text>
</View>
......
......@@ -11,8 +11,7 @@ class Test extends Component {
componentDidMount() {
var dataen = GetAllData()
//console.log(" ELHRGOIEHRGOIH", data)
this.setState({data: dataen})
dataen.then((data) => this.setState({data: data}))
}
......
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import SearchBox from '../components/SearchBox'
import SearchBox from '../components/SearchBox';
import { setPage } from '../actions/SetPageAction';
import { connect } from 'react-redux';
class ExplorePage extends Component {
componentDidMount(){
this.props.setPage("Explore")
}
render(){
return(
<View>
......@@ -12,4 +19,11 @@ class ExplorePage extends Component {
}
}
export default ExplorePage;
\ No newline at end of file
const mapDispatchToProps = (dispatch) => {
return {
setPage: (page) => dispatch(setPage(page)),
}
};
export default connect(null, mapDispatchToProps)(ExplorePage);
\ No newline at end of file
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Text, View } from 'react-native';
import Card from '../components/Card'
import Test from '../components/Test'
import { createStore, applyMiddleware } from 'redux';
import rootReducer from '../reducers/';
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import { connect } from 'react-redux';
import { setPage } from '../actions/SetPageAction';
const store = createStore(rootReducer, applyMiddleware(thunk))
class HomePage extends Component{
componentWillMount(){
this.props.setPage("Home")
}
render(){
return(
<View style={{
......@@ -20,10 +26,9 @@ class HomePage extends Component{
<Text style={{fontSize: 20, textAlign: "center", padding: 8}}>Click here to see a wordcloud showing the most popular locations</Text>
<Text style={{fontSize: 16, textAlign: "center", padding: 8}}>Below you see the five most popular places</Text>
<Card/>
<Card/>
</View>
/*
/*
<Provider store = {store}>
<Test/>
</Provider>
......@@ -32,4 +37,10 @@ class HomePage extends Component{
}
}
export default HomePage;
\ No newline at end of file
const mapDispatchToProps = (dispatch) => {
return {
setPage: (page) => dispatch(setPage(page)),
}
};
export default connect(null, mapDispatchToProps)(HomePage);
\ No newline at end of file
const initState = {
data: [],
}
const setDataReducer = (state = initState, action) => {
switch(action.type) {
case 'SET_DATA':
console.log("HEIHEIEHI ", action.data)
return{data: action.data}
default: return state;
};
}
export default setDataReducer;
\ No newline at end of file
const initState = {
page: "",
}
const setPageReducer = (state = initState, action) => {
switch(action.type) {
case 'SET_PAGE':
return{page: action.page}
default: return state;
};
}
export default setPageReducer;
\ No newline at end of file
......@@ -3,7 +3,7 @@ import destinationReducer from './DestinationReducer';
import lengthReducer from './LengthReducer';
import filterReducer from './FilterReducer';
import { combineReducers } from 'redux';
import setDataReducer from './SetDataReducer';
import setPageReducer from './SetPageReducer';
const rootReducer = combineReducers({
......@@ -11,7 +11,7 @@ const rootReducer = combineReducers({
sort: sortReducers,
destination: destinationReducer,
length: lengthReducer,
data: setDataReducer
page: setPageReducer
});
export default rootReducer
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment