Skip to content

Instantly share code, notes, and snippets.

View ogabrielguerra's full-sized avatar
🏠
Working from home

Gabriel Guerra ogabrielguerra

🏠
Working from home
View GitHub Profile
@ogabrielguerra
ogabrielguerra / RadioGroup.js
Created July 21, 2019 17:06
React JS Component for handling radio button groups.
import React from "react";
class RadioGroup extends React.Component{
constructor(props){
super(props);
this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
this.state = { selectedOption : this.props.checked }
}
@ogabrielguerra
ogabrielguerra / ImageToBase64.js
Created June 23, 2019 23:06
Javascript function to convert images to base64
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
@ogabrielguerra
ogabrielguerra / newObjFromState.js
Created May 25, 2019 00:07
REACT | Compose a new object from Component State
const states = this.state.proposalOptions;
let gathered = {};
Object.keys(states).map((key)=>{
gathered[key] = this.state.proposalOptions[key];
}
);
@ogabrielguerra
ogabrielguerra / onScrollStop.js
Last active May 14, 2019 12:38
Execute an action when user stops scrolling. | JS
let scroller=(callback, interval)=>{
//Inits the var
let scrollStop;
//Adds the event listener
window.addEventListener('scroll', function () {
clearTimeout(scrollStop);
scrollStop = setTimeout(function () {
//Execute the callback passed as reference
@ogabrielguerra
ogabrielguerra / jsonAttributesToObj.php
Last active September 29, 2018 12:02
A PHP method to convert JSON attributes into object attributes.
function convertJsonToObjAttributes($json){
$keys = array_keys((array)$json);
$jsonSize = count($json);
for( $i=0; $i<$jsonSize; $i++ )
$this->{$keys[$i]} = $json[$keys[$i]];
}