This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.tsx
208 lines (192 loc) · 6.07 KB
/
dashboard.tsx
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import Head from 'next/head'
import React, { useState } from 'react'
import { tabs } from '../src/types/navigation'
import MobileSidebar from '../src/components/dashboard/MobileSidebar'
import DesktopSidebar from '../src/components/dashboard/DesktopSidebar'
import TopBar from '../src/components/dashboard/TopBar'
import DashboardPanel from '../src/components/dashboard/panels/DashboardPanel'
import OrderPanel from '../src/components/dashboard/panels/OrderPanel'
import ProductPanel from '../src/components/dashboard/panels/ProductPanel'
import CategoryPanel from '../src/components/dashboard/panels/CategoryPanel'
import ColorPanel from '../src/components/dashboard/panels/ColorPanel'
import SizePanel from '../src/components/dashboard/panels/SizePanel'
import type { OveractUser } from '../src/types/OveractUser'
import { SessionContextProvider } from '@supabase/auth-helpers-react'
import { createClient } from '@supabase/supabase-js';
import { useColors } from '../src/graphql/queries/Color/colors'
import { useSizes } from '../src/graphql/queries/Size/sizes'
import { unstable_getServerSession } from 'next-auth'
import { authOptions } from './api/auth/[...nextauth]'
/**
* API data fetched from the server.
*/
interface DashboardProps {
/**
* GraphQL API URL.
*/
apiUrl: string;
/**
* Base URL to Supabase Storage.
*/
cdnUrl: string;
/**
* Supabase `anon` role API key.
*/
cdnAnonKey: string;
/**
* NextAuth user.
*/
user: OveractUser;
}
/**
* Validates admin user session and returns API data from the server.
* @param ctx Call context.
* @returns `Promise<DashboardProps>` or a redirection
*/
export async function getServerSideProps(ctx: any) {
// page protection - redirect to 404 page
const ses = await unstable_getServerSession(ctx.req, ctx.res, authOptions);
const user = ses?.user as OveractUser;
if(!ses || !user || user.role !== 'ADMIN') {
return {
redirect: {
destination: '/404',
permanent: false,
},
}
}
// build API url (client-side apiUrl is /)
const host = ctx.req.headers['host'];
const apiUrl = host !== '/'
// server query
? `http://${host}/api/graphql`
// client query
: `${host}api/graphql`;
const cdnUrl = process.env.NEXT_PUBLIC_SUPABASE_URL as string;
const cdnAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY as string;
return {
props: {
apiUrl,
cdnUrl,
cdnAnonKey,
user,
} as DashboardProps,
}
}
export default function DashboardPage({ apiUrl, cdnUrl, cdnAnonKey, user }: DashboardProps) {
/**
* Dashboard-scoped Supabase client for storage API access.
*/
const supabaseClient = createClient(cdnUrl, cdnAnonKey);
// colors
const { isLoading: isColorsLoading, data: colors } = useColors(apiUrl);
// sizes
const { isLoading: isSizesLoading, data: sizes } = useSizes(apiUrl);
// UI states
const [sidebarOpen, setSidebarOpen] = useState(false);
const [activeTab, setActiveTab] = useState(tabs.dashboard);
const [addCategoryModalOpen, setAddCategoryModalOpen] = useState(false);
const [addProductModalOpen, setAddProductModalOpen] = useState(false);
const [addColorModalOpen, setAddColorModalOpen] = useState(false);
const [addSizeModalOpen, setAddSizeModalOpen] = useState(false);
if(isColorsLoading || isSizesLoading) {
return (
<>
<Head>
<title>Dashboard | Overact</title>
</Head>
<div>
Data loading...
</div>
</>
)
}
return (
<SessionContextProvider
supabaseClient={supabaseClient}
>
<div>
<Head>
<title>Dashboard | Overact</title>
</Head>
{/* Closeable sidebar for mobile */}
<MobileSidebar
sidebarOpen={sidebarOpen}
setSidebarOpen={setSidebarOpen}
activeTab={activeTab}
setActiveTab={setActiveTab}
/>
{/* Static sidebar for desktop */}
<DesktopSidebar
sidebarOpen={sidebarOpen}
setSidebarOpen={setSidebarOpen}
activeTab={activeTab}
setActiveTab={setActiveTab}
/>
{/* Top bar and a panel */}
<div className="md:pl-64 flex flex-col">
<TopBar
username={user.name as string}
setSidebarOpen={setSidebarOpen}
/>
{/* Panels */}
<main className="flex-1">
{ activeTab == tabs.dashboard ? <DashboardPanel /> : <div/> }
{ activeTab == tabs.orders ? <OrderPanel /> : <div/> }
{
activeTab == tabs.products
?
<ProductPanel
colors={colors?.colors ?? []}
sizes={sizes?.sizes ?? []}
modalOpen={addProductModalOpen}
setModalOpen={setAddProductModalOpen}
storageUrl={cdnUrl}
apiUrl={apiUrl}
/>
:
<div/>
}
{
activeTab == tabs.categories
?
<CategoryPanel
modalOpen={addCategoryModalOpen}
setModalOpen={setAddCategoryModalOpen}
apiUrl={apiUrl}
/>
:
<div/>
}
{
activeTab == tabs.colors
?
<ColorPanel
colors={colors?.colors ?? []}
isLoading={isColorsLoading}
modalOpen={addColorModalOpen}
setModalOpen={setAddColorModalOpen}
apiUrl={apiUrl}
/>
:
<div/>
}
{
activeTab == tabs.sizes
?
<SizePanel
sizes={sizes?.sizes ?? []}
isLoading={isSizesLoading}
modalOpen={addSizeModalOpen}
setModalOpen={setAddSizeModalOpen}
apiUrl={apiUrl}
/>
:
<div/>
}
</main>
</div>
</div>
</SessionContextProvider>
);
}