Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angry happy note #40

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0b1d778
added link to readme
johannacatalinismith Jun 2, 2024
35f36d0
added map
johannacatalinismith Jun 2, 2024
ade2fde
added react router
johannacatalinismith Jun 2, 2024
07c3c5a
made map component
johannacatalinismith Jun 2, 2024
6187fe1
added css
johannacatalinismith Jun 2, 2024
67845b8
added a map id and a note
johannacatalinismith Jun 2, 2024
4567154
added css
johannacatalinismith Jun 3, 2024
da5ba2c
create route and placeholder form
johannacatalinismith Jun 3, 2024
617e5c8
set up backend and connected it to the frontend
johannacatalinismith Jun 3, 2024
3e0aa4e
add geolocation api and hide longitude and latitude
johannacatalinismith Jun 5, 2024
6142741
changed how map controls work
johannacatalinismith Jun 5, 2024
5fef7a7
made the map be on every page.
johannacatalinismith Jun 6, 2024
808c280
fixed to that one note is shown at the time
johannacatalinismith Jun 6, 2024
efa46d6
fix bug for closing notes
johannacatalinismith Jun 6, 2024
c5363cd
fix closing notes
johannacatalinismith Jun 6, 2024
2402692
hide notes after 2 weeks
johannacatalinismith Jun 6, 2024
06f4279
set note title to text
johannacatalinismith Jun 6, 2024
8012486
install material ui
johannacatalinismith Jun 10, 2024
480b3bf
add style
johannacatalinismith Jun 10, 2024
dc6369e
prettier
johannacatalinismith Jun 10, 2024
adba2f2
add error message
johannacatalinismith Jun 12, 2024
94feb3a
add environment variable
johannacatalinismith Jun 12, 2024
e685060
add readme
johannacatalinismith Jun 13, 2024
c6ff57b
add readme
johannacatalinismith Jun 13, 2024
6967255
add netlify.toml
johannacatalinismith Jun 18, 2024
e826208
add home page content
johannacatalinismith Jun 18, 2024
21383ab
move components into components folder
johannacatalinismith Jun 18, 2024
7313c45
move app layout into its own component
johannacatalinismith Jun 18, 2024
fc0b3fb
moved app component to components folder too
johannacatalinismith Jun 18, 2024
5b6ea40
move app component out of components folder
johannacatalinismith Jun 19, 2024
2f15de7
fix import
johannacatalinismith Jun 19, 2024
d813e73
increase defaultZoom
johannacatalinismith Jun 19, 2024
f1992e7
extract Map component
johannacatalinismith Jun 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Final Project
# Angry Happy Note

Replace this readme with your own information about your project.

Start by briefly describing the assignment in a sentence or two. Keep it short and to the point.
Final project for Technigo web developer bootcamp 2024. Angry Happy Note is a full-stack web app where users can leave angry (or happy) notes at their current location on a public map.

## The problem

Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next?
The app is built using Google Maps, React, Vite, and Mongo DB. It's hosted on Netlify, Render, and Mongo DB Atlas.

## View it live

Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about.
https://happyangrynote.netlify.app/
53 changes: 47 additions & 6 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import express from "express";
import cors from "cors";
import mongoose from 'mongoose'

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/flowershop"
mongoose.connect(mongoUrl)
mongoose.Promise = Promise
import mongoose from "mongoose";

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/flowershop";
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;

const Note = mongoose.model("Note", {
latitude: {
type: Number,
required: true,
},
longitude: {
type: Number,
required: true,
},
text: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});

// Defines the port the app will run on. Defaults to 8080, but can be overridden
// when starting the server. Example command to overwrite PORT env variable value:
Expand All @@ -24,8 +41,32 @@ app.get("/", (req, res) => {
res.send("Hello Technigo!");
});

const maxAge = 14 * 24 * 60 * 60 * 1000;
app.get("/notes", async (req, res) => {
const notes = await Note.find({
createdAt: { $gt: new Date(Date.now() - maxAge) },
});
res.json(notes);
});

app.post("/notes", async (req, res) => {
const { latitude, longitude, text } = req.body;
const newNote = new Note({
latitude,
longitude,
text,
});
try {
const savedNote = await newNote.save();
res.status(201).json(savedNote);
} catch (error) {
res
.status(400)
.json({ message: "Could not save note to the database", error });
}
});

// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
});
4 changes: 4 additions & 0 deletions frontend/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[redirects]]
from= "/*"
to = "/index.html"
status = 200
10 changes: 8 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@fontsource/roboto": "^5.0.13",
"@mui/material": "^5.15.19",
"@vis.gl/react-google-maps": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-router-dom": "^6.23.1"
},
"devDependencies": {
"@types/react": "^18.2.15",
Expand All @@ -23,4 +29,4 @@
"eslint-plugin-react-refresh": "^0.4.3",
"vite": "^4.4.5"
}
}
}
72 changes: 72 additions & 0 deletions frontend/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@import url('https://fonts.googleapis.com/css2?family=Rock+Salt&display=swap');


.App-header {
font-family: "Rock Salt", cursive;
font-weight: 400;
font-style: normal;
background: rgba(240, 237, 25, 0.91);
color: #000000;
font-size: 12px;
margin: 0;
text-align: center;
padding: 10px;
position: absolute;
top: 0;
left: 0;
right: 0;
z-index: 1;
}

.App-header a {
color: #1334ef;
text-decoration: underline;
font-family: sans-serif;
font-size: 15px;
}

.App-map {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}

.App-menu {
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: row;
justify-content: center;
column-gap: 15px;
}


/* https://github.com/remix-run/react-router/blob/dev/examples/view-transitions/src/index.css this is about the view transitions that ive added. */
.App-outlet {
view-transition-name: outlet;
}

::view-transition-old(outlet) {
animation: 500ms cubic-bezier(0.4, 0, 1, 1) both fade-out,
500ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;
}

::view-transition-new(outlet) {
animation: 500ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in,
500ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;
}

@keyframes slide-from-right {
from {
transform: translateX(500px);
}
}

@keyframes slide-to-left {
to {
transform: translateX(-500px);
}
}
127 changes: 123 additions & 4 deletions frontend/src/App.jsx

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is correct but readability can be improved, take a look at this refactored code:

import { useEffect, useState } from "react";
import {
  createBrowserRouter,
  Link,
  Outlet,
  RouterProvider,
  useLocation,
} from "react-router-dom";
import { APIProvider, Map } from "@vis.gl/react-google-maps";
import { Note } from "./Note";
import { HomePage } from "./HomePage";
import { FormPage } from "./FormPage";
import { LocationContext } from "./LocationContext";
import "./App.css";

const sweden = { lat: 62.3875, lng: 16.325556 };

const AppLayout = () => {
  const { pathname } = useLocation();
  const [notes, setNotes] = useState([]);
  const [location, setLocation] = useState(sweden);
  const [noteId, setNoteId] = useState("");

  useEffect(() => {
    const url = `${import.meta.env.VITE_BACKEND_URL}/notes`;
    fetch(url)
      .then((response) => response.json())
      .then((data) => setNotes(data));
  }, [pathname]);

  useEffect(() => {
    if (pathname === "/new") {
      navigator.geolocation.getCurrentPosition((position) => {
        setLocation({
          lat: position.coords.latitude,
          lng: position.coords.longitude,
        });
      });
    }
  }, [pathname]);

  return (
    <div className="App">
      <header className="App-header">
        <h1 className="App-title">Angry happy note</h1>
        <ul className="App-menu">
          <li>
            <Link to="/" unstable_viewTransition>
              Note map
            </Link>
          </li>
          <li>
            <Link to="/new" unstable_viewTransition>
              Add note
            </Link>
          </li>
        </ul>
      </header>

      <APIProvider apiKey={import.meta.env.VITE_GOOGLE_MAPS_API_KEY}>
        <Map
          className="App-map"
          defaultCenter={sweden}
          defaultZoom={5}
          center={pathname === "/new" && location.lat && location}
          zoom={pathname === "/new" && location.lat !== sweden.lat && 16}
          gestureHandling="greedy"
          disableDefaultUI
          mapId="happyangrynote"
        >
          {pathname === "/" &&
            notes.map((note) => (
              <Note
                key={note._id}
                {...note}
                open={note._id === noteId}
                onClick={() => setNoteId(note._id === noteId ? "" : note._id)}
              />
            ))}
        </Map>
      </APIProvider>

      <div className="App-outlet">
        <LocationContext.Provider value={location}>
          <Outlet />
        </LocationContext.Provider>
      </div>
    </div>
  );
};

