Skip to content

Commit

Permalink
[#122] Removal/modification of components
Browse files Browse the repository at this point in the history
Removed or modified components for MVP
  • Loading branch information
filip-kopecky committed Jan 5, 2023
1 parent 283ff91 commit fb45d48
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 441 deletions.
11 changes: 1 addition & 10 deletions src/component/misc/UserDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import {
UncontrolledDropdown,
} from "reactstrap";
import Routes from "../../util/Routes";
import { useDispatch, useSelector } from "react-redux";
import { useSelector } from "react-redux";
import classNames from "classnames";
import TermItState from "../../model/TermItState";
import { logout } from "../../action/ComplexActions";
import "./UserDropdown.scss";
import { useI18n } from "../hook/useI18n";
import { ThunkDispatch } from "../../util/Types";

interface UserDropdownProps {
dark: boolean;
Expand All @@ -28,8 +26,6 @@ function hashPath(path: string): string {
export const UserDropdown: React.FC<UserDropdownProps> = (props) => {
const { i18n } = useI18n();
const user = useSelector((state: TermItState) => state.user);
const dispatch: ThunkDispatch = useDispatch();
const onLogout = () => dispatch(logout());
return (
<UncontrolledDropdown nav={true}>
<DropdownToggle
Expand All @@ -52,11 +48,6 @@ export const UserDropdown: React.FC<UserDropdownProps> = (props) => {
<i className="fas fa-user" />
<span>{i18n("main.user-profile")}</span>
</DropdownItem>
<DropdownItem divider={true} />
<DropdownItem onClick={onLogout}>
<i className="fas fa-sign-out-alt" />
<span>{i18n("main.logout")}</span>
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
);
Expand Down
2 changes: 1 addition & 1 deletion src/component/misc/__tests__/UserDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ describe("UserDropdown", () => {
expect(
wrapper.find(UncontrolledDropdown).find(DropdownMenu).find(DropdownItem)
.length
).toBe(3);
).toBe(1);
});
});
102 changes: 7 additions & 95 deletions src/component/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ import { connect } from "react-redux";
import { injectIntl } from "react-intl";
import withI18n, { HasI18n } from "../hoc/withI18n";
import TermItState from "../../model/TermItState";
import { ThunkDispatch } from "../../util/Types";
import User, { UserData } from "../../model/User";
import { AsyncAction } from "../../action/ActionType";
import AsyncActionStatus from "../../action/AsyncActionStatus";
import Routing from "../../util/Routing";
import Routes from "../../util/Routes";
import User from "../../model/User";
import ProfileView from "./ProfileView";
import ProfileActionButtons from "./ProfileActionButtons";
import ProfileEditForm from "./ProfileEditForm";
import { updateProfile } from "../../action/AsyncUserActions";
import HeaderWithActions from "../misc/HeaderWithActions";
import { Card, CardBody } from "reactstrap";
import WindowTitle from "../misc/WindowTitle";
import UserRoles from "../administration/UserRoles";

interface ProfileProps extends HasI18n {
user: User;
updateProfile: (user: User) => Promise<AsyncAction>;
}

interface ProfileState {
Expand All @@ -39,55 +30,6 @@ export class Profile extends React.Component<ProfileProps, ProfileState> {
};
}

private onChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
const newState = Object.assign({}, this.state);
newState[e.currentTarget.name!] = e.currentTarget.value;
this.setState(newState);
};

private onKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
this.onSubmit();
}
};

private onSubmit = (): void => {
if (!this.isValid()) {
return;
}

const userData: UserData = {
...this.props.user,
firstName: this.state.firstName,
lastName: this.state.lastName,
};

this.props
.updateProfile(new User(userData))
.then((asyncResult: AsyncAction) => {
if (asyncResult.status === AsyncActionStatus.SUCCESS) {
this.showProfileView();
}
});
};

