Commit 4159c86e authored by Martin Kraft's avatar Martin Kraft Committed by Martin Kraft

MM-18357: Adds ability to retrieve team search results paginated. (#953)

* MM-18357: Adds ability to retrieve team search results paginated.

* MM-18357: Change for removal of API parameter.
parent 26e200e3
......@@ -713,4 +713,58 @@ describe('Actions.Teams', () => {
assert.equal(error, null);
});
it('searchTeams', async () => {
const userClient = TestHelper.createClient4();
nock(Client4.getUsersRoute()).
post('').
query(true).
reply(201, TestHelper.fakeUserWithId());
const user = await TestHelper.basicClient4.createUser(
TestHelper.fakeUser(),
null,
null,
TestHelper.basicTeam.invite_id
);
nock(Client4.getUsersRoute()).
post('/login').
reply(200, user);
await userClient.login(user.email, 'password1');
nock(Client4.getTeamsRoute()).
post('').
reply(201, TestHelper.fakeTeamWithId());
const userTeam = await userClient.createTeam(
TestHelper.fakeTeam()
);
nock(Client4.getTeamsRoute()).
post('/search').
reply(200, [TestHelper.basicTeam, userTeam]);
const {data} = await store.dispatch(Actions.searchTeams('test', 0));
const moreRequest = store.getState().requests.teams.getTeams;
if (moreRequest.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(moreRequest.error));
}
nock(Client4.getTeamsRoute()).
post('/search').
reply(200, {teams: [TestHelper.basicTeam, userTeam], total_count: 2});
const response = await store.dispatch(Actions.searchTeams('test', '', false, true));
const paginatedRequest = store.getState().requests.teams.getTeams;
if (paginatedRequest.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(paginatedRequest.error));
}
assert.ok(response.data.teams.length === 2);
});
});
......@@ -138,16 +138,36 @@ export function getTeams(page = 0, perPage: number = General.TEAMS_CHUNK_SIZE, i
};
}
export function searchTeams(term: string): ActionFunc {
return bindClientFunc({
clientFunc: Client4.searchTeams,
onRequest: TeamTypes.GET_TEAMS_REQUEST,
onSuccess: [TeamTypes.RECEIVED_TEAMS_LIST, TeamTypes.GET_TEAMS_SUCCESS],
onFailure: TeamTypes.GET_TEAMS_FAILURE,
params: [
term,
],
});
export function searchTeams(term: string, page?: number, perPage?: number): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: TeamTypes.GET_TEAMS_REQUEST, data: null}, getState);
let response;
try {
response = await Client4.searchTeams(term, page, perPage);
} catch (error) {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(batchActions([
{type: TeamTypes.GET_TEAMS_FAILURE, error},
logError(error),
]), getState);
return {error};
}
const teams = response.teams || response;
dispatch(batchActions([
{
type: TeamTypes.RECEIVED_TEAMS_LIST,
data: teams,
},
{
type: TeamTypes.GET_TEAMS_SUCCESS,
},
]), getState);
return {data: response};
};
}
export function createTeam(team: Team): ActionFunc {
......
......@@ -1008,12 +1008,12 @@ export default class Client4 {
);
};
searchTeams = (term: string) => {
searchTeams = (term: string, page?: number, perPage?: number) => {
this.trackEvent('api', 'api_search_teams');
return this.doFetch(
`${this.getTeamsRoute()}/search`,
{method: 'post', body: JSON.stringify({term})}
{method: 'post', body: JSON.stringify({term, page, per_page: perPage})}
);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment