Use apiClient

This commit is contained in:
Philipp Czora
2018-07-10 16:52:23 +02:00
parent 0648586092
commit eb99eab354
4 changed files with 51 additions and 60 deletions

View File

@@ -16,17 +16,31 @@ const styles = {
}
};
class Login extends React.Component<Props> {
state = {};
handleUsernameChange(event) {
type Props = {
classes: any,
login: (username: string, password: string) => void
};
type State = {
username: string,
password: string
};
class Login extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { username: "", password: "" };
}
handleUsernameChange(event: SyntheticInputEvent<HTMLInputElement>) {
this.setState({ username: event.target.value });
}
handlePasswordChange(event) {
handlePasswordChange(event: SyntheticInputEvent<HTMLInputElement>) {
this.setState({ password: event.target.value });
}
handleSubmit(event) {
handleSubmit(event: Event) {
event.preventDefault();
this.props.login(this.state.username, this.state.password);
}

View File

@@ -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());
}
})
.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 => {
return apiClient.post(LOGIN_URL, login_data).then(response => {
if (response.ok) {
dispatch(getIsAuthenticated());
dispatch(loginSuccessful());
}
},
error => console.log("error logging in: " + error)
);
});
};
}

View File

@@ -6,10 +6,7 @@ type Props = {
user: any
};
export default class UserRow extends React.Component<Props> {
render() {
return (
<tr>
@@ -22,7 +19,6 @@ export default class UserRow extends React.Component<Props> {
<DeleteUserButton user={this.props.user} />
</td>
</tr>
);
}
}

View File

@@ -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) {
@@ -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,15 +105,14 @@ 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) {