const router = createBrowserRouter([
  {
    path: "/",
    element: <AppLayout />,
    children: [
      {
        index: true,
        element: <HomePage />,
      },
      {
        path: "new",
        element: <FormPage />,
      },
    ],
  },
]);

export const App = () => (
  <RouterProvider
    router={router}
    future={{
      v7_startTransition: true,
    }}
  />
);

Original file line number Diff line number Diff line change
@@ -1,8 +1,127 @@
export const App = () => {
import { useEffect, useState } from "react";
// here i imported the router ... https://github.com/remix-run/react-router/tree/dev/examples/view-transitions
import {
createBrowserRouter,
Link,
Outlet,
RouterProvider,
useLocation,
} from "react-router-dom";

import { HomePage } from "./components/HomePage";
import { FormPage } from "./components/FormPage";
import { LocationContext } from "./LocationContext";
import { Map } from "./components/Map";
import { Note } from "./components/Note";

import "./App.css";

const sweden = { lat: 62.3875, lng: 16.325556 };

const AppLayout = () => {
let { pathname } = useLocation();
const [notes, setNotes] = useState([]);
const [location, setLocation] = useState(sweden);
const [noteId, setNoteId] = useState("");

useEffect(() => {
const url = `${import.meta.env.VITE_BACKEND_URL}/notes`;
fetch(url)
.then((response) => response.json())
.then((data) => setNotes(data));
// fetch more notes, every time you change page
}, [pathname]);

useEffect(() => {
if (pathname === "/") {
} else if (pathname === "/new") {
navigator.geolocation.getCurrentPosition((position) => {
setLocation({
lat: position.coords.latitude,
lng: position.coords.longitude,
});
});
}
}, [pathname]);
// Plug in Material ui https://mui.com/base-ui/react-input/
return (
<>
<h1>Welcome to Final Project!</h1>
</>
<div className="App">
<header className="App-header">
<h1 className="App-title">Angry happy note</h1>
<ul className="App-menu">
<li>
<Link to="/" unstable_viewTransition>
Note map
</Link>
</li>
<li>
<Link to="/new" unstable_viewTransition>
Add note
</Link>
</li>
</ul>
</header>

<Map
defaultCenter={sweden}
defaultZoom={5}
center={pathname === "/new" && location.lat && location}
zoom={pathname === "/new" && location.lat !== sweden.lat && 16}
>
<>
{pathname === "/" &&
// this is so the user can open one note at a time
notes.map((note) => (
<Note
key={note._id}
{...note}
open={note._id == noteId}
onClick={() => {
if (note._id === noteId) {
setNoteId("");
} else {
setNoteId(note._id);
}
}}
/>
))}
</>
</Map>
<div className="App-outlet">
<LocationContext.Provider value={location}>
<Outlet />
</LocationContext.Provider>
</div>
</div>
);
};

// https://github.com/remix-run/react-router/blob/dev/examples/view-transitions/src/main.tsx
// https://stackblitz.com/github/remix-run/react-router/tree/main/examples/view-transitions?file=README.md this is how to make the note slide in and out
const router = createBrowserRouter([
{
path: "/",
element: <AppLayout />,
children: [
{
index: true,
element: <HomePage />,
},
{
path: "/new",
element: <FormPage />,
},
],
},
]);

// https://reactrouter.com/
export const App = () => (
<RouterProvider
router={router}
future={{
// Wrap all state updates in React.startTransition()
v7_startTransition: true,
}}
/>
);
3 changes: 3 additions & 0 deletions frontend/src/LocationContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from "react";

export const LocationContext = createContext({ lat: 0, lng: 0 });
44 changes: 44 additions & 0 deletions frontend/src/components/FormPage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.FormPage {
display: grid;
place-items: center;
width: 100%;
height: 100vh;
}

.FormPage-paper {
background: #f0ed18 !important;
}

.FormPage-form {
display: grid;
gap: 10px;
padding: 20px;
border-radius: 5px;
}

.FormPage-label {
font-size: 20px;
color: #ffffff;
}

.FormPage-textarea {
width: 100%;
height: 100px;
padding: 10px;
font-size: 16px;
}

.FormPage-error {
color: #960000;
font-family: sans-serif;
}

.FormPage-submit {
padding: 10px;
font-size: 16px;
background-color: #1a8121;
color: #ffffff;
border: none;
border-radius: 5px;
cursor: pointer;
}
Loading