Skip to content

Instantly share code, notes, and snippets.

View guimochila's full-sized avatar
🎯
Focusing

Guilherme guimochila

🎯
Focusing
View GitHub Profile
@guimochila
guimochila / Search.tsx
Last active March 20, 2022 14:41
Downshift and react-query
import { resetIdCounter, useCombobox } from 'downshift'
import Image from 'next/image'
import { useEffect, useRef, useState } from 'react'
import debounce from 'lodash/debounce'
import { DropDown, DropDownItem, SearchStyles } from './styled/DropDown'
import useSearch from '../hooks/useSearch'
function Search() {
resetIdCounter()
const [searchTerm, setSearchTerm] = useState<string | undefined>('')
@guimochila
guimochila / auth.tsx
Created September 21, 2021 14:54
Authentication in React App - Typescript
import * as React from 'react';
import { client } from '../utils/httpClient';
type User = {
_id: string;
name: string;
email: string;
photo: string;
credits: number;
} | null;
@guimochila
guimochila / objectis.js
Created March 8, 2021 14:48
Object.is basic polyfill
if (!Object.is || true) {
Object.is = function ObjectIs(x, y) {
var xNegZero = isItNegZero(x);
var yNegZero = isItNegZero(y);
if(xNegZero || yNegZero) {
return xNegZero && yNegZero;
} else if (isItNaN(x) && isItNaN(y)) {
return true;
} else {
@guimochila
guimochila / providerCompose.js
Created October 21, 2019 15:10 — forked from stolinski/providerCompose.js
ProviderComposer
function ProviderComposer({ contexts, children }) {
return contexts.reduceRight(
(kids, parent) =>
React.cloneElement(parent, {
children: kids,
}),
children
);
}
// Private variable
var books = [];
// Private functions
// Sort the books in alphabetical order
function sortBooks() {
return books.sort((a, b) => (a > b ? 1 : -1));
}
// Public functions
@guimochila
guimochila / Webpack config sample
Created February 8, 2017 09:01
Webpack configuration sample
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProd = nodeEnv === 'production';
const sourcePath = path.join(__dirname, './client');
const staticsPath = path.join(__dirname, './static');
@guimochila
guimochila / chat.js
Created January 29, 2017 23:43
Refactored chat.js
var MyChatController = (function(io, $, moment, Mustache) {
var socket = io();
/*
appController - Main chat control
*/
var appCtrl = {
init: function() {
console.log('App started.');
uiCtrl.setupEventListeners();
networkCtrl.init();
@guimochila
guimochila / chat.js
Created January 28, 2017 18:52
Code to be re-written.
var socket = io();
function scrollToBottom() {
// Selectors
var messages = jQuery('#messages');
var newMessage = messages.children('li:last-child');
// Heights
var clientHeight = messages.prop('clientHeight');
var scrollTop = messages.prop('scrollTop');
var scrollHeight = messages.prop('scrollHeight');
@guimochila
guimochila / balanceparens.js
Last active January 28, 2017 17:33
Balancing Parens - How to balance parens using reduce.
function balanceParens(string) {
return !string.split('').reduce((previous, char) => {
if (previous < 0) { return previous; }
if (char === '(') { return ++previous; }
if (char === ')') { return --previous; }
return previous;
}, 0);
}
balanceParens(")((())())()(");