Playing around with Hotwire ⚡️
Tags: webdev, javascript, rails
Published: 2024-06-05 21:17:15

Hey :wave:,

Let's jump into my bi-weekly update in the [ramen profitability series](https://dev.to/joelzwarrington/series/27288), where I'm sharing progress on my latest project: [HomeSuiteApartment](https://homesuiteapartment.com), a tool to manage rental properties.

In this update, I've been focused on the workflow to inquire about units and scheduling viewings, and I touched up the subscription page. Given my current schedule (working fulltime with a 1 year old), I'm pretty happy with what I was able to accomplish in these past two weeks, and am looking forward to setting a open-beta launch date in the next month or two.

## Updating the listing page

I updated the listing page, and have since added a form to submit inquiries and additional messages on-top of the inquiry.

![Screenshot of a unit listing with inquiry form at the bottom](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rgdao1xlgc02xhb7sa9.png)

{% youtube HqlNv13deXg %}

Once the inquiry is submitted, the property manager will be able to respond in their inbox, and schedule a viewing.

![Screenshot showing list of inquiries](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juykdvafzkrp8fhwfa0a.png)

{% youtube ZuqR0GNJoys %}

As you can see in the example above, I'm using [Turbo Frames](https://turbo.hotwired.dev/) to give a single-page application feel,  while not having to write any JavaScript.

Here's a boiled down example of how you can accomplish a view that:
```erb
<%= link_to inquiry, data: { turbo_frame: "inquiry" } %> 

<%# this is a placeholder for the selected inquiry %>
<%= turbo_frame_tag "inquiry" %>
```

Now, in your `#show` action, you can simply wrap your item in the turbo frame tag, and instead of following the redirect, Turbo will replace the frame with id `inquiry` from the response. 

```erb
<%= turbo_frame_tag "inquiry" do %>
  <%= turbo_frame_tag inquiry do %>
    <p>This is an inquiry!</p>
  <% end %>
<% end %>
```

A major benefit to this, is that if you want to implement single-page application-like features, all you need to do is use a few custom HTML elements, and some data-attributes. So if you're a Ruby on Rails developer, you really won't need to do much to decompose your views into frames.

You can use [Turbo Frames](https://turbo.hotwired.dev/handbook/frames) on form submission using [Turbo Streams](https://turbo.hotwired.dev/handbook/streams). [Turbo Streams](https://turbo.hotwired.dev/handbook/streams) allow you to modify [Turbo Frames](https://turbo.hotwired.dev/handbook/frames) very precisely with these actions: _append_, _prepend_, (insert) _before_, (insert) _after_, _replace_, _update_, _remove_, and _refresh_.

In my example, when I submit a new message to the inquiry, I'm appending the message to the list, and also am clearing out the message form. Similar to decomposing with [Turbo Frames](https://turbo.hotwired.dev/handbook/frames), you're only ever sprinkling in things as needed, and you don't need to add any boilerplate up-front.

Most of your controllers still function as normal Ruby on Rails controllers using redirects, but when you need the extra functionality, or want to speed up page loads, you can use [Turbo Streams](https://turbo.hotwired.dev/handbook/streams).

I highly recommend checking out [Hotwire](https://hotwired.dev/), it's really been a breath of fresh air for me. You get to do a lot more with less JavaScript, and allows you to get the same benefits as other frameworks/libraries (such as react) without having to significantly change your development process.

If you're interested, you should have a look at the [Hotrails tutorial](https://www.hotrails.dev/) which goes over all of the concepts introduced by Turbo and Stimulus.

## Subscriptions

In the last update, I got subscriptions working, and integrated with [Stripe](https://stripe.com). I'm using the [Pay gem](https://github.com/pay-rails/pay) to manage the subscriptions, as it provides a lot of built-in functionality, and the [Stripe Ruby Client](https://github.com/stripe/stripe-ruby) for other API calls not supported with the [Pay gem](https://github.com/pay-rails/pay).

In this update, I went ahead and fleshed out the page with pricing and features. So we started with this:

![Subscription page before the redesign](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7h9v74bwh36nxtlm1lv.png)

and, ended with this:
![Subscription page after the redesign](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffefg9mm38szupfy5ynz.png)

I won't take credit for the re-design though. If you're using the [Tailwind CSS Library](https://tailwindcss.com/) you should checkout [Tailwind UI](https://tailwindui.com/). It's helped me scaffold a few components and pages quite easily, without having a designer onboard.

## What's next!

- Further improving the viewing + inquiry pages
- Improve the unit to listing workflow

See you in two weeks!