ream88

ream88

Hello,

I’m currently facing a challenge with my Phoenix app, which displays statistics about the products we support. I’m extending its functionality to allow users to specify a date range route parameter to filter the statistics. My goal is to define /stats and /stats/:range in my router.

However, I already have a /stats/:country route that takes a two-character ISO country code, such as /stats/AT for Austrian statistics.

  live "/stats", StatisticsLive.General
  live "/stats/:country", StatisticsLive.ByCountry
  live "/stats/:range", StatisticsLive.General

These two routes now conflict with each other, and I can’t define constraints like in Rails using the :constraints option (at least that I know of).

I now tried the following, because I have a fixed list of countries:

  live "/stats", StatisticsLive.General

  for country <- Countries.all() do
    live "/stats/#{country}", StatisticsLive.ByCountry, String.to_existing_atom(country)
  end

  live "/stats/:range", StatisticsLive.General

This works by misusing the live_action value passed to the LiveView. However, I need to convert the values to atoms back and forth, can’t access the value via the params map, don’t see it in logs, and overall, it just doesn’t feel right.

Is there a reason why this syntax isn’t currently supported?

  for country <- Countries.all() do
    live "/stats/#{country}", StatisticsLive.ByCountry, params: %{country: country}
  end

I don’t want to introduce a custom plug for this, as it reduces the clarity and visibility of my routes.

Showing Posts 1 to 6

thiagomajesk

thiagomajesk

I don’t recall Phoenix ever supporting route constraints because you can just use pattern-matching to emulate the same behavior (although I agree it could be useful as it’s more explicit and allows for conditional dispatching). The way I’d go about this is just specifying different routes, so live "/stats/country/:country" and live "/stats/range/:range". You always have the option to keep using live actions and handle_params to handle constraints.

sodapopcan

sodapopcan

Routes are compiled so I don’t think something like Rails’ regex-based constraints could ever be possible. I also don’t see a big difference between params: %{country: country} and your live action approach.

I agree with @thiagomajesk that having sub routes is the way to go, but if you really don’t want to, I would make live components for each stat type and delegate in the router:

def render(%{param: param} = assigns) when param in @countries do
  ~H"""
  <.live_component id="countries" module={MyAppWeb.CountryStatsComponent} countries={@countries} />
  """
end

def render(assigns) do
  ~H"""
  <.live_component id="range" module={MyAppWeb.RangeStatsComponent} ranges={@ranges} />
  """
end

That’s just a high-level example and I’m leaving out some context—there are several ways you could accomplish this.

KP123

KP123

Am I correct in assuming that the resource is “stats” and that country and range is just a filter over those resources? If so then use a query parameter. Something like /stats?country=us&range=1-5

Edit: This has the benefit of allowing the user to select a range in a specific country instead of one or the other

ream88

ream88 OP

I will also introduce the :range filter to the country URLs like this: /stats/AT/yesterday

sodapopcan

sodapopcan

What’s the usecase for wanting URLs like this? Is there a big difference between ByCountry and General? Otherwise, filters like these are exactly what query parameters are for.

KP123

KP123

I’d still use query params for this. Instead of range I’d use from. Then you can have ranges displayed like so:

  • Range of days: /stats?from=2024-20-06&to=2024-30-06
  • A specific day: /stats?from=2024-30-06&to=2024-30-06
  • Special case values: /stats?from=yesterday

And of course every filter you dream up in the future just works /stats?from=today&country=fr&future-filter=arg

You could even do /stats?yesterday. Paths are for hierarchical data, query params for key-value data

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews