UserDetailsContent.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import { observer } from "mobx-react";
  15. import React from "react";
  16. import { Link } from "react-router";
  17. import styled from "styled-components";
  18. import { ThemePalette, ThemeProps } from "@src/components/Theme";
  19. import Button from "@src/components/ui/Button";
  20. import CopyMultilineValue from "@src/components/ui/CopyMultilineValue";
  21. import CopyValue from "@src/components/ui/CopyValue";
  22. import StatusImage from "@src/components/ui/StatusComponents/StatusImage";
  23. import type { User } from "@src/@types/User";
  24. import type { Project } from "@src/@types/Project";
  25. const Wrapper = styled.div<any>`
  26. ${ThemeProps.exactWidth(ThemeProps.contentWidth)}
  27. margin: 0 auto;
  28. padding-left: 126px;
  29. `;
  30. const Info = styled.div<any>`
  31. display: flex;
  32. flex-wrap: wrap;
  33. margin-top: 32px;
  34. margin-left: -32px;
  35. `;
  36. const LinkStyled = styled(Link)`
  37. color: ${ThemePalette.primary};
  38. text-decoration: none;
  39. `;
  40. const Field = styled.div<any>`
  41. ${ThemeProps.exactWidth("calc(50% - 32px)")}
  42. margin-bottom: 32px;
  43. margin-left: 32px;
  44. `;
  45. const Value = styled.div<any>``;
  46. const Label = styled.div<any>`
  47. font-size: 10px;
  48. font-weight: ${ThemeProps.fontWeights.medium};
  49. color: ${ThemePalette.grayscale[3]};
  50. text-transform: uppercase;
  51. margin-bottom: 3px;
  52. `;
  53. const LoadingWrapper = styled.div<any>`
  54. display: flex;
  55. justify-content: center;
  56. width: 100%;
  57. margin: 32px 0 64px 0;
  58. `;
  59. const Buttons = styled.div<any>`
  60. margin-top: 64px;
  61. display: flex;
  62. justify-content: space-between;
  63. `;
  64. const ButtonsColumn = styled.div<any>`
  65. display: flex;
  66. flex-direction: column;
  67. button {
  68. margin-bottom: 16px;
  69. &:last-child {
  70. margin-bottom: 0;
  71. }
  72. }
  73. `;
  74. export type Props = {
  75. user: User | null;
  76. loading: boolean;
  77. projects: Project[];
  78. userProjects: Project[];
  79. isLoggedUser: boolean;
  80. onUpdatePasswordClick: () => void;
  81. onDeleteClick: () => void;
  82. };
  83. @observer
  84. class UserDetailsContent extends React.Component<Props> {
  85. renderLoading() {
  86. if (!this.props.loading) {
  87. return null;
  88. }
  89. return (
  90. <LoadingWrapper>
  91. <StatusImage />
  92. </LoadingWrapper>
  93. );
  94. }
  95. renderButtons() {
  96. if (this.props.loading) return null;
  97. return (
  98. <Buttons>
  99. <ButtonsColumn>
  100. <Button hollow onClick={this.props.onUpdatePasswordClick}>
  101. Change password
  102. </Button>
  103. </ButtonsColumn>
  104. <ButtonsColumn>
  105. <Button
  106. alert
  107. hollow
  108. onClick={() => {
  109. this.props.onDeleteClick();
  110. }}
  111. disabled={this.props.isLoggedUser}
  112. >
  113. Delete user
  114. </Button>
  115. </ButtonsColumn>
  116. </Buttons>
  117. );
  118. }
  119. renderUserProjects(
  120. projects: { label: string; id: string }[]
  121. ): React.ReactNode {
  122. return projects.map((project, i) => (
  123. <span key={project.id}>
  124. {project.label ? (
  125. <LinkStyled to={`/projects/${project.id}`}>
  126. {project.label}
  127. </LinkStyled>
  128. ) : (
  129. project.id
  130. )}
  131. {i < projects.length - 1 ? ", " : ""}
  132. </span>
  133. ));
  134. }
  135. renderInfo() {
  136. if (this.props.loading || !this.props.user) {
  137. return null;
  138. }
  139. const { user } = this.props;
  140. const primaryProject = this.props.projects.find(
  141. p => user.project_id === p.id
  142. );
  143. const primaryProjectName = primaryProject
  144. ? primaryProject.name
  145. : user.project_id;
  146. const userProjects: { label: string; id: string }[] =
  147. this.props.userProjects.map(up => {
  148. const projectInfo = this.props.projects.find(p => p.id === up.id);
  149. if (projectInfo) {
  150. return { label: projectInfo.name, id: up.id };
  151. }
  152. return { label: "", id: up.id };
  153. });
  154. return (
  155. <Info>
  156. <Field>
  157. <Label>Name</Label>
  158. {this.renderValue(user.name)}
  159. </Field>
  160. <Field>
  161. <Label>Description</Label>
  162. {user.description ? (
  163. <CopyMultilineValue value={user.description} />
  164. ) : (
  165. <Value>-</Value>
  166. )}
  167. </Field>
  168. <Field>
  169. <Label>ID</Label>
  170. {this.renderValue(user.id)}
  171. </Field>
  172. <Field>
  173. <Label>Email</Label>
  174. {this.renderValue(user.email || "-")}
  175. </Field>
  176. <Field>
  177. <Label>Primary Project</Label>
  178. {this.renderValue(primaryProjectName || "-")}
  179. </Field>
  180. <Field>
  181. <Label>Project Membership</Label>
  182. {userProjects.length > 0 ? (
  183. this.renderUserProjects(userProjects)
  184. ) : (
  185. <Value>-</Value>
  186. )}
  187. </Field>
  188. <Field>
  189. <Label>Enabled</Label>
  190. <Value>{user.enabled ? "Yes" : "No"}</Value>
  191. </Field>
  192. </Info>
  193. );
  194. }
  195. renderValue(value: string) {
  196. return value !== "-" ? (
  197. <CopyValue value={value} maxWidth="90%" />
  198. ) : (
  199. <Value>{value}</Value>
  200. );
  201. }
  202. render() {
  203. return (
  204. <Wrapper>
  205. {this.renderInfo()}
  206. {this.renderLoading()}
  207. {this.renderButtons()}
  208. </Wrapper>
  209. );
  210. }
  211. }
  212. export default UserDetailsContent;