Skip to content

Instantly share code, notes, and snippets.

View bradyclifford's full-sized avatar

Brady Clifford bradyclifford

View GitHub Profile
@bradyclifford
bradyclifford / removeAllLocalStaleBranches.sh
Created February 12, 2018 00:02
Removes all local branches of which are stale
git fetch -p && for branch in `git branch -vv --no-color | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch;
@bradyclifford
bradyclifford / ServiceCollectionExtensions
Created May 2, 2018 18:40
.Net Core Service Collection
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Web.Extensions
{
public static class ServiceCollectionExtensions
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const UserAvatar = ({ user, size }) => (
<img
className={`user-avatar ${size || ""}`}
alt="user avatar"
src={user.avatar}
/>
@bradyclifford
bradyclifford / App.js
Last active June 27, 2019 15:02
Polling in React
import React, { useState, useEffect } from 'react';
import './App.css';
import { Route, Switch } from 'react-router';
import { Container, Breadcrumb, BreadcrumbItem, Button } from 'reactstrap';
import useFetch from './util/useFetch';
import { Home } from './routes/Home';
import { Buyer } from './routes/Buyer';
import { Seller } from './routes/Seller';
import { Demo } from './routes/Demo';
import { Scoreboard } from './components/Scoreboard';
@bradyclifford
bradyclifford / Test.js
Created February 2, 2020 00:30
JavaScript Date Assessment
/* Assessment:
* Native date creation -> Non-ISO will create date in current timezone
* Native date creation -> ISO will create date in GMT timezone
- When parsing "literal" ISO, parseISO & parseJSON are same - identical to native
* When parsing "short" ISO, parseISO converts ISO string yyyy/mm/dd to local timezone (adds time offset) unlike native
* formatISO should be used for converstion to date string yyyy/mm/dd? What happens when it is converted back into a DateTime? Assume UTC?
* DatePicker, push in a parseISO date or ISO string, outputs a date type with HST offset
*
*/
describe('Date: Timezones', () => {
@bradyclifford
bradyclifford / SessionProvider.js
Created June 22, 2020 07:29
React Context Provider
import React, { createContext, useContext, useState } from '~/session/react';
import Session from 'sessionModule';
import { resetSessionRequest } from 'session-client';
const SessionContext = createContext(Session.default());
SessionContext.displayName = 'Session';
function useSession(initialSession) {
const [session, setSession] = useState(initialSession);
@bradyclifford
bradyclifford / useFetch.js
Created November 4, 2020 21:28
Axios Cancelation Hook
import axios from 'axios';
import { useEffect } from 'react';
function useEffectAsync(callbackAsync, useEffectDeps) {
var source = axios.CancelToken.source();
useEffect(()=> {
callbackAsync(source.token)
.catch(error => {
if(!axios.isCancel(error)) {
throw error;
@bradyclifford
bradyclifford / useSafeState.js
Last active November 5, 2020 15:41
Safe Unmounted React Hooks
import { useState, useRef, useEffect, useCallback } from 'react';
/*
* useSafeState prevents state from being updated if a component has been
* unmounted during an async process, thus preventing React from throwing errors
* around it.
*
* Usage is just like regular useState:
* const [myState, setMyState] = useSafeState("Initial state");
*/
const getDomainWithoutSubdomain = hostname => {
const parts = hostname.split('.')
return parts
.slice(0)
.slice(-(parts.length === 4 ? 3 : 2))
.join('.')
}
@bradyclifford
bradyclifford / app-insights.ejs
Last active August 25, 2021 03:21
Application Insights
<% if (htmlWebpackPlugin.options.applicationInsightsInstrumentationKey) { %>
<!-- AppInsights -->
<!-- Intended to be blocking, don't defer or load async -->
<script type="text/javascript">
<%
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript
const {applicationInsightsInstrumentationKey: instrumentationKey, appInsights: props = {}} = htmlWebpackPlugin.options;
const { role, roleInstance, version, onInit, onAfterInit, cfg, ...rest} = props;