use me link of index resource

This commit is contained in:
Maren Süwer
2018-10-11 10:26:54 +02:00
parent 8b7dcc379b
commit ae92842763
5 changed files with 27 additions and 17 deletions

View File

@@ -46,7 +46,6 @@ type Props = {
// dispatcher functions // dispatcher functions
fetchMe: (link: string) => void, fetchMe: (link: string) => void,
fetchIndexResources: () => void,
// context props // context props
t: string => string t: string => string
@@ -54,7 +53,6 @@ type Props = {
class App extends Component<Props> { class App extends Component<Props> {
componentDidMount() { componentDidMount() {
//this.props.fetchIndexResources();
if (this.props.meLink) this.props.fetchMe(this.props.meLink); if (this.props.meLink) this.props.fetchMe(this.props.meLink);
} }
@@ -110,8 +108,7 @@ class App extends Component<Props> {
const mapDispatchToProps = (dispatch: any) => { const mapDispatchToProps = (dispatch: any) => {
return { return {
fetchMe: (link: string) => dispatch(fetchMe(link)), fetchMe: (link: string) => dispatch(fetchMe(link))
fetchIndexResources: () => dispatch(fetchIndexResources())
}; };
}; };

View File

@@ -98,6 +98,7 @@ class Login extends React.Component<Props, State> {
} }
renderRedirect = () => { renderRedirect = () => {
this.props.fetchIndexResources();
const { from } = this.props.location.state || { from: { pathname: "/" } }; const { from } = this.props.location.state || { from: { pathname: "/" } };
return <Redirect to={from} />; return <Redirect to={from} />;
}; };
@@ -167,8 +168,13 @@ const mapStateToProps = state => {
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
login: (link: string, username: string, password: string) => login: (
dispatch(login(link, username, password)) loginLink: string,
meLink: string,
username: string,
password: string
) => dispatch(login(loginLink, meLink, username, password)),
fetchIndexResources: () => dispatch(fetchIndexResources())
}; };
}; };

View File

@@ -5,7 +5,7 @@ import * as types from "./types";
import { apiClient, UNAUTHORIZED_ERROR } from "@scm-manager/ui-components"; import { apiClient, UNAUTHORIZED_ERROR } from "@scm-manager/ui-components";
import { isPending } from "./pending"; import { isPending } from "./pending";
import { getFailure } from "./failure"; import { getFailure } from "./failure";
import { fetchIndexResources, getMeLink } from "./indexResource"; import { callFetchIndexResources } from "./indexResource";
// Action // Action
@@ -122,14 +122,9 @@ export const fetchMeFailure = (error: Error) => {
}; };
}; };
// urls
const ME_URL = "/me";
// side effects // side effects
const callFetchMe = (link: string): Promise<Me> => { const callFetchMe = (link: string): Promise<Me> => {
console.log(link);
return apiClient return apiClient
.get(link) .get(link)
.then(response => { .then(response => {
@@ -156,11 +151,11 @@ export const login = (
return apiClient return apiClient
.post(loginLink, login_data) .post(loginLink, login_data)
.then(response => { .then(response => {
return dispatch(fetchIndexResources()); return callFetchIndexResources();
}) })
.then(response => { .then(response => {
const meLink = response._links.me.href; const meLink = response._links.me.href;
return dispatch(callFetchMe(meLink)); return callFetchMe(meLink);
}) })
.then(me => { .then(me => {
dispatch(loginSuccess(me)); dispatch(loginSuccess(me));

View File

@@ -93,6 +93,14 @@ describe("auth actions", () => {
headers: { "content-type": "application/json" } headers: { "content-type": "application/json" }
}); });
fetchMock.getOnce("/api/v2/", {
_links: {
me: {
href: "/me"
}
}
});
const expectedActions = [ const expectedActions = [
{ type: LOGIN_PENDING }, { type: LOGIN_PENDING },
{ type: LOGIN_SUCCESS, payload: me } { type: LOGIN_SUCCESS, payload: me }

View File

@@ -21,12 +21,16 @@ export const FETCH_INDEXRESOURCES_FAILURE = `${FETCH_INDEXRESOURCES}_${
const INDEX_RESOURCES_LINK = "/"; const INDEX_RESOURCES_LINK = "/";
export const callFetchIndexResources = (): Promise<IndexResources> => {
return apiClient.get(INDEX_RESOURCES_LINK).then(response => {
return response.json();
});
};
export function fetchIndexResources() { export function fetchIndexResources() {
return function(dispatch: any) { return function(dispatch: any) {
dispatch(fetchIndexResourcesPending()); dispatch(fetchIndexResourcesPending());
return apiClient return callFetchIndexResources()
.get(INDEX_RESOURCES_LINK)
.then(response => response.json())
.then(resources => { .then(resources => {
dispatch(fetchIndexResourcesSuccess(resources)); dispatch(fetchIndexResourcesSuccess(resources));
}) })