Skip to content

Commit

Permalink
Merge pull request #52 from Andrevile/feature/#28-PersonalProfile
Browse files Browse the repository at this point in the history
Feature/#28 personal profile
  • Loading branch information
Andrevile authored May 11, 2022
2 parents 01492c1 + f25ba95 commit a862542
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 43 deletions.
7 changes: 2 additions & 5 deletions apis/base.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export default class BaseAPI {

this.instance.interceptors.request.use((config: CustomAxiosRequestConfig) => {
if (config.isRequiredLogin) {
this.handleAuthenticationInterceptor;
// config.headers[
// 'Authorization'
// ] = `Bearer "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTEyMjkwODguMDQ3MjY3LCJleHAiOjE2NTE4MzM4ODguMDQ3MjY3LCJzb2NpYWxfaWQiOiIxMDc1MTg4NDkwMzk1ODQ2MTI2ODYifQ.JqVEYVlTvLB_8YYZBuWEe6fYO75xTZtA1PmMYOUAH_o"`;
config = this.handleAuthenticationInterceptor(config);
}

return config;
Expand Down Expand Up @@ -62,7 +59,7 @@ export default class BaseAPI {
return {
...config,
headers: {
Authorization: `Bearer ${this.getJwtToken()}`,
Authorization: `Bearer "${this.getJwtToken()}"`,
},
};
}
Expand Down
23 changes: 23 additions & 0 deletions apis/teams.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TeamTypes } from 'types/team';
import BaseAPI from './base.api';

import type { CustomAxiosRequestConfig } from './type';

class TeamsAPI extends BaseAPI {
//https://apibora.shop/api/team/
checkTeamProfile(params: unknown) {
return this.get<TeamTypes>(`/${params}`);
}

// editUser(params: unknown, body: UserEditForm, config: CustomAxiosRequestConfig) {
// return this.put(`/${params}`, body, config);
// }
// kakaoOauth(body, config?: CustomAxiosRequestConfig) {
// return this.post<PostOauthBody, PostOauthResponse>('kakao', body, { ...config });
// }
// googleOauth(body, config?: CustomAxiosRequestConfig) {
// return this.post<PostOauthBody, PostOauthResponse>('google', body, { ...config });
// }
}

export default new TeamsAPI('team');
2 changes: 2 additions & 0 deletions constants/imgUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const edit_icon = '/images/edit-icon.svg';
export const following_icon = '/images/icon-add-round.svg';

export const message_icon = '/images/icon-message.svg';

export const team_profile_icon = '/images/team-profile-icon.svg';
7 changes: 6 additions & 1 deletion constants/tabMenu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const tabMenuArr = [
export const userTabMenuArr = [
{ id: 'post', name: '작업물', isActive: true },
{ id: 'scrap', name: '스크랩', isActive: false },
];

export const teamTabMenuArr = [
{ id: 'post', name: '작업물', isActive: true },
{ id: 'member', name: '멤버', isActive: false },
];
18 changes: 18 additions & 0 deletions hooks/useForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { ChangeEvent, Dispatch, SetStateAction, useCallback, useState } from 'react';

const useForm = <T>(
initialData: T
): [T, Dispatch<SetStateAction<T>>, (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void] => {
const [values, setValues] = useState(initialData);
const handler = useCallback(
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setValues({ ...values, [name]: value });
},
[values, setValues]
);

return [values, setValues, handler];
};

export default useForm;
4 changes: 0 additions & 4 deletions hooks/useImageHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const useImageHandle = (name: string) => {
secretAccessKey: process.env.NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY,
});

console.log(awsObj);

const myBucket = new AWS.S3({
params: { Bucket: process.env.NEXT_PUBLIC_AWS_STORAGE_BUCKET_NAME },
region: process.env.NEXT_PUBLIC_AWS_REGION,
Expand All @@ -19,8 +17,6 @@ const useImageHandle = (name: string) => {
const storeImage = async (e: React.ChangeEvent<HTMLInputElement>) => {
const currentFile = (e.target as HTMLInputElement).files[0];
const currentFileName = currentFile.name.replaceAll(' ', '');
console.log(currentFile);
console.log(currentFileName);

const params = {
ACL: 'public-read',
Expand Down
64 changes: 36 additions & 28 deletions pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { useQuery, useMutation, useQueryClient } from 'react-query';
import Image from 'next/image';
import styled from 'styled-components';
Expand All @@ -9,7 +9,7 @@ import ImageUploadWrapper from 'components/common/ImageUploadWrapper';
import { ProfileIcon, ProfileWrapper, CameraIcon, CameraIconWrapper } from 'components/common/Atomic/Profile';
import { TabButton } from 'components/common/Atomic/Tabs/TabButton';
import { camera_icon, default_profile } from 'constants/imgUrl';
import { tabMenuArr } from 'constants/tabMenu';
import { userTabMenuArr } from 'constants/tabMenu';
import usersApi from 'apis/users.api';
import { UserEditForm } from 'types/user';
import { userEditForm } from 'utils/userEditForm';
Expand All @@ -18,25 +18,39 @@ import { Keyword } from 'components/common/Atomic/Tabs/Keyword';
import ProfileEdit from 'components/Profile/ProfileEdit';
import UploadProduct from 'components/Profile/UploadProduct';
import { UploadButton } from 'components/common/Atomic/Tabs/Button';
import useForm from 'hooks/useForm';
import Router from 'next/router';
import { userInfo } from 'recoil/auth';
import { useRecoilState } from 'recoil';

const Profile = () => {
const queryClient = useQueryClient();
const { isLoading, isError, error, data } = useQuery(['user-profile'], () => usersApi.checkUsers(4), {
onSuccess: (data) => {
setTestForm(userEditForm(data));
},
}); // useQuery로 유저정보 받아옴.
const [, setUserInfo] = useRecoilState(userInfo);

const { mutate: userInfoMutate } = useMutation(() => usersApi.editUser(4, testForm, { isRequiredLogin: true }), {
onSuccess: (data) => {
console.log('data', data);
queryClient.setQueryData('user-profile', data);
},
});
const { isLoading, isError, error, data } = useQuery(
['user-profile'],
() => usersApi.checkUsers(sessionStorage.getItem('id')),
{
onSuccess: (data) => {
setValues(userEditForm(data));
setUserInfo(data);
console.log(data);
},
}
);

const { mutate: userInfoMutate } = useMutation(
() => usersApi.editUser(sessionStorage.getItem('id'), values, { isRequiredLogin: true }),
{
onSuccess: ({ data }) => {
queryClient.setQueryData('user-profile', data);
},
}
);

const [editMode, setEditMode] = useState<boolean>(false);
const [currentTab, setCurrentTab] = useState('post');
const [testForm, setTestForm] = useState<UserEditForm>();
const [values, setValues, handler] = useForm<UserEditForm>();
//Suspense를 사용하게 된다면, useQuery를 여러개 선언하는것은 사용할 수 없으므로, useQueries를 사용해야함
const Items = {
post: ['작업물1'], //['아이템1', '아이템2'],
Expand Down Expand Up @@ -66,7 +80,7 @@ const Profile = () => {

const selectTab = useCallback(
(id: string) => () => {
tabMenuArr.forEach((tab) => {
userTabMenuArr.forEach((tab) => {
if (tab.id === id) {
tab.isActive = true;
setCurrentTab(tab.id);
Expand All @@ -78,17 +92,11 @@ const Profile = () => {
[currentTab]
);

const testFormHook = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
const { name, value } = e.target;
console.log('name = %s value = %s', name, value);
console.log('banner : ', testForm.backgroundImage);
console.log('profile : ', testForm.profileImage);
console.log('description : ', testForm.description);
setTestForm({ ...testForm, [name]: value });
},
[testForm, setTestForm]
);
useEffect(() => {
if (!sessionStorage.getItem('jwtToken')) {
Router.push('/');
}
}, []);
if (isLoading) {
return <h1>Loading</h1>;
}
Expand Down Expand Up @@ -144,7 +152,7 @@ const Profile = () => {
<span>{numberWithCommas(data?.followingCount)}</span>
</FollowInfo>
{editMode ? (
<DescriptionArea name="description" onChange={testFormHook} placeholder="사용자 소개를 입력해주세요." />
<DescriptionArea name="description" onChange={handler} placeholder="사용자 소개를 입력해주세요." />
) : (
<p>{data?.description}</p>
)}
Expand All @@ -157,7 +165,7 @@ const Profile = () => {
</InfoAside>
</InfoWrapper>
<div style={{ marginBottom: '40px' }}>
{tabMenuArr.map((tab, i) => (
{userTabMenuArr.map((tab, i) => (
<TabButton active={tab.isActive} key={i} onClick={selectTab(tab.id)}>
{tab.name}
<span>{Items[tab.id].length}</span>
Expand Down
6 changes: 3 additions & 3 deletions pages/user/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Layout from 'components/Layout';
import Banner from 'components/Profile/Banner';
import ItemList from 'components/Profile/ItemList';
import { TabButton } from 'components/common/Atomic/Tabs/TabButton';
import { tabMenuArr } from 'constants/tabMenu';
import { userTabMenuArr } from 'constants/tabMenu';
import usersApi from 'apis/users.api';
import { useRouter } from 'next/router';
import Image from 'next/image';
Expand Down Expand Up @@ -48,7 +48,7 @@ const UserProfile: React.FC = () => {

const selectTab = useCallback(
(id: string) => () => {
tabMenuArr.forEach((tab) => {
userTabMenuArr.forEach((tab) => {
if (tab.id === id) {
tab.isActive = true;
setCurrentTab(tab.id);
Expand Down Expand Up @@ -101,7 +101,7 @@ const UserProfile: React.FC = () => {
</InfoAside>
</InfoWrapper>
<div style={{ marginBottom: '40px' }}>
{tabMenuArr.map((tab, i) => (
{userTabMenuArr.map((tab, i) => (
<TabButton active={tab.isActive} key={i} onClick={selectTab(tab.id)}>
{tab.name}
<span>{Items[tab.id].length}</span>
Expand Down
8 changes: 8 additions & 0 deletions public/images/team-profile-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions recoil/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { atom } from 'recoil';
import { User } from 'types/user';

export interface UserCategoryType {
mainCategory: number;
Expand All @@ -21,6 +22,11 @@ export interface socialLoginState {
socialType: string;
}

export const userInfo = atom<User | null>({
key: 'userInfo',
default: null,
});

export const userRegisterInfoState = atom<UserRegisterInfoType>({
key: 'userRegisterInfoState',
default: {
Expand Down
11 changes: 11 additions & 0 deletions types/team.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface TeamTypes {
id: number;
title: string;
description: string;
leader: number;
teamProfileImage: string;
backgroundImage: string;
postCount: number;
memberCount: number;
teamFollowCount: number;
}
2 changes: 1 addition & 1 deletion types/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface User {
id: number;
id: string;
email: string;
nickname: string;
description: string;
Expand Down
1 change: 0 additions & 1 deletion utils/userEditForm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { User, UserEditForm } from 'types/user';

export const userEditForm = (user: User): UserEditForm => {
console.log('user', user);
return {
email: user.email,
nickname: user.nickname,
Expand Down

0 comments on commit a862542

Please sign in to comment.