Skip to content

Commit

Permalink
Merge pull request #66 from traP-jp/feature/#57
Browse files Browse the repository at this point in the history
♻️ useUserInfoのリファクタリングとそれに伴うエラーの修整
  • Loading branch information
cp-20 authored Jun 24, 2023
2 parents 4d3e149 + bcbb72c commit 1d27785
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/components/AuthButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { getApiBaseUrl } from '@/lib/getApiBaseUrl';

export const AuthButton: FC = () => {
const theme = useMantineTheme();
const { userId } = useUserInfo() ?? { userId: undefined };
const { data, error } = useUserInfo();
const { push } = useRouter();
const isMobile = useMediaQuery(theme.fn.smallerThan('sm'));

if (userId !== undefined) {
if (error === undefined && data !== undefined) {
return (
<>
<UserAvatar userId={userId} iconSize={isMobile ? 24 : 32} />
<UserAvatar userId={data.id} iconSize={isMobile ? 24 : 32} />
</>
);
}
Expand Down
18 changes: 10 additions & 8 deletions src/components/DashBoard/MyAchieves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ import { Loader } from '@mantine/core';
import type { FC } from 'react';
import useSWR from 'swr';
import { MissionList } from '@/components/MissionList';
import { useUserInfo } from '@/hooks/useUserInfo';
import { fetcher } from '@/lib/fetcher';
import { getApiBaseUrl } from '@/lib/getApiBaseUrl';
import type { GetMissionsResponse, GetUserResponse } from '@/schema/schema';
import type { GetMissionsResponse } from '@/schema/schema';

export const MyAchieves: FC = () => {
const { data } = useSWR<GetUserResponse>(
`${getApiBaseUrl()}/users/me`,
fetcher,
);
const { data: user, error } = useUserInfo();
const { data: missions } = useSWR<GetMissionsResponse>(
`${getApiBaseUrl()}/missions`,
fetcher,
);

if (user == undefined || missions === undefined)
return <Loader variant="oval" />;

if (error !== undefined) return <div>Something went wrong</div>;

const achieves = missions
? missions.filter((mission) => data?.achieves.includes(mission.id))
? missions.filter((mission) => user.achieves.includes(mission.id))
: undefined;

if (data == undefined) return <Loader variant="oval" />;

return <MissionList missions={achieves} />;
};
10 changes: 2 additions & 8 deletions src/components/DashBoard/Ranking.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { css } from '@emotion/react';
import { Center, Skeleton, Text, useMantineTheme } from '@mantine/core';
import type { FC } from 'react';
import useSWR from 'swr';
import { getRankSuffix } from '@/components/DashBoard/getRankSuffix';
import { fetcher } from '@/lib/fetcher';
import { getApiBaseUrl } from '@/lib/getApiBaseUrl';
import type { GetUserResponse } from '@/schema/schema';
import { useUserInfo } from '@/hooks/useUserInfo';

export const Ranking: FC = () => {
const theme = useMantineTheme();
const { data } = useSWR<GetUserResponse>(
`${getApiBaseUrl()}/users/me`,
fetcher,
);
const { data } = useUserInfo();

const getRankColor = (rank: number) =>
rank === 1
Expand Down
10 changes: 2 additions & 8 deletions src/components/DashBoard/useMyAchievesCount.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import useSWR from 'swr';
import { fetcher } from '@/lib/fetcher';
import { getApiBaseUrl } from '@/lib/getApiBaseUrl';
import type { GetUserResponse } from '@/schema/schema';
import { useUserInfo } from '@/hooks/useUserInfo';

export const useMyAchievesCount = () => {
const { data } = useSWR<GetUserResponse>(
`${getApiBaseUrl()}/users/me`,
fetcher,
);
const { data } = useUserInfo();

return data?.achieves.length;
};
10 changes: 4 additions & 6 deletions src/components/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export const UserAvatar: FC<UserAvatarProps> = ({ userId, iconSize }) => {
};

export const MyUserAvatar: FC<Omit<UserAvatarProps, 'userId'>> = (props) => {
const info = useUserInfo();
const { data, error } = useUserInfo();

if (info === undefined) return null;
if (data === undefined) return <Skeleton circle />;

const { userId } = info;
if (error !== undefined) return null;

if (userId === undefined) return <Skeleton circle />;

return <UserAvatar userId={userId} {...props} />;
return <UserAvatar userId={data.id} {...props} />;
};
14 changes: 7 additions & 7 deletions src/hooks/useUserInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import type { SWRResponse } from 'swr';
import useSWR from 'swr';
import { useConfirmation } from '@/features/confirmation/useConfirmation';
import type { FetchError } from '@/lib/fetcher';
Expand All @@ -11,13 +12,16 @@ type UseUserInfoOptions = {
requireAuth?: boolean;
};

export const useUserInfo = (option?: UseUserInfoOptions) => {
export const useUserInfo = (
option?: UseUserInfoOptions,
): SWRResponse<GetUserResponse, FetchError> => {
const { push } = useRouter();
const { openConfirmationDialog } = useConfirmation();
const { data, error } = useSWR<GetUserResponse, FetchError>(
const response = useSWR<GetUserResponse, FetchError>(
`${getApiBaseUrl()}/users/me`,
fetcher,
);
const { error } = response;

useEffect(() => {
if (!option?.requireAuth) return;
Expand All @@ -36,9 +40,5 @@ export const useUserInfo = (option?: UseUserInfoOptions) => {
});
}, [error, openConfirmationDialog, option?.requireAuth, push]);

if (error) {
return undefined;
}

return { userId: data?.id };
return response;
};
8 changes: 3 additions & 5 deletions src/pages/missions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import useSWR from 'swr';
import { Description } from '@/components/Description';
import { Layout } from '@/components/Layout';
import { MissionList } from '@/components/MissionList';
import { useUserInfo } from '@/hooks/useUserInfo';
import { fetcher } from '@/lib/fetcher';
import { getApiBaseUrl } from '@/lib/getApiBaseUrl';
import type { GetMissionsResponse, GetUserResponse } from '@/schema/schema';
import type { GetMissionsResponse } from '@/schema/schema';

const Missions: NextPage = () => {
const theme = useMantineTheme();
Expand All @@ -16,10 +17,7 @@ const Missions: NextPage = () => {
`${getApiBaseUrl()}/missions`,
fetcher,
);
const { data: user, error: userError } = useSWR<GetUserResponse>(
`${getApiBaseUrl()}/users/me`,
fetcher,
);
const { data: user, error: userError } = useUserInfo();

return (
<>
Expand Down
37 changes: 20 additions & 17 deletions src/pages/missions/[missionId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,31 @@ type MissionPageProps = {
const Mission: NextPage<MissionPageProps> = ({ missionName }) => {
const theme = useMantineTheme();
const { missionId } = useRouter().query as { missionId: string };
const { data, mutate } = useSWR<GetMissionResponse>(
const { data: mission, mutate } = useSWR<GetMissionResponse>(
`${getApiBaseUrl()}/missions/${missionId}`,
fetcher,
);
const { userId } = useUserInfo() ?? { userId: undefined };
const { data: user, error: userError } = useUserInfo();
const { notify } = useNotification();
const { animate, Canvas } = useClearAnimation();

const toggleClearHandler = async () => {
if (data === undefined) return;
if (userId === undefined) return;
if (mission === undefined) return;
if (user === undefined) return;
if (userError !== undefined) return;

const clear = userId ? !data.achievers.includes(userId) : false;
const clear = user.id ? !mission.achievers.includes(user.id) : false;

try {
await mutate(updateMissionState(missionId, userId, clear), {
await mutate(updateMissionState(missionId, user.id, clear), {
populateCache: true,
revalidate: false,
rollbackOnError: true,
optimisticData: {
...data,
...mission,
achievers: clear
? data.achievers.concat(userId)
: data.achievers.filter((user) => user !== userId),
? mission.achievers.concat(user.id)
: mission.achievers.filter((achiever) => achiever !== user.id),
},
});

Expand Down Expand Up @@ -115,8 +116,8 @@ const Mission: NextPage<MissionPageProps> = ({ missionName }) => {
line-height: 2rem;
`}
>
{data ? (
data.name
{mission ? (
mission.name
) : (
<Skeleton width="70%" height="2rem" radius="xl" />
)}
Expand All @@ -127,14 +128,14 @@ const Mission: NextPage<MissionPageProps> = ({ missionName }) => {
padding: 1rem;
`}
>
{data ? (
{mission ? (
<Text
color="dimmed"
css={css`
line-height: 1.15rem;
`}
>
{data.description}
{mission.description}
</Text>
) : (
<Skeleton width="100%" height="1.15rem" radius="xl" />
Expand All @@ -145,8 +146,10 @@ const Mission: NextPage<MissionPageProps> = ({ missionName }) => {

<Center>
<div>
{data ? (
userId !== undefined && data.achievers.includes(userId) ? (
{mission ? (
user !== undefined &&
userError === undefined &&
mission.achievers.includes(user.id) ? (
<Stack>
<Button variant="filled" size="lg" disabled>
クリア済み
Expand Down Expand Up @@ -197,8 +200,8 @@ const Mission: NextPage<MissionPageProps> = ({ missionName }) => {
達成した人
</h2>
<Flex p="1rem" gap="md">
{data
? data.achievers.map((achiever) => (
{mission
? mission.achievers.map((achiever) => (
<UserAvatar
userId={achiever}
key={achiever}
Expand Down

0 comments on commit 1d27785

Please sign in to comment.