-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor opening times picker with stimulus controller #1434
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
06008fb
initial commit for opening-times-stimulus-controller
aaaaargZombies 8ba5793
add some lifecycle stuff and make data visible
aaaaargZombies 0acb8c3
add some bascic user interaction
aaaaargZombies 3297faf
add label text I missed, use flexbox to keep inputs on the same row
aaaaargZombies b859b3a
add some notes and rename a method
aaaaargZombies 13a9a5a
add function to convert form input to openingHoursSpecification
aaaaargZombies 0575e0e
extract openingHoursSpec coonversion logic into a function
aaaaargZombies e6d435f
add chronological sorting to openingTimes data
aaaaargZombies dc5b819
reset form fields, update comments
aaaaargZombies 12eba85
fix from reset bug, make a function name more explicit
aaaaargZombies 4037677
add the ability to remove opening times
aaaaargZombies 5faf0b8
add some more text to the list items to make the funcionality clearer
aaaaargZombies cba65a4
refactor for tidyness
aaaaargZombies f68ee07
add allday checkbox
aaaaargZombies 321114a
improve opening times list UI
aaaaargZombies 52c064e
fix broken row / columns in partners form
aaaaargZombies a959667
add spacing between open-times list and form
aaaaargZombies 0d5760b
group labels visually on opening times form
aaaaargZombies 8f79928
add an allDay condition to openingHoursEnglish
aaaaargZombies b18a40a
fix sorting bug in chrome
aaaaargZombies 0466685
add system test for opening-times stimulus controller on partners page
aaaaargZombies 77f737d
remove old opening-times picker
aaaaargZombies 1e5872e
delete old comments
aaaaargZombies 94f5829
fix memory leak from non-deleted nodes
aaaaargZombies 7efe7a2
fix broken opening times input layout on laptop
aaaaargZombies 346ab0d
move all day option to end of opening times inputs
aaaaargZombies 0953fde
reset opening times form based on last user interaction
aaaaargZombies 24e6a33
add a disconnect method to opening_times_controller
aaaaargZombies acfe7ad
align text in opening time inputs
aaaaargZombies 921820f
Merge branch 'main' into 1417-opening-times-stimulus-controller
aaaaargZombies 4998b67
replace sorting and filtering logic with lodash
aaaaargZombies b2e6bd0
move opening times markup into a partial
aaaaargZombies 406951b
import only lodash methods used in opening times controller
aaaaargZombies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import isEqual from "lodash/isEqual"; | ||
import orderBy from "lodash/orderBy"; | ||
|
||
const dayOrder = [ | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday", | ||
"Sunday", | ||
]; | ||
|
||
// https://schema.org/OpeningHoursSpecification | ||
const openingHoursSpec = (day, open, close) => ({ | ||
"@type": "OpeningHoursSpecification", | ||
dayOfWeek: `http://schema.org/${day}`, | ||
opens: `${open}:00`, | ||
closes: `${close}:00`, | ||
}); | ||
|
||
const openingHoursObj = (openSpec) => ({ | ||
day: openSpec.dayOfWeek.split("/").filter((str) => str.includes("day"))[0], | ||
open: openSpec.opens.slice(0, 5), | ||
close: openSpec.closes.slice(0, 5), | ||
}); | ||
|
||
const sortedOpeningHours = (openSpecArray) => | ||
orderBy(openSpecArray, [ | ||
(el) => dayOrder.indexOf(openingHoursObj(el).day), | ||
(el) => parseFloat(openingHoursObj(el).open.replace(":", ".")), | ||
]); | ||
|
||
const nextDay = (day) => { | ||
const index = | ||
dayOrder.indexOf(day) + 1 < dayOrder.length ? dayOrder.indexOf(day) + 1 : 0; | ||
return dayOrder[index]; | ||
}; | ||
|
||
const openingHoursEnglish = (openSpec) => { | ||
const { day, open, close } = openingHoursObj(openSpec); | ||
return open === "00:00" && close === "23:59" | ||
? `${day} all day` | ||
: `${day} from ${open} to ${close}`; | ||
}; | ||
|
||
const element = (type, content = "", classes = []) => { | ||
const el = document.createElement(type); | ||
el.innerHTML = content; | ||
classes.forEach((className) => { | ||
el.classList.add(className); | ||
}); | ||
return el; | ||
}; | ||
|
||
// Connects to data-controller="opening-times" | ||
export default class extends Controller { | ||
static values = { data: Array }; | ||
static targets = ["textarea", "list", "day", "allDay", "open", "close"]; | ||
|
||
connect() { | ||
this.dataValue = sortedOpeningHours(this.dataValue); | ||
this.resetForm(); | ||
} | ||
|
||
disconnect() { | ||
// clear nodes & eventListeners which don't have stimulus attributes | ||
// This might be unnecessary | ||
this.listTarget.replaceChildren(); | ||
} | ||
|
||
resetForm(day = "Monday", open = "00:00", close = "00:00") { | ||
const allDay = open === "00:00" && close === "23:59"; | ||
this.dayTarget.value = day; | ||
this.allDayTarget.checked = allDay; | ||
this.openTarget.value = open; | ||
this.closeTarget.value = close; | ||
this.openTarget.disabled = allDay; | ||
this.closeTarget.disabled = allDay; | ||
} | ||
|
||
dataValueChanged() { | ||
this.updateTextarea(); | ||
this.updateList(); | ||
} | ||
|
||
updateTextarea() { | ||
this.textareaTarget.value = JSON.stringify(this.dataValue); | ||
} | ||
|
||
updateList() { | ||
this.listTarget.replaceChildren( | ||
// This function takes separate params so we map and spread the data array. | ||
...this.dataValue.map((openSpec) => { | ||
const li = element("li", openingHoursEnglish(openSpec), [ | ||
"list-group-item", | ||
"d-flex", | ||
"align-items-center", | ||
"justify-content-between", | ||
]); | ||
const btn = element("button", "Remove", [ | ||
"btn", | ||
"btn-danger", | ||
"btn-sm", | ||
]); | ||
btn.onclick = () => { | ||
// remove opening time | ||
this.dataValue = [...this.dataValue].filter( | ||
(el) => !isEqual(el, openSpec), | ||
); | ||
}; | ||
li.appendChild(btn); | ||
return li; | ||
}), | ||
); | ||
} | ||
|
||
allDay(event) { | ||
event.preventDefault(); | ||
if (this.allDayTarget.checked) { | ||
this.openTarget.value = "00:00"; | ||
this.closeTarget.value = "23:59"; | ||
this.openTarget.disabled = true; | ||
this.closeTarget.disabled = true; | ||
} | ||
if (!this.allDayTarget.checked) { | ||
this.openTarget.disabled = false; | ||
this.closeTarget.disabled = false; | ||
} | ||
} | ||
|
||
addOpeningTime(event) { | ||
event.preventDefault(); | ||
const day = this.dayTarget.value; | ||
const open = this.openTarget.value; | ||
const close = this.closeTarget.value; | ||
this.resetForm(nextDay(day), open, close); | ||
this.dataValue = sortedOpeningHours([ | ||
...this.dataValue, | ||
openingHoursSpec(day, open, close), | ||
]); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tip:
let nextIndex = (dayOrder.indexOf(day) + 1) % dayOrder.length;