typescript
import { createContext } from 'react';

export type ToastVariant = 'success' | 'error' | 'info';

export interface Toast {
  id: number;

Imperative Toast Notification System with React Context and Auto-Dismiss

react toast context
by codesnips 4 tabs
php
<?php

namespace App\Security\Voter;

use App\Entity\Comment;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

Enforce Comment Ownership With a Symfony Voter and Controller Authorization Check

symfony security voter
by codesnips 3 tabs
javascript
export const required = (msg = 'This field is required') => (value) =>
  value && value.trim() !== '' ? '' : msg;

export const minLength = (n, msg) => (value) =>
  value && value.length >= n ? '' : msg || `Must be at least ${n} characters`;

Field-by-Field Signup Validation with a Reusable useForm Hook in React

react hooks forms
by codesnips 4 tabs
ruby
class User < ApplicationRecord
  has_many :posts
  has_many :comments

  scope :digest_subscribers, -> {
    where(digest_opt_in: true).where.not(confirmed_at: nil)

Weekly Digest Emails in Rails with a Cron Job and Query Scopes

rails activejob sidekiq-cron
by codesnips 4 tabs
python
from datetime import datetime

from pydantic import BaseModel


class UserV1(BaseModel):

Versioning a FastAPI API With APIRouter Mounted Under /v1 and /v2 Prefixes

fastapi api-versioning apirouter
by codesnips 4 tabs
java
package com.example.health;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;

Custom Spring Boot Actuator Health Indicators for Database and Disk

spring-boot actuator health-check
by codesnips 3 tabs
rust
use std::fs;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct Migration {
    pub version: i64,

Sequential Schema Migrations With Version Tracking in Rust and Postgres

migrations postgres sql
by codesnips 3 tabs
typescript
import { Injectable, computed, signal } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class LoadingService {
  private readonly activeRequests = signal(0);

Global Loading Spinner via In-Flight Request Tracking in an Angular HttpInterceptor

angular http-interceptor rxjs
by codesnips 4 tabs
php
<?php

use App\Http\Controllers\CommentController;
use Illuminate\Support\Facades\Route;

// The ->scoped() call forces {comment} to resolve through $post->comments,

Scoped Route-Model Binding for Nested Resources in Laravel

laravel routing route-model-binding
by codesnips 3 tabs
go
package limiter

import (
	"context"
	"errors"
)

Leaky-Bucket Concurrency Limiter with a Buffered Semaphore Channel in Go

go concurrency rate-limiting
by codesnips 3 tabs
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Building Filterable, Sortable Product Listings with Laravel Query Scopes

laravel eloquent query-scopes
by codesnips 3 tabs
ruby
require "csv"

class ProductImporter
  BATCH_SIZE = 500

  attr_reader :errors, :inserted

Bulk-Insert a Product CSV in Rails with a Service Object and upsert_all

rails csv bulk-insert
by codesnips 3 tabs