Skip to content

Instantly share code, notes, and snippets.

View rohailtaha's full-sized avatar
💥
Focusing

Rohail Taha rohailtaha

💥
Focusing
View GitHub Profile
@rohailtaha
rohailtaha / 1_subscribe-button.jsx
Last active March 16, 2024 10:08
Stripe subscriptions in Nextjs/Nodejs with React
'use client'
import { SUBSCRIPTION_PLAN_NAME } from '@/types/types';
import { loadStripe } from '@stripe/stripe-js';
type SubscribeButtonProps = {
subscriptionPlanName: SUBSCRIPTION_PLAN_NAME;
};
function SubscribeButton({ subscriptionPlanName }: SubscribeButtonProps) {
@rohailtaha
rohailtaha / firestore-reusable-methods.ts
Created August 11, 2023 09:49
Firestore Reusable Methods
// max operations that can be performed in a batch
export const MAX_BATCH_SIZE = 500
export async function addDoc(collectionRef: string, data: Record<any, any>) {
const database = getFirestoreDatabase()
return await addDocFirebase(collection(database, collectionRef), data)
}
export async function setDoc(docRef: string, data: Record<any, any>) {
@rohailtaha
rohailtaha / socket.io-methods.js
Created February 21, 2022 19:27
Socket.io related stuff
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
@rohailtaha
rohailtaha / apollo-server-micro-2.js
Last active January 22, 2022 13:30
GraphQL related code
// Heres how to start micro server without running "npx micro" command:-
// just run node <filename> to execute the file:
const { ApolloServer } = require('apollo-server-micro');
const typeDefs = require('./type-defs');
const resolvers = require('./resolvers');
const micro = require('micro');
const apolloServer = new ApolloServer({ typeDefs, resolvers });
const startingServer = apolloServer.start();
import { useSession } from "next-auth/react"
import { useRouter } from "next/router"
// It only renders the children if user is authenticated, otherwise it will redirect to
// redirect Url.
function Auth({ children, waitContent = null, redirectUrl }) {
const { data: session, status } = useSession()
const loading = status === "loading"
const router = useRouter()
@rohailtaha
rohailtaha / useDocumentClickListener.js
Last active April 1, 2022 12:51
Custom hooks for react
export function useDocumentClickListener(eventListener) {
useEffect(() => {
document.addEventListener("click", eventListener)
return () => document.removeEventListener("click", eventListener)
}, [eventListener])
return null
}
@rohailtaha
rohailtaha / products.json
Created January 16, 2022 05:33
A gist for fake json data of all kinds
{
"products": [
{
"id": 1,
"title": "Product 1",
"price": "1000",
"desc": "This is product 1. Very good product. It is very easy to use."
},
{
"id": 2,
@rohailtaha
rohailtaha / NextPageButton.js
Last active December 20, 2021 07:41
React | Redux Pagination
import { useDispatch } from 'react-redux';
import { set_current_page } from '../../../../../actions/pagination/pagination-actions';
export default function NextPageButton({ currentPage, disabled }) {
const dispatch = useDispatch();
return (
<li>
<button
className={`page${
@rohailtaha
rohailtaha / NextPageButton.js
Created November 12, 2021 16:07
Pagination in React
function NextPageButton({ updateCurrentPage, currentPage, isDisabled }) {
const handleClick = () => updateCurrentPage(currentPage + 1);
return (
<li>
<button
className={`page${isDisabled ? '' : ' page--clickable'}`}
onClick={handleClick}
disabled={isDisabled}
style={{ cursor: `${isDisabled ? 'auto' : 'pointer'}` }}
@rohailtaha
rohailtaha / data.js
Last active March 16, 2024 11:33
Reusable javascript code snippets
export const MONTHS: Record<number, string> = {
0: 'January',
1: 'February',
2: 'March',
3: 'April',
4: 'May',
5: 'June',
6: 'July',
7: 'August',
8: 'September',