Merge branch 'main' of https://git.coastit.co.za/caitlin/CRM_ENVR
This commit is contained in:
@@ -12,7 +12,7 @@ import {
|
||||
setSessionContext,
|
||||
SESSION_COOKIE,
|
||||
} from "@/lib/auth"
|
||||
import { query } from "@/lib/db"
|
||||
|
||||
|
||||
function jsonResponse(data: unknown, status: number) {
|
||||
return new Response(JSON.stringify(data), {
|
||||
@@ -115,11 +115,6 @@ export async function POST(request: NextRequest) {
|
||||
true
|
||||
)
|
||||
|
||||
await query(
|
||||
`UPDATE users SET preferences = preferences || $2::jsonb WHERE id = $1`,
|
||||
[dbUser.id, JSON.stringify({ website_theme: "default" })],
|
||||
)
|
||||
|
||||
const token = await createSession(dbUser.id, dbUser.role_name)
|
||||
await setSessionContext(dbUser.id, ipAddress)
|
||||
|
||||
|
||||
@@ -8,12 +8,13 @@ export async function GET() {
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const result = await query(
|
||||
`SELECT preferences->>'website_theme' AS website_theme FROM users WHERE id = $1`,
|
||||
`SELECT preferences->>'website_theme' AS website_theme, preferences->>'color_theme' AS color_theme FROM users WHERE id = $1`,
|
||||
[user.id],
|
||||
)
|
||||
|
||||
const websiteTheme = result.rows[0]?.website_theme || "default"
|
||||
return NextResponse.json({ websiteTheme })
|
||||
const colorTheme = result.rows[0]?.color_theme || null
|
||||
return NextResponse.json({ websiteTheme, colorTheme })
|
||||
} catch (error) {
|
||||
console.error("Website theme GET error:", error)
|
||||
return NextResponse.json({ error: "Failed to load website theme" }, { status: 500 })
|
||||
@@ -26,14 +27,31 @@ export async function PUT(request: NextRequest) {
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const body = await request.json()
|
||||
const theme = body.websiteTheme || "default"
|
||||
const update: Record<string, string> = {}
|
||||
if (body.websiteTheme !== undefined) {
|
||||
update.website_theme = body.websiteTheme || "default"
|
||||
}
|
||||
if (body.colorTheme !== undefined) {
|
||||
update.color_theme = body.colorTheme || "default"
|
||||
}
|
||||
|
||||
await query(
|
||||
`UPDATE users SET preferences = preferences || $2::jsonb WHERE id = $1`,
|
||||
[user.id, JSON.stringify({ website_theme: theme })],
|
||||
if (Object.keys(update).length > 0) {
|
||||
await query(
|
||||
`UPDATE users SET preferences = preferences || $2::jsonb WHERE id = $1`,
|
||||
[user.id, JSON.stringify(update)],
|
||||
)
|
||||
}
|
||||
|
||||
const result = await query(
|
||||
`SELECT preferences->>'website_theme' AS website_theme, preferences->>'color_theme' AS color_theme FROM users WHERE id = $1`,
|
||||
[user.id],
|
||||
)
|
||||
|
||||
return NextResponse.json({ success: true, websiteTheme: theme })
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
websiteTheme: result.rows[0]?.website_theme || "default",
|
||||
colorTheme: result.rows[0]?.color_theme || null,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Website theme PUT error:", error)
|
||||
return NextResponse.json({ error: "Failed to save website theme" }, { status: 500 })
|
||||
|
||||
@@ -33,6 +33,11 @@ export default function RootLayout({
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<body className={`${inter.variable} min-h-screen antialiased`}>
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `(function(){try{var t=localStorage.getItem("crm-color-theme");if(t&&t!=="default")document.documentElement.classList.add(t);var w=localStorage.getItem("crm-website-theme");if(w==="spidey")document.documentElement.classList.add("theme-spidey")}catch(e){}})()`,
|
||||
}}
|
||||
/>
|
||||
<ThemeProvider attribute="class" defaultTheme="light" disableTransitionOnChange>
|
||||
<WebsiteThemeProvider>
|
||||
{children}
|
||||
|
||||
@@ -9,8 +9,6 @@ import { cn } from "@/lib/utils"
|
||||
import { Sun, Moon, Monitor, Palette, Shield } from "lucide-react"
|
||||
import { useWebsiteTheme } from "@/providers/website-theme-provider"
|
||||
|
||||
const COLOR_THEME_KEY = "crm-color-theme"
|
||||
|
||||
const modeOptions = [
|
||||
{ value: "light", icon: Sun, label: "Light" },
|
||||
{ value: "dark", icon: Moon, label: "Dark" },
|
||||
@@ -35,38 +33,15 @@ const backgroundOptions = [
|
||||
{ value: "spidey", label: "Spidey", icon: Shield, color: "bg-red-600", ring: "ring-red-600", desc: "Dark theme with red accents" },
|
||||
]
|
||||
|
||||
function getStoredColorTheme(): string {
|
||||
if (typeof window === "undefined") return "default"
|
||||
return localStorage.getItem(COLOR_THEME_KEY) || "default"
|
||||
}
|
||||
|
||||
function applyColorTheme(theme: string) {
|
||||
const colorThemes = ["default","ocean","forest","sunset","midnight","rose","amber","violet","slate","ruby"]
|
||||
const classes = document.documentElement.className.split(" ").filter(c => !colorThemes.includes(c))
|
||||
if (theme !== "default") {
|
||||
classes.push(theme)
|
||||
}
|
||||
document.documentElement.className = classes.join(" ").trim()
|
||||
localStorage.setItem(COLOR_THEME_KEY, theme)
|
||||
}
|
||||
|
||||
export function ThemeSettings() {
|
||||
const { theme, setTheme } = useTheme()
|
||||
const { websiteTheme, setWebsiteTheme } = useWebsiteTheme()
|
||||
const [colorTheme, setColorTheme] = useState("default")
|
||||
const { websiteTheme, setWebsiteTheme, colorTheme, setColorTheme } = useWebsiteTheme()
|
||||
const [mounted, setMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true)
|
||||
setColorTheme(getStoredColorTheme())
|
||||
applyColorTheme(getStoredColorTheme())
|
||||
}, [])
|
||||
|
||||
function handleColorChange(value: string) {
|
||||
setColorTheme(value)
|
||||
applyColorTheme(value)
|
||||
}
|
||||
|
||||
if (!mounted) return null
|
||||
|
||||
return (
|
||||
@@ -107,7 +82,7 @@ export function ThemeSettings() {
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<RadioGroup value={colorTheme} onValueChange={handleColorChange} className="grid grid-cols-5 gap-4">
|
||||
<RadioGroup value={colorTheme} onValueChange={setColorTheme} className="grid grid-cols-5 gap-4">
|
||||
{themeOptions.map(({ value, label, color, ring }) => (
|
||||
<div key={value}>
|
||||
<RadioGroupItem value={value} id={`color-${value}`} className="peer sr-only" />
|
||||
|
||||
@@ -3,14 +3,19 @@
|
||||
import { createContext, useContext, useState, useEffect, ReactNode, useCallback } from "react"
|
||||
|
||||
const WEBSITE_THEME_KEY = "crm-website-theme"
|
||||
const COLOR_THEME_KEY = "crm-color-theme"
|
||||
|
||||
const themeClasses: Record<string, string> = {
|
||||
spidey: "theme-spidey",
|
||||
}
|
||||
|
||||
const colorThemes = ["default","ocean","forest","sunset","midnight","rose","amber","violet","slate","ruby"]
|
||||
|
||||
interface WebsiteThemeContextValue {
|
||||
websiteTheme: string
|
||||
setWebsiteTheme: (theme: string) => Promise<void>
|
||||
colorTheme: string
|
||||
setColorTheme: (theme: string) => Promise<void>
|
||||
loading: boolean
|
||||
}
|
||||
|
||||
@@ -27,9 +32,19 @@ function applyWebsiteTheme(theme: string) {
|
||||
root.className = classes.join(" ").trim()
|
||||
}
|
||||
|
||||
function getStoredTheme(): string | null {
|
||||
function applyColorTheme(theme: string) {
|
||||
const root = document.documentElement
|
||||
const classes = root.className.split(" ").filter((c) => !colorThemes.includes(c))
|
||||
if (theme !== "default") {
|
||||
classes.push(theme)
|
||||
}
|
||||
root.className = classes.join(" ").trim()
|
||||
localStorage.setItem(COLOR_THEME_KEY, theme)
|
||||
}
|
||||
|
||||
function getStored(key: string): string | null {
|
||||
if (typeof window === "undefined") return null
|
||||
return localStorage.getItem(WEBSITE_THEME_KEY)
|
||||
return localStorage.getItem(key)
|
||||
}
|
||||
|
||||
function storeTheme(theme: string) {
|
||||
@@ -39,22 +54,40 @@ function storeTheme(theme: string) {
|
||||
|
||||
export function WebsiteThemeProvider({ children }: { children: ReactNode }) {
|
||||
const [websiteTheme, setWebsiteThemeState] = useState<string>("default")
|
||||
const [colorTheme, setColorThemeState] = useState<string>("default")
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/api/settings/website-theme")
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((data) => {
|
||||
const theme = data?.websiteTheme || "default"
|
||||
setWebsiteThemeState(theme)
|
||||
storeTheme(theme)
|
||||
applyWebsiteTheme(theme)
|
||||
const wt = data?.websiteTheme || "default"
|
||||
setWebsiteThemeState(wt)
|
||||
storeTheme(wt)
|
||||
applyWebsiteTheme(wt)
|
||||
|
||||
if (data?.colorTheme) {
|
||||
setColorThemeState(data.colorTheme)
|
||||
applyColorTheme(data.colorTheme)
|
||||
} else {
|
||||
const stored = getStored(COLOR_THEME_KEY)
|
||||
if (stored) {
|
||||
setColorThemeState(stored)
|
||||
applyColorTheme(stored)
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
const stored = getStoredTheme()
|
||||
const stored = getStored(WEBSITE_THEME_KEY)
|
||||
const theme = stored || "default"
|
||||
setWebsiteThemeState(theme)
|
||||
applyWebsiteTheme(theme)
|
||||
|
||||
const storedColor = getStored(COLOR_THEME_KEY)
|
||||
if (storedColor) {
|
||||
setColorThemeState(storedColor)
|
||||
applyColorTheme(storedColor)
|
||||
}
|
||||
})
|
||||
.finally(() => setLoading(false))
|
||||
}, [])
|
||||
@@ -74,8 +107,22 @@ export function WebsiteThemeProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const setColorTheme = useCallback(async (theme: string) => {
|
||||
setColorThemeState(theme)
|
||||
applyColorTheme(theme)
|
||||
try {
|
||||
await fetch("/api/settings/website-theme", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ colorTheme: theme }),
|
||||
})
|
||||
} catch {
|
||||
console.warn("Failed to persist color theme")
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<WebsiteThemeContext.Provider value={{ websiteTheme, setWebsiteTheme, loading }}>
|
||||
<WebsiteThemeContext.Provider value={{ websiteTheme, setWebsiteTheme, colorTheme, setColorTheme, loading }}>
|
||||
{children}
|
||||
</WebsiteThemeContext.Provider>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user