All files / src/store user-store.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 11/11
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                                                                            15x   15x                   5x   6x 4x 4x 4x 4x     33x 33x 2x         4x 4x 3x                           49x                     83x              
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
 
export interface UserProfile {
  id: string;
  name: string;
  email: string;
  phone?: string;
  nationality?: string;
  username: string;
  avatar?: string | null;
}
 
export interface UserPreferences {
  theme: 'light' | 'dark' | 'system';
  language: string;
  timezone: string;
  currency: string;
  notifications: {
    email: boolean;
    push: boolean;
    sms: boolean;
  };
}
 
export interface UserState {
  // State
  profile: UserProfile | null;
  preferences: UserPreferences | null;
  isProfileLoading: boolean;
  isPreferencesLoading: boolean;
  profileError: string | null;
  preferencesError: string | null;
 
  // Actions
  setProfile: (profile: UserProfile) => void;
  setPreferences: (preferences: UserPreferences) => void;
  setProfileLoading: (loading: boolean) => void;
  setPreferencesLoading: (loading: boolean) => void;
  setProfileError: (error: string | null) => void;
  setPreferencesError: (error: string | null) => void;
  updateProfile: (updates: Partial<UserProfile>) => void;
  updatePreferences: (updates: Partial<UserPreferences>) => void;
  clearUserData: () => void;
}
 
export const useUserStore = create<UserState>()(
  persist(
    (set, get) => ({
      // Initial state
      profile: null,
      preferences: null,
      isProfileLoading: false,
      isPreferencesLoading: false,
      profileError: null,
      preferencesError: null,
 
      // Actions
      setProfile: profile => set({ profile, profileError: null }),
      setPreferences: preferences =>
        set({ preferences, preferencesError: null }),
      setProfileLoading: loading => set({ isProfileLoading: loading }),
      setPreferencesLoading: loading => set({ isPreferencesLoading: loading }),
      setProfileError: error => set({ profileError: error }),
      setPreferencesError: error => set({ preferencesError: error }),
 
      updateProfile: updates => {
        const currentProfile = get().profile;
        if (currentProfile) {
          set({ profile: { ...currentProfile, ...updates } });
        }
      },
 
      updatePreferences: updates => {
        const currentPreferences = get().preferences;
        if (currentPreferences) {
          set({
            preferences: {
              ...currentPreferences,
              ...updates,
              notifications: {
                ...currentPreferences.notifications,
                ...(updates.notifications || {}),
              },
            },
          });
        }
      },
 
      clearUserData: () =>
        set({
          profile: null,
          preferences: null,
          profileError: null,
          preferencesError: null,
          isProfileLoading: false,
          isPreferencesLoading: false,
        }),
    }),
    {
      name: 'user-storage',
      partialize: state => ({
        profile: state.profile,
        preferences: state.preferences,
      }),
    }
  )
);