Skip to content
generate-store-action logo

Generate Store Action

generate-store-action

Generate Zustand async action with loading/error states and toast notifications. Use when adding new API calls to the health store.

SKILL.md

Full skill instructions

Generate Store Action

Generate a Zustand async action for the health store with loading/​error state management.

Usage

When user requests to add a store action, ask for:

  1. Action name (e.g., "fetchWaterLogs", "addSleepLog", "updateWeight")
  2. API endpoint (e.g., "/​api/​water-intake", "/​api/​sleep")
  3. HTTP method (GET, POST, PUT, DELETE)
  4. Request/​response types
  5. State updates needed (what gets stored in Zustand)

Implementation Pattern

Based on src/​lib/​store/​healthStore.ts existing patterns.

Pattern Structure

Add to existing store in src/​lib/​store/​healthStore.ts:

// In HealthState interface:
interface HealthState {
  // ... existing state ...
  waterLogs: WaterLog[];

  // ... existing actions ...
  fetchWaterLogs: (date: string) => Promise<void>;
  addWaterLog: (log: Omit<WaterLog, 'id' | 'createdAt'>) => Promise<void>;
  deleteWaterLog: (id: string) => Promise<void>;
}

// In the store creation:
export const useHealthStore = create<HealthState>((set, get) => ({
  // ... existing state ...
  waterLogs: [],

  // ... existing actions ...

  fetchWaterLogs: async (date: string) => {
    set({ isLoading: true, error: null });
    try {
      const response = await fetch(`/​api/​water-intake?date=${date}`);
      if (!response.ok) throw new Error('Failed to fetch water logs');
      const data = await response.json();
      set({ waterLogs: data, isLoading: false });
    } catch (err: any) {
      set({ error: err.message, isLoading: false });
      toast.error(err.message || 'Failed to fetch water logs');
    }
  },

  addWaterLog: async (log: Omit<WaterLog, 'id' | 'createdAt'>) => {
    set({ isLoading: true, error: null });
    try {
      const response = await fetch('/​api/​water-intake', {
        method: 'POST',
        headers: { 'Content-Type': 'application/​json' },
        body: JSON.stringify(log),
      });
      if (!response.ok) throw new Error('Failed to add water log');
      const newLog = await response.json();
      set({
        waterLogs: [...get().waterLogs, newLog],
        isLoading: false,
      });
      toast.success('Water intake logged');
    } catch (err: any) {
      set({ error: err.message, isLoading: false });
      toast.error(err.message || 'Failed to add water log');
    }
  },

  deleteWaterLog: async (id: string) => {
    set({ isLoading: true, error: null });
    try {
      const response = await fetch(`/​api/​water-intake?id=${id}`, {
        method: 'DELETE',
      });
      if (!response.ok) throw new Error('Failed to delete water log');
      set({
        waterLogs: get().waterLogs.filter((log) => log.id !== id),
        isLoading: false,
      });
      toast.success('Water log deleted');
    } catch (err: any) {
      set({ error: err.message, isLoading: false });
      toast.error(err.message || 'Failed to delete water log');
    }
  },
}));

Key Conventions

  • Always wrap in set({ isLoading: true, error: null })
  • Use await fetch() with proper method and headers
  • Check !response.ok to handle HTTP errors
  • Parse response with await response.json()
  • Use get() to access current state in actions
  • Update state with set() including isLoading: false
  • Call toast.success() on success
  • Call toast.error() on failure
  • Include error message in toast: err.message || 'Default message'
  • For arrays: use spread operator to create new array
  • State updates are immutable (don't mutate existing state)

Steps

  1. Ask user for action name, API endpoint, method, and types
  2. Open src/​lib/​store/​healthStore.ts
  3. Add action type to HealthState interface
  4. Add initial state if needed (empty array, null, etc.)
  5. Implement action function following the pattern above
  6. Include proper error handling and toast notifications
  7. Format with Prettier

Implementation Checklist

  • Action added to HealthState interface
  • Initial state added if new data field
  • Async action with try-catch
  • Loading state managed (set/​unset)
  • Error state managed
  • API endpoint correct
  • HTTP method correct
  • Response validation (!response.ok)
  • Toast notifications added
  • State updates are immutable
  • get() used to access current state

More Other skills

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View

Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.

179.7K 100.1K
View

Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.

179.7K 89.5K
View

A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).

179.7K 76.1K
View

Fundamental design principles for writing maintainable, testable Python code.

40.2K 21.9K
View

Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.

40.2K 20K
View

Generate Playwright tests from scenarios using interactive browser exploration and validation.

39.7K 18.2K
View

Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.

40.2K 14.7K
View

GDPR-compliant data handling with consent management, data subject rights, and privacy controls.

40.2K 14.3K
View

Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.

40.2K 13.5K
View

Detailed multi-file refactor planning with safety checks and rollback guidance.

39.7K 13K
View