Skip to content

Instantly share code, notes, and snippets.

View Gaurav8757's full-sized avatar
🏠
Working from home

Gaurav Kumar Gaurav8757

🏠
Working from home
View GitHub Profile
@Gaurav8757
Gaurav8757 / forwardRef.js
Created February 12, 2026 06:15
forwardRef allows a parent component to send a ref to a child so the parent can directly access: a child DOM element or a child’s imperative methods.
// Child Component (Custom Input + Auto Focus)
// “forwardRef lets a parent component directly access a child’s DOM element or method through a ref,
// which is not possible with normal props.”
import React, { forwardRef } from "react";
const TextInput = forwardRef((props, ref) => {
return <input ref={ref} {...props} />;
});
@Gaurav8757
Gaurav8757 / RegexValidation.js
Created January 8, 2026 07:02
Regex Validation (Strong Patterns)
export const regexValidators = {
fullName: z
.string()
.regex(/^[a-zA-Z0-9_]{3,16}$/, "Only letters, numbers & underscore"),
phone: z
.string()
.regex(/^\+91\d{10}$/, "Phone must be +91XXXXXXXXXX"),
strongPassword: z
@Gaurav8757
Gaurav8757 / asyncValidation.js
Created January 8, 2026 06:59
ASYNC VALIDATION — Email Existence Check or other field you can add
import api from "./api"; // your axios instance
export const checkEmailExists = async (email) => {
try {
const res = await api.post("/auth/check-email", { email });
return res.data.exists; // true / false
} catch (e) {
return false;
}
};
@Gaurav8757
Gaurav8757 / QueryClientProvider.js
Last active January 2, 2026 07:43
Query Client Provider USE IN AS PROVIDER
// Important Notes
queryFn: ApiServices.getLandingPage(query) // ❌ executes immediately
queryFn: () => ApiServices.getLandingPage(query) // ✅ pass a function. / queryFn must be a function that returns a Promise
// React Query will call it internally, handle await, and track isLoading, isError, data automatically
// mutate fn
mutationFn: async (payload) => {
const res = await ApiServices.registerUserWithRoles(payload); // Or with extra logic:
@Gaurav8757
Gaurav8757 / useMutation.js
Last active January 2, 2026 07:05
useMutation from '@tanstack/react-query';
// apis/APIs.js
class ApiServices {
async updateProfile(data) {
try {
const res = await api.put("/user/update-profile", data);
return res?.data;
} catch (error) {
throw error.response?.data || error;
} finally {
console.log("updateProfile: FINALLY executed");
@Gaurav8757
Gaurav8757 / useQuery.js
Last active January 4, 2026 18:46
( useQuery | useSuspenseQuery ) from "@tanstack/react-query";
import { useQuery, useSuspenseQuery } from "@tanstack/react-query";
import ApiServices from "../../apis/APIs.js";
// api has try, catch, finally
class ApiServices {
async getLandingPage(query = {}) {
try {
const response = await api.get(`/user/dashboard`, {
params: query,
});
@Gaurav8757
Gaurav8757 / otp-next-prev.js
Created October 25, 2025 12:31
OTP NEXT-PREVIOUS CURSOR
<div class="otp-inputs">
<input type="text" class="text-box no-arrow otp" id="otp1" maxlength="1"
onkeydown="otpFocusHandler(event, 1)" autocomplete="off" />
<input type="text" class="text-box no-arrow otp" id="otp2" maxlength="1"
onkeydown="otpFocusHandler(event, 2)" autocomplete="off" />
<input type="text" class="text-box no-arrow otp" id="otp3" maxlength="1"
onkeydown="otpFocusHandler(event, 3)" autocomplete="off" />
<input type="text" class="text-box no-arrow otp" id="otp4" maxlength="1"
onkeydown="otpFocusHandler(event, 4)" autocomplete="off" />
</div>
@Gaurav8757
Gaurav8757 / access-refresh-token.js
Last active March 10, 2026 11:43
AccessToken vs RefreshToken
// services
// Verify OTP & issue tokens
export async function verifyOtp(mobile) {
// Find or create user
let user = await prisma.user.findUnique({ where: { mobile } });
// ✅ If not found, create user
// if (!user) {
// user = await prisma.user.create({
// data: {
// mobile,
@Gaurav8757
Gaurav8757 / Two_Date.jsx
Last active March 10, 2026 11:46
Date dd-MM-yyyy dikhe input fields mein but api pe dd/MM/yyyy jaye/save ho
// formatDate helper functions
const formatDate = (dateString, format = "dd/MM/yyyy") => {
if (!dateString) return ""; // Handle empty date
const date = new Date(dateString);
if (isNaN(date.getTime())) return "Invalid Date"; // Check if date is valid
const day = String(date.getDate()).padStart(2, "0");
const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are zero-based
const year = date.getFullYear();
@Gaurav8757
Gaurav8757 / CustomDataTable.jsx
Last active March 10, 2026 11:47
Custom Data Tables with responsive
// Backend Controller
export const viewAdvisorListing = async (req, res) => {
const {
advId,
page = 1,
limit = 150,
startDate,
endDate,
policyNo,
insuredName,