Skip to content

Instantly share code, notes, and snippets.

const form = document.querySelector("form");
form.addEventListener("submit", submitUserData);
@ranwahle
ranwahle / self-authenticating-proxy-in-node.js
Created November 7, 2019 15:19
Self Authenticating proxy in node.js
const getArguments = require('get-arguments-lib'); // Getting port & proxy hostname for arguments
const read = require('read'); // Use to silence password
const args = getArguments(process.argv);
const port = args.port || 8080;
const hostname = args.hostname
@ranwahle
ranwahle / map-array-with-validate.js
Last active July 2, 2019 10:32
Map aray with validation
export const mapArrayItems = (data = []) => {
if (!Array.isArray(data)) {
return null;
}
return data.map((item) => {
// some mapping
})
}
@ranwahle
ranwahle / map-tree-node.js
Created July 2, 2019 10:18
Map tree node
export const mapTreeNodes = (data = []) => data.map((item) => {
// Do somethong
})
@ranwahle
ranwahle / onpopstate-with-navigating-out-confirmation.js
Created June 16, 2019 07:16
onpopstate with navigating out confirmation
window.onpopstate = async (e) => {
const treeBuilder = module.Router.router.routingSnapshotTreeBuilder
const prevUrl = history.state && history.state.prev;
const currentRouteData = treeBuilder.buildRouteTree(prevUrl);
const router = module.Router.router
if (await router.canGoOn(window.location.pathname, null, currentRouteData && currentRouteData.deactivateGuard, prevUrl )) {
this.changeRoute(window.location.pathname)
} else {
history.back();
}
@ranwahle
ranwahle / navigate-with-both-guard-functions.js
Created June 16, 2019 07:04
Navigate with both guard functions
async navigate(url) {
// Check if you may exit the current URL
const oldRouteData = this.routingSnapshotTreeBuilder.buildRouteTree(window.location.pathname);
try {
url = url === '/' ? url : new URL(url).pathname;
} catch (err) {
throw Error(`Cannot construct url from ${url}`)
@ranwahle
ranwahle / can-go-on-with-both-guards.js
Last active June 16, 2019 06:51
Can Go On, with both guards
async canGoOn(routeData, guard, oldGuard, prevUrl) {
let result = true;
if (oldGuard) {
result = await oldGuard(prevUrl);
}
if (guard && result) {
result = await guard(routeData)
}
@ranwahle
ranwahle / route-configuration-deactivate-guard.js
Created June 16, 2019 06:43
Route configuration - DeactivateGuard
{
path: '/addImage',
element: 'div',
attributes: {is: 'add-image'},
deactivateGuard: confirmExit
}
@ranwahle
ranwahle / confirm-exit-guard.js
Created June 16, 2019 06:37
Confirm Exit guard
const confirmExit = async () => {
const confirmModal = document.createElement('confirm-modal');
confirmModal.comfirmQuestion = 'Are you sure?';
document.body.appendChild(confirmModal);
const result = await confirmModal.getUserResult();
document.body.removeChild(confirmModal);
return result;
}
@ranwahle
ranwahle / guard-on-router-outlet.js
Last active June 9, 2019 19:50
Guard on Router-outlet
async changeRoute(newRoute) {
const treeBuilder = module.Router.router.routingSnapshotTreeBuilder
const router = module.Router.router
const newRouteData = treeBuilder.buildRouteTree(newRoute)
if (!newRouteData) {
throw Error(`Could not build tree for ${newRoute}`)
}