generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from dxw/url-params-3
Drive page content with URL parameters
- Loading branch information
Showing
17 changed files
with
75 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class PredictionController extends Controller { | ||
static targets = ["showButton", "hideButton", "guidance"]; | ||
static targets = ["showButton", "hideButton", "guidance", "zoneSelector"]; | ||
|
||
toggleGuidance() { | ||
this.showButtonTarget.classList.toggle("hidden"); | ||
this.hideButtonTarget.classList.toggle("hidden"); | ||
this.guidanceTarget.classList.toggle("hidden"); | ||
} | ||
|
||
changeZone() { | ||
const selectedZone = this.zoneSelectorTarget.value; | ||
|
||
// Update the URL with the new zone | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("zone", selectedZone); | ||
window.history.pushState({}, "", url); | ||
|
||
// Submit the form to reload the turbo frame | ||
this.zoneSelectorTarget.form.requestSubmit(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,23 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Connects to data-controller="tab" | ||
export default class extends Controller { | ||
static classes = ["active", "inactive"]; | ||
static targets = ["dayPrediction", "day", "daqiValue"]; | ||
export default class TabController extends Controller { | ||
static targets = ["daySelector"]; | ||
|
||
connect() {} | ||
|
||
switch_tab() { | ||
this.makeAllTabsInactive(); | ||
this.removeDaqiAlertClasses(); | ||
changeTab(event) { | ||
const selectedDay = event.currentTarget.dataset.day; | ||
|
||
this.addAlertClassIfNotTodayAndHasAirQualityAlert(); | ||
this.dayPredictionTarget.classList.toggle(this.activeClass); | ||
this.dayPredictionTarget.classList.toggle(this.inactiveClass); | ||
} | ||
|
||
makeAllTabsInactive() { | ||
document.querySelectorAll(".tabs .tab").forEach((el) => { | ||
el.classList.remove(this.activeClass); | ||
el.classList.add(this.inactiveClass); | ||
}); | ||
} | ||
|
||
addAlertClassIfNotTodayAndHasAirQualityAlert() { | ||
const daqiValue = this.getDaqiValue(); | ||
if (this.dayTarget.innerText !== "Today" && daqiValue > 3) { | ||
this.addDaqiAlertClass(); | ||
} | ||
} | ||
// Add the new date to the URL | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("day", selectedDay); | ||
window.history.pushState({}, "", url); | ||
|
||
addDaqiAlertClass() { | ||
const daqiValue = this.getDaqiValue(); | ||
this.dayPredictionTarget.classList.add( | ||
`daqi-alert-after-today-selected-level-${daqiValue}` | ||
); | ||
} | ||
|
||
removeDaqiAlertClasses() { | ||
document.querySelectorAll(".tabs .tab").forEach((el) => { | ||
const daqiAlertClassRegex = new RegExp( | ||
"daqi-alert-after-today-selected-level-(\\d{1,2})" | ||
); | ||
|
||
const fullClassName = Array.from(el.classList).find((className) => | ||
className.match(daqiAlertClassRegex) | ||
); | ||
|
||
if (fullClassName) { | ||
el.classList.remove(fullClassName); | ||
} | ||
}); | ||
} | ||
// Update contents of daySelector | ||
this.daySelectorTarget.value = selectedDay; | ||
|
||
getDaqiValue() { | ||
return this.daqiValueTarget.innerText.split("/")[0].split(" ")[1]; | ||
// Submit the form to reload the turbo frame | ||
this.daySelectorTarget.form.requestSubmit(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
<%= turbo_frame_tag "alert_guidance" do %> | ||
<% if air_pollution_prediction.air_quality_alert %> | ||
<div class="alert-guidance bg-<%= air_pollution_prediction.air_quality_alert.daqi_label.parameterize %>-alert-guidance-panel m-5 p-10"> | ||
<p class="font-semibold mb-4"> | ||
<%= t("air_quality_alert.#{air_pollution_prediction.air_quality_alert.daqi_level}.guidance.title") %> | ||
</p> | ||
<div class="text-xs"> | ||
<%= t("air_quality_alert.#{air_pollution_prediction.air_quality_alert.daqi_level}.guidance.detail_html") %> | ||
</div> | ||
<% if air_pollution_prediction.air_quality_alert %> | ||
<div class="alert-guidance bg-<%= air_pollution_prediction.air_quality_alert.daqi_label.parameterize %>-alert-guidance-panel m-5 p-10"> | ||
<p class="font-semibold mb-4"> | ||
<%= t("air_quality_alert.#{air_pollution_prediction.air_quality_alert.daqi_level}.guidance.title") %> | ||
</p> | ||
<div class="text-xs"> | ||
<%= t("air_quality_alert.#{air_pollution_prediction.air_quality_alert.daqi_level}.guidance.detail_html") %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<%= turbo_frame_tag "day_predictions" do %> | ||
<dl class="predictions p-4"> | ||
<%= render(PredictionComponent.new(prediction: forecast.uv)) %> | ||
<%= render(PredictionComponent.new(prediction: forecast.pollen)) if forecast.pollen.valid? %> | ||
<%= render "temperature_prediction", prediction: forecast.temperature %> | ||
</dl> | ||
<% end %> | ||
<dl class="predictions p-4"> | ||
<%= render(PredictionComponent.new(prediction: forecast.uv)) %> | ||
<%= render(PredictionComponent.new(prediction: forecast.pollen)) if forecast.pollen.valid? %> | ||
<%= render "temperature_prediction", prediction: forecast.temperature %> | ||
</dl> |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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