Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add profile page #30

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/layouts/Private/components/AvatarSubmenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const AvatarSubmenu = ({ user }: AvatarSubmenuProps): JSX.Element | null => {
key="profile-button"
label="Profile"
variant="neutral"
href={`/profile/${user.id}`}
/>,
<Button
IconLeft={<SignOut />}
Expand Down
41 changes: 41 additions & 0 deletions src/pages/profile/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';

import { UserProps } from '@types';
import nookies from 'nookies';

import PrivateLayout from 'layouts/Private';

import ProfilePage from 'views/Profile';

type ServerSideProps = GetServerSideProps<{
user: UserProps;
}>;

const Page = ({ user }: InferGetServerSidePropsType<ServerSideProps>) => {
return (
<PrivateLayout user={user}>
<ProfilePage user={user} />
</PrivateLayout>
);
};

export const getServerSideProps: ServerSideProps = async (context) => {
const { user: cookiesUser } = nookies.get(context);

if (!cookiesUser) {
return {
redirect: {
destination: '/signin',
permanent: false,
},
};
}

const user = JSON.parse(cookiesUser) as UserProps;

return {
props: { user },
};
};

export default Page;
11 changes: 11 additions & 0 deletions src/views/Profile/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Post {
id: string;
body: string;
author_id: string;
created_at: Date;
}

export interface PostNewPostParams {
author_id: Post['author_id'];
body: Post['body'];
}
188 changes: 188 additions & 0 deletions src/views/Profile/__snapshots__/tests.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Profile page should render correctly 1`] = `
.c2 {
position: relative;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
width: auto;
height: 4.4rem;
border: 0.1rem solid #C9CAC8;
border-radius: 0.4rem;
margin: 0;
padding: 0 1.4rem;
gap: 1.2rem;
text-transform: none;
-webkit-transition: background-color 0.2s, color 0.2s;
transition: background-color 0.2s, color 0.2s;
cursor: pointer;
color: #181917;
background-color: #FDFDFD;
border-color: #30312F;
}

.c2:hover {
background-color: #82ADB2;
}

.c2 svg {
color: inherit;
}

.c2:hover {
color: #FDFDFD;
background-color: #181917;
}

.c2 .spin {
-webkit-animation-name: ieAXeN;
animation-name: ieAXeN;
-webkit-animation-duration: 2s;
animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
}

.c4 {
position: relative;
padding: 0;
margin: 0;
font-size: 2.4rem;
font-weight: 500;
font-family: 'Montserrat',sans-serif;
line-height: 4.0rem;
font-weight: bold;
color: #181917;
text-transform: none;
box-sizing: border-box;
}

.c0 {
grid-area: Content;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
padding: 2.0rem;
gap: 2.0rem;
width: 100%;
max-width: 600px;
height: 100%;
margin: auto;
overflow-y: auto;
}

.c1 {
display: grid;
grid-template-columns: 10rem 100%;
grid-template-rows: auto;
grid-template-areas: 'Back .' 'AuthorMetadata AuthorMetadata';
width: 100%;
gap: 1.2rem;
}

.c5 {
display: grid;
grid-template-columns: 1fr;
width: 100%;
gap: 1.4rem;
margin-bottom: auto;
}

.c3 {
grid-area: AuthorMetadata;
}

.c6 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
height: 100%;
width: 100%;
}

<main
class="c0"
>
<div
class="c1"
>
<button
class="c2"
>
<svg
fill="currentColor"
height="1em"
viewBox="0 0 256 256"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<rect
fill="none"
height="256"
width="256"
/>
<polyline
fill="none"
points="92.1 148.1 28.1 148.1 28.1 84.1"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
/>
<path
d="M224,184A96,96,0,0,0,60.1,116.1l-32,32"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="16"
/>
</svg>
Home
</button>
<section
class="c3"
>
<h2
class="c4"
>
John's profile
</h2>
</section>
</div>
<div
class="c5"
>
<div
class="c6"
>
You have no posts yet
</div>
</div>
</main>
`;
38 changes: 38 additions & 0 deletions src/views/Profile/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useCallback, useState } from 'react';

import { useMutation, useQuery } from '@tanstack/react-query';
import { UserProps } from '@types';

import { AllPostsProps, getUserPosts } from 'services/posts.api';

import { QUERY_KEYS } from 'constant';

import ProfileTemplate from './template';

interface Props {
user: UserProps;
}

const Profile = ({ user }: Props): JSX.Element => {
const [posts, setPosts] = useState<AllPostsProps[]>([]);

const loadPosts = useCallback(async () => {
const postsData = await getUserPosts({
author_id: user.id
});

setPosts(postsData);
}, [user.id]);

const query = useQuery([QUERY_KEYS.posts], loadPosts);

return (
<ProfileTemplate
userData={user}
isLoading={query.isLoading}
postsData={posts}
/>
);
};

export default Profile;
67 changes: 67 additions & 0 deletions src/views/Profile/template/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { UserProps } from '@types';
import {ArrowArcLeft} from 'phosphor-react';

import Button from 'components/Button';
import Img from 'components/Img';
import Text from 'components/Text';

import { PrivateContent } from 'layouts/Private/styles';

import { AllPostsProps } from 'services/posts.api';

import * as Styles from './styles';

interface Props {
userData: UserProps;
isLoading: boolean;
postsData: AllPostsProps[];
}

const HomeTemplate = ({
userData,
isLoading,
postsData,
}: Props): JSX.Element => {
return (
<PrivateContent>
<Styles.ProfileWrapper>
<Button
IconLeft={<ArrowArcLeft />}
label="Home"
href="/home"
variant='outline'
/>

<Styles.AuthorMetadata>
<Text label={`${userData.first_name}'s profile`} dimension="heading2" isBold />
</Styles.AuthorMetadata>
</Styles.ProfileWrapper>
<Styles.PostsWrapper>
{Boolean(postsData.length) ? (
postsData.map((post) => (
<Styles.Post key={post.id}>
<Styles.AvatarWrapper>
<Img src={post.users.avatar_url} alt={post.users.first_name} />
</Styles.AvatarWrapper>

<Styles.AuthorMetadata>
<Text
label={`${post.users.first_name} ${post.users.last_name}`}
isBold
/>
</Styles.AuthorMetadata>

<Styles.PostMessage>
<Text label={post.body} />
</Styles.PostMessage>
</Styles.Post>
))
) : (
<Styles.Empty>You have no posts yet</Styles.Empty>
)}
</Styles.PostsWrapper>
</PrivateContent>
);
};

export default HomeTemplate;
Loading