-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LifetimeWithExpirationDate component
- Loading branch information
Showing
3 changed files
with
124 additions
and
16 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
18 changes: 18 additions & 0 deletions
18
src/component-library/Pages/Rule/LifetimeWithExpirationDateInput.stories.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,18 @@ | ||
import { Meta, StoryFn } from "@storybook/react"; | ||
import { LifetimeWithExpirationDateInput } from "./LifetimeWithExpiryDateInput"; | ||
|
||
export default { | ||
"title": "Components/Pages/Rule/Components", | ||
component: LifetimeWithExpirationDateInput, | ||
} as Meta<typeof LifetimeWithExpirationDateInput>; | ||
|
||
const Template: StoryFn<typeof LifetimeWithExpirationDateInput> = (args) => <LifetimeWithExpirationDateInput {...args} />; | ||
|
||
export const LifeTimeWithExpirationDateInputStory = Template.bind({}); | ||
|
||
LifeTimeWithExpirationDateInputStory.args = { | ||
onChange: (date, lifetime) => {console.log(date, lifetime)}, | ||
initialdate: new Date(), | ||
disabled: false, | ||
placeholder: "Select a date", | ||
} |
84 changes: 84 additions & 0 deletions
84
src/component-library/Pages/Rule/LifetimeWithExpiryDateInput.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,84 @@ | ||
import { DateInput } from "@/component-library/Input/DateInput" | ||
import { NumInput } from "@/component-library/Input/NumInput" | ||
import { type } from "@testing-library/user-event/dist/types/utility" | ||
import { useState } from "react" | ||
import { twMerge } from "tailwind-merge" | ||
|
||
type LifetimeWithExpirationDateInputProps = { | ||
onChange(date: Date, lifetime: number): void, // communicate back to parent | ||
initialdate?: Date, // the current value, set by the parent | ||
disabled?: boolean, | ||
placeholder?: string, | ||
} | ||
export const LifetimeWithExpirationDateInput: React.FC<LifetimeWithExpirationDateInputProps> = ( | ||
{ | ||
onChange, | ||
initialdate, | ||
disabled = false, | ||
placeholder = "Select a date", | ||
}: LifetimeWithExpirationDateInputProps) => { | ||
const calculateLifetimeInDays = (date: Date) => { | ||
const currentDate = new Date() | ||
const diffInTime = date.getTime() - currentDate.getTime() | ||
const diffInDays = diffInTime / (1000 * 3600 * 24) | ||
return Math.ceil(diffInDays) | ||
} | ||
const calculateDateFromLifetime = (lifetime: number) => { | ||
const currentDate = new Date() | ||
const newDate = new Date(currentDate.getTime() + lifetime * 24 * 60 * 60 * 1000) | ||
return newDate | ||
} | ||
const [lifetimeInDays, setLifetimeInDays] = useState<number>(() => { | ||
if (initialdate) { | ||
return calculateLifetimeInDays(initialdate) | ||
} | ||
return 0 | ||
}) | ||
const [date, setDate] = useState<Date | undefined>(initialdate) | ||
const onDateChange = (date: Date) => { | ||
console.log(date) | ||
const newLifetime = calculateLifetimeInDays(date) | ||
setLifetimeInDays(newLifetime) | ||
} | ||
return ( | ||
<div className={twMerge( | ||
"p-2 space-y-4", | ||
"rounded-md border", | ||
"dark:border-2", | ||
"bg-white dark:bg-gray-800" | ||
)} | ||
> | ||
<div className="flex flex-col space-y-2"> | ||
<span className="text-xl dark:text-white">Lifetime (days)</span> | ||
<NumInput | ||
value={lifetimeInDays} | ||
onChange={(event) => { | ||
const value = event.target.value | ||
if(value === undefined || value === null) { | ||
return | ||
} | ||
if(value === "") { | ||
setLifetimeInDays(0) | ||
setDate(new Date()) | ||
return | ||
} | ||
if(typeof value === "string") { | ||
try { | ||
const intValue = parseInt(value) | ||
setLifetimeInDays(intValue) | ||
const newDate = calculateDateFromLifetime(parseInt(value)) | ||
setDate(newDate) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
}} | ||
/> | ||
</div> | ||
<div className="flex flex-col space-y-2"> | ||
<span className="text-xl dark:text-white">Expiry Date</span> | ||
<DateInput onchange={onDateChange} initialdate={date} disabled={disabled} placeholder={placeholder} /> | ||
</div> | ||
</div> | ||
) | ||
} |