mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-19 22:25:24 +00:00
Initial commit 🚀
This commit is contained in:
14
4-natours/after-section-12/public/js/alerts.js
Normal file
14
4-natours/after-section-12/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) => {
|
||||
hideAlert();
|
||||
const markup = `<div class="alert alert--${type}">${msg}</div>`;
|
||||
document.querySelector('body').insertAdjacentHTML('afterbegin', markup);
|
||||
window.setTimeout(hideAlert, 5000);
|
||||
};
|
||||
8993
4-natours/after-section-12/public/js/bundle.js
Normal file
8993
4-natours/after-section-12/public/js/bundle.js
Normal file
File diff suppressed because one or more lines are too long
1
4-natours/after-section-12/public/js/bundle.js.map
Normal file
1
4-natours/after-section-12/public/js/bundle.js.map
Normal file
File diff suppressed because one or more lines are too long
55
4-natours/after-section-12/public/js/index.js
Normal file
55
4-natours/after-section-12/public/js/index.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/* eslint-disable */
|
||||
import '@babel/polyfill';
|
||||
import { displayMap } from './mapbox';
|
||||
import { login, logout } from './login';
|
||||
import { updateSettings } from './updateSettings';
|
||||
|
||||
// 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');
|
||||
|
||||
// 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 name = document.getElementById('name').value;
|
||||
const email = document.getElementById('email').value;
|
||||
updateSettings({ name, email }, '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 = '';
|
||||
});
|
||||
38
4-natours/after-section-12/public/js/login.js
Normal file
38
4-natours/after-section-12/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: 'http://127.0.0.1:3000/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: 'http://127.0.0.1:3000/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-12/public/js/mapbox.js
Normal file
50
4-natours/after-section-12/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
|
||||
}
|
||||
});
|
||||
};
|
||||
25
4-natours/after-section-12/public/js/updateSettings.js
Normal file
25
4-natours/after-section-12/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'
|
||||
? 'http://127.0.0.1:3000/api/v1/users/updateMyPassword'
|
||||
: 'http://127.0.0.1:3000/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