mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
use index resource for users
This commit is contained in:
@@ -12,13 +12,15 @@ import {
|
|||||||
} from "../modules/users";
|
} from "../modules/users";
|
||||||
import { Page } from "@scm-manager/ui-components";
|
import { Page } from "@scm-manager/ui-components";
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
|
import {getUsersLink} from "../../modules/indexResource";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
loading?: boolean,
|
loading?: boolean,
|
||||||
error?: Error,
|
error?: Error,
|
||||||
|
usersLink: string,
|
||||||
|
|
||||||
// dispatcher functions
|
// dispatcher functions
|
||||||
addUser: (user: User, callback?: () => void) => void,
|
addUser: (link: string, user: User, callback?: () => void) => void,
|
||||||
resetForm: () => void,
|
resetForm: () => void,
|
||||||
|
|
||||||
// context objects
|
// context objects
|
||||||
@@ -37,7 +39,7 @@ class AddUser extends React.Component<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
createUser = (user: User) => {
|
createUser = (user: User) => {
|
||||||
this.props.addUser(user, this.userCreated);
|
this.props.addUser(this.props.usersLink, user, this.userCreated);
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -61,8 +63,8 @@ class AddUser extends React.Component<Props> {
|
|||||||
|
|
||||||
const mapDispatchToProps = dispatch => {
|
const mapDispatchToProps = dispatch => {
|
||||||
return {
|
return {
|
||||||
addUser: (user: User, callback?: () => void) => {
|
addUser: (link: string, user: User, callback?: () => void) => {
|
||||||
dispatch(createUser(user, callback));
|
dispatch(createUser(link, user, callback));
|
||||||
},
|
},
|
||||||
resetForm: () => {
|
resetForm: () => {
|
||||||
dispatch(createUserReset());
|
dispatch(createUserReset());
|
||||||
@@ -73,7 +75,9 @@ const mapDispatchToProps = dispatch => {
|
|||||||
const mapStateToProps = (state, ownProps) => {
|
const mapStateToProps = (state, ownProps) => {
|
||||||
const loading = isCreateUserPending(state);
|
const loading = isCreateUserPending(state);
|
||||||
const error = getCreateUserFailure(state);
|
const error = getCreateUserFailure(state);
|
||||||
|
const usersLink = getUsersLink(state);
|
||||||
return {
|
return {
|
||||||
|
usersLink,
|
||||||
loading,
|
loading,
|
||||||
error
|
error
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,16 +26,18 @@ import {
|
|||||||
|
|
||||||
import { DeleteUserNavLink, EditUserNavLink } from "./../components/navLinks";
|
import { DeleteUserNavLink, EditUserNavLink } from "./../components/navLinks";
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
|
import { getUsersLink } from "../../modules/indexResource";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
name: string,
|
name: string,
|
||||||
user: User,
|
user: User,
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
error: Error,
|
error: Error,
|
||||||
|
usersLink: string,
|
||||||
|
|
||||||
// dispatcher functions
|
// dispatcher functions
|
||||||
deleteUser: (user: User, callback?: () => void) => void,
|
deleteUser: (user: User, callback?: () => void) => void,
|
||||||
fetchUser: string => void,
|
fetchUser: (string, string) => void,
|
||||||
|
|
||||||
// context objects
|
// context objects
|
||||||
t: string => string,
|
t: string => string,
|
||||||
@@ -45,7 +47,7 @@ type Props = {
|
|||||||
|
|
||||||
class SingleUser extends React.Component<Props> {
|
class SingleUser extends React.Component<Props> {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.fetchUser(this.props.name);
|
this.props.fetchUser(this.props.usersLink, this.props.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
userDeleted = () => {
|
userDeleted = () => {
|
||||||
@@ -124,8 +126,9 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
isFetchUserPending(state, name) || isDeleteUserPending(state, name);
|
isFetchUserPending(state, name) || isDeleteUserPending(state, name);
|
||||||
const error =
|
const error =
|
||||||
getFetchUserFailure(state, name) || getDeleteUserFailure(state, name);
|
getFetchUserFailure(state, name) || getDeleteUserFailure(state, name);
|
||||||
|
const usersLink = getUsersLink(state);
|
||||||
return {
|
return {
|
||||||
|
usersLink,
|
||||||
name,
|
name,
|
||||||
user,
|
user,
|
||||||
loading,
|
loading,
|
||||||
@@ -135,8 +138,8 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
|
|
||||||
const mapDispatchToProps = dispatch => {
|
const mapDispatchToProps = dispatch => {
|
||||||
return {
|
return {
|
||||||
fetchUser: (name: string) => {
|
fetchUser: (link: string, name: string) => {
|
||||||
dispatch(fetchUser(name));
|
dispatch(fetchUser(link, name));
|
||||||
},
|
},
|
||||||
deleteUser: (user: User, callback?: () => void) => {
|
deleteUser: (user: User, callback?: () => void) => {
|
||||||
dispatch(deleteUser(user, callback));
|
dispatch(deleteUser(user, callback));
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { Page, Paginator } from "@scm-manager/ui-components";
|
|||||||
import { UserTable } from "./../components/table";
|
import { UserTable } from "./../components/table";
|
||||||
import type { User, PagedCollection } from "@scm-manager/ui-types";
|
import type { User, PagedCollection } from "@scm-manager/ui-types";
|
||||||
import CreateUserButton from "../components/buttons/CreateUserButton";
|
import CreateUserButton from "../components/buttons/CreateUserButton";
|
||||||
|
import { getUsersLink } from "../../modules/indexResource";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
users: User[],
|
users: User[],
|
||||||
@@ -26,19 +27,20 @@ type Props = {
|
|||||||
canAddUsers: boolean,
|
canAddUsers: boolean,
|
||||||
list: PagedCollection,
|
list: PagedCollection,
|
||||||
page: number,
|
page: number,
|
||||||
|
usersLink: string,
|
||||||
|
|
||||||
// context objects
|
// context objects
|
||||||
t: string => string,
|
t: string => string,
|
||||||
history: History,
|
history: History,
|
||||||
|
|
||||||
// dispatch functions
|
// dispatch functions
|
||||||
fetchUsersByPage: (page: number) => void,
|
fetchUsersByPage: (link: string, page: number) => void,
|
||||||
fetchUsersByLink: (link: string) => void
|
fetchUsersByLink: (link: string) => void
|
||||||
};
|
};
|
||||||
|
|
||||||
class Users extends React.Component<Props> {
|
class Users extends React.Component<Props> {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.fetchUsersByPage(this.props.page);
|
this.props.fetchUsersByPage(this.props.usersLink, this.props.page);
|
||||||
}
|
}
|
||||||
|
|
||||||
onPageChange = (link: string) => {
|
onPageChange = (link: string) => {
|
||||||
@@ -107,6 +109,8 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
const loading = isFetchUsersPending(state);
|
const loading = isFetchUsersPending(state);
|
||||||
const error = getFetchUsersFailure(state);
|
const error = getFetchUsersFailure(state);
|
||||||
|
|
||||||
|
const usersLink = getUsersLink(state);
|
||||||
|
|
||||||
const page = getPageFromProps(ownProps);
|
const page = getPageFromProps(ownProps);
|
||||||
const canAddUsers = isPermittedToCreateUsers(state);
|
const canAddUsers = isPermittedToCreateUsers(state);
|
||||||
const list = selectListAsCollection(state);
|
const list = selectListAsCollection(state);
|
||||||
@@ -117,14 +121,15 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
error,
|
error,
|
||||||
canAddUsers,
|
canAddUsers,
|
||||||
list,
|
list,
|
||||||
page
|
page,
|
||||||
|
usersLink
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const mapDispatchToProps = dispatch => {
|
const mapDispatchToProps = dispatch => {
|
||||||
return {
|
return {
|
||||||
fetchUsersByPage: (page: number) => {
|
fetchUsersByPage: (link: string, page: number) => {
|
||||||
dispatch(fetchUsersByPage(page));
|
dispatch(fetchUsersByPage(link, page));
|
||||||
},
|
},
|
||||||
fetchUsersByLink: (link: string) => {
|
fetchUsersByLink: (link: string) => {
|
||||||
dispatch(fetchUsersByLink(link));
|
dispatch(fetchUsersByLink(link));
|
||||||
|
|||||||
@@ -32,21 +32,19 @@ export const DELETE_USER_PENDING = `${DELETE_USER}_${types.PENDING_SUFFIX}`;
|
|||||||
export const DELETE_USER_SUCCESS = `${DELETE_USER}_${types.SUCCESS_SUFFIX}`;
|
export const DELETE_USER_SUCCESS = `${DELETE_USER}_${types.SUCCESS_SUFFIX}`;
|
||||||
export const DELETE_USER_FAILURE = `${DELETE_USER}_${types.FAILURE_SUFFIX}`;
|
export const DELETE_USER_FAILURE = `${DELETE_USER}_${types.FAILURE_SUFFIX}`;
|
||||||
|
|
||||||
const USERS_URL = "users";
|
|
||||||
|
|
||||||
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
|
const CONTENT_TYPE_USER = "application/vnd.scmm-user+json;v=2";
|
||||||
|
|
||||||
// TODO i18n for error messages
|
// TODO i18n for error messages
|
||||||
|
|
||||||
// fetch users
|
// fetch users
|
||||||
|
|
||||||
export function fetchUsers() {
|
export function fetchUsers(link: string) {
|
||||||
return fetchUsersByLink(USERS_URL);
|
return fetchUsersByLink(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchUsersByPage(page: number) {
|
export function fetchUsersByPage(link: string, page: number) {
|
||||||
// backend start counting by 0
|
// backend start counting by 0
|
||||||
return fetchUsersByLink(USERS_URL + "?page=" + (page - 1));
|
return fetchUsersByLink(link + "?page=" + (page - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchUsersByLink(link: string) {
|
export function fetchUsersByLink(link: string) {
|
||||||
@@ -60,7 +58,7 @@ export function fetchUsersByLink(link: string) {
|
|||||||
})
|
})
|
||||||
.catch(cause => {
|
.catch(cause => {
|
||||||
const error = new Error(`could not fetch users: ${cause.message}`);
|
const error = new Error(`could not fetch users: ${cause.message}`);
|
||||||
dispatch(fetchUsersFailure(USERS_URL, error));
|
dispatch(fetchUsersFailure(link, error));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -89,8 +87,8 @@ export function fetchUsersFailure(url: string, error: Error): Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//fetch user
|
//fetch user
|
||||||
export function fetchUser(name: string) {
|
export function fetchUser(link: string, name: string) {
|
||||||
const userUrl = USERS_URL + "/" + name;
|
const userUrl = link.endsWith("/") ? link + name : link + "/" + name;
|
||||||
return function(dispatch: any) {
|
return function(dispatch: any) {
|
||||||
dispatch(fetchUserPending(name));
|
dispatch(fetchUserPending(name));
|
||||||
return apiClient
|
return apiClient
|
||||||
@@ -137,11 +135,11 @@ export function fetchUserFailure(name: string, error: Error): Action {
|
|||||||
|
|
||||||
//create user
|
//create user
|
||||||
|
|
||||||
export function createUser(user: User, callback?: () => void) {
|
export function createUser(link: string, user: User, callback?: () => void) {
|
||||||
return function(dispatch: Dispatch) {
|
return function(dispatch: Dispatch) {
|
||||||
dispatch(createUserPending(user));
|
dispatch(createUserPending(user));
|
||||||
return apiClient
|
return apiClient
|
||||||
.post(USERS_URL, user, CONTENT_TYPE_USER)
|
.post(link, user, CONTENT_TYPE_USER)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
dispatch(createUserSuccess());
|
dispatch(createUserSuccess());
|
||||||
if (callback) {
|
if (callback) {
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ const response = {
|
|||||||
responseBody
|
responseBody
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const URL = "users";
|
||||||
const USERS_URL = "/api/v2/users";
|
const USERS_URL = "/api/v2/users";
|
||||||
|
|
||||||
const error = new Error("KAPUTT");
|
const error = new Error("KAPUTT");
|
||||||
@@ -146,7 +147,7 @@ describe("users fetch()", () => {
|
|||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
|
|
||||||
return store.dispatch(fetchUsers()).then(() => {
|
return store.dispatch(fetchUsers(URL)).then(() => {
|
||||||
expect(store.getActions()).toEqual(expectedActions);
|
expect(store.getActions()).toEqual(expectedActions);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -157,7 +158,7 @@ describe("users fetch()", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(fetchUsers()).then(() => {
|
return store.dispatch(fetchUsers(URL)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(FETCH_USERS_PENDING);
|
expect(actions[0].type).toEqual(FETCH_USERS_PENDING);
|
||||||
expect(actions[1].type).toEqual(FETCH_USERS_FAILURE);
|
expect(actions[1].type).toEqual(FETCH_USERS_FAILURE);
|
||||||
@@ -169,7 +170,7 @@ describe("users fetch()", () => {
|
|||||||
fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod);
|
fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod);
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(fetchUser("zaphod")).then(() => {
|
return store.dispatch(fetchUser(URL, "zaphod")).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
|
expect(actions[1].type).toEqual(FETCH_USER_SUCCESS);
|
||||||
@@ -183,7 +184,7 @@ describe("users fetch()", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(fetchUser("zaphod")).then(() => {
|
return store.dispatch(fetchUser(URL, "zaphod")).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
expect(actions[0].type).toEqual(FETCH_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
|
expect(actions[1].type).toEqual(FETCH_USER_FAILURE);
|
||||||
@@ -201,7 +202,7 @@ describe("users fetch()", () => {
|
|||||||
fetchMock.getOnce(USERS_URL, response);
|
fetchMock.getOnce(USERS_URL, response);
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(createUser(userZaphod)).then(() => {
|
return store.dispatch(createUser(URL, userZaphod)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(CREATE_USER_PENDING);
|
expect(actions[0].type).toEqual(CREATE_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(CREATE_USER_SUCCESS);
|
expect(actions[1].type).toEqual(CREATE_USER_SUCCESS);
|
||||||
@@ -214,7 +215,7 @@ describe("users fetch()", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(createUser(userZaphod)).then(() => {
|
return store.dispatch(createUser(URL, userZaphod)).then(() => {
|
||||||
const actions = store.getActions();
|
const actions = store.getActions();
|
||||||
expect(actions[0].type).toEqual(CREATE_USER_PENDING);
|
expect(actions[0].type).toEqual(CREATE_USER_PENDING);
|
||||||
expect(actions[1].type).toEqual(CREATE_USER_FAILURE);
|
expect(actions[1].type).toEqual(CREATE_USER_FAILURE);
|
||||||
@@ -235,7 +236,7 @@ describe("users fetch()", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const store = mockStore({});
|
const store = mockStore({});
|
||||||
return store.dispatch(createUser(userZaphod, callback)).then(() => {
|
return store.dispatch(createUser(URL, userZaphod, callback)).then(() => {
|
||||||
expect(callMe).toBe("yeah");
|
expect(callMe).toBe("yeah");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user