Skip to content

Instantly share code, notes, and snippets.

View MorenoMdz's full-sized avatar
🎯
Focusing

MorenoMdz

🎯
Focusing
  • Self
  • Florianópolis
View GitHub Profile
@MorenoMdz
MorenoMdz / enter-to-sumbit.js
Created May 29, 2021 02:14
Small method to listen for Enter key press
const Input = () => {
const handleKeyDown = (event) => {
if (event.key === 'Enter') {
console.log('do validate')
}
}
return <input type="text" onKeyDown={handleKeyDown} />
}
@MorenoMdz
MorenoMdz / discordHook.js
Created May 23, 2021 13:36
Firebase cloud function discord hook example
const sendDiscordMessage = async (type = 'info', name, message) => {
const webhook = require('webhook-discord');
const Hook = new webhook.Webhook(
'your-discord-hook-key'
);
if (type === 'info') {
return Hook.info(name, message);
}
if (type === 'warn') {
return Hook.warn(name, message);
@MorenoMdz
MorenoMdz / stopBilling.js
Created May 23, 2021 13:35
Example of firebase billing shutdown cloud function
// const admin = require('firebase-admin');
const { sendDiscordMessage } = require('./helpers');
const { google } = require('googleapis');
const { GoogleAuth } = require('google-auth-library');
const PROJECT_ID = 'project-name';
const PROJECT_NAME = `projects/${PROJECT_ID}`;
const billing = google.cloudbilling('v1').projects;
@MorenoMdz
MorenoMdz / firestoreLocalExport.sh
Created May 21, 2021 18:24
Bash script that will export your local firestore data and rename the previous data.
#!/bin/bash
# yea yea windows will require WSL
red=$(tput setaf 1)
green=$(tput setaf 2)
bg_blue=$(tput setab 4)
bg_cyan=$(tput setab 6)
reset=$(tput sgr0)
nl=$(tput il 1)
echo "${red}red text ${green}green text${reset}"
@MorenoMdz
MorenoMdz / batchWrapper.js
Last active July 21, 2023 10:55
Firebase Firestore batch more than 500 docs or operations
const batchWrapper = async (documentRef, action, update) => {
const batchArray = [];
batchArray.push(db.batch());
let operationCounter = 0;
let batchIndex = 0;
documentRef.forEach(doc => {
console.log('Org cleanup: deleting notifications', doc.id);
if (action === 'delete') {
batchArray[batchIndex].delete(doc.ref);
@MorenoMdz
MorenoMdz / reactfire_hook.js
Created March 16, 2021 22:41
Firebase ReactFire react hook example
import React, { useCallback } from 'react';
import { useFirestoreCollectionData, useFirestore } from 'reactfire';
function useProjects() {
// lazy load the Firestore SDK
const firestore = useFirestore()
const projectsRef = firestore.collection('projects')
// subscribe to the do throws a Promise for Suspense to catch,
// and then streams live updates
@MorenoMdz
MorenoMdz / fbconfig.js
Last active February 17, 2021 22:31
Firebase Local Emulator config file example
import firebase from "firebase/app";
import "firebase/auth";
import "firebase/functions";
import "firebase/firestore";
let config = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DB_URL,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
@MorenoMdz
MorenoMdz / setFromObjects.js
Created January 9, 2021 01:04
Create a Set from an Array of Objects Javascript
const users = redeem.data.map(item => item.employeeProfile);
const uniques = new Set(users.map(e => JSON.stringify(e)));
const uniqueUsers = Array.from(uniques).map(e => JSON.parse(e));
@MorenoMdz
MorenoMdz / useEffectCleanup.js
Last active January 5, 2021 16:13
React Firebase useEffect cleanup example
import React, { useState, useEffect, useRef, useCallback } from 'react';
const mountedRef = useRef(true);
const fetchData = useCallback(async () => {
try {
// do your async call here
const ref = await db
.collection('stuff')
.where('stuff', '==', id)
@MorenoMdz
MorenoMdz / clearFSCollection.js
Created October 19, 2020 14:59
Clear firestore collection from emulator
// Paste this in:
function clearCollection(path) {
const ref = firestore.collection(path)
ref.onSnapshot((snapshot) => {
snapshot.docs.forEach((doc) => {
ref.doc(doc.id).delete()
})
})
}
// Use it like this: