From eb99eab354a43ce5acfd368d5fcd6f288eb44511 Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Tue, 10 Jul 2018 16:52:23 +0200 Subject: [PATCH] Use apiClient --- scm-ui/src/containers/Login.js | 24 +++++++++--- scm-ui/src/modules/login.js | 52 +++++++++----------------- scm-ui/src/users/containers/UserRow.js | 6 +-- scm-ui/src/users/modules/users.js | 29 +++++++------- 4 files changed, 51 insertions(+), 60 deletions(-) diff --git a/scm-ui/src/containers/Login.js b/scm-ui/src/containers/Login.js index e811927831..d7c3e0fba7 100644 --- a/scm-ui/src/containers/Login.js +++ b/scm-ui/src/containers/Login.js @@ -16,17 +16,31 @@ const styles = { } }; -class Login extends React.Component { - state = {}; - handleUsernameChange(event) { +type Props = { + classes: any, + login: (username: string, password: string) => void +}; + +type State = { + username: string, + password: string +}; + +class Login extends React.Component { + constructor(props: Props) { + super(props); + this.state = { username: "", password: "" }; + } + + handleUsernameChange(event: SyntheticInputEvent) { this.setState({ username: event.target.value }); } - handlePasswordChange(event) { + handlePasswordChange(event: SyntheticInputEvent) { this.setState({ password: event.target.value }); } - handleSubmit(event) { + handleSubmit(event: Event) { event.preventDefault(); this.props.login(this.state.username, this.state.password); } diff --git a/scm-ui/src/modules/login.js b/scm-ui/src/modules/login.js index 53e2049ec8..d3fc8c64e2 100644 --- a/scm-ui/src/modules/login.js +++ b/scm-ui/src/modules/login.js @@ -1,7 +1,10 @@ //@flow -const LOGIN_URL = "/scm/api/rest/v2/auth/access_token"; -const AUTHENTICATION_INFO_URL = "/scm/api/rest/v2/me"; +import { apiClient } from "../apiclient"; +import { isRegExp } from "util"; + +const LOGIN_URL = "/auth/access_token"; +const AUTHENTICATION_INFO_URL = "/me"; export const LOGIN = "scm/auth/login"; export const LOGIN_REQUEST = "scm/auth/login_request"; @@ -19,21 +22,12 @@ export function getIsAuthenticatedRequest() { } export function getIsAuthenticated() { - return function(dispatch) { + return function(dispatch: any) { dispatch(getIsAuthenticatedRequest()); - - return fetch(AUTHENTICATION_INFO_URL, { - credentials: "same-origin", - headers: { - Cache: "no-cache" - } - }) + return apiClient + .get(AUTHENTICATION_INFO_URL) .then(response => { - if (response.ok) { - return response.json(); - } else { - dispatch(isNotAuthenticated()); - } + return response.json(); }) .then(data => { if (data) { @@ -66,27 +60,17 @@ export function login(username: string, password: string) { var login_data = { cookie: true, grant_type: "password", - password: username, - username: password + username, + password }; - return function(dispatch) { + return function(dispatch: any) { dispatch(loginRequest()); - return fetch(LOGIN_URL, { - method: "POST", - headers: { - "Content-Type": "application/json; charset=utf-8" - }, - credentials: "same-origin", - body: JSON.stringify(login_data) - }).then( - response => { - if (response.ok) { - dispatch(getIsAuthenticated()); - dispatch(loginSuccessful()); - } - }, - error => console.log("error logging in: " + error) - ); + return apiClient.post(LOGIN_URL, login_data).then(response => { + if (response.ok) { + dispatch(getIsAuthenticated()); + dispatch(loginSuccessful()); + } + }); }; } diff --git a/scm-ui/src/users/containers/UserRow.js b/scm-ui/src/users/containers/UserRow.js index 077ffa7fc3..5431178ba9 100644 --- a/scm-ui/src/users/containers/UserRow.js +++ b/scm-ui/src/users/containers/UserRow.js @@ -6,10 +6,7 @@ type Props = { user: any }; - - export default class UserRow extends React.Component { - render() { return ( @@ -19,10 +16,9 @@ export default class UserRow extends React.Component { - + - ); } } diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index a2a157d1c9..1fdc499535 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -1,10 +1,10 @@ // @flow -import {apiClient, PAGE_NOT_FOUND_ERROR} from '../../apiclient'; +import { apiClient, PAGE_NOT_FOUND_ERROR } from "../../apiclient"; const FETCH_USERS = "scm/users/FETCH"; const FETCH_USERS_SUCCESS = "scm/users/FETCH_SUCCESS"; const FETCH_USERS_FAILURE = "scm/users/FETCH_FAILURE"; -const FETCH_USERS_NOTFOUND = 'scm/users/FETCH_NOTFOUND'; +const FETCH_USERS_NOTFOUND = "scm/users/FETCH_NOTFOUND"; const DELETE_USER = "scm/users/DELETE"; const DELETE_USER_SUCCESS = "scm/users/DELETE_SUCCESS"; @@ -34,10 +34,10 @@ function usersNotFound(url: string) { } export function fetchUsers() { - - return function(dispatch) { + return function(dispatch: any => void) { dispatch(requestUsers()); - return apiClient.get(USERS_URL) + return apiClient + .get(USERS_URL) .then(response => { return response; }) @@ -49,14 +49,14 @@ export function fetchUsers() { .then(data => { dispatch(fetchUsersSuccess(data)); }) - .catch((err) => { + .catch(err => { if (err === PAGE_NOT_FOUND_ERROR) { dispatch(usersNotFound(USERS_URL)); } else { dispatch(failedToFetchUsers(USERS_URL, err)); } }); - } + }; } function fetchUsersSuccess(users: any) { @@ -67,7 +67,7 @@ function fetchUsersSuccess(users: any) { } export function shouldFetchUsers(state: any): boolean { - if(state.users.users == null){ + if (state.users.users == null) { return true; } return false; @@ -81,8 +81,6 @@ export function fetchUsersIfNeeded() { }; } - - function requestDeleteUser(url: string) { return { type: DELETE_USER, @@ -92,7 +90,7 @@ function requestDeleteUser(url: string) { function deleteUserSuccess() { return { - type: DELETE_USER_SUCCESS, + type: DELETE_USER_SUCCESS }; } @@ -107,16 +105,15 @@ function deleteUserFailure(url: string, err: Error) { export function deleteUser(username: string) { return function(dispatch) { dispatch(requestDeleteUser(username)); - return apiClient.delete(USERS_URL + '/' + username) + return apiClient + .delete(USERS_URL + "/" + username) .then(() => { dispatch(deleteUserSuccess()); }) - .catch((err) => dispatch(deleteUserFailure(username, err))); - } + .catch(err => dispatch(deleteUserFailure(username, err))); + }; } - - export default function reducer(state: any = {}, action: any = {}) { switch (action.type) { case FETCH_USERS: