mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-19 14:15:24 +00:00
Initial commit 🚀
This commit is contained in:
14
4-natours/after-section-14/public/js/alerts.js
Normal file
14
4-natours/after-section-14/public/js/alerts.js
Normal file
@@ -0,0 +1,14 @@
|
||||
/* eslint-disable */
|
||||
|
||||
export const hideAlert = () => {
|
||||
const el = document.querySelector('.alert');
|
||||
if (el) el.parentElement.removeChild(el);
|
||||
};
|
||||
|
||||
// type is 'success' or 'error'
|
||||
export const showAlert = (type, msg, time = 7) => {
|
||||
hideAlert();
|
||||
const markup = `<div class="alert alert--${type}">${msg}</div>`;
|
||||
document.querySelector('body').insertAdjacentHTML('afterbegin', markup);
|
||||
window.setTimeout(hideAlert, time * 1000);
|
||||
};
|
||||
591
4-natours/after-section-14/public/js/bundle.js
Normal file
591
4-natours/after-section-14/public/js/bundle.js
Normal file
File diff suppressed because one or more lines are too long
1
4-natours/after-section-14/public/js/bundle.js.map
Normal file
1
4-natours/after-section-14/public/js/bundle.js.map
Normal file
File diff suppressed because one or more lines are too long
71
4-natours/after-section-14/public/js/index.js
Normal file
71
4-natours/after-section-14/public/js/index.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/* eslint-disable */
|
||||
import '@babel/polyfill';
|
||||
import { displayMap } from './mapbox';
|
||||
import { login, logout } from './login';
|
||||
import { updateSettings } from './updateSettings';
|
||||
import { bookTour } from './stripe';
|
||||
import { showAlert } from './alerts';
|
||||
|
||||
// DOM ELEMENTS
|
||||
const mapBox = document.getElementById('map');
|
||||
const loginForm = document.querySelector('.form--login');
|
||||
const logOutBtn = document.querySelector('.nav__el--logout');
|
||||
const userDataForm = document.querySelector('.form-user-data');
|
||||
const userPasswordForm = document.querySelector('.form-user-password');
|
||||
const bookBtn = document.getElementById('book-tour');
|
||||
|
||||
// DELEGATION
|
||||
if (mapBox) {
|
||||
const locations = JSON.parse(mapBox.dataset.locations);
|
||||
displayMap(locations);
|
||||
}
|
||||
|
||||
if (loginForm)
|
||||
loginForm.addEventListener('submit', e => {
|
||||
e.preventDefault();
|
||||
const email = document.getElementById('email').value;
|
||||
const password = document.getElementById('password').value;
|
||||
login(email, password);
|
||||
});
|
||||
|
||||
if (logOutBtn) logOutBtn.addEventListener('click', logout);
|
||||
|
||||
if (userDataForm)
|
||||
userDataForm.addEventListener('submit', e => {
|
||||
e.preventDefault();
|
||||
const form = new FormData();
|
||||
form.append('name', document.getElementById('name').value);
|
||||
form.append('email', document.getElementById('email').value);
|
||||
form.append('photo', document.getElementById('photo').files[0]);
|
||||
|
||||
updateSettings(form, 'data');
|
||||
});
|
||||
|
||||
if (userPasswordForm)
|
||||
userPasswordForm.addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
document.querySelector('.btn--save-password').textContent = 'Updating...';
|
||||
|
||||
const passwordCurrent = document.getElementById('password-current').value;
|
||||
const password = document.getElementById('password').value;
|
||||
const passwordConfirm = document.getElementById('password-confirm').value;
|
||||
await updateSettings(
|
||||
{ passwordCurrent, password, passwordConfirm },
|
||||
'password'
|
||||
);
|
||||
|
||||
document.querySelector('.btn--save-password').textContent = 'Save password';
|
||||
document.getElementById('password-current').value = '';
|
||||
document.getElementById('password').value = '';
|
||||
document.getElementById('password-confirm').value = '';
|
||||
});
|
||||
|
||||
if (bookBtn)
|
||||
bookBtn.addEventListener('click', e => {
|
||||
e.target.textContent = 'Processing...';
|
||||
const { tourId } = e.target.dataset;
|
||||
bookTour(tourId);
|
||||
});
|
||||
|
||||
const alertMessage = document.querySelector('body').dataset.alert;
|
||||
if (alertMessage) showAlert('success', alertMessage, 20);
|
||||
38
4-natours/after-section-14/public/js/login.js
Normal file
38
4-natours/after-section-14/public/js/login.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/* eslint-disable */
|
||||
import axios from 'axios';
|
||||
import { showAlert } from './alerts';
|
||||
|
||||
export const login = async (email, password) => {
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'POST',
|
||||
url: '/api/v1/users/login',
|
||||
data: {
|
||||
email,
|
||||
password
|
||||
}
|
||||
});
|
||||
|
||||
if (res.data.status === 'success') {
|
||||
showAlert('success', 'Logged in successfully!');
|
||||
window.setTimeout(() => {
|
||||
location.assign('/');
|
||||
}, 1500);
|
||||
}
|
||||
} catch (err) {
|
||||
showAlert('error', err.response.data.message);
|
||||
}
|
||||
};
|
||||
|
||||
export const logout = async () => {
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'GET',
|
||||
url: '/api/v1/users/logout'
|
||||
});
|
||||
if ((res.data.status = 'success')) location.reload(true);
|
||||
} catch (err) {
|
||||
console.log(err.response);
|
||||
showAlert('error', 'Error logging out! Try again.');
|
||||
}
|
||||
};
|
||||
50
4-natours/after-section-14/public/js/mapbox.js
Normal file
50
4-natours/after-section-14/public/js/mapbox.js
Normal file
@@ -0,0 +1,50 @@
|
||||
/* eslint-disable */
|
||||
export const displayMap = locations => {
|
||||
mapboxgl.accessToken =
|
||||
'pk.eyJ1Ijoiam9uYXNzY2htZWR0bWFubiIsImEiOiJjam54ZmM5N3gwNjAzM3dtZDNxYTVlMnd2In0.ytpI7V7w7cyT1Kq5rT9Z1A';
|
||||
|
||||
var map = new mapboxgl.Map({
|
||||
container: 'map',
|
||||
style: 'mapbox://styles/jonasschmedtmann/cjvi9q8jd04mi1cpgmg7ev3dy',
|
||||
scrollZoom: false
|
||||
// center: [-118.113491, 34.111745],
|
||||
// zoom: 10,
|
||||
// interactive: false
|
||||
});
|
||||
|
||||
const bounds = new mapboxgl.LngLatBounds();
|
||||
|
||||
locations.forEach(loc => {
|
||||
// Create marker
|
||||
const el = document.createElement('div');
|
||||
el.className = 'marker';
|
||||
|
||||
// Add marker
|
||||
new mapboxgl.Marker({
|
||||
element: el,
|
||||
anchor: 'bottom'
|
||||
})
|
||||
.setLngLat(loc.coordinates)
|
||||
.addTo(map);
|
||||
|
||||
// Add popup
|
||||
new mapboxgl.Popup({
|
||||
offset: 30
|
||||
})
|
||||
.setLngLat(loc.coordinates)
|
||||
.setHTML(`<p>Day ${loc.day}: ${loc.description}</p>`)
|
||||
.addTo(map);
|
||||
|
||||
// Extend map bounds to include current location
|
||||
bounds.extend(loc.coordinates);
|
||||
});
|
||||
|
||||
map.fitBounds(bounds, {
|
||||
padding: {
|
||||
top: 200,
|
||||
bottom: 150,
|
||||
left: 100,
|
||||
right: 100
|
||||
}
|
||||
});
|
||||
};
|
||||
20
4-natours/after-section-14/public/js/stripe.js
Normal file
20
4-natours/after-section-14/public/js/stripe.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/* eslint-disable */
|
||||
import axios from 'axios';
|
||||
import { showAlert } from './alerts';
|
||||
const stripe = Stripe('pk_test_BUkd0ZXAj6m0q0jMyRgBxNns00PPtgvjjr');
|
||||
|
||||
export const bookTour = async tourId => {
|
||||
try {
|
||||
// 1) Get checkout session from API
|
||||
const session = await axios(`/api/v1/bookings/checkout-session/${tourId}`);
|
||||
// console.log(session);
|
||||
|
||||
// 2) Create checkout form + chanre credit card
|
||||
await stripe.redirectToCheckout({
|
||||
sessionId: session.data.session.id
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
showAlert('error', err);
|
||||
}
|
||||
};
|
||||
25
4-natours/after-section-14/public/js/updateSettings.js
Normal file
25
4-natours/after-section-14/public/js/updateSettings.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/* eslint-disable */
|
||||
import axios from 'axios';
|
||||
import { showAlert } from './alerts';
|
||||
|
||||
// type is either 'password' or 'data'
|
||||
export const updateSettings = async (data, type) => {
|
||||
try {
|
||||
const url =
|
||||
type === 'password'
|
||||
? '/api/v1/users/updateMyPassword'
|
||||
: '/api/v1/users/updateMe';
|
||||
|
||||
const res = await axios({
|
||||
method: 'PATCH',
|
||||
url,
|
||||
data
|
||||
});
|
||||
|
||||
if (res.data.status === 'success') {
|
||||
showAlert('success', `${type.toUpperCase()} updated successfully!`);
|
||||
}
|
||||
} catch (err) {
|
||||
showAlert('error', err.response.data.message);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user