Adding Toaster message in Phoenix LiveView

Search for a command to run...

No comments yet. Be the first to comment.
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Problem Statement While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

This blog is about adding a Toaster message in the Phoenix LiveView application. We are going to implement this with a combination of Elixir and Javascript. Before, starting to build a toaster let's first answer the most obvious question "What is a Toast?"
Toasts are non-interactive, passive, and asynchronous short messages for users. Generally, they are used as an interface feedback pattern for informing the user about the results of an action. This is the most bookish answer about what is a Toast. In the easiest form, it is a popup that provides simple feedback about an operation.
As per definition, a toaster is a popup that provides feedback about an operation. For example, if the user tries to submit a form by clicking the submit button. The user should get feedback after the submission as "Your account is created successfully".
In our LiveView application, we want this event(toaster display) should happen after some operation. We are going to call our toaster display logic through pushEvent after that operation. So, our toaster generation logic will be as follows
push_eventpush_event will be handled by the handleEvent handler in the javascript HookpushEvent handler will ultimately trigger the toaster popup.Now, that we are ready with our roadmap let's start building 👷
Toaster libraryI wanted a vanilla javascript library that should be lightweight and highly supportive in the js community. While doing some research I came across toastify-js. Let's install it in the application. Navigate to the assets directory by running cd assets then run
npm install toastify-js
toastify styles in our app.css by adding the following line@import "../node_modules/toastify-js/src/toastify.css";
After installing toastify we will then add the javascript logic. We will add a mounted callback, which is called and initialized with some initial value as soon as the element on which we are applying it is mounted in the DOM.
As we have discussed that our toaster should be called after our elixir code will trigger handle_event. So, we will add our event handler in our mounted callback.
message_toaster.js in assets/js folder.handleEvent named toast. import Toastify from 'toastify-js'
MessageToaster = {
mounted() {
this.handleEvent('toast', (payload) => {
Toastify({
text: payload.message,
duration: 3000
}).showToast();
})
}
}
export default MessageToaster
Toastify and use it in the handleEvent. handleEvent, we have a callback function with a payload param. It will have a custom message which will be used in the Toastify.At last but not least we will include our MessageToaster hook in our LiveSocket. For this, we will open the app.js file and import MessageToaster and include it in our Hooks object, and add the Hooks to our LiveSocket constructor as follows.
import MessageToaster from "./message_toaster"
let Hooks = { MessageToaster }
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks })
# Notice `hooks: Hooks`
phx-hook attribute on the root page with value as the hook name i.e MessageToaster. We will add our phx-hook="MessageToaster" on the live.html.heex page.Add the following code
# lib/project_name_web/templates/layout/live.html.heex
<div phx-hook="MessageToaster">
<%= @content %>
</div>
toaster from Elixir code after a certain event. For testing purposes, we are going to add a button in one of our LiveView's HTML code as follows# live/user_registration_live.html.heex
<button phx-click="submit_form">Register User</button>
handle_event for the submit_formdef handle_event('submit_form', params, socket) do
# Form submission logic
end
handle_event after our form submission logic we will make handle_event def handle_event("submit_form", params, socket) do
# Form submission logic
....
....
....
{:noreply, push_event(socket, "toast", %{message: "Your record has been created successfully"})}
end

I hope you like this blog. If you have any questions then please comment below. Thanks for reading 😊.