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.
Following the same pattern we established for the zone selector, we remove the link from each tab and instead add a hidden form field and submit the form when a tab is clicked on.
- Loading branch information
1 parent
de781bc
commit 80c92f7
Showing
4 changed files
with
17 additions
and
58 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
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
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