enh(#4): Adds new frontend

This commit is contained in:
Anthony Al Lazkani 2024-12-19 01:35:29 +02:00
parent 6800c90936
commit 11e9ee6a32
24 changed files with 17008 additions and 2 deletions

26
.gitignore vendored
View file

@ -9,4 +9,30 @@ build/
# IDE
.vscode/
.idea/
*.db
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
node_modules/
.pnp/
.pnp.js
# testing
coverage/
# production
build/
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

View file

@ -1,11 +1,17 @@
Server:
host: 127.0.0.1
port: 8000
Web:
host: 127.0.0.1
port: 8000
Database:
username: foo
password: bar
#url: "sqlite+pysqlite:///:memory:"
url: "sqlite+pysqlite:///shortenit.db"
Shortener:
# *CAUTION*: Enabling this check if the ID already exists before returning it.
# Even though this guarantees that the ID doesn't exist, this might inflict

46
frontend/README.md Normal file
View file

@ -0,0 +1,46 @@
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).

16263
frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

52
frontend/package.json Normal file
View file

@ -0,0 +1,52 @@
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.121",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"axios": "^1.7.9",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^7.0.1",
"react-scripts": "5.0.1",
"react-toastify": "^11.0.1",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.4.20",
"postcss": "^8.4.49",
"tailwindcss": "^3.4.15"
}
}

View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Shorten It</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

View file

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

24
frontend/src/App.css Normal file
View file

