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

Commit

Permalink
Merge pull request #24 from nodes-vapor/feature/update-file-structure
Browse files Browse the repository at this point in the history
Split into multiple files
  • Loading branch information
steffendsommer committed Jun 5, 2018
2 parents 75e9dc1 + 155f5b6 commit 564bbf1
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ bower_components/
.swift-version
CMakeLists.txt
Package.pins
Package.resolved
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "Flash", targets: ["Flash"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc"),
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/leaf.git", from: "3.0.0-rc"),
],
targets: [
Expand Down
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

This package is to ease using flash message between your views

![image](https://cloud.githubusercontent.com/assets/1279756/21659442/fcfdd126-d2ca-11e6-8157-d6860aa02363.png)
![image](https://github.com/nodes-vapor/flash/blob/master/flash.png)

# Installation

Expand All @@ -20,10 +20,40 @@ Update your `Package.swift` file.

## Getting started 🚀

TODO. While we make the docs, feel free to look at how [Admin Panel](https://github.com/nodes-vapor/admin-panel/tree/vapor-3) uses this package.
First make sure that you've imported Flash everywhere when needed:

```swift
import Flash
```

### Adding the provider

```swift
public func register(_ services: inout Services) throws {
try services.register(FlashProvider())
}
```

### Adding the middleware

TODO

### Adding the Leaf tag

TODO

## Using Flash messages

TODO

### Example of HTML

#### Not using the Bootstrap package

TODO

#### Using the Bootstrap package

The below example uses the Vapor 3 [Bootstrap package](https://github.com/nodes-vapor/bootstrap) for generating the alert html.

```html
Expand Down
9 changes: 9 additions & 0 deletions Sources/Flash/Extensions/Future+Flash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vapor

public extension Future where T: Response {
public func flash(_ type: Flash.Kind, _ message: String) -> Future<Response> {
return self.map(to: Response.self) { res in
return res.flash(type, message)
}
}
}
10 changes: 10 additions & 0 deletions Sources/Flash/Extensions/Response+Flash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Vapor

public extension Response {
public func flash(_ type: Flash.Kind, _ message: String) -> Response {
if let container = try? privateContainer.make(FlashContainer.self) {
container.flashes.append(.init(type, message))
}
return self
}
}
10 changes: 10 additions & 0 deletions Sources/Flash/Extensions/SubContainer+Flash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Vapor

extension SubContainer {
public func flash(_ type: Flash.Kind, _ message: String) -> SubContainer {
if let container = try? self.make(FlashContainer.self) {
container.flashes.append(.init(type, message))
}
return self
}
}
41 changes: 41 additions & 0 deletions Sources/Flash/Middlewares/FlashMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Vapor

public struct FlashMiddleware: Middleware, ServiceType {
private static let sessionKey = "_flash"

public static func makeService(for container: Container) throws -> FlashMiddleware {
return .init()
}

public init() {}

/// See Middleware.respond
public func respond(to req: Request, chainingTo next: Responder) throws -> Future<Response> {
try FlashMiddleware.handle(req: req)
return try next.respond(to: req)
.map(to: Response.self) { resp in
try FlashMiddleware.handle(req: req, resp: resp)
return resp
}
}

public static func handle(req: Request) throws {
let session = try req.session()

if let data = session[sessionKey]?.data(using: .utf8) {
let flash = try JSONDecoder().decode(FlashContainer.self, from: data)
let container = try req.privateContainer.make(FlashContainer.self)
container.new = flash.new
container.old = flash.old
}
}

public static func handle(req: Request, resp: Response) throws {
let container = try resp.privateContainer.make(FlashContainer.self)
let flash = try String(
data: JSONEncoder().encode(container),
encoding: .utf8
)
try req.session()[sessionKey] = flash
}
}
23 changes: 23 additions & 0 deletions Sources/Flash/Models/Flash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Vapor

public final class Flash: Codable {
public enum Kind: String, Codable {
case error
case success
case info
case warning
}

public var kind: Kind
public var message: String

public init(kind: Kind, message: String) {
self.kind = kind
self.message = message
}

public init(_ kind: Kind, _ message: String) {
self.kind = kind
self.message = message
}
}
16 changes: 16 additions & 0 deletions Sources/Flash/Models/FlashContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Vapor

public final class FlashContainer: Codable, Service {
public var new: [Flash] = []
public var old: [Flash] = []

public var flashes: [Flash] {
get {
return new
}

set {
new = newValue
}
}
}
22 changes: 22 additions & 0 deletions Sources/Flash/Providers/FlashProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Vapor

extension FlashProvider {
public static var tags: [String: TagRenderer] {
return ["flash": FlashTag()]
}
}

public final class FlashProvider: Provider {
public init() {}

public func register(_ services: inout Services) throws {
services.register(FlashMiddleware.self)
services.register { container in
return FlashContainer()
}
}

public func didBoot(_ container: Container) throws -> EventLoopFuture<Void> {
return .done(on: container)
}
}
14 changes: 7 additions & 7 deletions Sources/Flash/Tags/FlashTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public final class FlashTag: TagRenderer {
})

dict["errors"] = try .array(flash.flashes.compactMap { flash in
guard flash.type == .error else { return nil }
guard flash.kind == .error else { return nil }
return try flash.convertToTemplateData()
})

dict["warnings"] = try .array(flash.flashes.compactMap { flash in
guard flash.type == .warning else { return nil }
guard flash.kind == .warning else { return nil }
return try flash.convertToTemplateData()
})

dict["successes"] = try .array(flash.flashes.compactMap { flash in
guard flash.type == .success else { return nil }
guard flash.kind == .success else { return nil }
return try flash.convertToTemplateData()
})

dict["information"] = try .array(flash.flashes.compactMap { flash in
guard flash.type == .info else { return nil }
guard flash.kind == .info else { return nil }
return try flash.convertToTemplateData()
})

Expand All @@ -51,14 +51,14 @@ public final class FlashTag: TagRenderer {
extension Flash: TemplateDataRepresentable {
public func convertToTemplateData() throws -> TemplateData {
return TemplateData.dictionary([
"type": .string(self.type.rawValue),
"bootstrapType": .string(self.type.bootstrapClass),
"kind": .string(self.kind.rawValue),
"bootstrapClass": .string(self.kind.bootstrapClass),
"message": .string(self.message)
])
}
}

extension FlashType {
extension Flash.Kind {
var bootstrapClass: String {
switch self {
case .error: return "danger"
Expand Down
115 changes: 0 additions & 115 deletions Sources/Flash/flash.swift

This file was deleted.

Binary file added flash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 564bbf1

Please sign in to comment.