-
Notifications
You must be signed in to change notification settings - Fork 0
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
テナント一覧機能作成 #2
Merged
Merged
テナント一覧機能作成 #2
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import axios from "axios"; | ||
import { useEffect, useState } from "react"; | ||
import { useCookies } from "react-cookie"; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
const LOGIN_URL = process.env.REACT_APP_LOGIN_URL ?? ""; | ||
const API_ENDPOINT = process.env.REACT_APP_API_ENDPOINT ?? ""; | ||
const sleep = (second: number) => | ||
new Promise((resolve) => setTimeout(resolve, second * 1000)); | ||
|
||
const TenantList = () => { | ||
const [tenants, setTenants] = useState<any>(); | ||
const [tenantInfo, setTenantInfo] = useState<any>(); | ||
let jwtToken = window.localStorage.getItem("SaaSusIdToken") as string; | ||
const [cookies] = useCookies(["SaaSusRefreshToken"]); | ||
const navigate = useNavigate(); | ||
|
||
type Jwt = { | ||
[name: string]: string | number | boolean; | ||
}; | ||
|
||
const idTokenCheck = async () => { | ||
const base64Url = jwtToken.split(".")[1]; | ||
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); | ||
const decoded = JSON.parse( | ||
decodeURIComponent(escape(window.atob(base64))) | ||
) as Jwt; | ||
|
||
const expireDate = decoded["exp"] as number; | ||
const timestamp = parseInt(Date.now().toString().slice(0, 10)); | ||
if (expireDate <= timestamp) { | ||
try { | ||
console.log("token expired"); | ||
const res = await axios.get(`${API_ENDPOINT}/refresh`, { | ||
headers: { | ||
"X-Requested-With": "XMLHttpRequest", | ||
}, | ||
withCredentials: true, | ||
}); | ||
|
||
jwtToken = res.data.id_token; | ||
localStorage.setItem("SaaSusIdToken", jwtToken); | ||
|
||
await sleep(1); | ||
return; | ||
} catch (err) { | ||
console.log(err); | ||
window.location.href = LOGIN_URL; | ||
} | ||
} | ||
}; | ||
|
||
// ログインユーザの情報と所属テナント情報を取得 | ||
const GetUserinfo = async () => { | ||
const res = await axios.get(`${API_ENDPOINT}/userinfo`, { | ||
headers: { | ||
"X-Requested-With": "XMLHttpRequest", | ||
Authorization: `Bearer ${jwtToken}`, | ||
}, | ||
withCredentials: true, | ||
}); | ||
|
||
const tenantInfo = await Promise.all(res.data.tenants.map(async (tenant:any) => { | ||
const res = await axios.get(`${API_ENDPOINT}/tenant_attributes`, { | ||
headers: { | ||
"X-Requested-With": "XMLHttpRequest", | ||
Authorization: `Bearer ${jwtToken}`, | ||
}, | ||
withCredentials: true, | ||
params: { | ||
tenant_id: tenant.id, | ||
}, | ||
}); | ||
|
||
return res.data; | ||
})); | ||
|
||
console.log(tenantInfo); | ||
setTenants(res.data.tenants); | ||
setTenantInfo(tenantInfo); | ||
}; | ||
|
||
const handleUserListClick = async (tenantId:any) => { | ||
try { | ||
// ロールの取得 | ||
const jwtToken = window.localStorage.getItem("SaaSusIdToken"); | ||
const res = await axios.get(`${API_ENDPOINT}/userinfo`, { | ||
headers: { | ||
"X-Requested-With": "XMLHttpRequest", | ||
Authorization: `Bearer ${jwtToken}`, | ||
}, | ||
withCredentials: true, | ||
}); | ||
const role = res.data.tenants[0].envs[0].roles[0].role_name; | ||
|
||
res.data.tenants.map((tenant:any, index:any) => { | ||
if (tenant.id === tenantId) { | ||
const role = tenant.envs[0].roles[0].role_name; | ||
} | ||
}); | ||
|
||
// リダイレクト | ||
switch (role) { | ||
case "sadmin": | ||
navigate(`/sadmin/toppage?tenant_id=${tenantId}`); | ||
break; | ||
case "admin": | ||
navigate(`/admin/toppage?tenant_id=${tenantId}`); | ||
break; | ||
default: | ||
navigate(`/user/toppage?tenant_id=${tenantId}`); | ||
break; | ||
} | ||
} catch (error) { | ||
console.error('Error fetching user list:', error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const startTenantListPage = async () => { | ||
await idTokenCheck(); | ||
GetUserinfo(); | ||
}; | ||
|
||
startTenantListPage(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
テナント一覧 | ||
<table border={1} style={{ borderCollapse: 'collapse' }}> | ||
<thead> | ||
<tr> | ||
<td>テナントID</td> | ||
<td>テナント名</td> | ||
{tenantInfo && tenantInfo.length > 0 && Object.keys(tenantInfo[0]).map((key, index) => ( | ||
<td key={index}>{tenantInfo[0][key].display_name}</td> | ||
))} | ||
<td></td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{tenants?.map((tenant: any, tenantIndex: number) => { | ||
return ( | ||
<tr key={tenant.id}> | ||
<td>{tenant.id}</td> | ||
<td>{tenant.name}</td> | ||
{tenantInfo[tenantIndex] && Object.keys(tenantInfo[tenantIndex]).map((key) => ( | ||
<td key={key}> | ||
{tenantInfo[tenantIndex][key].attribute_type === 'bool' | ||
? tenantInfo[tenantIndex][key].value === true ? '設定済み' : '未設定' | ||
: tenantInfo[tenantIndex][key].value} | ||
</td> | ||
))} | ||
<td> | ||
<button onClick={() => handleUserListClick(tenant.id)}> | ||
ユーザ一覧に移動 | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
</> | ||
); | ||
}; | ||
|
||
export default TenantList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このsleepは必要な感じでしょうか
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implementation-sample-front-react/src/pages/UserPage.tsx
上記を参考に作成した処理になります。
コメントで
と記載されていたので、必要なsleepだと思っております。