Skip to content

Commit 430966f

Browse files
committed
refactor: admin editors component
1 parent 20419ad commit 430966f

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist-ssr
66
*.local
77
.env
88
.next
9+
.DS_Store
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { useEffect, useState } from 'react';
2+
import { ref, onValue, DataSnapshot } from '@firebase/database';
3+
import { db } from '@/lib/firebase';
4+
import { TextWidgetEditor } from '@/components/TextWidget';
5+
import { TimeWidgetEditor } from '@/components/TimeWidget';
6+
import { IFrameWidgetEditor } from '@/components/IFrameWidget';
7+
8+
const EditorMap = {
9+
text: TextWidgetEditor,
10+
time: TimeWidgetEditor,
11+
iframe: IFrameWidgetEditor,
12+
};
13+
14+
type Widget = {
15+
name: string;
16+
}
17+
18+
type WidgetList = { [key: string]: Widget }
19+
20+
type EditorsProps = {
21+
profile: string;
22+
};
23+
24+
const Editors = ({ profile }: EditorsProps) => {
25+
const [widgets, setWidgets] = useState<WidgetList>({});
26+
27+
useEffect(() => {
28+
const widgetsRef = ref(db, `/profiles/${profile}/widgets`);
29+
onValue(widgetsRef, (snap: DataSnapshot) => {
30+
setWidgets(snap.val() || {});
31+
});
32+
}, [profile]);
33+
34+
return (
35+
<div>
36+
{
37+
Object.keys(widgets).map((id) => {
38+
const widget: any = widgets[id];
39+
const Editor = EditorMap[widget.name];
40+
return <Editor key={`${profile}-${id}`} id={id} profile={profile} />
41+
})
42+
}
43+
</div>
44+
);
45+
};
46+
47+
export { Editors };

src/pages/admin/[id]/index.tsx

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,11 @@ import {
77
Box,
88
} from '@mui/material';
99
import { User } from '@firebase/auth';
10-
import { ref, onValue, DataSnapshot } from '@firebase/database';
1110
import { AuthProvider } from '@/lib/AuthProvider';
12-
import { auth, db } from '@/lib/firebase';
11+
import { auth } from '@/lib/firebase';
1312
import { Signin } from '@/components/admin/signin';
14-
import { TextWidgetEditor } from '@/components/TextWidget';
15-
import { TimeWidgetEditor } from '@/components/TimeWidget';
16-
import { IFrameWidgetEditor } from '@/components/IFrameWidget';
1713
import { Navbar } from '@/components/admin/Navbar';
18-
19-
const Editors = {
20-
text: TextWidgetEditor,
21-
time: TimeWidgetEditor,
22-
iframe: IFrameWidgetEditor,
23-
};
24-
25-
type Widget = {
26-
name: string;
27-
}
28-
29-
type WidgetList = { [key: string]: Widget }
30-
31-
const Widgets = ({ profile }: { profile: string }) => {
32-
const [widgets, setWidgets] = useState<WidgetList>({});
33-
34-
useEffect(() => {
35-
const widgetsRef = ref(db, `/profiles/${profile}/widgets`);
36-
onValue(widgetsRef, (snap: DataSnapshot) => {
37-
setWidgets(snap.val() || {});
38-
});
39-
}, [profile]);
40-
41-
return (
42-
<div>
43-
{
44-
Object.keys(widgets).map((id) => {
45-
const widget: any = widgets[id];
46-
const Editor = Editors[widget.name];
47-
return <Editor key={`${profile}-${id}`} id={id} profile={profile} />
48-
})
49-
}
50-
</div>
51-
);
52-
};
14+
import { Editors } from '@/components/admin/Editors';
5315

5416
const AdminIndexPage = () => {
5517
const router = useRouter();
@@ -82,7 +44,7 @@ const AdminIndexPage = () => {
8244
<Navbar profile={currentProfile} />
8345
<Container sx={{ flex: 1, overflow: 'auto' }}>
8446
<Box my={4}>
85-
<Widgets profile={currentProfile} />
47+
<Editors profile={currentProfile} />
8648
</Box>
8749
</Container>
8850
</Box>

0 commit comments

Comments
 (0)