Skip to content

Commit 881edce

Browse files
committed
meal property editing tweaks
1 parent b6a66d5 commit 881edce

17 files changed

Lines changed: 113 additions & 80 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
to watch the demo:
88
Visit: https://mums-meals.herokuapp.com
99

10+
to edit a meal name, date, or count:
11+
Click it. Alter the text. Press enter or click out. Confirm the change.
12+
1013
to run on your own machine:
1114
Have: node.js installed
1215
Clone or download this project

public-source/js/elements.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ export const mealsElement = {
3030
allMealBoxes: document.querySelectorAll(".meal-box"),
3131

3232
// ALL MEAL NAMES
33-
allMealNames: document.querySelectorAll(".meal-box__heading"),
33+
allNames: document.querySelectorAll(".meal-box__heading"),
3434

3535
// ALL 'DATE' KEYS
3636
allDateKeys: document.querySelectorAll(".date-key"),
3737
// ALL 'DATE' VALUES
3838
allDateValues: document.querySelectorAll(".date-value"),
3939

4040
// ALL 'COUNT' KEYS
41-
allHadCountKeys: document.querySelectorAll(".had-count-key"),
41+
allCountKeys: document.querySelectorAll(".had-count-key"),
4242
// ALL 'COUNT' VALUES
43-
allHadCountValues: document.querySelectorAll(".had-count-value"),
43+
allCountValues: document.querySelectorAll(".count-value"),
4444

4545
// ALL 'MEAL MADE TODAY' BUTTONS
4646
allTodayButtons: document.querySelectorAll(".meal-box__button")

public-source/js/lang/languages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const text = {
2525

2626
// ADD A MEAL IN THE HEADER
2727
headerAddMeal: {
28-
pl: "Ustawienia",
28+
pl: "Opcje",
2929
en: "Settings"
3030
},
3131

@@ -42,7 +42,7 @@ const text = {
4242
},
4343

4444
// MESSAGE FOR THE MEAL COOKED CONFIRM POPUP
45-
mealCookedConfirmMessage: {
45+
mealTodayConfirmMessage: {
4646
pl: "Na pewno ugotowałaś? Potwierdź:",
4747
en: "Are you sure you made this meal today?"
4848
},

public-source/js/lang/update-language.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function updateLanguage() {
1616
document.title = text.title[language];
1717

1818
//. MESSAGE FOR THE MEAL-COOKED-CONFIRM ALERT POPUP
19-
state.mealCookedConfirmMessage = text.mealCookedConfirmMessage[language];
19+
state.mealTodayConfirmMessage = text.mealTodayConfirmMessage[language];
2020

2121
//. THE BRAND NAME IN THE HEADER
2222
// check if the element(s) exists on the currently loaded page before you place any text in it
@@ -59,8 +59,8 @@ export default function updateLanguage() {
5959
}
6060

6161
//. 'COOKED COUNT' PHRASE
62-
if (mealsElement.allHadCountKeys) {
63-
mealsElement.allHadCountKeys.forEach(phrase => {
62+
if (mealsElement.allCountKeys) {
63+
mealsElement.allCountKeys.forEach(phrase => {
6464
phrase.innerHTML = text.cookedCountPhrase[language];
6565
})
6666
}

public-source/js/meals/edit-meal-property.js renamed to public-source/js/meals/eventify-editable-property.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import state from '../state';
22
import sendEditRequest from './send-edit-request';
3+
import sanitizeString from '../../../shared/sanitize-string';
4+
5+
export default function eventifyEditableProperty(allMealSpecifiedProperty, type) {
6+
7+
const updateType = {
8+
'name update': text => sanitizeString(text, 16, 22),
9+
'date update': state.editedPropertyOfMeal,
10+
'count update': state.editedPropertyOfMeal
11+
}
312

4-
export default function eventifyMealProperty(allMealSpecifiedProperty, type) {
513
allMealSpecifiedProperty.forEach(element => {
614
element.onfocus = handleFocus;
715
element.onblur = handleBlur;
@@ -28,10 +36,13 @@ export default function eventifyMealProperty(allMealSpecifiedProperty, type) {
2836
}
2937

3038
function handleKeyDown(e) {
31-
console.log("KEYDOWN ON:", e.target.innerText)
39+
3240
const kC = e.keyCode;
3341
const element = e.target;
3442

43+
// end, home, arrows
44+
if (kC >= 35 && kC <= 40) return;
45+
3546
// F5 || enter || escape
3647
if (kC == 116 || kC == 13 || kC == 27) {
3748
e.preventDefault();
@@ -44,7 +55,7 @@ export default function eventifyMealProperty(allMealSpecifiedProperty, type) {
4455
const payload = {
4556
type, // 'name update' or 'date update' or 'count update'
4657
id: element.dataset.mealId, // meal id
47-
value: element.innerText // meal name or meal date or meal count
58+
value: element.innerText.trim() // meal name or meal date or meal count
4859
}
4960
sendEditRequest(payload);
5061
} // sendPayload() ends

public-source/js/meals/meals.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import eventifyTodayButtonAnimation from './eventify-today-button-animation';
44
import getDate from '../../../shared/get-date';
55
import state from '../state';
66
import { screenLoader, headerElement, mealsElement } from '../elements';
7-
import prepareTodayButtons from "./prepare-today-buttons";
7+
import todayButtons from "./prepare-today-buttons";
88
import { eventifySearchMealsInput, focusOrUnfocusSearchMealsInput } from './search-input';
9-
import eventifyMealProperty from './edit-meal-property';
9+
import eventifyEditableProperty from './eventify-editable-property';
1010

1111
/// unique page identifier: page-id-meals
1212

@@ -26,7 +26,7 @@ document.addEventListener("DOMContentLoaded", function () {
2626

2727
//. SET UP 'TODAY' BUTTON ON EACH MEAL
2828
const todaysDate = getDate();
29-
prepareTodayButtons(mealsElement.allTodayButtons, state.mealCookedConfirmMessage, todaysDate);
29+
todayButtons(mealsElement.allTodayButtons, state.mealTodayConfirmMessage, todaysDate);
3030

3131
//. SEARCH INPUT
3232
headerElement.searchInput.value = "";
@@ -39,18 +39,18 @@ document.addEventListener("DOMContentLoaded", function () {
3939
createMasonryLayout(noOfColumnsDisplayed);
4040
})
4141

42-
//. MEAL NAME
43-
eventifyMealProperty(mealsElement.allMealNames, 'name update');
44-
eventifyMealProperty(mealsElement.allDateValues, 'date update');
45-
eventifyMealProperty(mealsElement.allHadCountValues, 'count update');
42+
//. SET UP EVENTS FOR MEAL NAME OR DATE OR COUNT
43+
eventifyEditableProperty(mealsElement.allNames, 'name update');
44+
eventifyEditableProperty(mealsElement.allDateValues, 'date update');
45+
eventifyEditableProperty(mealsElement.allCountValues, 'count update');
4646

47-
//. ANY KEY PRESSED ANYWHERE ON THE PAGE
47+
//. ANY KEY PRESSED FOCUSES SEARCH INPUT IF NO ELEMENT IS BEING EDITED
4848
document.onkeydown = (e) => {
4949

5050
// let meal properties be edited without interference from the search input
51-
const allMealNameElements = Array.from(mealsElement.allMealNames);
51+
const allMealNameElements = Array.from(mealsElement.allNames);
5252
const allDateValuesElements = Array.from(mealsElement.allDateValues);
53-
const allCountValuesElements = Array.from(mealsElement.allHadCountValues);
53+
const allCountValuesElements = Array.from(mealsElement.allCountValues);
5454
if (allMealNameElements.includes(e.target)
5555
|| allDateValuesElements.includes(e.target)
5656
|| allCountValuesElements.includes(e.target)) {

public-source/js/meals/prepare-today-buttons.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sendEditRequest from './send-edit-request';
22

3-
export default function prepareTodayButtons(allTodayButtons, mealCookedConfirmMessage, todaysDate) {
3+
export default function todayButtons(allTodayButtons, mealTodayConfirmMessage, todaysDate) {
44

55
for (let button of allTodayButtons) {
66
if (button.dataset.mealDate === todaysDate) {
@@ -9,7 +9,7 @@ export default function prepareTodayButtons(allTodayButtons, mealCookedConfirmMe
99
button.addEventListener("click", function (event) {
1010
event.preventDefault();
1111
if (!this.getAttribute("disabled")) {
12-
const confirmed = confirm(mealCookedConfirmMessage);
12+
const confirmed = confirm(mealTodayConfirmMessage);
1313
if (confirmed) {
1414
const payload = {
1515
type: "today update",

public-source/js/meals/send-edit-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default function sendEditRequest(payload) {
44
const url = '/meals/edit';
55

66
fetch(url, {
7-
method: 'POST',
7+
method: 'PUT',
88
headers: { 'Content-Type': 'application/json' },
99
body: stringPayload // body data type must match "Content-Type" header
1010
})

public-source/js/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const state = {
3030
},
3131

3232
// MESSAGE PLACEHOLDER FOR THE MEAL-COOKED TODAY CONFIRM POPUP
33-
mealCookedConfirmMessage: "",
33+
mealTodayConfirmMessage: "",
3434

3535
// MEAL NAME THAT IS BEING CURRENTLY EDITED
3636
editedPropertyOfMeal: "",

public-source/sass/_meals.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@
123123
&__heading {
124124
padding: 1.3rem 0.7rem 0.6rem;
125125
color: $color-meal-box-heading;
126+
caret-color: red;
126127
font-weight: 300;
127128
font-size: 2.2rem;
128129
text-align: center;
129130
outline: none;
131+
cursor: text;
130132
}
131133

132134
// <p> LAST HAD IT DATE
@@ -137,8 +139,11 @@
137139

138140
// <span> date displayed
139141
& .date-value {
142+
display: inline-block;
140143
color: #f38745;
144+
caret-color: black;
141145
outline: none;
146+
cursor: text;
142147
}
143148
}
144149

@@ -150,9 +155,12 @@
150155
font-size: 1.6rem;
151156

152157
// <span> count displayed
153-
& .had-count-value {
158+
& .count-value {
159+
display: inline-block;
154160
color: #22962f;
161+
caret-color: red;
155162
outline: none;
163+
cursor: text;
156164
}
157165

158166
// <span> if count == 0

0 commit comments

Comments
 (0)