Skip to content

Instantly share code, notes, and snippets.

@EvanBurbidge
EvanBurbidge / switch.js
Created November 24, 2022 08:48
examples of switch in react
// Class based
class MyComponent {
renderSwitch(param) {
switch(param) {
case 'foo':
return 'bar';
default:
return 'foo';
}
}
import { awaitWrap } from 'await-wrapper';
const fakePromise = () => new Promise(resolve => setTimeout(resolve("hi")), 500);
const fakePromiseReject = () => new Promise((__, reject) => setTimeout(reject("no")), 500);
const resolveCallback = response => {
// do something with response
}
const rejectCallback = error => {
// do something with error
@EvanBurbidge
EvanBurbidge / awaitWrap.js
Created April 22, 2022 10:15
Await wrapper example
import { awaitWrap } from 'await-wrapper';
const fakePromise = () => new Promise(resolve => setTimeout(resolve("hi")), 500);
const fakePromiseReject = () => new Promise((__, reject) => setTimeout(reject("no")), 500);
const fakePromiseHandler = async () => {
const { error, data } = await awaitWrap(fakePromise()); // { error: null, data: "hi" };
error ? console.error(error) : console.log(data);
}
@EvanBurbidge
EvanBurbidge / hi.js
Last active January 19, 2022 09:45
An automated selenium script for getting hi tokens
const { Builder, By } = require('selenium-webdriver');
(async function hi() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('https://web.hi.com');
const areaCode = await driver.findElement(By.css('.areaCode'));
await areaCode.click();
await driver.sleep(500);
const search = await driver.findElement(By.css('.searchs .uni-input-wrapper .uni-input-input'));
const cache = {};
function changePhonesStatus(phones = {}, unavailable = []) {
const key = JSON.stringify(unavailable);
// generates a string array e.g. "[1,2,3]"
if (cache[key]) {
return cache[key];
}
// we need to look at each department and update the phones for that department.
const updatedPhones = Object.keys(phones.departments).reduce((acc, key) => {
const subscribeToPHoneData = async () => {
// fetch the phones url
const phones = await getData(PHONES_URL);
const phoneAvailabilityCheck = setInterval(async () => {
const { unavailableIds } = await getData(AVAILABILITY_URL);
changePhonesStatus(phones, unavailable);
// should return an object with the updated phone system
/*
if(someCondition) { clearInterval(phoneAvailabilityCheck) }
*/
@EvanBurbidge
EvanBurbidge / Logout.js
Created December 9, 2021 15:24
Logout script expo client auth0
import React from 'react';
import jwtDecode from 'jwt-decode';
import * as AuthSession from 'expo-auth-session';
import { openAuthSessionAsync } from 'expo-web-browser';
import { Alert, Button, Platform, StyleSheet, Text, View } from 'react-native';
const auth0ClientId = "";
const authorizationEndpoint = "https://youraccount.eu.auth0.com/v2/logout";
import * as AuthSession from 'expo-auth-session';
import jwtDecode from 'jwt-decode';
import { Alert, Platform, StyleSheet, Image } from 'react-native';
// you need to swap out these details with your auth0 credientals
const auth0ClientId = "";
const authorizationEndpoint = "https://yourtennant.auth0.com/authorize";
const useProxy = Platform.select({ web: false, default: true });
@EvanBurbidge
EvanBurbidge / Login.js
Created December 5, 2021 16:24
Example of auth0 login
import React from 'react';
import { StyleSheet, Text, View, Button, Alert } from 'react-native';
import { AuthSession } from 'expo';
import jwtDecode from 'jwt-decode';
/*
You need to swap out the Auth0 client id and domain with
the one from your Auth0 client.
In your Auth0 client, you need to also add a url to your authorized redirect urls.
For this application, I added https://auth.expo.io/@arielweinberger/auth0-example because I am
@EvanBurbidge
EvanBurbidge / persistedHookState.js
Created November 17, 2021 15:21
An example of React hooks using a single data source with React context.
import { useState, createContext, useContext, useEffect } from "react";
import "./styles.css";
const AuthContext = createContext({});
const useAuth = () => useContext(AuthContext);
const AuthProvider = ({ children }) => {
const [username, setUsername] = useState("");
const handleUpdateUsername = ({ target: { value } }) => setUsername(value);