-
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
購入履歴画面を追加 #108
Open
grtw2116
wants to merge
11
commits into
main
Choose a base branch
from
update-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
購入履歴画面を追加 #108
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c59e615
購入履歴画面を追加
grtw2116 45e90ab
format
grtw2116 df030ab
Merge branch 'main' into update-ui
grtw2116 b6a1805
Merge branch 'main' into update-ui
grtw2116 264befa
リファクタリング
grtw2116 7249533
フォーマット
grtw2116 e37f004
ファイル名を変更
grtw2116 eccf26d
フォーマット
grtw2116 329c1e7
利用者選択時に一瞬だけ「購入履歴がありません」と表示されるのを修正
grtw2116 357da5b
購入履歴削除時に確認ダイアログを表示する
grtw2116 ced3a2d
Merge branch 'main' into update-ui
grtw2116 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
2 changes: 1 addition & 1 deletion
2
...c/features/home/components/MemberPane.tsx → frontend/src/components/MemberPane.tsx
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
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
117 changes: 117 additions & 0 deletions
117
frontend/src/features/purchase-history/components/HistoryCard.tsx
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,117 @@ | ||
import { | ||
AspectRatio, | ||
Box, | ||
Flex, | ||
HStack, | ||
IconButton, | ||
Image, | ||
Spacer, | ||
Text, | ||
useDisclosure, | ||
} from "@chakra-ui/react"; | ||
import { History } from "types"; | ||
import { DateFormatter } from "util/DateFormatter"; | ||
import { Backend } from "util/Backend"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { faTrash } from "@fortawesome/free-solid-svg-icons"; | ||
import { useEffect, useState } from "react"; | ||
import LogoDefaultItem from "image/default_item.svg"; | ||
import { HistoryDeleteConfirm } from "./HistoryDeleteConfirm"; | ||
|
||
type Props = { | ||
history: History; | ||
onDeleteHistory: (history: History) => void; | ||
}; | ||
|
||
function HistoryCard({ history, onDeleteHistory: deleteHistory }: Props) { | ||
// TODO: 画像処理の共通化 | ||
const [imageURL, setImageURL] = useState<string>(""); | ||
const { isOpen, onClose, onOpen } = useDisclosure(); | ||
|
||
useEffect(() => { | ||
let ignore = false; | ||
|
||
async function getImage() { | ||
if (history === null) { | ||
setImageURL(LogoDefaultItem); | ||
return; | ||
} | ||
|
||
const res = await Backend.getItemImage(history.itemId); | ||
if (!ignore) { | ||
if (res === null) { | ||
console.log( | ||
history.itemId + ": Custom image not found. So use default image." | ||
); | ||
setImageURL(LogoDefaultItem); | ||
return; | ||
} | ||
setImageURL(URL.createObjectURL(res)); | ||
} | ||
} | ||
getImage(); | ||
|
||
return () => { | ||
ignore = true; | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Flex | ||
_first={{ borderTop: "1px", borderColor: "blackAlpha.200" }} | ||
borderBottom="1px" | ||
borderColor="blackAlpha.200" | ||
justify="space-between" | ||
alignItems="center" | ||
px={4} | ||
py={2} | ||
> | ||
<AspectRatio | ||
ratio={1 / 1} | ||
w={16} | ||
border="1px" | ||
borderColor="blackAlpha.300" | ||
rounded={8} | ||
> | ||
{history.itemId === null ? ( | ||
<Box /> | ||
) : ( | ||
<Image src={imageURL} objectFit="cover" /> | ||
)} | ||
</AspectRatio> | ||
<Box ml={4}> | ||
<HStack spacing={2} align="center"> | ||
<Text fontSize="xl" fontWeight="bold"> | ||
{history.itemName} | ||
</Text> | ||
</HStack> | ||
<Text textColor="gray"> | ||
{DateFormatter.convertDateFormat(history.date) + | ||
", " + | ||
history.price + | ||
" 円"} | ||
</Text> | ||
</Box> | ||
<Spacer /> | ||
<IconButton | ||
variant="unstyled" | ||
aria-label="購入履歴を削除" | ||
onClick={onOpen} | ||
icon={<FontAwesomeIcon icon={faTrash} color="red" />} | ||
/> | ||
</Flex> | ||
<HistoryDeleteConfirm | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
historyId={history.historyId} | ||
onClickDeleteButton={() => { | ||
deleteHistory(history); | ||
onClose(); | ||
}} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export { HistoryCard }; |
42 changes: 42 additions & 0 deletions
42
frontend/src/features/purchase-history/components/HistoryDeleteConfirm.tsx
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,42 @@ | ||
import { | ||
Button, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
} from "@chakra-ui/react"; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
historyId: number; | ||
onClickDeleteButton: () => void; | ||
}; | ||
|
||
function HistoryDeleteConfirm({ isOpen, onClose, onClickDeleteButton }: Props) { | ||
return ( | ||
<Modal isOpen={isOpen} onClose={onClose} isCentered> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>購入履歴の削除</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
<p>購入履歴を削除しますか?</p> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button mr={3} onClick={onClose}> | ||
キャンセル | ||
</Button> | ||
<Button colorScheme="red" onClick={onClickDeleteButton}> | ||
削除 | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
} | ||
|
||
export { HistoryDeleteConfirm }; |
45 changes: 45 additions & 0 deletions
45
frontend/src/features/purchase-history/components/HistoryList.tsx
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,45 @@ | ||
import { Center, Heading, Text } from "@chakra-ui/react"; | ||
import { History } from "types"; | ||
import { HistoryCard } from "./HistoryCard"; | ||
|
||
type Props = { | ||
histories: History[]; | ||
isMemberSelected: boolean; | ||
onDeleteHistory: (history: History) => void; | ||
}; | ||
|
||
function HistoryList({ histories, isMemberSelected, onDeleteHistory }: Props) { | ||
if (isMemberSelected === false) { | ||
return ( | ||
<Center h="100%"> | ||
<Heading size="md" textColor="gray"> | ||
利用者を選択してください | ||
</Heading> | ||
</Center> | ||
); | ||
} | ||
|
||
if (histories.length === 0) { | ||
return ( | ||
<Center> | ||
<Text size="md" textColor="gray"> | ||
購入履歴がありません | ||
</Text> | ||
</Center> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{histories.map((history) => ( | ||
<HistoryCard | ||
key={history.historyId} | ||
history={history} | ||
onDeleteHistory={onDeleteHistory} | ||
/> | ||
))} | ||
</> | ||
); | ||
} | ||
|
||
export { HistoryList }; |
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,86 @@ | ||
import { useState } from "react"; | ||
import { Heading, useToast } from "@chakra-ui/react"; | ||
import { | ||
LeftColumn, | ||
RightColumn, | ||
TwoColumnLayout, | ||
} from "components/TwoColumnLayout"; | ||
import { MemberPane } from "components/MemberPane"; | ||
import { HistoryList } from "./components/HistoryList"; | ||
import { Backend } from "util/Backend"; | ||
import { History } from "types"; | ||
import { useMembersDispatch } from "contexts/MembersContext"; | ||
|
||
function PurchaseHistory() { | ||
const [selectedMemberId, setSelectedMemberId] = useState<string>(""); | ||
const [histories, setHistories] = useState<History[]>([]); | ||
const dispatchMembers = useMembersDispatch(); | ||
const toast = useToast(); | ||
|
||
async function fetchHistories(memberId: string) { | ||
const histories = await Backend.getUserHistory(memberId); | ||
if (histories === null) { | ||
console.error("getUserHistory: failed"); | ||
return; | ||
} | ||
setHistories(histories); | ||
} | ||
|
||
async function deleteHistory(history: History) { | ||
if (!(await Backend.recall(history.historyId))) { | ||
console.error("recall: failed"); | ||
toast({ | ||
title: "購入履歴の削除に失敗しました", | ||
status: "error", | ||
duration: 2000, | ||
isClosable: true, | ||
}); | ||
return; | ||
} | ||
|
||
// memberの利用金額を更新 | ||
dispatchMembers({ | ||
type: "purchaseCanceled", | ||
id: history.memberId, | ||
price: history.price, | ||
}); | ||
|
||
// historyを更新 | ||
setHistories(histories.filter((h) => h.historyId !== history.historyId)); | ||
|
||
// toastを表示 | ||
toast({ | ||
title: "購入履歴を削除しました", | ||
status: "success", | ||
duration: 2000, | ||
isClosable: true, | ||
}); | ||
} | ||
|
||
return ( | ||
<TwoColumnLayout> | ||
<LeftColumn> | ||
<MemberPane | ||
selectedMemberId={selectedMemberId} | ||
onClickMemberCard={async (memberId) => { | ||
// 購入履歴の取得が完了するまでmemberIdをセットしない | ||
await fetchHistories(memberId); | ||
setSelectedMemberId(memberId); | ||
}} | ||
/> | ||
</LeftColumn> | ||
<RightColumn> | ||
<Heading size="md" mb={4}> | ||
購入履歴 | ||
</Heading> | ||
<HistoryList | ||
histories={histories} | ||
isMemberSelected={selectedMemberId !== ""} | ||
onDeleteHistory={deleteHistory} | ||
/> | ||
</RightColumn> | ||
</TwoColumnLayout> | ||
); | ||
} | ||
|
||
export { PurchaseHistory }; |
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.
Boxはどういうときに表示されますか?
historyのitemIdってnullもあり得るんでしたっけ(覚えてない)