Skip to content

Instantly share code, notes, and snippets.

View eugene-ilyin's full-sized avatar
🎯
Focusing

Eugene Ilyin eugene-ilyin

🎯
Focusing
View GitHub Profile
@eugene-ilyin
eugene-ilyin / catch-errors.js
Created January 17, 2020 04:16
React boilerplate to catch any error in application
componentDidCatch (error, errorInfo) {
// Log only errors in component tree, which can break the rendering.
// @see https://reactjs.org/docs/error-boundaries.html
if (this.props.logError) {
this.props.logError(error, errorInfo)
}
}
componentDidMount () {
// Development mode - catches any error.
@eugene-ilyin
eugene-ilyin / Angular get active route path
Created June 14, 2019 11:15
This snippet returns a path of active route in Angular
constructor(private router: Router,
private actRoute: ActivatedRoute) { }
...
this.router.events.filter(event => event instanceof NavigationEnd).subscribe((event) => {
const getActiveRoutePath = (routeSnapshot: ActivatedRouteSnapshot) => {
if (routeSnapshot.routeConfig) {
// Check top level route.
return routeSnapshot.routeConfig.path;
} else {
import {Component, Directive, ElementRef, QueryList, ViewChildren} from "@angular/core";
// DON'T FORGET TO DECLARE A DIRECTIVE IN YOUR MODULE.
// Selector by class
@Directive({selector: '.live-screen__user-microphone'})
export class microphoneIconSelector {
// Define a property to make it available in object, returned by directive.
nativeElement: any;
constructor(private element: ElementRef) {
this.nativeElement = element.nativeElement;
@eugene-ilyin
eugene-ilyin / login.php
Created August 3, 2015 09:30
Login programmatically
<?php
/**
* Drupal 7.
*/
$user_obj = user_load(1);
$form_state = array();
$form_state['uid'] = $user_obj->uid;
user_login_submit(array(), $form_state);
@eugene-ilyin
eugene-ilyin / request_is_ajax.php
Last active January 28, 2016 16:59
Check that request is AJAX
/**
* Check if current request is AJAX.
*/
function MODULE_NAME_is_ajax() {
$http_request = isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
// Check ajax in IE.
$request_uri = isset($_SERVER['REQUEST_URI'])
&& strtolower($_SERVER['REQUEST_URI']) == '/system/ajax';
@eugene-ilyin
eugene-ilyin / drupal_check_that_url_is_really_external.php
Created October 27, 2014 14:25
Drupal check that url is really external
/**
* Check url for external property.
*/
function module_url_is_external($path) {
global $base_url;
$url_host = parse_url($path);
if (empty($url_host['host']) && empty($url_host['scheme'])) {
return FALSE;
} else {
if ($url_host['scheme'] . '://' . $url_host['host'] === substr($base_url, 0, strlen($url_host['scheme'] . '://' . $url_host['host']))) {
@eugene-ilyin
eugene-ilyin / cookie.js
Created October 21, 2014 08:50
Work with cookies in native Javascript
@eugene-ilyin
eugene-ilyin / render_menu.php
Last active October 26, 2017 23:47
Render menu programmatically in Drupal 7
// One level menu.
$menu = menu_tree('main-menu');
$menu_items = render($menu);
// Menu with many levels.
menu_tree_all_data('main-menu');
$menu = menu_build_tree('main-menu');
$menu_items = render(menu_tree_output($menu));