Merge pull request 'main' (#3) from cereci5049/8:main into main

Reviewed-on: harshscool955952/8#3
This commit is contained in:
harshscool955952 2024-08-17 06:56:53 +02:00
commit 9170a6c84f
20 changed files with 269 additions and 308 deletions

View file

@ -5,7 +5,7 @@ import
proc htmxEmail*(ctx:Context)=
var
em = ctx.urlForm["email"]
em = ctx.urlForm["em"]
val: Validity
if em == "":
val.message = "Last Name is Required"
@ -17,7 +17,7 @@ proc htmxEmail*(ctx:Context)=
proc htmxPassword*(ctx:Context)=
var
pw = ctx.urlForm["password"]
pw = ctx.urlForm["pw"]
val: Validity
if pw == "":
val.message = "Last Name is Required"

View file

@ -10,13 +10,12 @@ proc htmxLogin*(ctx: Context) =
var
form = ctx.urlForm
em =form["email"]
ps=form["password"]
em =form["em"]
ps=form["pw"]
val: Validity
validity = initTable[string, Validity]()
ctx &= initCookie("user2", "$(%* temp[1])")
if em.len==0 or ps.len==0:
for a, b in form:
if form[a] == "":

View file

@ -3,29 +3,44 @@ import
nimja/parser,
json,
../db/users,
../model/model,
../lib/mics
../model/[model, rand],
../lib/[mics, htmx]
proc signup*(ctx: Context): string=
if ctx.httpMethod == HttpGet:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile(".." / "view" / "signup.nimja", baseDir = getScriptDir())
compileTemplateFile(".." / "view" / "signup.html", baseDir = getScriptDir())
elif ctx.httpMethod == HttpPost:
var
firstName = ctx.urlForm["fn"]
lastName = ctx.urlForm["ln"]
email = ctx.urlForm["em"]
phone = ctx.urlForm["ph"]
password = ctx.urlForm["pw"]
form = ctx.urlForm
firstName = form["fn"]
lastName = form["ln"]
email = form["em"]
phone = form["ph"]
password = form["pw"]
conn = newTurso()
val: Validity
validity = initTable[string, Validity]()
echo firstName, lastName, email, password
if firstName.len == 0 or lastName.len == 0 or email.len == 0 or phone.len == 0 or password.len == 0:
ctx.send(parseJson("""{"ok": false, "failure": "Missing parameters"}"""))
for a, b in form:
if form[a] == "":
val.name = ""
val.message = "Field is Required"
val.mark = "is-invalid"
validity[a] = val
else:
val.name = b
val.message = ""
val.mark = ""
validity[a] = val
ctx.send htmxSignup(validity, "")
else:
var user = User(
firstName: firstName,
@ -37,7 +52,7 @@ proc signup*(ctx: Context): string=
)
conn.createPost(user)
ctx &= user
ctx.redirect("/")
ctx &= initCookie("user", $(%* user))
ctx.response.headers.add("HX-Redirect", "/")
else:
ctx.send(parseJson("""{"ok": false, "failure": "Invalid method"}"""))
ctx.send(parseJson("""{"ok": false, "failure": "Invalid method"}"""))

View file

@ -6,24 +6,27 @@ import
../model/model
proc setupUsers*(conn: Turso) =
conn.execute("""
CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
createdAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
updatedAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
accessLevel INTEGER NOT NULL
)
""")
## setupUsers creates the Users table if it does not exist
conn.execute("""
CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
createdAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
updatedAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
accessLevel INTEGER NOT NULL
)
""")
proc createPost*(conn: Turso, user: User)=
## createPost inserts a new user into the Users table
conn.execute(fmt"""INSERT INTO users (firstName, lastName, email, phone, password, accessLevel) VALUES ("{user.firstName}", "{user.lastName}", "{user.email}", "{user.phone}", "{user.password}", {user.accessLevel});""")
proc getUser*(conn: Turso, email: string, password: string): (bool, User)=
## getUser retrieves a user from the Users table
echo "h1"
var
data = conn.getData(fmt"""SELECT * FROM users WHERE email = "{email}" and password = "{password}";""")

View file

@ -1,5 +1,5 @@
import
mike,
mike,
nimja,
strutils,
json,
@ -22,44 +22,44 @@ load()
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
echo user.firstName
compileTemplateFile("view" / "index.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "index.html", baseDir = getScriptDir())
"/shop" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "shop.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "shop.html", baseDir = getScriptDir())
"/contact" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "contact.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "contact.html", baseDir = getScriptDir())
"/cart" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "cart.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "cart.html", baseDir = getScriptDir())
"/checkout" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "checkout.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "checkout.html", baseDir = getScriptDir())
"/shop/:id" -> get:
var
prodId = capitalizeAscii(ctx.pathParams["id"])
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "shop-detail.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "shop-detail.html", baseDir = getScriptDir())
"/testimonial" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "testimonial.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "testimonial.html", baseDir = getScriptDir())
"/signup" -> [get, post]: ctx.signup()
@ -67,7 +67,7 @@ load()
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "login.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "login.html", baseDir = getScriptDir())
"/htmx/login" -> post: ctx.htmxLogin()
@ -79,14 +79,15 @@ load()
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "404.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "404.html", baseDir = getScriptDir())
# Experimental
"/hi" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
uploadcare_pub_key = getEnv("UPLOAD_CARE_PUBLIC_KEY")
compileTemplateFile("view" / "hi.nimja", baseDir = getScriptDir())
compileTemplateFile("view" / "hi.html", baseDir = getScriptDir())
"/delete-hi" -> post:
var

View file

@ -14,14 +14,14 @@ proc htmxLogin*(val: Table[string, Validity], message: string): string =
<label class="text-danger">{message}</label>
<div class="form-group mt-5">
<label for="email">Email:</label>
<label class="text-danger">{val["email"].message}</label>
<input type="email" name="email" id="email" class="form-control {val["email"].mark}" required autocomplete="off" value="{val["email"].name}">
<label class="text-danger">{val["em"].message}</label>
<input type="email" name="em" id="email" class="form-control {val["em"].mark}" required autocomplete="off" value="{val["em"].name}">
</div>
<div class="form-group">
<label for="password">Password:</label>
<label class="text-danger">{val["password"].message}</label>
<input type="password" name="password" id="password" class="form-control {val["password"].mark}" required autocomplete="off" value="{val["password"].name}">
<label class="text-danger">{val["pw"].message}</label>
<input type="password" name="pw" id="password" class="form-control {val["pw"].mark}" required autocomplete="off" value="{val["pw"].name}">
</div>
@ -41,7 +41,7 @@ proc htmxEmail*(email: string, input: Validity): string =
>
<label for="email">Email:</label>
<label class="text-danger">{input.message}</label>
<input type="email" name="email" id="email" class="form-control {input.mark}" required autocomplete="off"
<input type="email" name="em" id="email" class="form-control {input.mark}" required autocomplete="off"
hx-post="/htmx/email"
value="{email}"
>
@ -55,8 +55,118 @@ proc htmxPassword*(password: string, input: Validity): string =
>
<label for="password">Password:</label>
<label class="text-danger">{input.message}</label>
<input type="password" name="password" id="password" class="form-control {input.mark}" required autocomplete="off"
<input type="password" name="pw" id="password" class="form-control {input.mark}" required autocomplete="off"
hx-post="/htmx/password"
value="{password}"
>
</div>"""
</div>"""
proc htmxSignup*(val: Table[string, Validity], message: string): string =
result = fmt"""
<form id="input_form" class="" novalidate
hx-trigger="submit"
hx-post="/signup"
hx-target="#input_form"
>
<label class="text-danger">{message}</label>
<div class="form-group mt-5"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">First Name:</label>
<label class="text-danger">{val["fn"].message}</label>
<input type="text" name="fn" class="form-control {val["fn"].mark}" required autocomplete="off"
value="{val["fn"].name}"
hx-post="/htmx/fname"
>
<div class="form-group">
<label for="email">Last Name:</label>
<label class="text-danger">{val["ln"].message}</label>
<input type="text" name="ln" class="form-control {val["ln"].mark}" required autocomplete="off" value="{val["ln"].name}">
</div>
<div class="form-group">
<label for="email">Email:</label>
<label class="text-danger">{val["em"].message}</label>
<input type="email" name="em" class="form-control {val["em"].mark}" required autocomplete="off" value="{val["em"].name}">
</div>
<div class="form-group">
<label for="email">Phone:</label>
<label class="text-danger">{val["ph"].message}</label>
<input type="email" name="ph" class="form-control {val["ph"].mark}" required autocomplete="off" value="{val["ph"].name}">
</div>
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>
<label for="password">Password:</label>
<label class="text-danger"></label>
<input type="password" name="pw" id="password" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/password"
>
</div>
<hr>
<button id="b4" type="submit" class="btn btn-primary">Submit</button>
<a id="b4" href="/login" class="btn btn-outline-primary">Login</a>
<hr>
</form>"""
proc htmxFirstName*(firstName: string, input: Validity): string =
result = fmt"""
<div class="form-group mt-5"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">First Name:</label>
<label class="text-danger">{input.message}</label>
<input type="text" name="fn" class="form-control {input.mark}" required autocomplete="off"
value="{firstName}"
hx-post="/htmx/fname"
>
"""
proc htmxLastName*(lastName: string, input: Validity): string =
result = fmt"""
<div class="form-group mt-5"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Last Name:</label>
<label class="text-danger">{input.message}</label>
<input type="text" name="ln" class="form-control {input.mark}" required autocomplete="off"
value="{lastName}"
hx-post="/htmx/lname"
>
"""
proc htmxPhone*(phone: string, input: Validity): string =
result = fmt"""
<div class="form-group mt-5"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">Phone:</label>
<label class="text-danger">{input.message}</label>
<input type="email" name="ph" class="form-control {input.mark}" required autocomplete="off"
value="{phone}"
hx-post="/htmx/phone"
>
"""

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block spheader1 %} 404 Error {% endblock %}
{% block spheader2 %} 404 {% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block spheader1 %} Cart {% endblock %}
{% block spheader2 %} Cart {% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block spheader1 %} checkout {% endblock %}
{% block spheader2 %} checkout {% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block contact %} active {% endblock %}
{% block spheader1 %} contact {% endblock %}

View file

@ -1,155 +0,0 @@
{% extends "view/partials/_master.nimja" %}
{% block css %}
<link rel="stylesheet" href="/static/css/hi.css">
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@uploadcare/blocks@0.48.0/web/lr-file-uploader-regular.min.css"
/>
{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col">
<h1>Upload Product Image</h1>
<script type="module">
import * as LR from "https://cdn.jsdelivr.net/npm/@uploadcare/blocks@0.48.0/web/lr-file-uploader-regular.min.js";
LR.registerBlocks(LR);
</script>
<lr-config
ctx-name="my-uploader"
pubkey="{{uploadcare_pub_key}}"
max-local-file-size-bytes="10000000"
multiple="false"
img-only="true"
source-list="local, camera, dropbox, gdrive, gphotos"
></lr-config>
<lr-file-uploader-regular
ctx-name="my-uploader"
class="my-config"
>
</lr-file-uploader-regular>
<!-- The provider node is used to listen to the change event -->
<lr-upload-ctx-provider
ctx-name="my-uploader"
id="my-uploader-provider"
></lr-upload-ctx-provider>
<div id="previews"></div>
<script>
const providerNode = document.getElementById('my-uploader-provider');
const previewsNode = document.getElementById('previews');
providerNode.addEventListener('change', event => {
renderFiles(event.detail.allEntries.filter((file) => file.status === 'success'));
});
let url = "";
let uuid = "";
function renderFiles(files) {
const renderedFiles = files.map(file => {
const imgNode = document.createElement('img');
console.log(file);
imgNode.setAttribute('src', file.cdnUrl);
imgNode.setAttribute('alt', file.fileInfo.originalFilename);
imgNode.setAttribute('width', '100');
imgNode.setAttribute('height', '100');
url = file.cdnUrl;
uuid = file.uuid;
return imgNode;
});
console.log(...renderedFiles);
let formData = new FormData();
formData.append('img', url);
formData.append('img1', uuid);
console.log(formData);
let data = new URLSearchParams(formData);
// data.append('img', url);
// data.append('img1', uuid);
console.log(data);
let btnNode = document.createElement('button');
btnNode.setAttribute('id', 'b4');
btnNode.setAttribute('type', 'submit');
btnNode.setAttribute('class', 'btn btn-primary');
btnNode.addEventListener('click', function() {
console.log('Delete');
console.log(url);
console.log(uuid);
fetch('/delete-hi', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data,
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
// remove the image and the buttons from the preview
previewsNode.replaceChildren();
})
.catch((error) => {
console.error('Error:', error);
});
});
// btnNode.addEventListener('click', function() {
// console.log('Delete');
// console.log(url);
// console.log(uuid);
// });
// btnNode.addEventListener('click', function() {
// console.log('Submit');
// console.log(url);
// console.log(uuid);
// });
btnNode.innerHTML = 'Delete';
let btnNode1 = document.createElement('button');
btnNode1.setAttribute('id', 'b4');
btnNode1.setAttribute('type', 'submit');
btnNode1.setAttribute('class', 'btn btn-primary');
btnNode1.innerHTML = 'Submit';
btnNode1.addEventListener('click', function() {
console.log('Submit');
console.log(url);
console.log(uuid);
fetch('/jj', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
img: url,
}),
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
});
previewsNode.replaceChildren(...renderedFiles, btnNode, btnNode1);
}
// TODO: Add a button to delete the image from the server
// TODO: Add a button to submit the image url to the server
</script>
</div>
</div>
</div>
{% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block index %} active {% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block content %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block content %}
@ -21,7 +21,7 @@
>
<label for="email">Email:</label>
<label class="text-danger"></label>
<input type="email" name="email" id="email" class="form-control" required autocomplete="off"
<input type="email" name="em" id="email" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/email"
>
@ -33,7 +33,7 @@
>
<label for="password">Password:</label>
<label class="text-danger"></label>
<input type="password" name="password" id="password" class="form-control" required autocomplete="off"
<input type="password" name="pw" id="password" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/password"
>

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block shop %} active {% endblock %}
{% block spheader1 %} Shop-Details {% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block shop %} active {% endblock %}
{% block spheader1 %} Shop {% endblock %}

