generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: added users page layout * feat: get all users Co-authored-by: GuiSanto <guiontheworks@gmail.com>
- Loading branch information
1 parent
5789457
commit 2d79e64
Showing
8 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Controller, Get, Inject, UseGuards } from '@nestjs/common'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse | ||
} from '@nestjs/swagger'; | ||
|
||
import JwtAuthenticationGuard from 'libs/guards/jwtAuth.guard'; | ||
import { BadRequestResponse } from 'libs/swagger/errors/bad-request.swagger'; | ||
import { InternalServerErrorResponse } from 'libs/swagger/errors/internal-server-error.swagger'; | ||
import { UnauthorizedResponse } from 'libs/swagger/errors/unauthorized.swagger'; | ||
|
||
import UserDto from '../dto/user.dto'; | ||
import { GetUserApplication } from '../interfaces/applications/get.user.application.interface'; | ||
import { TYPES } from '../interfaces/types'; | ||
|
||
@ApiBearerAuth('access-token') | ||
@ApiTags('Users') | ||
@UseGuards(JwtAuthenticationGuard) | ||
@Controller('users') | ||
export default class UsersController { | ||
constructor( | ||
@Inject(TYPES.applications.GetUserApplication) | ||
private getUserApp: GetUserApplication | ||
) {} | ||
|
||
@ApiOperation({ summary: 'Retrieve a list of existing users' }) | ||
@ApiOkResponse({ description: 'Users successfully retrieved!', type: UserDto, isArray: true }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'Unauthorized', | ||
type: UnauthorizedResponse | ||
}) | ||
@ApiBadRequestResponse({ | ||
description: 'Bad Request', | ||
type: BadRequestResponse | ||
}) | ||
@ApiInternalServerErrorResponse({ | ||
description: 'Internal Server Error', | ||
type: InternalServerErrorResponse | ||
}) | ||
@Get() | ||
getAllUsers() { | ||
return this.getUserApp.getAllUsers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { GetServerSidePropsContext } from 'next'; | ||
|
||
import fetchData from 'utils/fetchData'; | ||
import { User } from '../types/user/user'; | ||
|
||
export const getAllUsers = (context?: GetServerSidePropsContext): Promise<User[]> => { | ||
return fetchData(`/users`, { context, serverSide: !!context }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,63 @@ | ||
import { ReactElement } from 'react'; | ||
import { ReactElement, Suspense } from 'react'; | ||
import { dehydrate, QueryClient, useQuery } from 'react-query'; | ||
import { GetServerSideProps, GetServerSidePropsContext } from 'next'; | ||
import { useSession } from 'next-auth/react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
|
||
import QueryError from 'components/Errors/QueryError'; | ||
import Layout from 'components/layouts/Layout'; | ||
import LoadingPage from 'components/loadings/LoadingPage'; | ||
import Flex from 'components/Primitives/Flex'; | ||
import { getAllUsers } from '../../api/userService'; | ||
import requireAuthentication from '../../components/HOC/requireAuthentication'; | ||
import { toastState } from '../../store/toast/atom/toast.atom'; | ||
import { ToastStateEnum } from '../../utils/enums/toast-types'; | ||
|
||
const Users = () => { | ||
const { data: session } = useSession({ required: true }); | ||
const setToastState = useSetRecoilState(toastState); | ||
|
||
if (!session) return null; // after getUsers issue, need to add || !data to the if | ||
return <Flex direction="column" />; | ||
const { data } = useQuery(['users'], () => getAllUsers(), { | ||
enabled: true, | ||
refetchOnWindowFocus: false, | ||
onError: () => { | ||
setToastState({ | ||
open: true, | ||
content: 'Error getting the users', | ||
type: ToastStateEnum.ERROR | ||
}); | ||
} | ||
}); | ||
|
||
if (!session || !data) return null; | ||
|
||
return ( | ||
<Flex direction="column"> | ||
<Suspense fallback={<LoadingPage />}> | ||
<QueryError> | ||
{data.map((user) => ( | ||
<h2>{user.email}</h2> | ||
))} | ||
</QueryError> | ||
</Suspense> | ||
</Flex> | ||
); | ||
}; | ||
|
||
Users.getLayout = (page: ReactElement) => <Layout>{page}</Layout>; | ||
|
||
export default Users; | ||
|
||
export const getServerSideProps: GetServerSideProps = requireAuthentication( | ||
async (context: GetServerSidePropsContext) => { | ||
const queryClient = new QueryClient(); | ||
await queryClient.prefetchQuery('users', () => getAllUsers(context)); | ||
|
||
return { | ||
props: { | ||
dehydratedState: JSON.parse(JSON.stringify(dehydrate(queryClient))) | ||
} | ||
}; | ||
} | ||
); | ||
|