@ -0,0 +1,24 @@
@import url("https://fonts.googleapis.com/css2?family=Jost:ital,wght@0,100..900;1,100..900&family=Outfit:wght@100..900&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Jost:ital,wght@0,100..900;1,100..900&family=Outfit:wght@100..900&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap");
body {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Outfit", sans-serif;
background-color: #f5f5f5;
}
:root {
--color-primary: #f5f5f5;
--color-secondary: #19253a;
--color-tertiary: #1d4ed8;
--text-color: #333;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}

28
frontend/src/App.tsx Normal file
View file

@ -0,0 +1,28 @@
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import "./App.css";
// Pages & Components
import Navbar from "./components/navbar/Navbar";
import Footer from "./components/footer/Footer";
import Home from "./pages/home/Home";
import About from "./pages/about/About";
import Contact from "./pages/contact/Contact";
function App() {
return (
<div className="App">
<Router>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
<Footer />
</Router>
</div>
);
}
export default App;

View file

@ -0,0 +1,5 @@
.footer {
width: 100%;
height: 200px;
background-color: var(--color-secondary);
}

View file

@ -0,0 +1,10 @@
import React from "react";
import "./Footer.css";
function Footer() {
return (
<footer className="footer"></footer>
);
}
export default Footer;

View file

@ -0,0 +1,88 @@
.navbar {
background-color: #19253a;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 20px;
}
.logo a {
color: #fff;
font-size: 24px;
font-weight: bold;
}
.navbar-container {
display: flex;
}
.navbar-container ul {
display: flex;
gap: 20px;
}
.navbar-container ul li {
padding: 8px 15px;
border-radius: 5px;
transition: 0.2s;
}
.navbar-container ul li a {
color: #fff;
display: block;
}
.navbar-container ul li:hover {
background-color: #1d4ed8;
}
.hamburger {
display: none;
position: absolute;
right: 5%;
color: #fff;
font-size: 30px;
}
.navbar-btn {
padding: 8px 30px;
border-radius: 5px;
background-color: #1d4ed8;
border: 1px solid #f5f5f5;
color: #fff;
border: none;
cursor: pointer;
}
@media (max-width: 600px) {
.hamburger {
display: flex;
}
.navbar {
flex-direction: column;
align-items: flex-start;
padding: 10px 20px;
}
.logo {
justify-content: flex-start;
align-items: flex-start;
width: 100%;
}
.navbar-container {
width: 100%;
}
.navbar-container ul {
display: none;
flex-direction: column;
width: 100%;
text-align: center;
}
.navbar-container ul.open {
display: flex;
}
}

View file

@ -0,0 +1,39 @@
import React, { useState } from "react";
import "./Navbar.css";
import { Link } from "react-router-dom";
import { FaBars } from "react-icons/fa6";
function Navbar() {
const [menuOpen, setMenuOpen] = useState<boolean>(false);
const toggleMenu = (): void => {
setMenuOpen((prevState) => !prevState);
};
return (
<header className="navbar">
<div className="logo">
<Link to="/">Shorten It</Link>
</div>
<FaBars className="hamburger" onClick={toggleMenu} />
<nav className="navbar-container">
<ul className={menuOpen ? "open" : ""}>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contact">Contact</Link>
</li>
<button className="navbar-btn">Login</button>
</ul>
</nav>
</header>
);
}
export default Navbar;

15
frontend/src/index.tsx Normal file
View file

@ -0,0 +1,15 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals

View file

View file

@ -0,0 +1,8 @@
import React from "react";
import "./About.css";
function About() {
return <h1>ABOUT PAGE</h1>;
}
export default About;

View file

View file

@ -0,0 +1,6 @@
import React from "react";
import "./Contact.css";
export default function Contact() {
return <h1>CONTACT</h1>;
}

View file

@ -0,0 +1,166 @@
.home-page {
height: 80vh;
width: 80%;
display: flex;
align-items: center;
justify-content: center;
margin: auto;
}
.home-container {
height: 300px;
width: 800px;
border-radius: 5px;
box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.3), 0px 4px 6px rgba(0, 0, 0, 0.1);
display: grid;
grid-template-columns: 2fr 1fr;
transition: box-shadow 0.3s ease-in-out;
}
.home-container:hover {
box-shadow: 0px 20px 25px rgba(0, 0, 0, 0.3), 0px 8px 10px rgba(0, 0, 0, 0.2);
}
.left-side {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
gap: 10px;
padding-bottom: 20%;
padding-left: 5%;
padding-right: 5%;
}
.left-side h1 {
color: var(--color-tertiary);
border-bottom: 1px solid var(--color-tertiary);
}
.url-input {
display: flex;
gap: 2px;
}
.url-input input {
/* position: relative; */
width: 250px;
height: 40px;
outline: none;
border-radius: 5px;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
border: 1px solid rgba(0, 0, 0, 0.4);
padding-left: 5px;
white-space: nowrap;
}
.url-input button {
border-radius: 5px;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
background-color: var(--color-tertiary);
border: none;
min-width: 80px;
color: #fff;
transition: 0.2s;
}
.url-input button:hover {
opacity: 0.8;
}
.right-side {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
border-left: 2px solid rgba(0, 0, 0, 0.1);
padding: 0 5%;
}
.right-side-title h1 {
color: var(--color-tertiary);
}
.right-side-content p {
font-family: "Roboto", sans-serif;
font-size: 14px;
/* color: var(--text-color); */
color: #808080;
}
@media (max-width: 730px) {
.home-container {
grid-template-columns: 1fr;
height: auto;
width: auto;
}
.left-side {
padding-bottom: 5%;
}
.right-side {
border-left: none;
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
}
@media (max-width: 475px) {
.url-input input {
height: 25px;
width: 200px;
}
.left-side h1 {
font-size: 25px;
}
.right-side-title h1 {
font-size: 25px;
}
.right-side-content p {
font-size: 13px;
}
.url-input button {
min-width: 60px;
}
}
@media (max-width: 475px) {
.url-input {
flex-direction: column;
}
.url-input button {
height: 30px;
border-radius: 5px;
}
.url-input input {
border-radius: 5px;
}
}
.custom-toast {
font-family: "Roboto", "sans-serif";
font-size: 13px;
}
@keyframes fadeIn {
to {
opacity: 1;
transform: translateY(0);
}
}
.url-input.shortened {
opacity: 0;
transform: translateY(-20px);
animation: fadeIn 0.3s forwards;
}

View file

@ -0,0 +1,111 @@
import React, { FormEvent, useState, useEffect } from "react";
import "./Home.css";
import axios from "axios";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
function Home() {
const [url, setUrl] = useState<string>("");
const [shortenedUrl, setShortenedUrl] = useState<string>("");
async function ShortenIt(e: React.FormEvent) {
e.preventDefault();
if (!url) {
toast.error("Please provide a URL", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
});
return;
}
try {
const timestamp = Math.floor(Date.now() / 1000);
// Send the POST request to the backend
await axios
.post("http://127.0.0.1:8000/shortenit", {
url: url,
timestamp: timestamp,
})
.then((response) => {
if (response) {
const code: string = response.data.url;
const fullShortenedUrl: string = `http://127.0.0.1:8000/${code}`;
setShortenedUrl(fullShortenedUrl);
}
});
} catch (error) {
console.log(error);
}
}
async function copyURL() {
try {
if (shortenedUrl) {
await navigator.clipboard.writeText(shortenedUrl);
toast.success("Copied to Clipboard", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
});
}
} catch (error) {
console.log(error);
}
}
return (
<div className="home-page">
<div className="home-container">
<div className="left-side">
<h1>Paste the URL</h1>
<form className="url-input" onSubmit={ShortenIt}>
<input
type="text"
placeholder="www.example.com"
aria-label="URL input field"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
<button type="submit">Shorten It</button>
</form>
{shortenedUrl && (
<div className="url-input shortened">
<input type="text" readOnly value={shortenedUrl} />
<button onClick={copyURL}>Copy</button>
</div>
)}
</div>
<div className="right-side">
<div className="right-side-title">
<h1>Shorten It</h1>
</div>
<div className="right-side-content">
<p>
Shortenit is a free and open-source URL shortening service
designed for simplicity and efficiency.
</p>
</div>
</div>
</div>
<ToastContainer />
</div>
);
}
export default Home;

