Last active
April 25, 2017 08:18
-
-
Save ricklancee/34d3b18454554d20a03faa44ed04cd9b to your computer and use it in GitHub Desktop.
Simple laravel notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| export default function notification(selector) { | |
| const root = document.querySelector(selector); | |
| const toast = root.querySelector('.notifications__toast'); | |
| if (toast) { | |
| setTimeout(() => { | |
| toast.classList.add('show'); | |
| const hideToast = () => { | |
| toast.classList.remove('show'); | |
| toast.removeEventListener('click', clickHandler); | |
| }; | |
| const clickHandler = () => hideToast(); | |
| toast.addEventListener('click', clickHandler); | |
| window.setTimeout(hideToast, 4000); | |
| }, 300); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .notifications__toast { | |
| position: fixed; | |
| bottom: 2em; | |
| right: 1em; | |
| max-width: 365px; | |
| width: auto; | |
| padding: 1.25em 2em; | |
| font-size: 14px; | |
| background-color: #eee; | |
| will-change: transform; | |
| transition: transform 0.225s cubic-bezier(0.0, 0.0, 0.2, 1); | |
| transform: translateY(calc(100% + 2em)); | |
| > p { | |
| display: flex; | |
| align-items: center; | |
| strong:first-child { | |
| margin-right: 15px; | |
| } | |
| } | |
| &.show { | |
| transform: translateY(0); | |
| } | |
| } | |
| .notifications__toast--success { | |
| background-color: #52bb56; | |
| color: #174217; | |
| } | |
| .notifications__toast--error { | |
| background: #FF5722; | |
| color: #441708; | |
| } | |
| .notifications__toast--info { | |
| background-color: #2196F3; | |
| color: #031523; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Services; | |
| use Illuminate\Support\Facades\Facade; | |
| /** | |
| * @see \Illuminate\Session\SessionManager | |
| * @see \Illuminate\Session\Store | |
| */ | |
| class NotificationFacade extends Facade | |
| { | |
| /** | |
| * Get the registered name of the component. | |
| * | |
| * @return string | |
| */ | |
| protected static function getFacadeAccessor() | |
| { | |
| return 'notification'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Services; | |
| use Session; | |
| use Illuminate\Session\SessionManager; | |
| class NotificationService | |
| { | |
| /** | |
| * @var SessionManager | |
| */ | |
| private $session; | |
| /** | |
| * NotificationService constructor | |
| * | |
| * @param SessionManager $session | |
| */ | |
| function __construct(SessionManager $session) | |
| { | |
| $this->session = $session; | |
| } | |
| /** | |
| * Flash a success notification | |
| * | |
| * @param string $message | |
| * @param string $description | |
| * | |
| * @return void | |
| */ | |
| public function success(string $message) { | |
| $this->flash('success', $message); | |
| } | |
| /** | |
| * Flash an info notification | |
| * | |
| * @param string $message | |
| * @param string $description | |
| * | |
| * @return void | |
| */ | |
| public function info(string $message) { | |
| $this->flash('info', $message); | |
| } | |
| /** | |
| * Flash a warning notification | |
| * | |
| * @param string $message | |
| * @param string $description | |
| * | |
| * @return void | |
| */ | |
| public function warning(string $message) { | |
| $this->flash('warning', $message); | |
| } | |
| /** | |
| * Flash an error notification | |
| * | |
| * @param string $message | |
| * @param string $description | |
| * | |
| * @return void | |
| */ | |
| public function error(string $message) { | |
| $this->flash('error', $message); | |
| } | |
| public function all() { | |
| $notifications = Session::get('notifications'); | |
| Session::forget('notifications'); | |
| return $notifications; | |
| } | |
| private function flash(string $type, string $message) { | |
| $notifications = $this->session->get('notifications', []); | |
| $this->session->flash('notifications', array_merge($notifications, [compact('type', 'message', 'description')])); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment