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

セッションが切れた際にログアウトさせる #777

Merged
merged 6 commits into from
Jun 26, 2024
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
36 changes: 26 additions & 10 deletions view/next-project/src/components/layout/MainLayout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,46 @@ import { useRouter } from 'next/router';
import React, { useEffect, useState } from 'react';
import { useRecoilState } from 'recoil';
import s from './MainLayout.module.css';

import { authAtom } from '@/store/atoms';
import { authAtom, userAtom } from '@/store/atoms';
import 'tailwindcss/tailwind.css';
import { Header, SideNav } from '@components/common';
import { User } from '@type/common';
import { get_with_token_valid } from '@utils/api/api_methods';

interface LayoutProps {
children?: React.ReactNode;
}

export default function MainLayout(props: LayoutProps) {
const router = useRouter();
const [auth] = useRecoilState(authAtom);
const [auth, setAuth] = useRecoilState(authAtom);
const [_, setUser] = useRecoilState(userAtom);
const [isSideNavOpen, setIsSideNavOpen] = useState(true);

useEffect(() => {
if (router.isReady) {
if (!auth.isSignIn) {
router.push('/');
const getCurrentUserUrl = process.env.CSR_API_URI + '/current_user';
get_with_token_valid(getCurrentUserUrl, auth.accessToken).then((result) => {
if (!result) {
localStorage.clear();
} else if (auth.isSignIn === true && router.pathname == '/') {
router.push('/purchaseorders');
const authData = {
isSignIn: false,
accessToken: '',
};
setAuth(authData);
setUser({} as User);
router.push('/');
} else {
if (router.isReady) {
if (!auth.isSignIn) {
router.push('/');
localStorage.clear();
} else if (auth.isSignIn === true && router.pathname == '/') {
router.push('/purchaseorders');
}
}
}
}
}, [router, auth]);
});
}, [router]);

return (
<>
Expand Down
22 changes: 6 additions & 16 deletions view/next-project/src/pages/budgets/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Tabs, TabList, TabPanels, Tab, TabPanel } from '@chakra-ui/react';
import clsx from 'clsx';
import Head from 'next/head';
import { useState, useEffect, useMemo } from 'react';
import { useState, useEffect } from 'react';
import { RiAddCircleLine } from 'react-icons/ri';

import { useRecoilValue } from 'recoil';
import OpenExpenditureAddModalButton from '@/components/budgets/OpenExpenditureAddModalButton';
import OpenExpenseAddModalButton from '@/components/budgets/OpenExpenseAddModalButton';
import OpenExpenseDeleteModalButton from '@/components/budgets/OpenExpenseDeleteModalButton';
import OpenExpenseEditModalButton from '@/components/budgets/OpenExpenseEditModalButton';
import { authAtom } from '@/store/atoms';
import { getCurrentUser } from '@/utils/api/currentUser';
import { userAtom } from '@/store/atoms';
import { get } from '@api/api_methods';
import DetailModal from '@components/budgets/DetailModal';
import OpenAddModalButton from '@components/budgets/OpenAddModalButton';
Expand Down Expand Up @@ -58,25 +57,16 @@ export async function getServerSideProps() {

export default function BudgetList(props: Props) {
const { budgets, sources, years, expenses } = props;
const auth = useRecoilValue(authAtom);
const user = useRecoilValue(userAtom);
const [currentUser, setCurrentUser] = useState<User>();
const [budgetViews, setBudgetViews] = useState<BudgetView[]>(props.budgets);
const [expenseViews, setExpenseViews] = useState<ExpenseView[]>(props.expenses);

useEffect(() => {
const getUser = async () => {
const res = await getCurrentUser(auth);
setCurrentUser(res);
};
getUser();
}, [auth]);
setCurrentUser(user);
}, []);

const isDisabled = useMemo(() => {
if (currentUser) {
return !(currentUser.roleID === 2 || currentUser.roleID === 3);
}
return true;
}, [currentUser]);
const isDisabled = !(currentUser?.roleID === 2 || currentUser?.roleID === 3);

const [forcusExpense, setForcusExpense] = useState<ExpenseView | null>(null);
const [isOpen, setIsOpen] = useState(false);
Expand Down
69 changes: 18 additions & 51 deletions view/next-project/src/pages/fund_informations/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import clsx from 'clsx';
import Head from 'next/head';
import { useEffect, useCallback, useState, useMemo } from 'react';
import { useEffect, useState, useMemo } from 'react';
import { useRecoilValue } from 'recoil';

import { authAtom } from '@/store/atoms';
import { userAtom } from '@/store/atoms';
import { get } from '@api/api_methods';
import { getCurrentUser } from '@api/currentUser';
import { put } from '@api/fundInformations';
import { Title, Card } from '@components/common';
import { Checkbox } from '@components/common';
Expand Down Expand Up @@ -67,9 +65,13 @@ export default function FundInformations(props: Props) {
const teachers: Teacher[] = props.teachers;
const users: User[] = props.users;
const departments: Department[] = props.departments;
const auth = useRecoilValue(authAtom);
const user = useRecoilValue(userAtom);
const [currentUser, setCurrentUser] = useState<User>();

useEffect(() => {
setCurrentUser(user);
}, []);

// 募金一覧
const [fundInformationViews, setFundInformationViews] = useState<FundInformationView[]>(
props.fundInformationView,
Expand Down Expand Up @@ -97,53 +99,18 @@ export default function FundInformations(props: Props) {
const [isFirstChecks, setIsFirstChecks] = useState<boolean[]>([]);
const [isLastChecks, setIsLastChecks] = useState<boolean[]>([]);

const isDeveloper = useMemo(() => {
if (currentUser?.roleID == 2) {
return true;
} else {
return false;
}
}, [currentUser?.roleID]);

const isFinanceDirector = useMemo(() => {
if (currentUser?.roleID == 3) {
return true;
} else {
return false;
}
}, [currentUser?.roleID]);

const isFinanceStaff = useMemo(() => {
if (currentUser?.bureauID == 3 || currentUser?.bureauID == 4) {
return true;
} else {
return false;
}
}, [currentUser?.bureauID]);

const isDisabled = useCallback(
(fundViewItem: FundInformationView) => {
if (
fundViewItem.fundInformation.userID == currentUser?.id ||
isDeveloper ||
isFinanceStaff ||
isFinanceDirector
) {
return false;
} else {
return true;
}
},
[currentUser?.id, isDeveloper, isFinanceStaff, isFinanceDirector],
);
const isDeveloper = currentUser?.roleID == 2;
const isFinanceDirector = currentUser?.roleID == 3;
const isFinanceStaff = currentUser?.bureauID == 3 || currentUser?.bureauID == 4;

useEffect(() => {
const getUser = async () => {
const res = await getCurrentUser(auth);
setCurrentUser(res);
};
getUser();
}, []);
const isDisabled = (fundViewItem: FundInformationView) => {
return !(
fundViewItem.fundInformation.userID == currentUser?.id ||
isDeveloper ||
isFinanceStaff ||
isFinanceDirector
);
};

useEffect(() => {
if (fundInformationViews) {
Expand Down
57 changes: 19 additions & 38 deletions view/next-project/src/pages/purchaseorders/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import Head from 'next/head';
import { useCallback, useEffect, useState, useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import PrimaryButton from '@/components/common/OutlinePrimaryButton/OutlinePrimaryButton';
import { authAtom } from '@/store/atoms';
import { userAtom } from '@/store/atoms';
import { put } from '@/utils/api/purchaseOrder';
import { createPurchaseOrdersCsv } from '@/utils/createPurchaseOrdersCsv';
import { downloadFile } from '@/utils/downloadFile';
import { get } from '@api/api_methods';
import { getCurrentUser } from '@api/currentUser';
import { Card, Checkbox, Title, BureauLabel } from '@components/common';
import MainLayout from '@components/layout/MainLayout';
import DetailModal from '@components/purchaseorders/DetailModal';
Expand Down Expand Up @@ -67,7 +66,7 @@ const formatYYYYMMDD = (date: Date) => {
};

export default function PurchaseOrders(props: Props) {
const auth = useRecoilValue(authAtom);
const user = useRecoilValue(userAtom);
const [currentUser, setCurrentUser] = useState<User>();
const [purchaseOrderChecks, setPurchaseOrderChecks] = useState<boolean[]>([]);
const [purchaseOrderID, setPurchaseOrderID] = useState<number>(1);
Expand All @@ -82,6 +81,10 @@ export default function PurchaseOrders(props: Props) {
setIsOpen(true);
};

useEffect(() => {
setCurrentUser(user);
}, []);

const formatDate = (date: string) => {
const datetime = date.replace('T', ' ');
const datetime2 = datetime.substring(5, datetime.length - 10).replace('-', '/');
Expand All @@ -96,7 +99,6 @@ export default function PurchaseOrders(props: Props) {
const getPurchaseOrders = async () => {
const getPurchaseOrderViewUrlByYear =
process.env.CSR_API_URI + '/purchaseorders/details/' + selectedYear;
console.log(getPurchaseOrderViewUrlByYear);
const getPurchaseOrderByYears = await get(getPurchaseOrderViewUrlByYear);
setPurchaseOrderViews(getPurchaseOrderByYears);
};
Expand Down Expand Up @@ -148,40 +150,19 @@ export default function PurchaseOrders(props: Props) {
setPurchaseOrderViews(newPurchaseOrderViews);
};

const isFinanceDirector = useMemo(() => {
if (currentUser?.roleID === 3) {
return true;
} else {
return false;
}
}, [currentUser?.roleID]);

const isDisabled = useCallback(
(purchaseOrderViewItem: PurchaseOrderView) => {
if (
!purchaseOrderViewItem.purchaseOrder.financeCheck &&
(currentUser?.roleID === 2 ||
currentUser?.roleID === 3 ||
currentUser?.id === purchaseOrderViewItem.purchaseOrder.userID)
) {
return false;
} else {
return true;
}
},
[currentUser?.id, currentUser?.roleID, purchaseOrderViews],
);
const isFinanceDirector = currentUser?.roleID === 3;

useEffect(() => {
const getUser = async () => {
const res = await getCurrentUser(auth);
setCurrentUser(res);
};
getUser();
const isDisabled = useCallback((purchaseOrderViewItem: PurchaseOrderView) => {
return (
purchaseOrderViewItem.purchaseOrder.financeCheck &&
!(
currentUser?.roleID === 2 ||
currentUser?.roleID === 3 ||
currentUser?.id === purchaseOrderViewItem.purchaseOrder.userID
)
);
}, []);

console.log(props.expenseByPeriods);

return (
<MainLayout>
<Head>
Expand Down Expand Up @@ -320,17 +301,17 @@ export default function PurchaseOrders(props: Props) {
onOpen(purchaseOrderViewItem.purchaseOrder.id || 0, purchaseOrderViewItem);
}}
>
<div className='overflow-hidden text-ellipsis whitespace-nowrap text-center text-sm text-black-600'>
<div className='flex justify-center overflow-hidden text-ellipsis whitespace-nowrap text-sm text-black-600'>
{purchaseOrderViewItem.purchaseItem &&
purchaseOrderViewItem.purchaseItem.map(
(purchaseItem: PurchaseItem, index: number) => (
<>
<p key={purchaseItem.id}>
{purchaseOrderViewItem.purchaseItem.length - 1 === index ? (
<>{purchaseItem.item}</>
) : (
<>{purchaseItem.item},</>
)}
</>
</p>
),
)}
</div>
Expand Down
50 changes: 17 additions & 33 deletions view/next-project/src/pages/purchasereports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import Head from 'next/head';
import { useCallback, useEffect, useState, useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import PrimaryButton from '@/components/common/OutlinePrimaryButton/OutlinePrimaryButton';
import { authAtom } from '@/store/atoms';
import { userAtom } from '@/store/atoms';
import { put } from '@/utils/api/api_methods';
import { createPurchaseReportCsv } from '@/utils/createPurchaseReportCsv';
import { downloadFile } from '@/utils/downloadFile';
import { get } from '@api/api_methods';
import { getCurrentUser } from '@api/currentUser';
import { Card, Checkbox, Title, BureauLabel } from '@components/common';
import MainLayout from '@components/layout/MainLayout';
import DetailModal from '@components/purchasereports/DetailModal';
Expand Down Expand Up @@ -71,7 +70,7 @@ const formatYYYYMMDD = (date: Date) => {
};

export default function PurchaseReports(props: Props) {
const auth = useRecoilValue(authAtom);
const user = useRecoilValue(userAtom);
const [currentUser, setCurrentUser] = useState<User>();
const [purchaseReportID, setPurchaseReportID] = useState<number>(1);
const [purchaseReportViewItem, setPurchaseReportViewItem] = useState<PurchaseReportView>();
Expand Down Expand Up @@ -138,21 +137,20 @@ export default function PurchaseReports(props: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedYear]);

const isDisabled = useCallback(
(purchaseReportView: PurchaseReportView) => {
if (
!purchaseReportView.purchaseReport.financeCheck &&
(currentUser?.roleID === 2 ||
currentUser?.roleID === 3 ||
currentUser?.id === purchaseReportView.purchaseReport.userID)
) {
return false;
} else {
return true;
}
},
[currentUser?.roleID, currentUser?.id],
);
useEffect(() => {
setCurrentUser(user);
}, []);

const isDisabled = useCallback((purchaseReportView: PurchaseReportView) => {
return (
purchaseReportView.purchaseReport.financeCheck &&
!(
currentUser?.roleID === 2 ||
currentUser?.roleID === 3 ||
currentUser?.id === purchaseReportView.purchaseReport.userID
)
);
}, []);

const updatePurchaseReport = async (purchaseReportID: number, purchaseReport: PurchaseReport) => {
const url = process.env.CSR_API_URI + '/purchasereports/' + purchaseReportID;
Expand All @@ -175,21 +173,7 @@ export default function PurchaseReports(props: Props) {
}
}, [purchaseReportViews]);

const isFinanceDirector = useMemo(() => {
if (currentUser?.roleID === 3) {
return true;
} else {
return false;
}
}, [currentUser?.roleID]);

useEffect(() => {
const getUser = async () => {
const res = await getCurrentUser(auth);
setCurrentUser(res);
};
getUser();
}, []);
const isFinanceDirector = currentUser?.roleID === 3;

return (
<MainLayout>
Expand Down
Loading
Loading