Skip to content

Instantly share code, notes, and snippets.

@swport
swport / trpc-mock.tsx
Last active October 13, 2023 06:01
Mock tRPC query and mutation calls using msw (works well with both vitest and jest)
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, type RenderOptions } from "@testing-library/react";
import { createTRPCReact, httpLink } from "@trpc/react-query";
import { createTRPCMsw } from "msw-trpc";
import { type ReactElement } from "react";
import superjson from "superjson";
import { type AppRouter } from "@/server/api/root";
const mockedTRPC = createTRPCReact<AppRouter>({
unstable_overrides: {
@swport
swport / .gitconfig
Last active February 1, 2023 06:03 — forked from digitaljhelms/.gitconfig
My personal ~/.gitconfig file
[user]
name = Sumit Wadhwa
email = digitaljhelms@gmail.com
[alias]
aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /' | sort # list all aliases
cb = !git branch | grep ^* | cut -d' ' -f2
branch-name = !git cb # alias to "cb" alias
st = status
ci = commit
cie = commit --allow-empty-message -m ''
@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();
@swport
swport / states_regions_provinces.php
Created January 21, 2021 13:57
States / Regions / Provinces List PHP Array
<?php
return array (
0 =>
array (
'id' => 3901,
'name' => 'Badakhshan',
'country_id' => 1,
'state_code' => 'BDS',
),
1 =>
@swport
swport / countries_list.php
Created January 21, 2021 13:53
Countries List PHP Array
<?php
return array (
0 =>
array (
'id' => 1,
'name' => 'Afghanistan',
'iso3' => 'AFG',
'iso2' => 'AF',
'phone_code' => '93',
'capital' => 'Kabul',
@swport
swport / woocommerce_payment_process_rest.php
Last active June 25, 2023 06:31
WordPress Rest API endpoint to process payments via WC
<?php
/*
* Suitable for NON-HOSTED payment gateways wallets, funds, etc.
*
* You can also process HOSTED payment gateways like paypal, but you'll get a rediect URL at the end
* and you have to handle that depending on what client (android, ios) you're catering to.
* You can have the redirect URL open up in a web-view-client and collect payment.
*
**/
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="de"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="de"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="de"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="de"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title></title>
@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'
* )
* );
@swport
swport / rebuild-cache.php
Created April 30, 2018 05:34 — forked from phpdreams/rebuild-cache.php
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']);