Skip to content

Commit

Permalink
Add useProjectQuery for handling project detail fetching
Browse files Browse the repository at this point in the history
- Added `useProjectQuery` hook to handle project detail fetching for `ProjectDetailPage` component
- Updated network handler to reflect axios' error response in msw implementation
- Updated test cases to reflect changes and removed unnecessary props in `ProjectDetailPage`
  • Loading branch information
HelNershingThapa committed Jun 29, 2023
1 parent d583d81 commit fe2353c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
14 changes: 14 additions & 0 deletions frontend/src/api/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ export const useProjectsQuery = (fullProjectsQuery, action) => {
});
};

export const useProjectQuery = (projectId) => {
const fetchProject = ({ signal, queryKey }) => {
const [, projectId] = queryKey;
return api().get(`projects/${projectId}/`, {
signal,
});
};

return useQuery({
queryKey: ['project', projectId],
queryFn: fetchProject,
});
};

const backendToQueryConversion = {
difficulty: 'difficulty',
campaign: 'campaign',
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/network/tests/server-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,14 @@ const faultyHandlers = [
}),
);
}),
rest.get(API_URL + 'projects/:id/', failedToConnectError),
rest.get(API_URL + 'projects/:id/', async (req, res, ctx) => {
return res.once(
ctx.status(403),
ctx.json({
SubCode: `Project Not Found`,
}),
);
}),
rest.get('http://127.0.0.1:8111/version', failedToConnectError),
rest.post(
API_URL + 'projects/:projectId/tasks/actions/lock-for-mapping/:taskId',
Expand Down
46 changes: 21 additions & 25 deletions frontend/src/views/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useExploreProjectsQueryParams, stringify } from '../hooks/UseProjectsQu
import { useSetTitleTag } from '../hooks/UseMetaTags';
import { NotFound } from './notFound';
import { ProjectDetailPlaceholder } from '../components/projectDetail/projectDetailPlaceholder';
import { useProjectsQuery } from '../api/projects';
import { useProjectsQuery, useProjectQuery } from '../api/projects';

const ProjectCreate = React.lazy(() => import('../components/projectCreate/index'));

Expand Down Expand Up @@ -163,39 +163,35 @@ export const MoreFilters = () => {
export const ProjectDetailPage = () => {
const { id } = useParams();
const navigate = useNavigate();
const [error, loading, data] = useFetch(`projects/${id}/`, id);
const { data: project, status, error } = useProjectQuery(id);

useEffect(() => {
window.scrollTo(0, 0);
}, [id]);

return (
return status === 'loading' ? (
<ReactPlaceholder
showLoadingAnimation={true}
customPlaceholder={<ProjectDetailPlaceholder />}
delay={1000}
ready={loading === false}
>
{!error && (
<ProjectDetail
project={data}
projectLoading={loading}
tasksError={error}
tasks={data.tasks}
navigate={navigate}
type="detail"
/>
)}
{error && (
<>
{error.message === 'PrivateProject' ? (
<PrivateProjectError />
) : (
<NotFound projectId={id} />
)}
</>
ready={false}
/>
) : status === 'error' ? (
<>
{error.response.data.SubCode === 'PrivateProject' ? (
<PrivateProjectError />
) : (
<NotFound projectId={id} />
)}
</ReactPlaceholder>
</>
) : (
<ProjectDetail
project={project.data}
projectLoading={false}
tasksError={false}
tasks={project.data.tasks}
navigate={navigate}
type="detail"
/>
);
};

Expand Down
24 changes: 15 additions & 9 deletions frontend/src/views/tests/project.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ describe('Project Detail Page', () => {
store.dispatch({ type: 'SET_LOCALE', locale: 'es-AR' });
});
renderWithRouter(
<ReduxIntlProviders>
<ProjectDetailPage id={123} navigate={() => jest.fn()} />
</ReduxIntlProviders>,
<QueryClientProviders>
<ReduxIntlProviders>
<ProjectDetailPage />
</ReduxIntlProviders>
</QueryClientProviders>,
);
await waitFor(() => {
expect(screen.getByText(/sample project/i)).toBeInTheDocument();
Expand All @@ -244,9 +246,11 @@ describe('Project Detail Page', () => {
<Route
path="projects/:id"
element={
<ReduxIntlProviders>
<ProjectDetailPage id={123} navigate={() => jest.fn()} />
</ReduxIntlProviders>
<QueryClientProviders>
<ReduxIntlProviders>
<ProjectDetailPage />
</ReduxIntlProviders>
</QueryClientProviders>
}
/>
</Routes>
Expand All @@ -268,9 +272,11 @@ describe('Project Detail Page', () => {
<Route
path="projects/:id"
element={
<ReduxIntlProviders>
<ProjectDetailPage id={123} navigate={() => jest.fn()} />
</ReduxIntlProviders>
<QueryClientProviders>
<ReduxIntlProviders>
<ProjectDetailPage />
</ReduxIntlProviders>
</QueryClientProviders>
}
/>
</Routes>
Expand Down

0 comments on commit fe2353c

Please sign in to comment.