| 
									
										
										
										
											2023-03-15 16:30:27 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  |   import type { PageData } from "./$types"; | 
					
						
							|  |  |  |   import { DateTime, Duration } from "luxon"; | 
					
						
							|  |  |  |   import { Alert, Button } from "sveltestrap"; | 
					
						
							| 
									
										
										
										
											2023-05-22 14:48:48 +02:00
										 |  |  |   import { PUBLIC_MEDIA_URL } from "$env/static/public"; | 
					
						
							| 
									
										
										
										
											2023-03-30 16:05:40 +02:00
										 |  |  |   import { fastFetchClient } from "$lib/api/fetch"; | 
					
						
							| 
									
										
										
										
											2023-03-15 16:30:27 +01:00
										 |  |  |   import type { APIError } from "$lib/api/entities"; | 
					
						
							|  |  |  |   import { addToast } from "$lib/toast"; | 
					
						
							|  |  |  |   import ErrorAlert from "$lib/components/ErrorAlert.svelte"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   export let data: PageData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const createdAt = data.exportData?.created_at | 
					
						
							|  |  |  |     ? DateTime.fromISO(data.exportData.created_at).toLocal() | 
					
						
							| 
									
										
										
										
											2023-03-16 16:43:28 +00:00
										 |  |  |     : DateTime.fromSeconds(0); | 
					
						
							| 
									
										
										
										
											2023-03-15 16:30:27 +01:00
										 |  |  |   const now = DateTime.now().toLocal(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const availableFor = Duration.fromObject({ days: 7 }); | 
					
						
							|  |  |  |   const minTimeBetween = Duration.fromObject({ days: 1 }); | 
					
						
							|  |  |  |   const durationSinceCreated = now.diff(createdAt, "days"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let error: APIError | null = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const requestExport = async () => { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2023-03-30 16:05:40 +02:00
										 |  |  |       await fastFetchClient("/users/@me/export/start"); | 
					
						
							| 
									
										
										
										
											2023-03-15 16:30:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       addToast({ | 
					
						
							|  |  |  |         header: "Export in progress", | 
					
						
							|  |  |  |         body: "Your data is now being exported! Check back here in a few minutes.", | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       error = e as APIError; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   <h1> | 
					
						
							|  |  |  |     Data export | 
					
						
							|  |  |  |     <Button | 
					
						
							|  |  |  |       color="success" | 
					
						
							|  |  |  |       disabled={durationSinceCreated.days < minTimeBetween.days} | 
					
						
							|  |  |  |       on:click={requestExport}>Export your data</Button | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |   </h1> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   {#if error} | 
					
						
							|  |  |  |     <ErrorAlert {error} /> | 
					
						
							|  |  |  |   {/if} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   {#if data.exportData} | 
					
						
							|  |  |  |     <div> | 
					
						
							|  |  |  |       {#if durationSinceCreated.days < minTimeBetween.days} | 
					
						
							|  |  |  |         <Alert color="secondary" fade={false}> | 
					
						
							|  |  |  |           You can only export your data once a day. You can next export your data at <b | 
					
						
							|  |  |  |             >{createdAt.plus(minTimeBetween).toLocaleString(DateTime.DATETIME_MED)}</b | 
					
						
							|  |  |  |           >. | 
					
						
							|  |  |  |         </Alert> | 
					
						
							|  |  |  |       {/if} | 
					
						
							|  |  |  |       <p> | 
					
						
							|  |  |  |         You last exported your data at {createdAt.toLocaleString(DateTime.DATETIME_MED)}. | 
					
						
							|  |  |  |         <br /> | 
					
						
							|  |  |  |         This file will be available until {createdAt | 
					
						
							|  |  |  |           .plus(availableFor) | 
					
						
							|  |  |  |           .toLocal() | 
					
						
							|  |  |  |           .toLocaleString(DateTime.DATETIME_MED)} | 
					
						
							|  |  |  |         <br /> | 
					
						
							|  |  |  |         Download your export file below: | 
					
						
							|  |  |  |       </p> | 
					
						
							|  |  |  |       <p> | 
					
						
							| 
									
										
										
										
											2023-05-22 14:48:48 +02:00
										 |  |  |         <Button color="primary" href="{PUBLIC_MEDIA_URL}{data.exportData.path}" target="_blank" | 
					
						
							| 
									
										
										
										
											2023-03-15 16:30:27 +01:00
										 |  |  |           >Download data export</Button | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |       </p> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   {:else} | 
					
						
							|  |  |  |     <Alert color="secondary" fade={false}> | 
					
						
							|  |  |  |       You haven't exported your data in the past week. To create an export file, click the button | 
					
						
							|  |  |  |       above. | 
					
						
							|  |  |  |     </Alert> | 
					
						
							|  |  |  |   {/if} | 
					
						
							|  |  |  | </div> |