Skip to content

Commit

Permalink
Update for vapor 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed May 15, 2023
1 parent 4e3e7c6 commit 8713ca6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// swift-tools-version:5.2
// swift-tools-version:5.8
import PackageDescription

let package = Package(
name: "Flash",
platforms: [
.macOS(.v10_15)
.macOS(.v13)
],
products: [
.library(name: "Flash", targets: ["Flash"]),
Expand Down
47 changes: 17 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Flash ⚡️
[![Swift Version](https://img.shields.io/badge/Swift-5.2-brightgreen.svg)](http://swift.org)
[![Swift Version](https://img.shields.io/badge/Swift-5.8-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-4-30B6FC.svg)](http://vapor.codes)
[![Circle CI](https://circleci.com/gh/nodes-vapor/flash/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/flash)
[![codebeat badge](https://codebeat.co/badges/10cffe07-3d4f-420c-adb9-a98529671bfa)](https://codebeat.co/projects/github-com-nodes-vapor-flash-master)
[![codecov](https://codecov.io/gh/nodes-vapor/flash/branch/master/graph/badge.svg)](https://codecov.io/gh/nodes-vapor/flash)
[![Readme Score](http://readme-score-api.herokuapp.com/score.svg?url=https://github.com/nodes-vapor/flash)](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/flash)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/flash/master/LICENSE)

This package allows you to display Flash messages between your views.
Expand Down Expand Up @@ -43,14 +39,6 @@ First make sure that you've imported Flash everywhere when needed:
import Flash
```

### Adding the provider

```swift
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
try services.register(FlashProvider())
}
```

### Adding the middleware

You can either add the Flash middleware globally by doing:
Expand Down Expand Up @@ -115,32 +103,31 @@ Further, using the `message` property you will be able to pull out the message o

Without using any dependencies, this is how Flash messages could be rendered:

```javascript
```html
<div class="alerts">
#for(flash in flashes().all):
Message: #(flash.message)
Type: #(flash.kind)
#endfor
#(flashes)
</div>
```

#### Using the Bootstrap package

The below example uses the Vapor 3 [Bootstrap package](https://github.com/nodes-vapor/bootstrap) for generating the alert html.
Using the example above, this is how they are going to be rendered:

```javascript
```html
<div class="alerts">
#for(flash in flashes().all):
#bs:alert(flash.bootstrapClass) {
#(flash.message)
}
#endfor
<div class="alert alert-success" role="alert">
Successfully saved
</div>
<div class="alert alert-info" role="alert">
Email sent
</div>
<div class="alert alert-warning" role="alert">
Updated user
</div>
<div class="alert alert-danger" role="alert">
Something went wrong
</div>
</div>

```

Add the Flash html to one file and embed it in rest of your views or through a base layout, e.g.: `#embed("alerts")`.

## 🏆 Credits

This package is developed and maintained by the Vapor team at [Nodes](https://www.nodesagency.com).
Expand Down
57 changes: 15 additions & 42 deletions Sources/Flash/Tags/FlashTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,26 @@ import Leaf
import LeafKit
import Vapor

public struct FlashTag: LeafTag {
private enum DataKey {
static let all = "all"
}

public struct FlashTag: UnsafeUnescapedLeafTag {
public init() {}

public func render(_ ctx: LeafContext) throws -> LeafData {
guard let request = ctx.request else {
throw LeafContextError.requestNotPassedToRenderContext
}

var dictionary: [String: LeafData] = [
DataKey.all: request.flashes.leafData
]

for kind in Flash.Kind.allCases {
dictionary[kind.rawValue] = request.flashes.filter { $0.kind == kind }.leafData
}

return .dictionary(dictionary)
}
}

extension Collection where Element: LeafDataRepresentable {
var leafData: LeafData {
.array(compactMap { $0.leafData })
}
}

extension Flash: LeafDataRepresentable {
private enum DataKey {
static let kind = "kind"
static let bootstrapClass = "bootstrapClass"
static let message = "message"
}

public var leafData: LeafData {
.dictionary([
DataKey.kind: .string(kind.rawValue),
DataKey.bootstrapClass: .string(kind.bootstrapClass),
DataKey.message: .string(message)
])
let result: String = request.flashes.map { flash in
"""
<div class=\"alert alert-\(flash.kind.bootstrapClass)\" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
\(flash.message)
</div>
"""
}.joined(separator: "\n")

return .string(result)
}
}

Expand All @@ -55,18 +32,14 @@ enum LeafContextError: Error {
extension LeafContextError {
var identifier: String {
switch self {
case .requestNotPassedToRenderContext: return "requestNotPassedToRenderContext"
case .requestNotPassedToRenderContext: return "requestNotPassedToRenderContext"
}
}

var reason: String {
switch self {
case .requestNotPassedToRenderContext: return "Request not passed into render context."
case .requestNotPassedToRenderContext: return "Request not passed into render context."
}
}

var status: HTTPResponseStatus {
.internalServerError
}
}

0 comments on commit 8713ca6

Please sign in to comment.