Skip to content

Commit

Permalink
Merge pull request #192 from Arquisoft/feature-view-shared-routes
Browse files Browse the repository at this point in the history
Feature view shared routes
  • Loading branch information
jesQM authored Apr 24, 2020
2 parents ee0317e + 717117a commit fab7c0c
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/components/navBar/navBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import viadeLogo from './../../assets/logo/logo_alt.jpeg';
import viadeText from './../../assets/logo/logo_letters.jpeg';
import RouteManager from "./../../model/RouteManager";
import ShareView from '../../pages/ShareView';
import RouteSharedList from "../../pages/RouteSharedList";

const routeManager = RouteManager;

Expand Down Expand Up @@ -63,6 +64,8 @@ function MyNavBar(props) {
<NavDropdown.Item href="#routes/list">{t('navBarMyRoutes')}</NavDropdown.Item>
<NavDropdown.Item href="#routes/add">{t('navBarCreateRoute')}</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#routes/shared">{t('navBarSharedRoutes')}</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#routes/example">{t('navBarRouteHelp')}</NavDropdown.Item>
</NavDropdown>
</Nav>
Expand All @@ -87,6 +90,7 @@ function MyNavBar(props) {
<Route exact path="/register" component={SignUp} />
<Route exact path="/routes/add" render={() => <RouteCreation routeManager={routeManager} />} />
<Route exact path="/routes/list" render={() => <RouteList routeManager={routeManager} />} />
<Route exact path="/routes/shared" render={() => <RouteSharedList routeManager={routeManager} />} />
<Route exact path="/routes/example" render={() => <RouteHelp/>} />
<Route exact path="/home" component={Home} />
<Route exact path="/profile" render={() => <Profile routeManager={routeManager} />} />
Expand Down
78 changes: 42 additions & 36 deletions src/components/podService/podStoreHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class PodStorageHandler extends PodHandler{
*/
constructor(currentSession) {
super(currentSession);
this.sharedRoutesToAdd = [];
}

/**
Expand Down Expand Up @@ -115,7 +116,6 @@ export default class PodStorageHandler extends PodHandler{
}

async getRoutesSharedToMe(forEachRoute = () => {}){
let result = [];
await this._getSharedFolder(async function (file) {
// Transform file to JSON
let fileAsJSON = JSON.parse(file);
Expand All @@ -125,17 +125,14 @@ export default class PodStorageHandler extends PodHandler{
sharedRoutes = sharedRoutes.map((j) => { return j["@id"]; });
for (let i = 0; i < sharedRoutes.length; i++){
let fileUrl = sharedRoutes[i];
console.log(fileUrl);
await this.getFile(fileUrl).then( function(content) {
// Create routes from JSON
let routeObject = new MyRoute();
routeObject.modifyFromJsonLd(JSON.parse(content));
forEachRoute(routeObject);
result.push(routeObject);
}, (error) => {} );
}, (error) => {forEachRoute(null);} );
}
}.bind(this));
return result;
}

/**
Expand All @@ -152,25 +149,21 @@ export default class PodStorageHandler extends PodHandler{
const parser = new N3.Parser();
parser.parse(content, function (error, quad, prefixes) { // parse the content of the message
if (quad) {
if ( quad.predicate.id == "http://schema.org/text" ) { // If the quad is the url of the route
if ( quad.predicate.id === "http://schema.org/text" && quad.object.id.includes("/viade/routes/") ) { // If the quad is the url of the route
forEachMail(quad.object.id);
console.log("PUSHED " + quad.object.id);
//newRoutes.push(quad.object.id);
this.addRoutesAsShared([quad.object.id.split("\"").join("")]);
this._markEmailAsRead(url);
this.sharedRoutesToAdd.push(quad.object.id.split("\"").join(""));
}
}
}.bind(this));
}.bind(this),
(error) => {}
}.bind(this),
(error) => {}
);
}.bind(this));

//await this.addRoutesAsShared(newRoutes);

}.bind(this),
(error) => { }
);
this.addRoutesAsShared(this.sharedRoutesToAdd);
}

/**
Expand All @@ -195,31 +188,44 @@ export default class PodStorageHandler extends PodHandler{
}

async addRoutesAsShared(urls){
console.log("HasBeenShared! " + urls);

let content = JSON.stringify(
{
"@context": {
"@version": 1.1,
"routes": {
"@container": "@list",
"@id": "viade:routes"
},
"viade": "http://arquisoft.github.io/viadeSpec/"
},
"routes": urls.map((url) => {return {"@id": url.toString()}})
});
let file = null;
let filename = "en3a.json";

// 1.- Get a shared File
try {
file = await this.getFile(this.repository + this.defaultFolder + this.sharedDirectory + filename);
file = JSON.parse(file);
} catch (e) {
if (e.status !== 404) {
throw e;
} else {
file = {
"@context": {
"@version": 1.1,
"routes": {
"@container": "@list",
"@id": "viade:routes"
},
"viade": "http://arquisoft.github.io/viadeSpec/"
},
"routes": []
};
}
}

//console.log(urls);
//console.log(urls.map((url) => {return {"@id": url}}));
//console.log(content);
// 2.- Remove duplicated routes
let alreadyRoutes = file["routes"].map((url) => {return url["@id"];});
urls.forEach((url) => {
if (alreadyRoutes.indexOf(url) === -1) {
alreadyRoutes.push( url );
}
});
urls = [];

let filename = Date.now() + ".json";
this.storeFile(this.repository + this.defaultFolder + this.sharedDirectory + filename, content);
}
// 3.- Rewrite file
file["routes"] = alreadyRoutes.map((url) => {return {"@id": url.toString()}});

_markEmailAsRead(url) {
//console.log("READ! " + url);
this.storeFile(this.repository + this.defaultFolder + this.sharedDirectory + filename, JSON.stringify(file));
}

async getFolder(url) {
Expand Down
21 changes: 19 additions & 2 deletions src/components/routeList/RouteCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,24 @@ class RouteCard extends React.Component {
style={{ borderRadius: "5px", margin: "0", height: "100%", width: "100%" }}
/>
};
this.showShareButton = true;
if (props.showShareButton !== undefined) {
this.showShareButton = props.showShareButton;
}

this.showInfoButton = true;
if (props.showInfoButton !== undefined) {
this.showInfoButton = props.showInfoButton;
}
}

render() {

let buttons = [
this.showInfoButton && <Button variant="dark" href={`#routes/info/${this.route.id}`}>Info</Button>,
this.showShareButton && <Button variant="dark" style={{ margin: "16px" }} href={`#routes/share/${this.route.id}`}>Share</Button>
];

return (
<Card text="dark">
<Card.Header style={{ minHeight: "100px", height: "300px", padding: "0" }}>
Expand All @@ -29,8 +44,7 @@ class RouteCard extends React.Component {
<Card.Body>
<Card.Title style={{ fontSize: "24px" }}>{this.route.name}</Card.Title>
<Card.Text style={{ fontSize: "18px" }}>{this.route.description}</Card.Text>
<Button variant="dark" href={`#routes/info/${this.route.id}`}>Info</Button>
<Button variant="dark" style={{ margin: "16px" }} href={`#routes/share/${this.route.id}`}>Share</Button>
{buttons}
</Card.Body>
</Card >
);
Expand All @@ -40,6 +54,9 @@ class RouteCard extends React.Component {
this.setState({ mapComponent: this.state.mapComponent });
}

toggleShareButton(){
this.showShareButton = !this.showShareButton;
}
}


Expand Down
2 changes: 2 additions & 0 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const resources = {
"navBarRoutes": "Route management",
"navBarMyRoutes": "My routes",
"navBarCreateRoute": "Create a new route",
"navBarSharedRoutes" : "Shared to Me",
"navBarRouteHelp": "How do routes work?",
"routeListText": "Route List",
"friendCardDelete": "Delete",
Expand Down Expand Up @@ -110,6 +111,7 @@ const resources = {
"navBarRoutes": "Gestión de rutas",
"navBarMyRoutes": "Mis rutas",
"navBarCreateRoute": "Crear una nueva ruta",
"navBarSharedRoutes" : "Compartidas conmigo",
"navBarRouteHelp": "¿Cómo funcionan las rutas?",
"routeListText": "Lista de rutas",
"friendCardDelete": "Borrar",
Expand Down
11 changes: 10 additions & 1 deletion src/model/RouteManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class RouteManager {

constructor() {
this.routes = [];
this.resetRoutes();
}

getRoutes() {
Expand All @@ -16,8 +16,17 @@ class RouteManager {
this.routes.push(route);
}

addSharedRoute(route){
this.routesSharedToMe.push(route);
}

getSharedRouteById(id) {
return this.routesSharedToMe.find((route) => route.getId() === id);
}

resetRoutes() {
this.routes = [];
this.routesSharedToMe = [];
}

}
Expand Down
13 changes: 0 additions & 13 deletions src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ class Home extends Component {
<h3>V 1.0</h3>
</div>
);

// <!-- <button onClick={this.getEmail}>Check EMAIL!</button> -->
// <!-- <button onClick={this.getSharedToMe}>You were shared:</button> -->
}

async getEmail(){
await new PodStorageHandler(await auth.currentSession()).checkInbox((r) => {}); // console.log("Callback called:"); console.log(r);
}

async getSharedToMe(){
let routes = await new PodStorageHandler(await auth.currentSession()).getRoutesSharedToMe((r) => {console.log("Callback called:"); console.log(r);});
console.log("Function finished");
console.log(routes);
}

async printName() {
Expand Down
33 changes: 26 additions & 7 deletions src/pages/RouteList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ import MyRoute from "../model/MyRoute";
import $ from "jquery";

import 'react-toastify/dist/ReactToastify.css';
import RouteManager from "../model/RouteManager";

const auth = require('solid-auth-client');

class RouteList extends React.Component {

constructor(props) {
super(props);
this.routeManager = props.routeManager;
this.routeManager = RouteManager;
this.cardDeckSize = 4;
this.state = {
routes: [],
spinnerHidden: false
sharedRoutes : [],
spinnerHidden: false,
};
this.syncRoutesWithPod().then(() => {
this.state.spinnerHidden = true;
});
this.processedRoutes = 0;
this.retrievedRoutes = 0;
if (props.sync == undefined || props.sync == true) {
this.syncRoutesWithPod().then(() => {
this.state.spinnerHidden = true;
});
this.processedRoutes = 0;
this.retrievedRoutes = 0;
}
}

render() {
Expand Down Expand Up @@ -75,6 +79,8 @@ class RouteList extends React.Component {
let session = await auth.currentSession();
if (session !== null && session !== undefined) {
let storageHandler = new PodStorageHandler(session);

// Handle my Routes
storageHandler.getRoutes((routeJson, error) => {
if (routeJson === null) {
toast.error(i18n.t('alertAccessPOD'));
Expand Down Expand Up @@ -107,6 +113,19 @@ class RouteList extends React.Component {
}
}
);

// Handle Shared Routes
storageHandler.getRoutesSharedToMe( (route) => {
if (route === undefined || route == null) {
toast.error("We can't access your POD. Please, review its permissions");
} else {
this.routeManager.addSharedRoute(route);
let tempList = this.state.sharedRoutes;
tempList.push(route);
this.setState({ sharedRoutes: tempList });
$("#messageArea").empty();
}
});
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/pages/RouteSharedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from "react";
import RouteManager from "../model/RouteManager";
import {CardDeck, Spinner} from "react-bootstrap";
import RouteCard from "../components/routeList/RouteCard";
import {toast, ToastContainer} from "react-toastify";
import {Translation} from "react-i18next";
import PodStorageHandler from "../components/podService/podStoreHandler";
import MyRoute from "../model/MyRoute";
import $ from "jquery";
import RouteList from "./RouteList";
const auth = require('solid-auth-client');

export default class RouteSharedList extends RouteList {

constructor(props) {
super(props);
if (props.sync === undefined || props.sync == true)
this.readInbox();
}

async readInbox() {
new PodStorageHandler(await auth.currentSession()).checkInbox();
}

render() {
let routesForCardDecks = [];
let counter = 0;
while (counter <= this.state.sharedRoutes.length) {
routesForCardDecks.push(
<CardDeck style={{ padding: "1% 0% 1% 2%", width: "100%" }}>
{this.state.sharedRoutes.slice(counter, counter + this.cardDeckSize).map(
(r) => {return <RouteCard route={r} showShareButton={false} />;}
)}
</CardDeck>
);
counter += this.cardDeckSize;
}

return (
<div>
<ToastContainer
position={toast.POSITION.TOP_CENTER}
autoClose={5000}
/>
<Translation>
{
(t) => <h1 style={{ padding: "1%" }}>{t('routeListText')}</h1>
}
</Translation>
<Translation>
{
(t) => <h2 style={{ padding: "1%" }} hidden={this.state.spinnerHidden}>{t('routeListLoadingMessage')}</h2>
}
</Translation>

<Spinner id={"spinner"} hidden={this.state.spinnerHidden} animation="border" />
{routesForCardDecks}
<div id="messageArea">
{this.state.message}
</div>
</div>
);
}
}
Loading

0 comments on commit fab7c0c

Please sign in to comment.