2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-05-12 21:38:31 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-07-18 20:57:38 +08:00
import { Button , Col , Input , Modal , Row } from "antd" ;
2021-05-12 21:38:31 +08:00
import i18next from "i18next" ;
import React from "react" ;
2022-07-10 15:45:55 +08:00
import * as Setting from "./Setting" ;
import * as UserBackend from "./backend/UserBackend" ;
2022-01-07 20:34:27 +08:00
import { CountDownInput } from "./common/CountDownInput" ;
2021-11-28 20:18:03 +08:00
import { MailOutlined , PhoneOutlined } from "@ant-design/icons" ;
2021-05-12 21:38:31 +08:00
export const ResetModal = ( props ) => {
const [ visible , setVisible ] = React . useState ( false ) ;
const [ confirmLoading , setConfirmLoading ] = React . useState ( false ) ;
2021-05-15 11:47:09 +08:00
const [ dest , setDest ] = React . useState ( "" ) ;
const [ code , setCode ] = React . useState ( "" ) ;
2022-07-10 00:40:52 +08:00
const { buttonText , destType , application } = props ;
2021-05-12 21:38:31 +08:00
const showModal = ( ) => {
setVisible ( true ) ;
} ;
const handleCancel = ( ) => {
setVisible ( false ) ;
} ;
const handleOk = ( ) => {
if ( dest === "" ) {
2021-05-13 09:39:07 +08:00
Setting . showMessage ( "error" , i18next . t ( "user:Empty " + destType ) ) ;
2021-05-12 21:38:31 +08:00
return ;
}
if ( code === "" ) {
2021-06-17 22:43:30 +08:00
Setting . showMessage ( "error" , i18next . t ( "code:Empty Code" ) ) ;
2021-05-12 21:38:31 +08:00
return ;
}
setConfirmLoading ( true ) ;
UserBackend . resetEmailOrPhone ( dest , destType , code ) . then ( res => {
if ( res . status === "ok" ) {
2021-05-12 22:09:41 +08:00
Setting . showMessage ( "success" , i18next . t ( "user:" + destType + " reset" ) ) ;
2021-05-12 21:38:31 +08:00
window . location . reload ( ) ;
} else {
2021-05-12 22:09:41 +08:00
Setting . showMessage ( "error" , i18next . t ( "user:" + res . msg ) ) ;
2021-05-12 21:38:31 +08:00
setConfirmLoading ( false ) ;
}
2022-07-10 15:45:55 +08:00
} ) ;
} ;
2021-05-12 21:38:31 +08:00
2021-05-12 22:09:41 +08:00
let placeHolder = "" ;
2022-07-10 15:45:55 +08:00
if ( destType === "email" ) { placeHolder = i18next . t ( "user:Input your email" ) ; } else if ( destType === "phone" ) { placeHolder = i18next . t ( "user:Input your phone number" ) ; }
2021-05-12 22:09:41 +08:00
2021-05-12 21:38:31 +08:00
return (
< Row >
2021-05-15 20:44:40 +08:00
< Button type = "default" onClick = { showModal } >
2021-05-12 21:38:31 +08:00
{ buttonText }
< / B u t t o n >
< Modal
2021-06-10 20:04:39 +08:00
maskClosable = { false }
2021-05-12 21:38:31 +08:00
title = { buttonText }
visible = { visible }
okText = { buttonText }
cancelText = { i18next . t ( "user:Cancel" ) }
confirmLoading = { confirmLoading }
onCancel = { handleCancel }
onOk = { handleOk }
width = { 600 }
>
< Col style = { { margin : "0px auto 40px auto" , width : 1000 , height : 300 } } >
< Row style = { { width : "100%" , marginBottom : "20px" } } >
2021-05-20 21:09:12 +08:00
< Input
2021-11-28 20:18:03 +08:00
addonBefore = { destType === "email" ? i18next . t ( "user:New Email" ) : i18next . t ( "user:New phone" ) }
prefix = { destType === "email" ? < MailOutlined / > : < PhoneOutlined / > }
2021-05-20 21:09:12 +08:00
placeholder = { placeHolder }
onChange = { e => setDest ( e . target . value ) }
2021-05-12 21:38:31 +08:00
/ >
< / R o w >
< Row style = { { width : "100%" , marginBottom : "20px" } } >
2021-05-20 21:09:12 +08:00
< CountDownInput
2021-06-17 22:43:30 +08:00
textBefore = { i18next . t ( "code:Code You Received" ) }
2021-05-20 21:09:12 +08:00
onChange = { setCode }
2022-07-10 00:40:52 +08:00
onButtonClickArgs = { [ dest , destType , Setting . getApplicationName ( application ) ] }
2022-08-23 23:30:45 +08:00
application = { application }
2021-05-20 21:09:12 +08:00
/ >
2021-05-12 21:38:31 +08:00
< / R o w >
< / C o l >
< / M o d a l >
< / R o w >
2022-07-10 15:45:55 +08:00
) ;
} ;
2021-05-12 21:38:31 +08:00
2021-05-15 20:44:40 +08:00
export default ResetModal ;