Riyyi
1 month ago
10 changed files with 225 additions and 10 deletions
@ -0,0 +1,14 @@
|
||||
// Activate Bootstrap tooltips
|
||||
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]') |
||||
const tooltipList = [...tooltipTriggerList].map(element => new bootstrap.Tooltip(element)) |
||||
|
||||
// -----------------------------------------
|
||||
// HTMX events
|
||||
|
||||
function runOnceAfterSettle(func) { |
||||
// https://htmx.org/docs/#request-operations
|
||||
document.addEventListener("htmx:afterSettle", function handler(event) { |
||||
func(); |
||||
document.removeEventListener("htmx:afterSwap", handler); |
||||
}); |
||||
} |
@ -0,0 +1,54 @@
|
||||
import Vapor |
||||
|
||||
public func getState(request: Request) throws -> UserState { |
||||
guard let state = request.storage[UserStateKey.self] else { |
||||
throw Abort(.internalServerError) |
||||
} |
||||
|
||||
return state |
||||
} |
||||
|
||||
public final class StateMiddleware: AsyncMiddleware { |
||||
public func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response { |
||||
|
||||
// This code is run *before* the route endpoint code |
||||
|
||||
var setCookie: Bool = true |
||||
|
||||
let uuid: UUID |
||||
if let sessionID = request.cookies["SWIFTSESSID"]?.string, |
||||
let sessionUUID = UUID(uuidString: sessionID) { |
||||
setCookie = false |
||||
|
||||
uuid = sessionUUID |
||||
|
||||
if !request.application.manager.states.keys.contains(uuid.uuidString) { |
||||
request.application.manager.states[uuid.uuidString] = UserState() |
||||
} |
||||
} else { |
||||
uuid = UUID() |
||||
|
||||
// Register a new user state into the application storage |
||||
// https://docs.vapor.codes/advanced/services/ |
||||
request.application.manager.states[uuid.uuidString] = UserState() |
||||
} |
||||
|
||||
// Provide the user state to the request |
||||
request.storage[UserStateKey.self] = request.application.manager.states[uuid.uuidString] |
||||
|
||||
let response = try await next.respond(to: request) |
||||
|
||||
// This code is run *after* the route endpoint code |
||||
|
||||
if setCookie { |
||||
response.cookies["SWIFTSESSID"] = HTTPCookies.Value( |
||||
string: uuid.uuidString, |
||||
path: "/", |
||||
isSecure: true, |
||||
isHTTPOnly: true |
||||
) |
||||
} |
||||
|
||||
return response |
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
public struct ToastState: Sendable { |
||||
|
||||
enum Level: String { |
||||
case success = "success" |
||||
case error = "danger" |
||||
case warning = "warning" |
||||
case info = "info" |
||||
case verbose = "secondary" |
||||
} |
||||
|
||||
var message: String = "" |
||||
var title: String = "" |
||||
var level: Level = Level.verbose |
||||
|
||||
} |
@ -0,0 +1,32 @@
|
||||
import Vapor |
||||
|
||||
// ----------------------------------------- |
||||
|
||||
public final class UserState: @unchecked Sendable { |
||||
var toast: ToastState = ToastState() |
||||
} |
||||
|
||||
// ----------------------------------------- |
||||
|
||||
struct UserStateKey: StorageKey { |
||||
typealias Value = UserState |
||||
} |
||||
|
||||
struct UserStateManager: Sendable { |
||||
var states: [String: UserState] = [:] |
||||
} |
||||
|
||||
struct UserStateManagerKey : StorageKey { |
||||
typealias Value = UserStateManager |
||||
} |
||||
|
||||
extension Application { |
||||
var manager: UserStateManager { |
||||
get { |
||||
self.storage[UserStateManagerKey.self]! |
||||
} |
||||
set { |
||||
self.storage[UserStateManagerKey.self] = newValue |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
import Elementary |
||||
import ElementaryHTMX |
||||
|
||||
// Usage: |
||||
// |
||||
// let state = try getState(request: req) |
||||
// state.toast = ToastState(message: "", title: "", level: ToastState.Level.error) |
||||
// throw Abort(.badRequest, headers: ["HX-Trigger": "toast"]) |
||||
// |
||||
// The header "HX-Trigger" will make the part refresh and show the toast message |
||||
|
||||
struct ToastView: HTML { |
||||
|
||||
var state: ToastState = ToastState() |
||||
|
||||
// ------------------------------------- |
||||
|
||||
var content: some HTML { |
||||
div( |
||||
.class("toast-container"), .id("cdiv_toast"), |
||||
.hx.get("/toast"), |
||||
.hx.trigger(HTMLAttributeValue.HTMX.EventTrigger(rawValue: "toast from:body")), |
||||
.hx.swap(.outerHTML) |
||||
) { |
||||
div(.class("toast"), .id("toast")) { |
||||
div(.class("toast-header bg-\(state.level.rawValue) text-white")) { |
||||
strong(.class("me-auto")) { state.title } |
||||
button( |
||||
.class("btn-close btn-close-white"), .type(.button), |
||||
.data("bs-dismiss", value: "toast") |
||||
) {} |
||||
} |
||||
div(.class("toast-body")) { state.message } |
||||
} |
||||
if !state.message.isEmpty { |
||||
script { |
||||
""" |
||||
runOnceAfterSettle(function () { |
||||
const element = document.getElementById("toast"); |
||||
const toast = new bootstrap.Toast(element, { autohide: true, delay: 5000 }); |
||||
toast.show(); |
||||
|
||||
element.addEventListener("hidden.bs.toast", function () { |
||||
element.remove(); |
||||
}); |
||||
}); |
||||
""" |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue