Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from SystemEngineeringTeam/issue/14
Browse files Browse the repository at this point in the history
CurrentUser周りの変更
  • Loading branch information
wappon28dev authored May 28, 2024
2 parents d869e9a + 6248ec2 commit 1813677
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/layouts/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'the-new-css-reset/css/reset.css';
import { createGlobalStyle } from 'styled-components';
import LoginPage from '@/components/pages/login';
import Header from '@/components/shared/Header';
import { userAtomLoadable } from '@/stores/userAtom';
import { currentUserAtomLoadable } from '@/stores/currentUserAtom';
import { theme } from '@/utils/theme';

const { Header: HeaderContainer, Content } = Layout;
Expand All @@ -26,7 +26,7 @@ const GlobalStyle = createGlobalStyle`
`;

const BaseLayout = (): ReactElement => {
const user = useAtomValue(userAtomLoadable);
const user = useAtomValue(currentUserAtomLoadable);

if (user.state === 'hasError') throw Error('Failed to fetch user data');

Expand All @@ -44,7 +44,7 @@ const BaseLayout = (): ReactElement => {
<LoadingOutlined />
</Flex>
)}
{user.state === 'hasData' && (user.data.signined ? <Outlet /> : <LoginPage />)}
{user.state === 'hasData' && (user.data != null ? <Outlet /> : <LoginPage />)}
</Content>
</Layout>
</App>
Expand Down
11 changes: 5 additions & 6 deletions src/stores/userAtom.ts → src/stores/currentUserAtom.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { atom } from 'jotai';
import { loadable } from 'jotai/utils';
import { type SigninedUser } from '@/types/user';
import { type CurrentUser } from '@/types/user';
import { waitMs } from '@/utils/promise';

const fetchUser = async (): Promise<SigninedUser<boolean>> => {
const fetchCurrentUser = async (): Promise<CurrentUser> => {
await waitMs(2000);

const user: SigninedUser<boolean> = {
signined: true,
const user: CurrentUser = {
id: '0000-0000-0000-0000',
firstName: '智',
lastName: '佐藤',
Expand Down Expand Up @@ -38,5 +37,5 @@ const fetchUser = async (): Promise<SigninedUser<boolean>> => {
return user;
};

const userAtom = atom(async () => await fetchUser());
export const userAtomLoadable = loadable(userAtom);
const currentUserAtomAtom = atom(async () => await fetchCurrentUser());
export const currentUserAtomLoadable = loadable(currentUserAtomAtom);
10 changes: 1 addition & 9 deletions src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,4 @@ export type User<WithPrivate extends boolean> = UserBase &
(ActiveUserProps | OBOGMemberProps | ExternalMember) &
(WithPrivate extends true ? PrivateProps : Record<never, never>);

type IsSigninedUser = {
signined: true;
} & User<true>;

interface IsNotSigninedUser {
signined: false;
}

export type SigninedUser<T extends boolean> = T extends true ? IsSigninedUser : IsNotSigninedUser;
export type CurrentUser = User<true> | null;

0 comments on commit 1813677

Please sign in to comment.