Skip to content
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 33 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 29 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 Aug 9, 2022
8ba5793
add some lifecycle stuff and make data visible
aaaaargZombies Aug 9, 2022
0acb8c3
add some bascic user interaction
aaaaargZombies Aug 9, 2022
3297faf
add label text I missed, use flexbox to keep inputs on the same row
aaaaargZombies Aug 9, 2022
b859b3a
add some notes and rename a method
aaaaargZombies Aug 15, 2022
13a9a5a
add function to convert form input to openingHoursSpecification
aaaaargZombies Aug 15, 2022
0575e0e
extract openingHoursSpec coonversion logic into a function
aaaaargZombies Aug 15, 2022
e6d435f
add chronological sorting to openingTimes data
aaaaargZombies Aug 15, 2022
dc5b819
reset form fields, update comments
aaaaargZombies Aug 15, 2022
12eba85
fix from reset bug, make a function name more explicit
aaaaargZombies Aug 15, 2022
4037677
add the ability to remove opening times
aaaaargZombies Aug 15, 2022
5faf0b8
add some more text to the list items to make the funcionality clearer
aaaaargZombies Aug 15, 2022
cba65a4
refactor for tidyness
aaaaargZombies Aug 15, 2022
f68ee07
add allday checkbox
aaaaargZombies Aug 16, 2022
321114a
improve opening times list UI
aaaaargZombies Aug 16, 2022
52c064e
fix broken row / columns in partners form
aaaaargZombies Aug 16, 2022
a959667
add spacing between open-times list and form
aaaaargZombies Aug 16, 2022
0d5760b
group labels visually on opening times form
aaaaargZombies Aug 16, 2022
8f79928
add an allDay condition to openingHoursEnglish
aaaaargZombies Aug 16, 2022
b18a40a
fix sorting bug in chrome
aaaaargZombies Aug 16, 2022
0466685
add system test for opening-times stimulus controller on partners page
aaaaargZombies Aug 16, 2022
77f737d
remove old opening-times picker
aaaaargZombies Aug 16, 2022
1e5872e
delete old comments
aaaaargZombies Aug 16, 2022
94f5829
fix memory leak from non-deleted nodes
aaaaargZombies Aug 30, 2022
7efe7a2
fix broken opening times input layout on laptop
aaaaargZombies Aug 30, 2022
346ab0d
move all day option to end of opening times inputs
aaaaargZombies Aug 30, 2022
0953fde
reset opening times form based on last user interaction
aaaaargZombies Aug 30, 2022
24e6a33
add a disconnect method to opening_times_controller
aaaaargZombies Aug 30, 2022
acfe7ad
align text in opening time inputs
aaaaargZombies Aug 30, 2022
921820f
Merge branch 'main' into 1417-opening-times-stimulus-controller
aaaaargZombies Aug 30, 2022
4998b67
replace sorting and filtering logic with lodash
aaaaargZombies Aug 31, 2022
b2e6bd0
move opening times markup into a partial
aaaaargZombies Aug 31, 2022
406951b
import only lodash methods used in opening times controller
aaaaargZombies Aug 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/javascript/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "./src/datatable.js"
// Opening times stuff
import Vue from "vue"
import "vue-turbolinks"
import "./src/opening-times.js"

// Specific pages
import "./src/behaviors/all_behaviors.js"
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ application.register("image-preview", ImagePreviewController)
import LeafletController from "./leaflet_controller.js"
application.register("leaflet", LeafletController)

import OpeningTimesController from "./opening_times_controller.js"
application.register("opening-times", OpeningTimesController)

import Select2Controller from "./select2_controller.js"
application.register("select2", Select2Controller)
151 changes: 151 additions & 0 deletions app/javascript/controllers/opening_times_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { Controller } from "@hotwired/stimulus";

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) =>
[...openSpecArray]
.sort(
(a, b) =>
parseFloat(openingHoursObj(a).open.replace(":", ".")) -
ivan-kocienski-gfsc marked this conversation as resolved.
Show resolved Hide resolved
parseFloat(openingHoursObj(b).open.replace(":", ".")),
)
.sort(
ivan-kocienski-gfsc marked this conversation as resolved.
Show resolved Hide resolved
(a, b) =>
dayOrder.indexOf(openingHoursObj(a).day) -
dayOrder.indexOf(openingHoursObj(b).day),
);

const nextDay = (day) => {
const index =
Copy link
Contributor

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;

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 removeTime = (openSpecArray, openSpec) =>
[...openSpecArray].filter(
(el) => JSON.stringify(el) !== JSON.stringify(openSpec),
ivan-kocienski-gfsc marked this conversation as resolved.
Show resolved Hide resolved
);

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 = () => {
this.dataValue = removeTime(this.dataValue, 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),
]);
}
}
197 changes: 0 additions & 197 deletions app/javascript/src/opening-times.js

This file was deleted.

Loading