작성자 없음
작성자 정보가 삭제된 글입니다.
작성
·
290
0
안녕하세요, 강의를 따라하던 중
사용자를 찾을 수 없다는 페이지가 떠서 어디서 오류가 났는지 확인을 못하겠어서 질문드립니다.
혹시몰라 이 강의 까지 커밋한 깃허브 주소 올려봅니다,,
https://github.com/cccodus313/Blah-Blah/tree/start
import { ServiceLayout } from '@/components/service_layout';
import { useAuth } from '@/contexts/auth_user.context';
import { InAuthUser } from '@/models/in_auth_user';
import {
Avatar,
Box,
Button,
Flex,
FormControl,
FormLabel,
Switch,
Text,
Textarea,
useToast,
VStack,
} from '@chakra-ui/react';
import { GetServerSideProps, NextPage } from 'next';
import { useState, useEffect } from 'react';
import ResizeTextarea from 'react-textarea-autosize';
import axios, { AxiosResponse } from 'axios';
import MessageItem from '@/components/message_item';
import { InMessage } from '@/models/message/in_message';
interface Props {
userInfo: InAuthUser | null;
}
async function postMessage({
message,
uid,
author,
}: {
message: string;
uid: string;
author?: {
displayName: string;
photoURL?: string;
};
}) {
if (message.length <= 0) {
return {
result: false,
message: '내용을 입력해주세요',
};
}
try {
await fetch(`/api/message.add`, {
method: 'post',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
uid,
message,
author,
}),
});
return {
result: true,
};
} catch (err) {
console.error(err);
return {
result: false,
message: '등록 실패',
};
}
}
const UserHomePage: NextPage<Props> = function ({ userInfo }) {
const [message, setMessage] = useState('');
const [isAnonymous, setAnonymous] = useState(true);
const [messageList, setMessageList] = useState<InMessage[]>([]);
const toast = useToast();
const { authUser } = useAuth();
async function fetchMessageList(uid: string) {
try {
const resp = await fetch(`/api/message.list?uid=${uid}`);
if (resp.status === 200) {
const data = await resp.json();
setMessageList(data);
}
} catch (err) {
console.log(err);
}
}
useEffect(() => {
if (userInfo === null) return;
fetchMessageList(userInfo.uid);
}, [userInfo]);
if (userInfo === null) {
return <p>사용자를 찾을 수 없습니다.</p>;
}
const isOwner = authUser !== null && authUser.uid === userInfo.uid;
return (
<ServiceLayout title={`${userInfo.displayName}의 홈`} minH="100vh" backgroundColor="gray.50">
<Box maxW="md" mx="auto" pt="6">
<Box borderWidth="1px" borderRadius="lg" overflow="hidden" mb="2" bg="white">
<Flex p="6">
<Avatar size="lg" src={userInfo.photoURL ?? 'https://bit.ly/broken-link'} mr="2" />
<Flex direction="column" justify="center">
<Text fontSize="md">{userInfo.displayName}</Text>
<Text fontSize="xs">{userInfo.email}</Text>
</Flex>
</Flex>
</Box>
<Box borderWidth="1px" borderRadius="lg" overflow="hidden" mb="2" bg="white">
<Flex align="center" p="2">
<Avatar
size="xs"
src={isAnonymous ? 'https://bit.ly/broken-link' : authUser?.photoURL ?? 'https://bit.ly/broken-link'}
mr="2"
/>
<Textarea
bg="gray.100"
border="none"
boxShadow="none !important"
placeholder="어떤이야기를 하고 싶나요?"
borderRadius="md"
resize="none"
minH="unset"
overflow="hidden"
fontSize="xs"
mr="2"
minRows={1}
maxRows={7}
as={ResizeTextarea}
value={message}
onChange={(e) => {
if (e.target.value) {
const lineCount = (e.target.value.match(/[^\n]*\n[^\n]*/gi)?.length ?? 1) + 1;
if (lineCount > 7) {
toast({
title: '최대 7줄까지만 입력가능합니다',
position: 'top-right',
});
return;
}
}
setMessage(e.target.value);
}}
/>
<Button
disabled={message.length === 0}
bgColor="#FFB86C"
color="white"
colorScheme="yellow"
variant="solid"
size="sm"
onClick={async () => {
const postData: {
message: string;
uid: string;
author?: {
displayName: string;
photoURL?: string;
};
} = {
message,
uid: userInfo.uid,
};
if (isAnonymous === false) {
postData.author = {
photoURL: authUser?.photoURL ?? 'https://bit.ly/broken-link',
displayName: authUser?.displayName ?? 'anonymous',
};
}
const messageResp = await postMessage(postData);
if (messageResp.result === false) {
toast({ title: '등록실패', position: 'top-right' });
}
setMessage('');
}}
>
등록
</Button>
</Flex>
<FormControl display="flex" alignItems="center" mt="1" mx="2" pb="2">
<Switch
size="sm"
colorScheme="orange"
id="anonymous"
mr="1"
isChecked={isAnonymous}
onChange={() => {
if (authUser === null) {
toast({
title: '로그인이 필요합니다',
position: 'top-right',
});
return;
}
setAnonymous((prev) => !prev);
}}
/>
<FormLabel htmlFor="anonymous" mb="0" fontSize="xx-small">
Anonymous
</FormLabel>
</FormControl>
</Box>
<VStack spacing="12px" mt="6">
{messageList.map((messageData) => (
<MessageItem
key={`message-item${userInfo.uid}-${messageData.id}`}
item={messageData}
uid={userInfo.uid}
displayName={userInfo.displayName ?? ''}
photoURL={userInfo.photoURL ?? 'https://bit.ly/broken-link'}
isOwner={isOwner}
/>
))}
</VStack>
</Box>
</ServiceLayout>
);
};
export const getServerSideProps: GetServerSideProps<Props> = async ({ query }) => {
const { screenName } = query;
if (screenName === undefined) {
return {
props: {
userInfo: null,
},
};
}
try {
const protocol = process.env.PROTOCOL || 'http';
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || '3000';
const baseUrl = `${protocol}://${host}:${port}`;
const userInfoResp: AxiosResponse<InAuthUser> = await axios(`${baseUrl}/api/user.info/${screenName}`);
return {
props: {
userInfo: userInfoResp.data ?? null,
},
};
} catch (err) {
console.error(err);
return {
props: {
userInfo: null,
},
};
}
};
export default UserHomePage;