11import BAIModal , { BAIModalProps } from './BAIModal' ;
2- import { DatePicker , Form , FormInstance , message } from 'antd' ;
2+ import { DatePicker , Form , FormInstance , message , Select } from 'antd' ;
33import { BAIFlex } from 'backend.ai-ui' ;
44import dayjs from 'dayjs' ;
55import React , { useRef } from 'react' ;
@@ -18,6 +18,7 @@ const DeploymentTokenGenerationModal: React.FC<
1818> = ( { onRequestClose, onCancel, deploymentId, ...baiModalProps } ) => {
1919 const { t } = useTranslation ( ) ;
2020 const formRef = useRef < FormInstance > ( null ) ;
21+ // expiryOption은 Form.Item에서 관리
2122
2223 const [ commitCreateAccessToken , isInFlightCreateAccessToken ] =
2324 useMutation < DeploymentTokenGenerationModalMutation > ( graphql `
@@ -36,41 +37,51 @@ const DeploymentTokenGenerationModal: React.FC<
3637 ` ) ;
3738
3839 const handleOk = ( ) => {
39- formRef . current ?. validateFields ( ) . then ( ( values ) => {
40- const validUntil = values . datetime . unix ( ) ;
41- commitCreateAccessToken ( {
42- variables : {
43- input : {
44- validUntil : validUntil ,
45- modelDeploymentId : deploymentId ,
40+ formRef . current
41+ ?. validateFields ( )
42+ . then ( ( values ) => {
43+ let validUntil ;
44+ if ( values . expiryOption === 'custom' ) {
45+ validUntil = values . datetime . unix ( ) ;
46+ } else {
47+ const daysToAdd = parseInt ( values . expiryOption . replace ( 'days' , '' ) ) ;
48+ validUntil = dayjs ( ) . add ( daysToAdd , 'day' ) . unix ( ) ;
49+ }
50+
51+ commitCreateAccessToken ( {
52+ variables : {
53+ input : {
54+ validUntil : validUntil ,
55+ modelDeploymentId : deploymentId ,
56+ } ,
4657 } ,
47- } ,
48- onCompleted : ( res , errors ) => {
49- if ( ! res ?. createAccessToken ?. accessToken ) {
50- message . error ( t ( 'deployment.TokenGenerationFailed' ) ) ;
51- return ;
52- }
53- if ( errors && errors . length > 0 ) {
54- const errorMsgList = errors . map ( ( error ) => error . message ) ;
55- for ( const error of errorMsgList ) {
56- message . error ( error ) ;
58+ onCompleted : ( res , errors ) => {
59+ if ( ! res ?. createAccessToken ?. accessToken ) {
60+ message . error ( t ( 'deployment.TokenGenerationFailed' ) ) ;
61+ return ;
5762 }
58- } else {
59- message . success ( t ( 'deployment.TokenGenerated' ) ) ;
60- onRequestClose ( true ) ;
61- }
62- } ,
63- onError : ( err ) => {
64- if ( err ?. message ?. includes ( 'valid_until is older than now' ) ) {
65- message . error ( t ( 'deployment.TokenExpiredDateError' ) ) ;
66- return ;
67- } else {
68- message . error ( t ( 'deployment.TokenGenerationFailed' ) ) ;
69- console . log ( err ) ;
70- }
71- } ,
72- } ) ;
73- } ) ;
63+ if ( errors && errors . length > 0 ) {
64+ const errorMsgList = errors . map ( ( error ) => error . message ) ;
65+ for ( const error of errorMsgList ) {
66+ message . error ( error ) ;
67+ }
68+ } else {
69+ message . success ( t ( 'deployment.TokenGenerated' ) ) ;
70+ onRequestClose ( true ) ;
71+ }
72+ } ,
73+ onError : ( err ) => {
74+ if ( err ?. message ?. includes ( 'valid_until is older than now' ) ) {
75+ message . error ( t ( 'deployment.TokenExpiredDateError' ) ) ;
76+ return ;
77+ } else {
78+ message . error ( t ( 'deployment.TokenGenerationFailed' ) ) ;
79+ console . log ( err ) ;
80+ }
81+ } ,
82+ } ) ;
83+ } )
84+ . catch ( ( ) => { } ) ;
7485 } ;
7586
7687 return (
@@ -81,45 +92,75 @@ const DeploymentTokenGenerationModal: React.FC<
8192 onCancel = { ( ) => onRequestClose ( false ) }
8293 okText = { t ( 'button.Generate' ) }
8394 confirmLoading = { isInFlightCreateAccessToken }
84- centered
8595 title = { t ( 'deployment.GenerateNewToken' ) }
8696 >
8797 < Form
8898 ref = { formRef }
8999 preserve = { false }
90- labelCol = { { span : 10 } }
100+ labelCol = { { span : 12 } }
91101 initialValues = { {
92- datetime : dayjs ( ) . add ( 24 , 'hour' ) ,
102+ expiryOption : '7days' ,
103+ datetime : dayjs ( ) . add ( 7 , 'day' ) ,
93104 } }
94105 validateTrigger = { [ 'onChange' , 'onBlur' ] }
95106 >
96107 < BAIFlex direction = "column" gap = "sm" align = "stretch" justify = "center" >
97- < Form . Item
98- name = "datetime"
99- label = { t ( 'deployment.ExpiredDate' ) }
100- rules = { [
101- {
102- type : 'object' ,
103- required : true ,
104- message : t ( 'deployment.PleaseSelectTime' ) ,
105- } ,
106- ( ) => ( {
107- validator ( _ , value ) {
108- if ( value . isAfter ( dayjs ( ) ) ) {
109- return Promise . resolve ( ) ;
110- }
111- return Promise . reject (
112- new Error ( t ( 'deployment.TokenExpiredDateError' ) ) ,
113- ) ;
108+ < BAIFlex direction = "row" align = "stretch" justify = "around" >
109+ < Form . Item
110+ name = "expiryOption"
111+ label = { t ( 'deployment.ExpirationDate' ) }
112+ rules = { [
113+ {
114+ required : true ,
115+ message : t ( 'deployment.PleaseSelectTime' ) ,
114116 } ,
115- } ) ,
116- ] }
117- >
118- < DatePicker
119- showTime
120- format = "YYYY-MM-DD HH:mm:ss"
121- style = { { width : 200 } }
122- />
117+ ] }
118+ >
119+ < Select
120+ style = { { width : 200 } }
121+ options = { [
122+ { value : '7days' , label : t ( 'general.Days' , { num : 7 } ) } ,
123+ { value : '30days' , label : t ( 'general.Days' , { num : 30 } ) } ,
124+ { value : '90days' , label : t ( 'general.Days' , { num : 90 } ) } ,
125+ { value : 'custom' , label : t ( 'deployment.Custom' ) } ,
126+ ] }
127+ />
128+ </ Form . Item >
129+ </ BAIFlex >
130+ < Form . Item dependencies = { [ 'expiryOption' ] } noStyle >
131+ { ( { getFieldValue } ) =>
132+ getFieldValue ( 'expiryOption' ) === 'custom' ? (
133+ < BAIFlex direction = "row" align = "stretch" justify = "around" >
134+ < Form . Item
135+ name = "datetime"
136+ label = { t ( 'deployment.CustomExpirationDate' ) }
137+ rules = { [
138+ {
139+ type : 'object' as const ,
140+ required : true ,
141+ message : t ( 'deployment.PleaseSelectTime' ) ,
142+ } ,
143+ ( ) => ( {
144+ validator ( _ , value ) {
145+ if ( value && value . isAfter ( dayjs ( ) ) ) {
146+ return Promise . resolve ( ) ;
147+ }
148+ return Promise . reject (
149+ new Error ( t ( 'deployment.TokenExpiredDateError' ) ) ,
150+ ) ;
151+ } ,
152+ } ) ,
153+ ] }
154+ >
155+ < DatePicker
156+ showTime
157+ format = "YYYY-MM-DD HH:mm:ss"
158+ style = { { width : 200 } }
159+ />
160+ </ Form . Item >
161+ </ BAIFlex >
162+ ) : null
163+ }
123164 </ Form . Item >
124165 </ BAIFlex >
125166 </ Form >
0 commit comments