This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8700a49
commit 12c04a8
Showing
5 changed files
with
341 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { useState } from "react" | ||
import Button from "../../../components/fundamentals/button" | ||
import Modal from "../../../components/molecules/modal" | ||
import Select from "../../../components/molecules/select" | ||
|
||
const EditGiftCardModal = ({ | ||
handleClose, | ||
handleSave, | ||
giftCard, | ||
updating, | ||
regions, | ||
region, | ||
}) => { | ||
// const [code, setCode] = useState(giftCard.code) | ||
const [selectedRegion, setSelectedRegion] = useState({ | ||
value: region.id, | ||
label: region.name, | ||
}) | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault() | ||
if (handleSave) { | ||
handleSave({ region_id: selectedRegion.value }) | ||
} | ||
} | ||
|
||
const regionOptions = regions.map((r) => ({ | ||
label: r.name, | ||
value: r.id, | ||
})) | ||
|
||
return ( | ||
<Modal handleClose={handleClose} isLargeModal={true}> | ||
<form onSubmit={(e) => onSubmit(e)}> | ||
<Modal.Body isLargeModal={true}> | ||
<Modal.Header handleClose={handleClose}> | ||
<span className="inter-xlarge-semibold"> | ||
Edit Gift Card Details | ||
</span> | ||
</Modal.Header> | ||
<Modal.Content> | ||
{/* TODO: Missing backend support for updating code | ||
<InputField | ||
label="Code" | ||
name="code" | ||
value={code} | ||
onChange={({ currentTarget }) => setCode(currentTarget.value)} | ||
className="mb-4" | ||
/> */} | ||
<Select | ||
label="Region" | ||
options={regionOptions} | ||
value={selectedRegion} | ||
onChange={setSelectedRegion} | ||
/> | ||
</Modal.Content> | ||
<Modal.Footer> | ||
<div className="w-full flex justify-end"> | ||
<Button | ||
variant="ghost" | ||
size="small" | ||
onClick={handleClose} | ||
className="mr-2" | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
loading={updating} | ||
variant="primary" | ||
className="min-w-[100px]" | ||
size="small" | ||
type="submit" | ||
> | ||
Save | ||
</Button> | ||
</div> | ||
</Modal.Footer> | ||
</Modal.Body> | ||
</form> | ||
</Modal> | ||
) | ||
} | ||
export default EditGiftCardModal |
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,152 @@ | ||
import { RouteComponentProps } from "@reach/router" | ||
import { | ||
useAdminGiftCard, | ||
useAdminRegions, | ||
useAdminUpdateGiftCard, | ||
} from "medusa-react" | ||
import moment from "moment" | ||
import React, { useState } from "react" | ||
import Spinner from "../../../components/atoms/spinner" | ||
import Badge from "../../../components/fundamentals/badge" | ||
import DollarSignIcon from "../../../components/fundamentals/icons/dollar-sign-icon" | ||
import EditIcon from "../../../components/fundamentals/icons/edit-icon" | ||
import Breadcrumb from "../../../components/molecules/breadcrumb" | ||
import StatusSelector from "../../../components/molecules/status-selector" | ||
import BodyCard from "../../../components/organisms/body-card" | ||
import useToaster from "../../../hooks/use-toaster" | ||
import { getErrorMessage } from "../../../utils/error-messages" | ||
import { formatAmountWithSymbol } from "../../../utils/prices" | ||
import EditGiftCardModal from "./edit-gift-card-modal" | ||
import UpdateBalanceModal from "./update-balance-modal" | ||
|
||
type GiftCardDetailsProps = { | ||
id: string | ||
} & RouteComponentProps | ||
|
||
const GiftCardDetails: React.FC<GiftCardDetailsProps> = ({ id }) => { | ||
const { gift_card, isLoading } = useAdminGiftCard(id) | ||
const { regions } = useAdminRegions() | ||
|
||
const updateGiftCard = useAdminUpdateGiftCard(gift_card?.id) | ||
|
||
const toaster = useToaster() | ||
|
||
const [showUpdateBalance, setShowUpdateBalance] = useState(false) | ||
const [showEdit, setShowEdit] = useState(false) | ||
|
||
const actions = [ | ||
{ | ||
label: "Edit", | ||
onClick: () => setShowEdit(true), | ||
icon: <EditIcon size={20} />, | ||
}, | ||
{ | ||
label: "Update balance", | ||
onClick: () => setShowUpdateBalance(true), | ||
icon: <DollarSignIcon size={20} />, | ||
}, | ||
] | ||
|
||
const handleUpdate = (data) => { | ||
updateGiftCard.mutate( | ||
{ ...data }, | ||
{ | ||
onSuccess: () => { | ||
toaster("Succesfully updated Gift Card", "success") | ||
setShowEdit(false) | ||
setShowUpdateBalance(false) | ||
}, | ||
onError: (err) => toaster(getErrorMessage(err), "error"), | ||
} | ||
) | ||
} | ||
|
||
return ( | ||
<div> | ||
<Breadcrumb | ||
currentPage={"Gift Card Details"} | ||
previousBreadcrumb={"Gift Cards"} | ||
previousRoute="/a/gift-cards" | ||
/> | ||
<BodyCard | ||
className={"h-auto min-h-0 w-full"} | ||
title={`${gift_card?.code}`} | ||
subtitle={`Gift Card id: ${gift_card?.id}`} | ||
status={ | ||
<StatusSelector | ||
isDraft={!!gift_card?.is_disabled} | ||
activeState={"Active"} | ||
draftState={"Disabled"} | ||
onChange={() => | ||
handleUpdate({ is_disabled: !gift_card?.is_disabled }) | ||
} | ||
/> | ||
} | ||
actionables={actions} | ||
> | ||
{isLoading || !gift_card ? ( | ||
<div className="w-full pt-2xlarge flex items-center justify-center"> | ||
<Spinner size={"large"} variant={"secondary"} /> | ||
</div> | ||
) : ( | ||
<div className="flex justify-between"> | ||
<div className="flex mt-6 space-x-6 divide-x"> | ||
<div className="flex flex-col"> | ||
<div className="inter-smaller-regular text-grey-50 mb-1"> | ||
Original amount | ||
</div> | ||
<div> | ||
{formatAmountWithSymbol({ | ||
amount: gift_card?.value, | ||
currency: gift_card?.region.currency_code, | ||
})} | ||
</div> | ||
</div> | ||
<div className="flex flex-col pl-6"> | ||
<div className="inter-smaller-regular text-grey-50 mb-1"> | ||
Balance | ||
</div> | ||
<div> | ||
{formatAmountWithSymbol({ | ||
amount: gift_card?.balance, | ||
currency: gift_card?.region.currency_code, | ||
})} | ||
</div> | ||
</div> | ||
<div className="flex flex-col pl-6"> | ||
<div className="inter-smaller-regular text-grey-50 mb-1"> | ||
Created | ||
</div> | ||
<div>{moment(gift_card?.created_at).format("DD MMM YYYY")}</div> | ||
</div> | ||
</div> | ||
<div className="flex items-end"> | ||
<Badge variant="default">{gift_card?.region?.name}</Badge> | ||
</div> | ||
</div> | ||
)} | ||
</BodyCard> | ||
{showUpdateBalance && ( | ||
<UpdateBalanceModal | ||
giftCard={gift_card} | ||
currencyCode={gift_card?.region?.currency_code} | ||
handleClose={() => setShowUpdateBalance(false)} | ||
handleSave={handleUpdate} | ||
updating={updateGiftCard.isLoading} | ||
/> | ||
)} | ||
{showEdit && ( | ||
<EditGiftCardModal | ||
giftCard={gift_card} | ||
handleClose={() => setShowUpdateBalance(false)} | ||
handleSave={handleUpdate} | ||
regions={regions} | ||
region={gift_card?.region} | ||
updating={updateGiftCard.isLoading} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default GiftCardDetails |
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,79 @@ | ||
import React, { useState } from "react" | ||
import Tooltip from "../../../components/atoms/tooltip" | ||
import Button from "../../../components/fundamentals/button" | ||
import Modal from "../../../components/molecules/modal" | ||
import CurrencyInput from "../../../components/organisms/currency-input" | ||
|
||
const UpdateBalanceModal = ({ | ||
handleClose, | ||
handleSave, | ||
currencyCode, | ||
giftCard, | ||
updating, | ||
}) => { | ||
const [balance, setBalance] = useState(giftCard.balance) | ||
|
||
const handleChange = (amount: number | undefined) => { | ||
if (amount) { | ||
setBalance(amount) | ||
} | ||
} | ||
|
||
const onSubmit = () => { | ||
if (balance > giftCard.value) { | ||
return | ||
} | ||
|
||
if (handleSave) { | ||
handleSave({ balance }) | ||
} | ||
} | ||
|
||
return ( | ||
<Modal handleClose={handleClose}> | ||
<Modal.Body> | ||
<Modal.Header handleClose={handleClose}> | ||
<span className="inter-xlarge-semibold">Update Balance</span> | ||
</Modal.Header> | ||
<Modal.Content> | ||
<CurrencyInput readOnly currentCurrency={currencyCode} size="small"> | ||
<CurrencyInput.AmountInput | ||
amount={giftCard.balance} | ||
label="Price" | ||
onChange={handleChange} | ||
/> | ||
</CurrencyInput> | ||
</Modal.Content> | ||
<Modal.Footer> | ||
<div className="w-full flex justify-end"> | ||
<Button | ||
variant="ghost" | ||
size="small" | ||
onClick={handleClose} | ||
className="mr-2" | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
loading={updating} | ||
variant="primary" | ||
className="min-w-[100px]" | ||
size="small" | ||
onClick={onSubmit} | ||
disabled={balance > giftCard.value} | ||
> | ||
{balance > giftCard.value ? ( | ||
<Tooltip content="Balance is above original value"> | ||
Save | ||
</Tooltip> | ||
) : ( | ||
"Save" | ||
)} | ||
</Button> | ||
</div> | ||
</Modal.Footer> | ||
</Modal.Body> | ||
</Modal> | ||
) | ||
} | ||
export default UpdateBalanceModal |
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