-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedbackPurchase.js
More file actions
148 lines (129 loc) · 5.53 KB
/
feedbackPurchase.js
File metadata and controls
148 lines (129 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {capitalizeFirstLetter, uploadTempImage} from "./sellItemHelpers";
function initializePage(item) {
const itemTitle = (item.cleanedBrand || item.brand).trim() + "-" + item.category.toLowerCase();
document.getElementById('itemTitle').innerText = itemTitle;
const soldDate = new Date(item.soldDate).toLocaleDateString('sv-SE', { day: 'numeric', month: 'long', year: 'numeric' });
const subtitleText = `Köptes ${soldDate}${item.soldPlatform && item.soldPlatform !== 'Other'? ` via ${item.soldPlatform}`: ''}`;
document.getElementById('itemSubtitle').innerText = subtitleText;
const imageUrl = window.innerWidth <= 400 ?
item?.images?.modelImage || item?.images?.enhancedFrontImageSmall || item?.images?.enhancedFrontImage || item?.images?.frontImageSmall || item?.images?.frontImage :
item?.images?.modelImage || item?.images?.enhancedFrontImage || item?.images?.frontImage;
document.getElementById('itemImage').src = imageUrl;
}
async function goToReclaim(itemId) {
const res = await saveFeedback(itemId);
window.location.href = `/reclaim/?id=${itemId}`;
}
function addEventListeners() {
const params = getParamsObject();
document.getElementById('doneButton').addEventListener('click', async function () {
// Save reclaim
const res = await saveFeedback(params.id);
// Show confirmation
if (res) {
feedbackForm.style.display = 'none';
hideAllButtons();
toMaiButton.style.display = 'flex';
introDiv.style.display = 'none';
thankYouDiv.style.display = 'block';
const lowRating = parseInt(document.querySelector('input[name="rating"]:checked').value) <= 2;
reclaimLink.href = lowRating ? window.location.origin + `/reclaim/?id=${params.id}` : '#';
reclaimText.style.display = lowRating ? 'block' : 'none';
}
});
let inputs = document.querySelectorAll('input, select, textarea');
inputs.forEach(input => {
// Add an event listener to each input to clear validation message on input
input.addEventListener('input', (input) => {
input.setCustomValidity(''); // Clear validation message
});
if (input.tagName.toLowerCase() === 'select') {
input.addEventListener('change', () => {
input.setCustomValidity('');
});
}
});
const stars = document.querySelectorAll("input[type='radio'][name='rating']"); // Hämta radio-knapparna
stars.forEach((star, index) => {
star.addEventListener("change", function () {
updateStars(stars, index); // Fyll i stjärnorna med färg
stars.forEach(s => s.setCustomValidity(''));
commentContainer.style.display = 'block';
// Only show reclaimTextFeedbackFrom if rating is 3 or less
const selectedRating = parseInt(star.value);
if (selectedRating <= 3) {
reclaimTextFeedbackFrom.style.display = 'block';
} else {
reclaimTextFeedbackFrom.style.display = 'none';
}
});
});
document.getElementById('reclaimLink2').addEventListener('click', async function () {
await goToReclaim(params.id);
});
}
function updateStars(stars, selectedIndex) {
stars.forEach((star, index) => {
let starContainer = star.closest("label").querySelector(".radio-button-star");
if (index <= selectedIndex) {
starContainer.classList.add("filled"); // Add class to fill stars
} else {
starContainer.classList.remove("filled"); // Remove filled class
}
});
}
function hideAllButtons() {
doneButton.style.display = 'none';
}
async function validateInput() {
document.getElementById('feedbackFormInner').reportValidity();
return new Promise((resolve, reject) => {
// Custom validation for stars
const stars = document.querySelectorAll("input[type='radio'][name='rating']");
const selectedStar = document.querySelector("input[type='radio'][name='rating']:checked");
if (!selectedStar) {
stars[2].setCustomValidity(`Välj ett betyg först`);
document.getElementById('feedbackFormInner').reportValidity();
return resolve(false);
}
return resolve(true)
});
}
async function saveFeedback(itemId) {
if (!(await validateInput())) { return false }
const now = new Date();
const comment = feedbackComment.value || '';
const selectedRadio = document.querySelector('input[name="rating"]:checked');
const rating = selectedRadio ? parseInt(selectedRadio.value) : 0;
if (!rating) { return false }
let buyerFeedback = {
rating,
comment,
}
// Spinner
document.getElementById('doneButtonSpinner').style.display = 'block';
document.getElementById('doneButtonText').style.display = 'none';
// Save feedback
console.log('Will update: ', { itemId, buyerFeedback });
await callBackendApi(`/api/items/${itemId}/buyerFeedback`, {
data: { buyerFeedback },
requiresAuth: false,
});
return true
}
const getItem = async (itemId) => {
const res = await callBackendApi(`/api/items/${itemId}`);
return { ...(res?.data || {}), id: itemId };
}
const main = async () => {
const params = getParamsObject();
const item = params.id ? await getItem(params.id) : '';
if (!item) {
console.error("Invalid item id param");
location.href = '/';
}
initializePage(item);
addEventListeners();
triggerShowContent.click();
}
main();