Skip to content

Commit

Permalink
Merge pull request #622 from Aryex82/develop
Browse files Browse the repository at this point in the history
Change fetch implementation to RTK Query for Idle Members
  • Loading branch information
Ajeyakrishna-k authored Jun 21, 2023
2 parents c3274f6 + 2b4a6ea commit 6f58f46
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 69 deletions.
106 changes: 60 additions & 46 deletions __mocks__/handlers/members.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,66 @@ import { rest } from 'msw';
const URL = process.env.NEXT_PUBLIC_BASE_URL;

const membersHandlers = [
rest.get(`${URL}/members/idle`, (_, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
message: 'Idle members returned successfully!',
idleMemberUserNames: [
'rohan-rajgupta',
'sumit',
'swaraj',
'rohit',
'tanya',
'akshay',
'shubham',
'devashish',
'lakshay',
'rucha',
'swebert',
'nikhil',
'ishika',
'rajakvk',
'moses',
'prem',
'bhavesh',
'ankush',
'prakash',
'deipayan',
'mehul',
'ashwini',
'amanA',
'sagar',
'shankar',
'aman-saxena',
'harshith',
'pranav',
'ankur',
'pujarini',
'sanyogita',
'pavan',
'ankita',
'shashwat',
'vividh',
'rahil',
],
})
);
}),
rest.get(`${URL}/members/idle`, (_, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
message: 'Idle members returned successfully!',
idleMemberUserNames: [
'rohan-rajgupta',
'sumit',
'swaraj',
'rohit',
'tanya',
'akshay',
'shubham',
'devashish',
'lakshay',
'rucha',
'swebert',
'nikhil',
'ishika',
'rajakvk',
'moses',
'prem',
'bhavesh',
'ankush',
'prakash',
'deipayan',
'mehul',
'ashwini',
'amanA',
'sagar',
'shankar',
'aman-saxena',
'harshith',
'pranav',
'ankur',
'pujarini',
'sanyogita',
'pavan',
'ankita',
'shashwat',
'vividh',
'rahil',
],
})
);
}),
];

export const failedIdleMembersHandler = rest.get(
`${URL}/members/idle`,
(_, res, ctx) => {
return res(
ctx.status(500),
ctx.json({
statusCode: 500,
error: 'Internal Server Error',
message: 'An internal server error occurred',
})
);
}
);

export default membersHandlers;
73 changes: 73 additions & 0 deletions __tests__/Unit/hooks/membersApi.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { setupServer } from 'msw/node';
import handlers, {
failedIdleMembersHandler,
} from '../../../__mocks__/handlers/members.handler.js';

import { PropsWithChildren } from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import { Provider } from 'react-redux';
import { store } from '@/app/store';
import { useGetIdleMembersQuery } from '@/app/services/membersApi';

const server = setupServer(...handlers);

beforeAll(() => {
server.listen();
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

function Wrapper({
children,
}: PropsWithChildren<Record<string, never>>): JSX.Element {
return <Provider store={store()}>{children}</Provider>;
}

describe('useGetIdleMembersQuery', () => {
test('returns idle members', async () => {
const { result, waitForNextUpdate } = renderHook(
() => useGetIdleMembersQuery(),
{
wrapper: Wrapper,
}
);

const initialResponse = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(true);

await act(() => waitForNextUpdate());

const nextResponse = result.current;
expect(nextResponse.data).not.toBeUndefined();
expect(nextResponse.isLoading).toBe(false);
expect(nextResponse.isSuccess).toBe(true);
expect(nextResponse.data).toBeDefined();
});

test('checks for error response', async () => {
server.use(failedIdleMembersHandler);
const { result, waitForNextUpdate } = renderHook(
() => useGetIdleMembersQuery(),
{
wrapper: Wrapper,
}
);

const initialResponse = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(true);

await act(() => waitForNextUpdate());

const nextResponse = result.current;
expect(nextResponse.isError).toBe(true);
expect(nextResponse.error).toHaveProperty('status', 500);

expect(nextResponse.error).toHaveProperty('data', {
statusCode: 500,
error: 'Internal Server Error',
message: 'An internal server error occurred',
});
});
});
1 change: 1 addition & 0 deletions src/app/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const api = createApi({
'User',
'Tags',
'Levels',
'Idle_Members',
'User_Standup',
],
/**
Expand Down
22 changes: 22 additions & 0 deletions src/app/services/membersApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MEMBERS_IDLE } from '@/constants/url';
import { api } from './api';

type IdleMembersResponse = { idleMemberUserNames: string[] };
export const membersApi = api.injectEndpoints({
endpoints: (build) => ({
getIdleMembers: build.query<string[], void>({
query: () => MEMBERS_IDLE,
providesTags: ['Idle_Members'],
transformResponse: (response: IdleMembersResponse) => {
const filterMembers = response.idleMemberUserNames.filter(
(username: string) => username
);
const sortedIdleMembers = filterMembers.sort();

return sortedIdleMembers;
},
}),
}),
});

export const { useGetIdleMembersQuery } = membersApi;
1 change: 1 addition & 0 deletions src/constants/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const LOGIN_URL = `https://github.com/login/oauth/authorize?client_id=23c
export const MEMBERS_URL = 'https://members.realdevsquad.com';
export const CHALLENGES_URL = `${BASE_URL}/challenges`;
export const USER_SELF = `${BASE_URL}/users/self`;
export const MEMBERS_IDLE = `${BASE_URL}/members/idle`;
export const DEFAULT_AVATAR = '/Avatar.png';
export const RDS_LOGO = '/RDSLogo.png';
export const GITHUB_LOGO = '/github-white.png';
Expand Down
33 changes: 10 additions & 23 deletions src/pages/availability-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import updateTasksStatus from '@/helperFunctions/updateTasksStatus';
import { AVAILABLE } from '@/constants/task-status';
import { FEATURE } from '@/constants/task-type';
import { BASE_URL } from '@/constants/url';
import { useGetIdleMembersQuery } from '@/app/services/membersApi';

const AvailabilityPanel: FC = () => {
const [idleMembersList, setIdleMembersList] = useState<string[]>([]);
const [unAssignedTasks, setUnAssignedTasks] = useState<task[]>([]);
const [error, setError] = useState<boolean>(false);
const [isTaskLoading, setIsTaskLoading] = useState<boolean>(true);
const [isMemberLoading, setIsMemberLoading] = useState<boolean>(true);
const [refreshData, setRefreshData] = useState<boolean>(false);
const {
data: idleMembersList = [],
isError: isIdleMembersError,
isLoading: isIdleMemberLoading,
refetch: refreshMemberList,
} = useGetIdleMembersQuery();

useEffect(() => {
const fetchTasks = async () => {
Expand All @@ -36,42 +41,24 @@ const AvailabilityPanel: FC = () => {
setIsTaskLoading(false);
}
};
const fetchIdleUsers = async () => {
try {
const url = `${process.env.NEXT_PUBLIC_BASE_URL}/members/idle`;
const { requestPromise } = fetch({ url });
const fetchPromise = await requestPromise;
const { idleMemberUserNames } = fetchPromise.data;
const filterMembers = idleMemberUserNames.filter(
(username: string) => username
);
const sortedIdleMembers = filterMembers.sort();
setIdleMembersList(sortedIdleMembers);
setError(false);
} catch (Error) {
setError(true);
} finally {
setIsMemberLoading(false);
}
};
fetchTasks();
fetchIdleUsers();
}, [refreshData]);

let isErrorOrIsLoading;
if (error) {
if (error || isIdleMembersError) {
isErrorOrIsLoading = (
<span className={classNames.statusMessage}>
Something went wrong, please contact admin!
</span>
);
} else if (isTaskLoading || isMemberLoading) {
} else if (isTaskLoading || isIdleMemberLoading) {
isErrorOrIsLoading = (
<span className={classNames.statusMessage}>Loading...</span>
);
}

const getData = () => {
refreshMemberList();
setRefreshData(!refreshData);
};

Expand Down

0 comments on commit 6f58f46

Please sign in to comment.