Skip to content

Commit d461eb8

Browse files
authored
feat: prepare profile (#107)
1 parent 1dccf01 commit d461eb8

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

src/components/IFrameWidget/editor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import type { IFrameWidgetProps } from './types';
1111

1212
type Props = {
13+
profile: string;
1314
id: string;
1415
props: IFrameWidgetProps;
1516
};
@@ -33,13 +34,13 @@ class IFrameWidgetEditor extends Component<Props, IFrameWidgetProps> {
3334

3435
save(e: MouseEvent<HTMLButtonElement>) {
3536
e.preventDefault();
36-
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
37+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), this.state);
3738
}
3839

3940
delete(e: MouseEvent<HTMLButtonElement>) {
4041
e.preventDefault();
4142
if (confirm('本当に削除してよろしいですか?')) {
42-
set(ref(db, `/widgets/${this.props.id}`), null);
43+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}`), null);
4344
}
4445
}
4546

src/components/TextWidget/editor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { db } from '@/lib/firebase';
1313
import type { TextWidgetProps } from '@/components/TextWidget/types';
1414

1515
type Props = {
16+
profile: string;
1617
id: string;
1718
props: TextWidgetProps;
1819
};
@@ -93,13 +94,13 @@ class TextWidgetEditor extends Component<Props, TextWidgetProps> {
9394

9495
save(e: MouseEvent<HTMLButtonElement>) {
9596
e.preventDefault();
96-
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
97+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), this.state);
9798
}
9899

99100
delete(e: MouseEvent<HTMLButtonElement>) {
100101
e.preventDefault();
101102
if (confirm('本当に削除してよろしいですか?')) {
102-
set(ref(db, `/widgets/${this.props.id}`), null);
103+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}`), null);
103104
}
104105
}
105106

src/components/TimeWidget/editor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { db } from '@/lib/firebase';
1212
import type { TimeWidgetProps } from './types';
1313

1414
type Props = {
15+
profile: string;
1516
id: string;
1617
props: TimeWidgetProps;
1718
};
@@ -31,20 +32,21 @@ const FormGroup = styled.div`
3132
class TimeWidgetEditor extends Component<Props, TimeWidgetProps> {
3233
constructor(props: Props) {
3334
super(props);
35+
console.log(props);
3436
this.state = this.props.props;
3537
this.save = this.save.bind(this);
3638
this.delete = this.delete.bind(this);
3739
}
3840

3941
save(e: MouseEvent<HTMLButtonElement>) {
4042
e.preventDefault();
41-
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
43+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), this.state);
4244
}
4345

4446
delete(e: MouseEvent<HTMLButtonElement>) {
4547
e.preventDefault();
4648
if (confirm('本当に削除してよろしいですか?')) {
47-
set(ref(db, `/widgets/${this.props.id}`), null);
49+
set(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}`), null);
4850
}
4951
}
5052

src/pages/admin/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ type WidgetList = { [key: string]: Widget }
6767

6868
const Widgets = () => {
6969
const [widgets, setWidgets] = useState<WidgetList>({});
70+
const profile = 'default';
7071

7172
useEffect(() => {
72-
const widgetsRef = ref(db, '/widgets');
73+
const widgetsRef = ref(db, `/profiles/${profile}/widgets`);
7374
onValue(widgetsRef, (snap: DataSnapshot) => {
7475
if (snap?.val()) {
7576
setWidgets(snap.val());
@@ -83,14 +84,15 @@ const Widgets = () => {
8384
Object.keys(widgets).map((id) => {
8485
const widget: any = widgets[id];
8586
const Editor = Editors[widget.name];
86-
return <Editor key={id} id={id} props={widget.props} />
87+
return <Editor key={id} id={id} props={widget.props} profile={profile} />
8788
})
8889
}
8990
</div>
9091
);
9192
};
9293

9394
const AddWidgetModel = ({ open, onClose }: { open: boolean, onClose: () => void }) => {
95+
const profile = 'default';
9496
const [widgetId, setWidgetId] = useState("");
9597
const [widgetType, setWidgetType] = useState("text");
9698

@@ -152,7 +154,7 @@ const AddWidgetModel = ({ open, onClose }: { open: boolean, onClose: () => void
152154
</DialogContent>
153155
<DialogActions>
154156
<Button color="primary" variant="contained" onClick={() => {
155-
set(ref(db, `/widgets/${widgetId}`), {
157+
set(ref(db, `/profiles/${profile}/widgets/${widgetId}`), {
156158
name: widgetType,
157159
props: Editors[widgetType].defaultProps
158160
});

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

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

src/pages/preview/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ type WidgetList = { [key: string]: Widget }
2121

2222
const PreviewPage = () => {
2323
const [widgets, setWidgets] = useState<WidgetList>({});
24+
const profile = 'default';
2425

2526
useEffect(() => {
26-
const widgetsRef = ref(db, '/widgets');
27+
const widgetsRef = ref(db, `/profiles/${profile}/widgets`);
2728
onValue(widgetsRef, (snap: DataSnapshot) => {
2829
if (snap?.val()) {
2930
setWidgets(snap.val());

0 commit comments

Comments
 (0)