Skip to content

Commit

Permalink
feat: remove child components from state and use data instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustavb12 committed Aug 12, 2024
1 parent 99a44dd commit 2d9807e
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/instructor-toolbar/masquerade-widget/MasqueradeWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MasqueradeWidget extends Component {
this.state = {
autoFocus: false,
masquerade: 'Staff',
options: [],
active: {},
available: [],
shouldShowUserNameInput: false,
masqueradeUsername: null,
};
Expand Down Expand Up @@ -58,12 +59,30 @@ class MasqueradeWidget extends Component {
}

onSuccess(data) {
const options = this.parseAvailableOptions(data);
const { active, available } = this.parseAvailableOptions(data);
this.setState({
options,
active,
available,
});
}

getOptions() {
const options = this.state.available.map((group) => (
<MasqueradeWidgetOption
groupId={group.groupId}
groupName={group.name}
key={group.name}
role={group.role}
selected={this.state.active}
userName={group.userName}
userPartitionId={group.userPartitionId}
userNameInputToggle={(...args) => this.toggle(...args)}
onSubmit={(payload) => this.onSubmit(payload)}
/>
));
return options;
}

clearError() {
this.props.onError('');
}
Expand All @@ -80,19 +99,6 @@ class MasqueradeWidget extends Component {
const data = postData || {};
const active = data.active || {};
const available = data.available || [];
const options = available.map((group) => (
<MasqueradeWidgetOption
groupId={group.groupId}
groupName={group.name}
key={group.name}
role={group.role}
selected={active}
userName={group.userName}
userPartitionId={group.userPartitionId}
userNameInputToggle={(...args) => this.toggle(...args)}
onSubmit={(payload) => this.onSubmit(payload)}
/>
));
if (active.userName) {
this.setState({
autoFocus: false,
Expand All @@ -105,14 +111,13 @@ class MasqueradeWidget extends Component {
} else if (active.role === 'student') {
this.setState({ masquerade: 'Learner' });
}
return options;
return { active, available };
}

render() {
const {
autoFocus,
masquerade,
options,
shouldShowUserNameInput,
masqueradeUsername,
} = this.state;
Expand All @@ -126,7 +131,7 @@ class MasqueradeWidget extends Component {
{masquerade}
</Dropdown.Toggle>
<Dropdown.Menu>
{options}
{this.getOptions()}
</Dropdown.Menu>
</Dropdown>
</div>
Expand Down

0 comments on commit 2d9807e

Please sign in to comment.