Skip to content

Commit

Permalink
refactoring to react-routes 80% done
Browse files Browse the repository at this point in the history
  • Loading branch information
RingoRohe committed Mar 30, 2020
1 parent 79983cd commit d3c7195
Show file tree
Hide file tree
Showing 27 changed files with 502 additions and 285 deletions.
102 changes: 102 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-firebaseui": "^4.1.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"reset-css": "^5.0.1"
},
Expand Down
144 changes: 139 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
// React stuff
import React, { useState, useEffect } from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import firebase from './firebase/firebase';

// CSS and Libs
import 'assets/scss/App.scss';
import 'libs/loading_overlay/css/main.css'

// Models
import Nap from 'models/Nap';

// Components
import Login from 'components/login/Login';
import Dashboard from 'components/dashboard/Dashboard';
import NavBar from 'components/menu/NavBar';
import Header from 'components/header/Header';
import Footer from 'components/footer/Footer';
import ProfileMenu from 'components/profile/ProfileMenuView';
import Modal from "components/modal/Modal";
import Naps from 'components/naps/Naps';

function App() {
// Authentication
let [currentUser, setCurrentUser] = useState(null);

useEffect(() => {
Expand Down Expand Up @@ -38,11 +53,130 @@ function App() {
firebase.firestore().collection('users').doc(user.uid).set(newUserData, { merge: true });
}

if (currentUser) {
return <Dashboard firebase={firebase} currentUser={currentUser} />;
} else {
return <Login firebase={firebase} />
}
// Modal View
let [modalContent, setModalContent] = useState("");
let [modalVisibility, setModalVisibility] = useState(false);
const modal = {
setContent: (content) => setModalContent(content),
toggleVisibility: () => setModalVisibility(!modalVisibility),
hide: () => setModalVisibility(false),
show: () => setModalVisibility(true),
modalVisibility,
modalContent
};

// Naps
let [runningNap, setRunningNap] = useState(null);
// const nc = new NapsController(props.firebase, props.currentUser, runningNap);
useEffect(() => {
// console.log('useEffect in Dashboard.js');

// bind to running nap
if (currentUser && currentUser.uid) {
const ref = firebase.firestore().collection(`users/${currentUser.uid}/naps`);
let last = ref.orderBy('start').where('start', '>', 0).where('end', '==', 0).limit(1);
const unmountRunningNapListener = last.onSnapshot(snapshot => {
if (snapshot.empty) {
setRunningNap(null);
} else {
const nap = new Nap();
nap.fromFirebaseDoc(snapshot.docs[0]);
setRunningNap(nap);
}
}, err => {
console.log(`Encountered error: ${err}`);
});
return () => {
unmountRunningNapListener();
};
}

}, [currentUser]);

const naps = {
isNapRunning: () => {
return runningNap ? true : false;
},
startNap: () => {
let newNap = new Nap(Date.now());
const ref = firebase.firestore().collection(`users/${currentUser.uid}/naps`);
ref.add(newNap.toObject())
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
},

finishNap: () => {
const ref = firebase.firestore().collection(`users/${currentUser.uid}/naps`);
ref.doc(runningNap.id).update({ end: Date.now() });
},

deleteNap: nap => {
const ref = firebase.firestore().collection(`users/${currentUser.uid}/naps`);
ref.doc(nap.id).delete();
},

getNaps: (...args) => {
if (!currentUser || !currentUser.uid) {
return false;
}

let ref = firebase.firestore().collection(`users/${currentUser.uid}/naps`);

if (!isNaN(args[0]) && parseInt(args[0]) < 100) {
// get last n Items
return ref.orderBy("start", "desc").limit(parseInt(args[0]));
}

if (args.length === 0) {
// get all Items of today
let from = Date.now() - 1 * 24 * 60 * 60 * 1000; // - 1 day
let to = Date.now();
return ref
.where("start", ">=", from)
.where("start", "<=", to)
.orderBy("start", "desc");
}
}
};

return (
<BrowserRouter>
<div className="wrapper">
<Header />
<NavBar currentUser={currentUser} />
<ProfileMenu
currentUser={currentUser}
firebase={firebase}
modal={modal}
/>
<Route
exact
path="/"
render={props => (
<Dashboard
{...props}
firebase={firebase}
currentUser={currentUser}
naps={naps}
runningNap={runningNap}
/>
)}
/>
<Route exact path="/naps" render={props => <Naps {...props} naps={naps} />} />
<Route
exact
path="/login"
render={props => <Login {...props} firebase={firebase} />}
/>
<Footer />
<Modal modal={modal}>{modalContent}</Modal>
</div>
</BrowserRouter>
);

}

Expand Down
3 changes: 2 additions & 1 deletion src/assets/scss/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ body {
@import './main.scss';
@import './naps.scss';
@import './footer.scss';
@import './modal.scss';
@import './modal.scss';
@import './login.scss';
5 changes: 4 additions & 1 deletion src/assets/scss/globals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
}

.menuPoint {
height: 100%;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
cursor: pointer;
padding: 1rem;
min-width: 4rem;
min-width: 6rem;
text-decoration: none;

.icon {
color: $highlight;
Expand Down
1 change: 1 addition & 0 deletions src/assets/scss/header.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
header {
grid-area: header;
background-color: $black;
padding: 1rem;

h1 {
font-size: 3rem;
Expand Down
5 changes: 5 additions & 0 deletions src/assets/scss/login.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.login {
padding: 2rem;
background-color: $mid;
grid-area: main;
}
2 changes: 1 addition & 1 deletion src/assets/scss/mainMenu.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.mainMenu {
grid-area: nav;
display: flex;
justify-content: space-between;
justify-content: flex-start;
padding: 0 !important;

ul {
Expand Down
Loading

0 comments on commit d3c7195

Please sign in to comment.