81
src/view/signup.html Normal file
View file

@ -0,0 +1,81 @@
{% extends "../view/partials/_master.html" %}
{% block spheader1 %} Sign Up {% endblock %}
{% block spheader2 %} Sign Up {% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col">
<h1 class="mt-5">Sign Up</h1>
<form id="input_form" class="" novalidate
hx-trigger="submit"
hx-post="/signup"
hx-target="#input_form"
>
<label class="text-danger"></label>
<div class="form-group mt-5"
hx-target="this"
hx-swap="outerHTML"
>
<label for="email">First Name:</label>
<label class="text-danger"></label>
<input type="text" name="fn" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/fname"
>
</div>
<div class="form-group">
<label for="email">Last Name:</label>
<label class="text-danger"></label>
<input type="text" name="ln" class="form-control" required autocomplete="off">
</div>
<div class="form-group">
<label for="email">Email:</label>
<label class="text-danger"></label>
<input type="email" name="em" class="form-control" required autocomplete="off">
</div>
<div class="form-group">
<label for="email">Phone:</label>
<label class="text-danger"></label>
<input type="email" name="ph" class="form-control" required autocomplete="off">
</div>
<div class="form-group"
hx-target="this"
hx-swap="outerHTML"
>
<label for="password">Password:</label>
<label class="text-danger"></label>
<input type="password" name="pw" id="password" class="form-control" required autocomplete="off"
value=""
hx-post="/htmx/password"
>
</div>
<hr>
<button id="b4" type="submit" class="btn btn-primary">Submit</button>
<a id="b4" href="/login" class="btn btn-outline-primary">Login</a>
<hr>
</form>
</div>
</div>
</div>
{% endblock %}

View file

@ -1,93 +0,0 @@
{% extends "../view/partials/_master.nimja" %}
{% block spheader1 %} Sign Up {% endblock %}
{% block spheader2 %} Sign Up {% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col">
<h1 class="mt-5">Sign Up</h1>
<form id="input_form" method="post" class="" novalidate>
{# <!-- <label class="text-danger">{{loginError}}</label> --> #}
<label class="text-danger"></label>
<div class="form-group mt-5">
<label for="email">First Name:</label>
{# <!-- <label class="text-danger">{{emailError}}</label> --> #}
<label class="text-danger"></label>
{# <!-- {% if emailError != "" %}
<input type="email" name="email" id="email" class="form-control is-invalid" required autocomplete="off">
{% else %} --> #}
<input type="text" name="fn" class="form-control" required autocomplete="off">
{# <!-- {% endif %} --> #}
</div>
<div class="form-group">
<label for="email">Last Name:</label>
{# <!-- <label class="text-danger">{{emailError}}</label> --> #}
<label class="text-danger"></label>
{# <!-- {% if emailError != "" %}
<input type="email" name="email" id="email" class="form-control is-invalid" required autocomplete="off">
{% else %} --> #}
<input type="text" name="ln" class="form-control" required autocomplete="off">
{# <!-- {% endif %} --> #}
</div>
<div class="form-group">
<label for="email">Email:</label>
{# <!-- <label class="text-danger">{{emailError}}</label> --> #}
<label class="text-danger"></label>
{# <!-- {% if emailError != "" %}
<input type="email" name="email" id="email" class="form-control is-invalid" required autocomplete="off">
{% else %} --> #}
<input type="email" name="em" class="form-control" required autocomplete="off">
{# <!-- {% endif %} --> #}
</div>
<div class="form-group">
<label for="email">Phone:</label>
{# <!-- <label class="text-danger">{{emailError}}</label> --> #}
<label class="text-danger"></label>
{# <!-- {% if emailError != "" %}
<input type="email" name="email" id="email" class="form-control is-invalid" required autocomplete="off">
{% else %} --> #}
<input type="email" name="ph" class="form-control" required autocomplete="off">
{# <!-- {% endif %} --> #}
</div>
<div class="form-group">
<label for="password">Password:</label>
{# <!-- <label class="text-danger">{{passwordError}}</label> --> #}
<label class="text-danger"></label>
{# <!-- {% if passwordError != "" %}
<input type="password" name="password" id="password" class="form-control is-invalid" required autocomplete="off">
{% else %} --> #}
<input type="password" name="pw" id="password" class="form-control" required autocomplete="off">
{# <!-- {% endif %} --> #}
</div>
<hr>
<button id="b4" type="submit" class="btn btn-primary">Submit</button>
<a id="b4" href="/login" class="btn btn-outline-primary">Login</a>
<hr>
</form>
</div>
</div>
</div>
{% endblock %}

View file

@ -1,4 +1,4 @@
{% extends "view/partials/_master.nimja" %}
{% extends "view/partials/_master.html" %}
{% block content %}