1
1
import { makeStyles , useTheme } from "@material-ui/core/styles"
2
2
import RemoveCircleOutlineSharp from "@material-ui/icons/RemoveCircleOutlineSharp"
3
- import { useQuery } from "@tanstack/react-query"
3
+ import { useMutation , useQuery } from "@tanstack/react-query"
4
4
import { useMachine } from "@xstate/react"
5
- import { getLicenses } from "api/api"
5
+ import { getLicenses , removeLicense } from "api/api"
6
6
import { Header } from "components/DeploySettingsLayout/Header"
7
7
import { Stack } from "components/Stack/Stack"
8
8
import dayjs from "dayjs"
9
- import { FC , useEffect } from "react"
9
+ import { FC , useEffect , useState } from "react"
10
10
import { Helmet } from "react-helmet-async"
11
11
import { Link , useSearchParams } from "react-router-dom"
12
12
import { pageTitle } from "utils/page"
@@ -19,6 +19,8 @@ import Card from "@material-ui/core/Card"
19
19
import CardContent from "@material-ui/core/CardContent"
20
20
import Box from "@material-ui/core/Box"
21
21
import Skeleton from "@material-ui/lab/Skeleton"
22
+ import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
23
+ import { displayError , displaySuccess } from "components/GlobalSnackbar/utils"
22
24
23
25
const LicensesSettingsPage : FC = ( ) => {
24
26
const [ entitlementsState ] = useMachine ( entitlementsMachine )
@@ -27,11 +29,21 @@ const LicensesSettingsPage: FC = () => {
27
29
const [ searchParams , setSearchParams ] = useSearchParams ( )
28
30
const success = searchParams . get ( "success" )
29
31
const [ confettiOn , toggleConfettiOn ] = useToggle ( false )
32
+ const [ licenseIDMarkedForRemoval , setLicenseIDMarkedForRemoval ] = useState <
33
+ number | undefined
34
+ > ( undefined )
30
35
const { width, height } = useWindowSize ( )
31
36
37
+ const { mutate : removeLicenseApi , isLoading : isRemovingLicense } =
38
+ useMutation ( removeLicense )
39
+
32
40
const theme = useTheme ( )
33
41
34
- const { data : licenses , isLoading } = useQuery ( {
42
+ const {
43
+ data : licenses ,
44
+ isLoading,
45
+ refetch : refetchGetLicenses ,
46
+ } = useQuery ( {
35
47
queryKey : [ "licenses" ] ,
36
48
queryFn : ( ) => getLicenses ( ) ,
37
49
} )
@@ -49,14 +61,40 @@ const LicensesSettingsPage: FC = () => {
49
61
return (
50
62
< >
51
63
< Helmet >
52
- < title > { pageTitle ( "General Settings" ) } </ title >
64
+ < title > { pageTitle ( "License Settings" ) } </ title >
53
65
</ Helmet >
54
66
< Confetti
55
67
width = { width }
56
68
height = { height }
57
69
numberOfPieces = { confettiOn ? 200 : 0 }
58
70
colors = { [ theme . palette . primary . main , theme . palette . secondary . main ] }
59
71
/>
72
+ < ConfirmDialog
73
+ type = "info"
74
+ hideCancel = { false }
75
+ open = { licenseIDMarkedForRemoval !== undefined }
76
+ onConfirm = { ( ) => {
77
+ if ( ! licenseIDMarkedForRemoval ) {
78
+ return
79
+ }
80
+ removeLicenseApi ( licenseIDMarkedForRemoval , {
81
+ onSuccess : ( ) => {
82
+ displaySuccess ( "Successfully removed license" )
83
+ void refetchGetLicenses ( )
84
+ } ,
85
+ onError : ( ) => {
86
+ displayError ( "Failed to remove license" )
87
+ void refetchGetLicenses ( )
88
+ } ,
89
+ } )
90
+ setLicenseIDMarkedForRemoval ( undefined )
91
+ } }
92
+ onClose = { ( ) => setLicenseIDMarkedForRemoval ( undefined ) }
93
+ title = "Confirm removal"
94
+ confirmLoading = { isRemovingLicense }
95
+ confirmText = "Remove"
96
+ description = "Are you sure you want to remove this license?"
97
+ />
60
98
< Stack
61
99
alignItems = "baseline"
62
100
direction = "row"
@@ -72,7 +110,7 @@ const LicensesSettingsPage: FC = () => {
72
110
component = { Link }
73
111
to = "/settings/deployment/licenses/add"
74
112
>
75
- Add new License
113
+ Add new License key
76
114
</ Button >
77
115
</ Stack >
78
116
@@ -117,7 +155,9 @@ const LicensesSettingsPage: FC = () => {
117
155
118
156
< Stack direction = "column" spacing = { 0 } alignItems = "center" >
119
157
< span className = { styles . expirationDate } >
120
- { dayjs ( license . expires_at ) . format ( "MMMM D, YYYY" ) }
158
+ { dayjs
159
+ . unix ( license . claims . license_expires )
160
+ . format ( "MMMM D, YYYY" ) }
121
161
</ span >
122
162
< span className = { styles . expirationDateLabel } >
123
163
Valid until
@@ -128,6 +168,7 @@ const LicensesSettingsPage: FC = () => {
128
168
startIcon = { < RemoveCircleOutlineSharp /> }
129
169
variant = "text"
130
170
size = "small"
171
+ onClick = { ( ) => setLicenseIDMarkedForRemoval ( license . id ) }
131
172
>
132
173
Remove
133
174
</ Button >
@@ -143,7 +184,7 @@ const LicensesSettingsPage: FC = () => {
143
184
{ ! isLoading && licenses && licenses . length === 0 && (
144
185
< Stack spacing = { 4 } justifyContent = "center" alignItems = "center" >
145
186
< Button className = { styles . ctaButton } size = "large" >
146
- Add your license
187
+ Add your license key
147
188
</ Button >
148
189
</ Stack >
149
190
) }
0 commit comments