Skip to content

Instantly share code, notes, and snippets.

@swport
swport / useAxiosLoader.ts
Last active February 25, 2023 19:21
Axios react-hook to know how many network requests are currently running
import axios from 'axios';
const addToast = (message: string, type: 'success' | 'error') => {
/// add logic to show toast
};
// hook to know if there are network requests currently running
// and how many are running
export const useAxiosLoader = () => {
const [counter, setCounter] = React.useState(0);
@swport
swport / cart_context_reactjs.js
Last active June 6, 2021 07:53
Cart Context in ReactJS
import React, {
createContext,
useEffect,
useReducer
} from 'react';
const lStorage = require('store'); // store.js library
export const CartContext = createContext();
@astoilkov
astoilkov / readme.md
Last active November 16, 2024 12:52
Async Operations with useReducer Hook

Async Operations with useReducer Hook

9 March, 2019

We were discussing with @erusev what we can do with async operation when using useReducer() in our application. Our app is simple and we don't want to use a state management library. All our requirements are satisfied with using one root useReducer(). The problem we are facing and don't know how to solve is async operations.

In a discussion with Dan Abramov he recommends Solution 3 but points out that things are fresh with hooks and there could be better ways of handling the problem.

Problem

@swport
swport / dependencyResolver.php
Last active October 24, 2018 08:41
Simple Dependency Resolver in PHP. Dependency Autowiring.
<?php
/*
* USAGE:
*
* // this is where I define all my dependencies in case if they are not directly instantiable (resolvable)
* $deps = array(
* 'DependentClass' => array(
* 'depclass' => 'DependencyClass'
* )
* );
@phpdreams
phpdreams / rebuild-cache.php
Created April 28, 2018 00:17
Speaker List Cache Generation
<?php
// this will read in all the speakers, sort them by date, then store in a file for use later
$speakerList = [
'speaker-file',
];
$speakers_all = [];
foreach($speakerList as $speaker) {
$speakers_all[$speaker] = include_once("./speakers/{$speaker}.php");
$speakers_all[$speaker]['bullet-points'] = text2bullets($speakers_all[$speaker]['bullet-points']);
@heldervilela
heldervilela / woo-tabs.php
Created December 19, 2017 21:49
WooCommerce Admin Custom Product Data Tab
// First Register the Tab by hooking into the 'woocommerce_product_data_tabs' filter
add_filter( 'woocommerce_product_data_tabs', 'add_my_custom_product_data_tab' );
function add_my_custom_product_data_tab( $product_data_tabs ) {
$product_data_tabs['my-custom-tab'] = array(
'label' => __( 'My Custom Tab', 'woocommerce' ),
'target' => 'my_custom_product_data',
'class' => array( 'show_if_simple' ),
);
return $product_data_tabs;
}
@sators
sators / connect.php
Last active February 4, 2025 03:24
PHP MySQLi Amazon Aurora RDS EC2 IAM Role Based Authentication
<?php
/********* CONFIG ********/
$clusterEndpoint = "";
$clusterPort = 3306;
$clusterRegion = "us-east-1";
$dbUsername = "";
$dbDatabase = "";
/*************************/
@crittermike
crittermike / wget.sh
Last active March 28, 2025 18:44
Download an entire website with wget, along with assets.
# One liner
wget --recursive --page-requisites --adjust-extension --span-hosts --convert-links --restrict-file-names=windows --domains yoursite.com --no-parent yoursite.com
# Explained
wget \
--recursive \ # Download the whole site.
--page-requisites \ # Get all assets/elements (CSS/JS/images).
--adjust-extension \ # Save files with .html on the end.
--span-hosts \ # Include necessary assets from offsite as well.
--convert-links \ # Update links to still work in the static version.
@mburakerman
mburakerman / package.json
Last active September 26, 2022 17:32
Webpack 4 config.js (SCSS to CSS and Babel) 👌 The Simplest Usage 👌
{
"name": "webpack-sass",
"version": "1.0.0",
"scripts": {
"start": "webpack-dev-server --open --mode development",
"build": "webpack -p"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
@ckressibucher
ckressibucher / cliserver.php
Last active August 27, 2024 12:44
Router script for PHP built in server
<?php
// public/cliserver.php (router script)
if (php_sapi_name() !== 'cli-server') {
die('this is only for the php development server');
}
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$_SERVER['SCRIPT_NAME'])) {
// probably a static file...
return false;