-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-rewards.js
87 lines (72 loc) · 3.27 KB
/
configure-rewards.js
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
// Function to create a new tier element
function createTierElement() {
const tierElement = document.createElement('div');
tierElement.className = 'tier-element';
const rewardInput = document.createElement('input');
rewardInput.type = 'text';
rewardInput.placeholder = 'Tier reward';
rewardInput.required = true;
const pointsInput = document.createElement('input');
pointsInput.type = 'number';
pointsInput.min = '1';
pointsInput.step = '1';
//pointsInput.value = '100';
pointsInput.placeholder = "Add dollar Amount";
pointsInput.required = true;
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.textContent = 'Remove tier';
removeBtn.addEventListener('click', () => {
tierElement.remove();
});
tierElement.appendChild(rewardInput);
tierElement.appendChild(pointsInput);
tierElement.appendChild(removeBtn);
return tierElement;
}
// Add initial tier
document.getElementById('tiers-container').appendChild(createTierElement());
// Add new tier button click event
document.getElementById('add-tier-btn').addEventListener('click', () => {
document.getElementById('tiers-container').appendChild(createTierElement());
});
// Form submit event
document.getElementById('rewards-form').addEventListener('submit', (event) => {
event.preventDefault();
const rewardTiers = [];
const tiers = document.getElementsByClassName('tier-element');
for (const tier of tiers) {
const reward = tier.children[0].value;
const points = tier.children[1].value;
rewardTiers.push(`{'tierPoints': ${points}, 'tierReward': '${reward}'}`);
}
const restaurantID = localStorage.getItem('restaurantID');
const payload = {
restaurantID: restaurantID,
rewardTiers: rewardTiers
};
// Use the API Gateway SDK to make the API POST request to /programs
const apiGatewayUrl = '/programs';
var apigClient = apigClientFactory.newClient();
apigClient.programsPost({}, payload, {})
.then((response) => {
// Display success message and show the reward program
document.getElementById('result-message').textContent = 'Reward program successfully configured.';
document.getElementById('rewards-form').style.display = 'none';
const rewardProgramDisplay = document.getElementById('reward-program-display');
rewardProgramDisplay.style.display = 'block';
rewardProgramDisplay.innerHTML = '<h2>Reward Program</h2>';
for (const tier of rewardTiers) {
const tierElement = document.createElement('div');
tierElement.textContent = tier;
rewardProgramDisplay.appendChild(tierElement);
}
})
.catch((error) => {
// Display error message
document.getElementById('result-message').textContent = 'Error: ' + error.message;
});
});
// Note: The code assumes that the API endpoint '/programs' is set up to accept POST requests with the specified payload format.
// The code also assumes that the server will respond with a JSON object containing relevant information.
// The CSS file 'configure-rewards.css' is not provided, but you can create and customize it according to your design preferences.