Skip to content

Commit bf77923

Browse files
authored
fix: retrieve props for each widget editor component (#112)
1 parent da4f7e3 commit bf77923

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/components/IFrameWidget/editor.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import React, { Component, MouseEvent } from 'react';
22
import styled from 'styled-components';
3-
import { ref, set } from '@firebase/database';
3+
import { ref, set, onValue } from '@firebase/database';
44
import { db } from '@/lib/firebase';
55
import {
66
TextField,
77
Button,
88
Typography,
99
} from '@mui/material';
1010
import type { IFrameWidgetProps } from './types';
11+
import { IFrameWidget } from './widget';
1112

1213
type Props = {
1314
profile: string;
1415
id: string;
15-
props: IFrameWidgetProps;
1616
};
1717

1818
const FormGroup = styled.div`
@@ -27,7 +27,7 @@ const FormGroup = styled.div`
2727
class IFrameWidgetEditor extends Component<Props, IFrameWidgetProps> {
2828
constructor(props: Props) {
2929
super(props);
30-
this.state = this.props.props;
30+
this.state = IFrameWidgetEditor.defaultProps;
3131
this.save = this.save.bind(this);
3232
this.delete = this.delete.bind(this);
3333
}
@@ -44,6 +44,12 @@ class IFrameWidgetEditor extends Component<Props, IFrameWidgetProps> {
4444
}
4545
}
4646

47+
componentDidMount() {
48+
onValue(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), (snap) => {
49+
this.setState(snap.val());
50+
});
51+
}
52+
4753
render() {
4854
return (
4955
<div>

src/components/TextWidget/editor.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import {
88
} from '@mui/material';
99
import styled from 'styled-components';
1010
import { Property } from 'csstype';
11-
import { ref, set } from '@firebase/database';
11+
import { ref, set, onValue } from '@firebase/database';
1212
import { db } from '@/lib/firebase';
1313
import type { TextWidgetProps } from '@/components/TextWidget/types';
1414

1515
type Props = {
1616
profile: string;
1717
id: string;
18-
props: TextWidgetProps;
1918
};
2019

2120
const FormGroup = styled.div`
@@ -87,7 +86,7 @@ class Color {
8786
class TextWidgetEditor extends Component<Props, TextWidgetProps> {
8887
constructor(props: Props) {
8988
super(props);
90-
this.state = this.props.props;
89+
this.state = TextWidgetEditor.defaultProps;
9190
this.save = this.save.bind(this);
9291
this.delete = this.delete.bind(this);
9392
}
@@ -104,6 +103,12 @@ class TextWidgetEditor extends Component<Props, TextWidgetProps> {
104103
}
105104
}
106105

106+
componentDidMount() {
107+
onValue(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), (snap) => {
108+
this.setState(snap.val());
109+
});
110+
}
111+
107112
render() {
108113
return (
109114
<div>

src/components/TimeWidget/editor.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import {
77
FormControlLabel,
88
Checkbox,
99
} from '@mui/material';
10-
import { ref, set } from '@firebase/database';
10+
import { ref, set, onValue } from '@firebase/database';
1111
import { db } from '@/lib/firebase';
1212
import type { TimeWidgetProps } from './types';
1313

1414
type Props = {
1515
profile: string;
1616
id: string;
17-
props: TimeWidgetProps;
1817
};
1918

2019
const FormGroup = styled.div`
@@ -32,7 +31,7 @@ const FormGroup = styled.div`
3231
class TimeWidgetEditor extends Component<Props, TimeWidgetProps> {
3332
constructor(props: Props) {
3433
super(props);
35-
this.state = this.props.props;
34+
this.state = TimeWidgetEditor.defaultProps;
3635
this.save = this.save.bind(this);
3736
this.delete = this.delete.bind(this);
3837
}
@@ -49,6 +48,12 @@ class TimeWidgetEditor extends Component<Props, TimeWidgetProps> {
4948
}
5049
}
5150

51+
componentDidMount() {
52+
onValue(ref(db, `/profiles/${this.props.profile}/widgets/${this.props.id}/props`), (snap) => {
53+
this.setState(snap.val());
54+
});
55+
}
56+
5257
render() {
5358
return (
5459
<div>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const useStyles = makeStyles((theme) => ({
6464

6565
type Widget = {
6666
name: string;
67-
props: any;
6867
}
6968

7069
type WidgetList = { [key: string]: Widget }
@@ -85,7 +84,7 @@ const Widgets = ({ profile }: { profile: string }) => {
8584
Object.keys(widgets).map((id) => {
8685
const widget: any = widgets[id];
8786
const Editor = Editors[widget.name];
88-
return <Editor key={`${profile}-${id}`} id={id} props={widget.props} profile={profile} />
87+
return <Editor key={`${profile}-${id}`} id={id} profile={profile} />
8988
})
9089
}
9190
</div>

src/pages/admin/index.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ const AdminIndexPage = () => {
9393
const [userAnchorEl, setUserAnchorEl] = useState<HTMLElement | null>(null);
9494
const [profileAnchorEl, setProfileAnchorEl] = useState<HTMLElement | null>(null);
9595
const [addProfileDialogOpened, setAddProfileDialogOpened] = useState(false);
96-
const [currentProfile, setCurrentProfile] = useState('default');
9796
const [profiles, setProfiles] = useState<string[]>([]);
9897

9998
const isUserMenuOpen = Boolean(userAnchorEl);
@@ -149,10 +148,6 @@ const AdminIndexPage = () => {
149148
<Typography variant="h6" className={classes.title}>
150149
Admin
151150
</Typography>
152-
<Typography variant="h6" className={classes.title}>
153-
Profile:{' '}
154-
{currentProfile}
155-
</Typography>
156151
<Box sx={{ flexGrow: 1 }} />
157152
<Box sx={{ display: { xs: 'none', md: 'flex' } }}>
158153
<IconButton

0 commit comments

Comments
 (0)