File size: 2,807 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react'
import { useAuth } from './AuthContext'
import { stateNameToCode } from '../utils/stateMapping'

interface LocationData {
  address?: string
  state: string
  county: string
  city: string
  school_board?: string
  latitude?: number
  longitude?: number
}

interface LocationContextType {
  location: LocationData | null
  setLocation: (location: LocationData) => void
  clearLocation: () => void
  hasLocation: boolean
}

const LocationContext = createContext<LocationContextType | undefined>(undefined)

export const LocationProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const { user, isAuthenticated } = useAuth()
  const [location, setLocationState] = useState<LocationData | null>(null)

  // Load location from user profile or localStorage
  useEffect(() => {
    if (isAuthenticated && user) {
      // Use location from user profile if available
      if (user.state && user.city) {
        // Migrate full state names to state codes
        const stateCode = stateNameToCode(user.state)
        
        setLocationState({
          state: stateCode,
          county: user.county || '',
          city: user.city,
          school_board: user.school_board,
        })
      }
    } else {
      // Load from localStorage for anonymous users
      const savedLocation = localStorage.getItem('user_location')
      if (savedLocation) {
        try {
          const parsed = JSON.parse(savedLocation)
          
          // Migrate full state names to state codes
          if (parsed.state) {
            parsed.state = stateNameToCode(parsed.state)
          }
          
          setLocationState(parsed)
          
          // Save back the migrated version
          localStorage.setItem('user_location', JSON.stringify(parsed))
        } catch (e) {
          console.error('Failed to parse saved location:', e)
        }
      }
    }
  }, [user, isAuthenticated])

  const setLocation = (newLocation: LocationData) => {
    setLocationState(newLocation)
    
    // Save to localStorage for anonymous users
    if (!isAuthenticated) {
      localStorage.setItem('user_location', JSON.stringify(newLocation))
    }
  }

  const clearLocation = () => {
    setLocationState(null)
    localStorage.removeItem('user_location')
  }

  const hasLocation = location !== null && !!location.state && !!location.city

  return (
    <LocationContext.Provider value={{ location, setLocation, clearLocation, hasLocation }}>
      {children}
    </LocationContext.Provider>
  )
}

export const useLocation = () => {
  const context = useContext(LocationContext)
  if (context === undefined) {
    throw new Error('useLocation must be used within a LocationProvider')
  }
  return context
}