26
frontend/tsconfig.json Normal file
View file

@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}

18
poetry.lock generated
View file

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.8.5 and should not be changed by hand.
[[package]]
name = "alabaster"
@ -288,6 +288,20 @@ Werkzeug = ">=3.1"
async = ["asgiref (>=3.2)"]
dotenv = ["python-dotenv"]
[[package]]
name = "flask-cors"
version = "5.0.0"
description = "A Flask extension adding a decorator for CORS support"
optional = false
python-versions = "*"
files = [
{file = "Flask_Cors-5.0.0-py2.py3-none-any.whl", hash = "sha256:b9e307d082a9261c100d8fb0ba909eec6a228ed1b60a8315fd85f783d61910bc"},
{file = "flask_cors-5.0.0.tar.gz", hash = "sha256:5aadb4b950c4e93745034594d9f3ea6591f734bb3662e16e255ffbf5e89c88ef"},
]
[package.dependencies]
Flask = ">=0.9"
[[package]]
name = "greenlet"
version = "3.1.1"
@ -1105,4 +1119,4 @@ watchdog = ["watchdog (>=2.3)"]
[metadata]
lock-version = "2.0"
python-versions = "^3.12"
content-hash = "e0689e4af89d57338c88718b3006272cbce814abaeceda921afe0021d9a95794"
content-hash = "aecb7301bfd623020aa148d39996e2664a83810c77a9ed3533e7b6b96d6e5b0f"

View file

@ -44,6 +44,7 @@ PyYAML = "^6.0.2"
cloudant = "^2.15.0"
Flask = "^3.1.0"
trafaret = "^2.1.1"
flask-cors = "^5.0.0"
[tool.poetry.group.dev.dependencies]
black = "^24.10.0"

View file

@ -1,6 +1,7 @@
import logging
import os
from pathlib import Path
from flask_cors import CORS
import trafaret
from flask import Flask, abort, redirect, render_template, request
@ -22,6 +23,7 @@ class Web:
def init(self):
self.app = Flask(__name__)
self.setup_routes()
CORS(self.app)
def setup_routes(self):
self.app.add_url_rule("/", "/", self.handler.index, methods=["GET"])
@ -73,3 +75,5 @@ class SiteHandler:
def index(self):
return render_template("index.html")