Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

ml-archive/flash

Repository files navigation

Flash ⚡️

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

This package is to ease using flash message between your views

image

Installation

Update your Package.swift file.

.Package(url: "https://github.com/nodes-vapor/flash", majorVersion: 1)

Getting started 🚀

You can add the middleware either globally or to a route group.

Adding Middleware Globally

Sources/App/Config+Setup.swift

import Flash
public func setup() throws {
    // ...
    
    addConfigurable(middleware: FlashMiddleware(), name: "flash")
}

Config/droplet.json

Make sure both "sessions" and "flash" are present:

    "middleware": [
        "error",
        "date",
        "file",
        "sessions",
        "flash"
    ],

Adding Middleware to a Route Group

drop.group(FlashMiddleware()) { group in
   // Routes
}

Using flash messages ⚡️

Apply flash on a response, which will be shown on next request

return Response(redirect: "/admin/users").flash(.error, "Failed to save user")
return Response(redirect: "/admin/users").flash(.success, "Successfuly saved")
return Response(redirect: "/admin/users").flash(.warning, "Updated user")
return Response(redirect: "/admin/users").flash(.info, "Email sent")

Misc functions

// Add to request by string
try request.flash.add(custom: String, message: String)

// Add to request by enum
try request.flash.add(type: Helper.FlashType, message: String)

// Clear all flashes
try request.flash.clear()

// Show current flash messages again in next request
try request.flash.refresh()

Example of HTML

<!--Error-->
#if(request.storage._flash.error) {
    <div class="alert alert-danger alert-dismissible fade in to-be-animated-in" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <span class="fa fa-exclamation-circle"></span>
        #(request.storage._flash.error)
    </div>
}

<!--Success-->
#if(request.storage._flash.success) {
<div class="alert alert-success alert-dismissible fade in to-be-animated-in" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    <span class="fa fa-check-circle"></span>
    #(request.storage._flash.success)
</div>
}

<!--Warning-->
#if(request.storage._flash.warning) {
<div class="alert alert-warning alert-dismissible fade in to-be-animated-in" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    #(request.storage._flash.warning)
</div>
}

<!--Info-->
#if(request.storage._flash.info) {
<div class="alert alert-info alert-dismissible fade in to-be-animated-in" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    #(request.storage._flash.info)
</div>
}

Add the flash html to one file and embed it in rest of your views or through a base layout e.g.: #embed("Layout/Partials/Elements/alerts") or e.g.: #extend("Layout/Base-Layout")

🏆 Credits

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Tom.

📄 License

This package is open-sourced software licensed under the MIT license