CredentialsForm.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import React, { useEffect, useState, useContext, useMemo } from "react";
  2. import styled from "styled-components";
  3. import api from "shared/api";
  4. import aws from "assets/aws.png";
  5. import credsIcon from "assets/creds.png";
  6. import addCircle from "assets/add-circle.png";
  7. import { Context } from "shared/Context";
  8. import Heading from "components/form-components/Heading";
  9. import Helper from "./form-components/Helper";
  10. import InputRow from "./form-components/InputRow";
  11. import SaveButton from "./SaveButton";
  12. type Props = {
  13. goBack: () => void;
  14. proceed: (x: any) => void;
  15. };
  16. type AWSCredential = {
  17. created_at: string;
  18. id: number;
  19. user_id: number;
  20. project_id: number;
  21. aws_arn: string;
  22. };
  23. const CredentialsForm: React.FC<Props> = ({
  24. goBack,
  25. proceed,
  26. }) => {
  27. const { currentProject } = useContext(Context);
  28. const [awsCredentials, setAWSCredentials] = useState<AWSCredential[]>(null);
  29. const [isLoading, setIsLoading] = useState(true);
  30. const [awsAccessKeyID, setAWSAccessKeyID] = useState("");
  31. const [awsSecretAccessKey, setAWSSecretAccessKey] = useState("");
  32. const [selectedCredentials, setSelectedCredentials] = useState<AWSCredential>(null);
  33. const [showCreateForm, setShowCreateForm] = useState(false);
  34. const [createStatus, setCreateStatus] = useState("");
  35. useEffect(() => {
  36. console.log("great creds")
  37. api
  38. .getAWSIntegration(
  39. "<token>",
  40. {},
  41. {
  42. project_id: currentProject.id,
  43. }
  44. )
  45. .then(({ data }) => {
  46. console.log("creds data", data);
  47. if (!Array.isArray(data)) {
  48. console.log("no creds");
  49. setAWSCredentials([]);
  50. } else {
  51. setAWSCredentials(data);
  52. }
  53. setIsLoading(false);
  54. })
  55. .catch((err) => {
  56. console.error(err);
  57. });
  58. }, [currentProject]);
  59. const createCreds = () => {
  60. setCreateStatus("loading");
  61. api
  62. .createAWSIntegration(
  63. "<token>",
  64. {
  65. // Hardcoded for backward-compatibility
  66. // TODO: remove
  67. aws_region: "us-east-f",
  68. aws_access_key_id: awsAccessKeyID,
  69. aws_secret_access_key: awsSecretAccessKey,
  70. aws_assume_role_arn: "",
  71. },
  72. {
  73. id: currentProject.id,
  74. }
  75. )
  76. .then(({ data }) => {
  77. setCreateStatus("successful");
  78. proceed(data.cloud_provider_credentials_id);
  79. })
  80. .catch((err) => {
  81. console.error(err);
  82. setCreateStatus("Error creating credentials");
  83. });
  84. };
  85. const renderContent = () => {
  86. if (awsCredentials.length > 0 && !showCreateForm) {
  87. return (
  88. <>
  89. <CredentialList>
  90. {
  91. awsCredentials.map((cred: AWSCredential, i: number) => {
  92. return (
  93. <Credential
  94. key={cred.id}
  95. isSelected={cred.id === selectedCredentials?.id}
  96. onClick={() => {
  97. if (cred.id === selectedCredentials?.id) {
  98. setSelectedCredentials(null);
  99. } else {
  100. setSelectedCredentials(cred);
  101. }
  102. }}
  103. >
  104. <Icon src={credsIcon} />
  105. <Name>{cred.aws_arn || "n/a"}</Name>
  106. </Credential>
  107. );
  108. })
  109. }
  110. <CreateRow onClick={() => {
  111. setShowCreateForm(true);
  112. setSelectedCredentials(null);
  113. }}>
  114. <Icon src={addCircle} />
  115. Add new AWS credentials
  116. </CreateRow>
  117. </CredentialList>
  118. <Br height="34px" />
  119. <SaveButton
  120. disabled={!selectedCredentials && true}
  121. onClick={() => proceed(selectedCredentials.id)}
  122. clearPosition
  123. text="Continue"
  124. />
  125. </>
  126. );
  127. }
  128. return (
  129. <>
  130. <StyledForm>
  131. {
  132. awsCredentials.length > 0 && (
  133. <CloseButton onClick={() => setShowCreateForm(false)}>
  134. <i className="material-icons">close</i>
  135. </CloseButton>
  136. )
  137. }
  138. <InputRow
  139. type="string"
  140. value={awsAccessKeyID}
  141. setValue={(e: string) => setAWSAccessKeyID(e)}
  142. label="👤 AWS access ID"
  143. placeholder="ex: AKIAIOSFODNN7EXAMPLE"
  144. isRequired
  145. />
  146. <InputRow
  147. type="password"
  148. value={awsSecretAccessKey}
  149. setValue={(e: string) => setAWSSecretAccessKey(e)}
  150. label="🔒 AWS secret key"
  151. placeholder="○ ○ ○ ○ ○ ○ ○ ○ ○"
  152. isRequired
  153. />
  154. </StyledForm>
  155. <SaveButton
  156. disabled={awsAccessKeyID === "" || awsSecretAccessKey === ""}
  157. onClick={createCreds}
  158. status={createStatus}
  159. statusPosition="right"
  160. clearPosition
  161. text="Continue"
  162. />
  163. </>
  164. );
  165. }
  166. return (
  167. <>
  168. <Heading isAtTop>
  169. <BackButton width="140px" onClick={goBack}>
  170. <i className="material-icons">first_page</i>
  171. Select cloud
  172. </BackButton>
  173. <Spacer />
  174. <Img src={aws} />
  175. Set AWS credentials
  176. </Heading>
  177. <Helper>
  178. Select your credentials from the list below, or add a new set of credentials:
  179. </Helper>
  180. {
  181. isLoading ? (
  182. <>Loading . . .</>
  183. ) : (
  184. renderContent()
  185. )
  186. }
  187. </>
  188. );
  189. };
  190. export default CredentialsForm;
  191. const CloseButton = styled.div`
  192. position: absolute;
  193. top: 15px;
  194. right: 15px;
  195. padding: 5px;
  196. border-radius: 100px;
  197. display: flex;
  198. align-items: center;
  199. justify-content: center;
  200. cursor: pointer;
  201. background: #ffffff11;
  202. :hover {
  203. background: #ffffff22;
  204. > i {
  205. color: #ffffff;
  206. }
  207. }
  208. > i {
  209. font-size: 20px;
  210. color: #aaaabb;
  211. }
  212. `;
  213. const Spacer = styled.div`
  214. height: 1px;
  215. width: 17px;
  216. `;
  217. const Icon = styled.img`
  218. width: 15px;
  219. margin-right: 15px;
  220. `;
  221. const CreateRow = styled.div`
  222. height: 50px;
  223. display: flex;
  224. cursor: pointer;
  225. align-items: center;
  226. font-size: 13px;
  227. padding: 20px;
  228. background: #ffffff11;
  229. :hover {
  230. background: #ffffff18;
  231. }
  232. `;
  233. const Br = styled.div<{ height?: string }>`
  234. width: 100%;
  235. height: ${props => props.height || "20px"};
  236. `;
  237. const Img = styled.img`
  238. height: 18px;
  239. margin-right: 15px;
  240. `;
  241. const BackButton = styled.div`
  242. display: flex;
  243. align-items: center;
  244. justify-content: space-between;
  245. cursor: pointer;
  246. font-size: 13px;
  247. height: 35px;
  248. padding: 5px 13px;
  249. padding-right: 15px;
  250. border: 1px solid #ffffff55;
  251. border-radius: 100px;
  252. width: ${(props: { width: string }) => props.width};
  253. color: white;
  254. background: #ffffff11;
  255. :hover {
  256. background: #ffffff22;
  257. }
  258. > i {
  259. color: white;
  260. font-size: 16px;
  261. margin-right: 6px;
  262. margin-left: -2px;
  263. }
  264. `;
  265. const BackArrow = styled.div`
  266. width: 30px;
  267. height: 30px;
  268. margin-left: -5px;
  269. margin-right: 8px;
  270. display: flex;
  271. align-items: center;
  272. justify-content: center;
  273. z-index: 1;
  274. border-radius: 50%;
  275. right: 10px;
  276. top: 10px;
  277. cursor: pointer;
  278. :hover {
  279. background-color: #ffffff11;
  280. }
  281. > i {
  282. font-size: 20px;
  283. color: #aaaabb;
  284. }
  285. `;
  286. const Name = styled.div`
  287. font-size: 13px;
  288. font-weight: 500;
  289. `;
  290. const Credential = styled.div<{ isLast?: boolean; isSelected?: boolean }>`
  291. height: 50px;
  292. display: flex;
  293. cursor: pointer;
  294. align-items: center;
  295. padding: 20px;
  296. border-bottom: ${props => props.isLast ? "" : "1px solid #7a7b80"};
  297. background: ${props => props.isSelected ? "#ffffff33" : "#ffffff11"};
  298. :hover {
  299. background: ${props => props.isSelected ? "" : "#ffffff18"};
  300. }
  301. `;
  302. const CredentialList = styled.div`
  303. width: 100%;
  304. border: 1px solid #7a7b80;
  305. border-radius: 5px;
  306. `;
  307. const StyledForm = styled.div`
  308. position: relative;
  309. padding: 15px 30px 25px;
  310. border-radius: 5px;
  311. background: #26292e;
  312. border: 1px solid #494b4f;
  313. font-size: 13px;
  314. margin-bottom: 30px;
  315. `;