1
Fork 0
mirror of https://github.com/RGBCube/GDUS synced 2025-07-29 14:07:52 +00:00

Move JS to seperate file

This commit is contained in:
RGBCube 2024-01-07 22:35:02 +03:00
parent aec4e27bb6
commit 718e4a7713
No known key found for this signature in database
5 changed files with 68 additions and 37 deletions

1
.gitignore vendored
View file

@ -13,6 +13,7 @@
!.gitignore !.gitignore
!*.js
!*.md !*.md
!*.py !*.py
!*.rs !*.rs

24
server/Cargo.lock generated
View file

@ -574,6 +574,24 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "embed"
version = "0.1.0"
source = "git+https://github.com/RGBCube/embed-rs#7c41253f4f6006fcc2aad189c19132d2f0cccec2"
dependencies = [
"embed-macros",
]
[[package]]
name = "embed-macros"
version = "0.1.0"
source = "git+https://github.com/RGBCube/embed-rs#7c41253f4f6006fcc2aad189c19132d2f0cccec2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.33" version = "0.8.33"
@ -1327,6 +1345,7 @@ dependencies = [
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"chrono", "chrono",
"embed",
"maud", "maud",
"serde", "serde",
"sqlx", "sqlx",
@ -2353,3 +2372,8 @@ dependencies = [
"cc", "cc",
"pkg-config", "pkg-config",
] ]
[[patch.unused]]
name = "proc-macro2"
version = "1.0.75"
source = "git+https://github.com/RGBCube/proc-macro2#e1713f29a5488360d96178081ac0fca3d4abdb8a"

View file

@ -9,12 +9,16 @@ edition = "2021"
[dependencies] [dependencies]
actix-cors = "0.6.5" actix-cors = "0.6.5"
actix-web = "4.4.0" actix-web = "4.4.0"
chrono = "0.4.31" chrono = "0.4.31"
maud = { version = "0.25.0", features = [ "actix-web" ] } embed = { git = "https://github.com/RGBCube/embed-rs" }
serde = { version = "1.0.192", features = [ "derive" ] } maud = { version = "0.25.0", features = [ "actix-web" ] }
sqlx = { version = "0.7.3", features = [ "chrono", "sqlite", "runtime-tokio" ] } serde = { version = "1.0.192", features = [ "derive" ] }
tokio = { version = "1.34.0", features = [ "full" ] } sqlx = { version = "0.7.3", features = [ "chrono", "sqlite", "runtime-tokio" ] }
tokio = { version = "1.34.0", features = [ "full" ] }
[patch.crates-io]
proc-macro2 = { git = "https://github.com/RGBCube/proc-macro2" }
[profile.dev] [profile.dev]
incremental = true incremental = true

30
server/src/alert.js Normal file
View file

@ -0,0 +1,30 @@
const alertIfTime = () => {
const reminders = Array.from(
document
.querySelectorAll("li")
)
.map(item => {
content: item.querySelector("p").textContent,
timestamp: +item.getAttribute("data-timestamp"),
led: +item.getAttribute("data-led"),
});
const currentTime = Math.floor(Date.now() / 1000);
reminders.forEach(reminder => {
const differenceSeconds = currentTime - reminder.timestamp;
if (differenceSeconds < 1 * 60) {
if (reminder.led != 0) fetch("http://localhost:3000/led/toggle?" + new URLSearchParams({ number: reminder.led }).then(console.log);
fetch("http://localhost:3000/speak?" + new URLSearchParams({ text: reminder.content })).then(console.log);
}
});
};
alertIfTime();
setInterval(() => {
location.reload();
alertIfTime();
}, 1 * 60 * 1000);

View file

@ -89,36 +89,8 @@ async fn view(data: Data<SqlitePool>) -> web::Result<Markup> {
} }
} }
script defer {(PreEscaped(r##" script {
const alertIfTime = () => { (PreEscaped(embed::string!("alert.js")))
const reminders = Array.from( }
document
.querySelectorAll("li")
)
.map(item => ({
content: item.querySelector("p").textContent,
timestamp: +item.getAttribute("data-timestamp"),
led: +item.getAttribute("data-led"),
}));
const currentTime = Math.floor(Date.now() / 1000);
reminders.forEach(reminder => {
const differenceSeconds = currentTime - reminder.timestamp;
if (differenceSeconds < 1 * 60) {
if (reminder.led != 0) fetch("http://localhost:3000/led/toggle?" + new URLSearchParams({ number: reminder.led }).then(console.log);
fetch("http://localhost:3000/speak?" + new URLSearchParams({ text: reminder.content })).then(console.log);
}
});
};
alertIfTime();
setInterval(() => {
location.reload();
alertIfTime();
}, 1 * 60 * 1000);
"##))}
}) })
} }