private isValid(): boolean {
const { user } = this.props;
return (
(this.state.firstName !== user.firstName ||
this.state.lastName !== user.lastName) &&
this.state.firstName.trim().length > 0 &&
this.state.lastName.trim().length > 0
);
}

private showProfileEdit = () => this.setState({ edit: true });

private showProfileView = () => this.setState({ edit: false });

private navigateToChangePasswordRoute = () =>
Routing.transitionTo(Routes.changePassword);

public render() {
const { i18n, user } = this.props;

Expand All @@ -103,49 +45,19 @@ export class Profile extends React.Component<ProfileProps, ProfileState> {
</div>
</>
}
actions={this.renderActionButtons()}
/>
<Card id="panel-profile">
<CardBody>
{!this.state.edit ? (
<ProfileView user={user} />
) : (
<ProfileEditForm
firstName={this.state.firstName}
lastName={this.state.lastName}
onChange={this.onChange}
onSubmit={this.onSubmit}
onKeyPress={this.onKeyPress}
showProfileView={this.showProfileView}
isValid={this.isValid()}
/>
)}
<ProfileView user={user} />
</CardBody>
</Card>
</>
);
}

private renderActionButtons() {
return (
<ProfileActionButtons
edit={this.state.edit}
showProfileEdit={this.showProfileEdit}
navigateToChangePasswordRoute={this.navigateToChangePasswordRoute}
/>
);
}
}

export default connect(
(state: TermItState) => {
return {
user: state.user,
};
},
(dispatch: ThunkDispatch) => {
return {
updateProfile: (name: User) => dispatch(updateProfile(name)),
};
}
)(injectIntl(withI18n(Profile)));
export default connect((state: TermItState) => {
return {
user: state.user,
};
})(injectIntl(withI18n(Profile)));
85 changes: 0 additions & 85 deletions src/component/profile/ProfileEditForm.tsx

This file was deleted.

23 changes: 2 additions & 21 deletions src/component/profile/__tests__/Profile.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { mountWithIntl } from "../../../__tests__/environment/Environment";
import { Profile } from "../Profile";
import ProfileActionButtons from "../ProfileActionButtons";
import ProfileView from "../ProfileView";
import User from "../../../model/User";
import { AsyncAction } from "../../../action/ActionType";
import Generator from "../../../__tests__/environment/Generator";
import { intlFunctions } from "../../../__tests__/environment/IntlUtil";
import ProfileEditForm from "../ProfileEditForm";
import HeaderWithActions from "../../misc/HeaderWithActions";

describe("Profile", () => {
let updateProfile: (user: User) => Promise<AsyncAction>;
Expand All @@ -19,32 +16,16 @@ describe("Profile", () => {
});

it("correctly renders component if !this.state.edit", () => {
const wrapper = mountWithIntl(
<Profile updateProfile={updateProfile} user={user} {...intlFunctions()} />
);
const wrapper = mountWithIntl(<Profile user={user} {...intlFunctions()} />);
(wrapper.find(Profile).instance() as Profile).setState({ edit: false });
wrapper.update();

const actionButtons = wrapper
.find(HeaderWithActions)
.find(ProfileActionButtons);

expect(actionButtons.length).toEqual(1);
expect(wrapper.find(ProfileView).length).toEqual(1);
});

it("correctly renders component if this.state.edit", () => {
const wrapper = mountWithIntl(
<Profile updateProfile={updateProfile} user={user} {...intlFunctions()} />
);
const wrapper = mountWithIntl(<Profile user={user} {...intlFunctions()} />);
(wrapper.find(Profile).instance() as Profile).setState({ edit: true });
wrapper.update();

const actionButtons = wrapper
.find(HeaderWithActions)
.find(ProfileActionButtons);

expect(actionButtons.length).toEqual(1);
expect(wrapper.find(ProfileEditForm).length).toEqual(1);
});
});
Loading

0 comments on commit fb45d48

Please sign in to comment.