|
2 | 2 | import type { Price } from "$lib/types/collection" |
3 | 3 | import { Dialog, Portal } from "@skeletonlabs/skeleton-svelte" |
4 | 4 | import RotateCw from "@lucide/svelte/icons/rotate-cw" |
| 5 | + import { SvelteDate } from "svelte/reactivity" |
5 | 6 |
|
6 | 7 | let { |
7 | 8 | name, |
8 | 9 | username, |
9 | 10 | id, |
10 | | - price |
| 11 | + price, |
| 12 | + date_end |
11 | 13 | }: { |
12 | 14 | name: string |
13 | 15 | username: Promise<string | null> |
14 | 16 | id: string |
15 | | - price: Price | undefined |
| 17 | + price: Price |
| 18 | + date_end: string |
16 | 19 | } = $props() |
17 | 20 | let open = $state(false) |
18 | 21 |
|
|
22 | 25 | style: "currency", |
23 | 26 | currency: price?.currency ?? "eur" |
24 | 27 | }).format((value ?? 0) / 100) |
| 28 | +
|
| 29 | + const endDate = new Date(date_end) |
| 30 | + const startDate = new Date(endDate) |
| 31 | +
|
| 32 | + switch (price.interval) { |
| 33 | + case "week": |
| 34 | + startDate.setDate(startDate.getDate() - 7) |
| 35 | + break |
| 36 | + case "month": |
| 37 | + startDate.setMonth(startDate.getMonth() - 1) |
| 38 | + break |
| 39 | + case "year": |
| 40 | + startDate.setFullYear(startDate.getFullYear() - 1) |
| 41 | + break |
| 42 | + default: |
| 43 | + } |
| 44 | +
|
| 45 | + const start_date = startDate.getTime() |
| 46 | + const end_date = endDate.getTime() |
| 47 | +
|
| 48 | + const DAY = 24 * 3600000 |
| 49 | + const tenDayMS = 10 * DAY |
| 50 | + const intervalMs = end_date - start_date |
| 51 | + const tenPercentMs = intervalMs * 0.1 |
| 52 | + const windowMs = Math.min(tenPercentMs, tenDayMS) |
| 53 | +
|
| 54 | + const elapsedSinceStartMs = Date.now() - start_date |
| 55 | +
|
| 56 | + const endWindow = new Date(start_date + DAY + windowMs) |
25 | 57 | </script> |
26 | 58 |
|
27 | 59 | <Dialog {open} onOpenChange={(e) => (open = e.open)}> |
28 | | - <Dialog.Trigger class="btn preset-tonal"> |
| 60 | + <Dialog.Trigger disabled={elapsedSinceStartMs > windowMs} class="btn preset-tonal"> |
29 | 61 | <RotateCw size="16" /> |
30 | 62 | <span>Refund</span> |
31 | 63 | </Dialog.Trigger> |
32 | 64 | <Portal> |
33 | 65 | <Dialog.Backdrop class="fixed inset-0 z-50 bg-surface-50-950/50 backdrop-blur-sm" /> |
34 | 66 | <Dialog.Positioner class="fixed inset-0 z-50 flex items-center justify-center"> |
35 | 67 | <Dialog.Content |
36 | | - class="max-h-[95%] w-[70%] max-w-fit space-y-4 overflow-y-auto card bg-surface-100-900 p-4 shadow-xl" |
| 68 | + class="max-h-[95%] w-[70%] max-w-fit space-y-4 overflow-y-auto card rounded-md bg-surface-100-900 p-12 shadow-xl" |
37 | 69 | > |
38 | 70 | <Dialog.Title class="flex justify-between text-2xl font-bold"> |
39 | 71 | <h5 class="flex flex-col gap-4 h5 lg:flex-row lg:h4"> |
|
70 | 102 | </a>. |
71 | 103 | </p> |
72 | 104 | <p class="my-8">The total amount you will be refunded will be roughly <b>{priceStr}</b>.</p> |
| 105 | + |
| 106 | + <div class="mx-2 my-8 flex flex-col gap-4 rounded-md bg-surface-300-700 p-2 text-warning-500"> |
| 107 | + {#if elapsedSinceStartMs < DAY} |
| 108 | + <p> |
| 109 | + The refund option will be available after <span class="text-error-500">1 day</span> |
| 110 | + and you will have until the |
| 111 | + <span class="text-error-500">{endWindow.toLocaleString()}</span> |
| 112 | + to request it. |
| 113 | + </p> |
| 114 | + <p> |
| 115 | + While it's not available try reaching out to |
| 116 | + <span class="text-error-500"> |
| 117 | + {#await username} |
| 118 | + Loading... |
| 119 | + {:then username} |
| 120 | + {username} |
| 121 | + {/await} |
| 122 | + </span> |
| 123 | + and let him know if you are having issues! |
| 124 | + </p> |
| 125 | + {:else} |
| 126 | + <p> |
| 127 | + Refunding will be available until <span class="text-error-500"> |
| 128 | + {endWindow.toLocaleString()} |
| 129 | + </span> |
| 130 | + </p> |
| 131 | + {/if} |
| 132 | + </div> |
73 | 133 | </Dialog.Description> |
74 | 134 |
|
75 | 135 | <footer class="flex justify-end gap-4"> |
76 | 136 | <form id="refundsform" method="POST" action={"?/refund&id=" + id}> |
77 | | - <button type="submit" class="btn preset-filled-error-500"> Refund </button> |
| 137 | + <button |
| 138 | + type="submit" |
| 139 | + disabled={elapsedSinceStartMs < DAY || elapsedSinceStartMs > windowMs} |
| 140 | + class="btn preset-filled-error-500" |
| 141 | + > |
| 142 | + Refund |
| 143 | + </button> |
78 | 144 | </form> |
79 | 145 |
|
80 | 146 | <Dialog.CloseTrigger class="btn preset-tonal">Cancel</Dialog.CloseTrigger> |
|
0 commit comments