SyncButton.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {Button, Tooltip} from "antd";
  2. import {useState} from "react";
  3. import {useAppDispatch, useAppSelector} from "../../../../store";
  4. import {NginxApis} from "../../../../api/nginx.ts";
  5. import {Message} from "auto-antd";
  6. import {QuestionCircleOutlined} from "@ant-design/icons";
  7. import {createServerHost} from "../../utils/nginx.ts";
  8. import {NginxActions} from "../../../../store/slice/nginx.ts";
  9. import {INginxServer} from "../../../../models/nginx.ts";
  10. type IProps = {
  11. onSubmitData: ()=>Promise<false | INginxServer>
  12. upstream?: boolean
  13. }
  14. /**
  15. * 将server的配置同步到服务器
  16. * @constructor
  17. */
  18. export const SyncButton = ({ onSubmitData, upstream}: IProps) => {
  19. const [loading,setLoading] = useState(false)
  20. const nginx = useAppSelector(state => state.nginx.current);
  21. const dispatch = useAppDispatch()
  22. /**
  23. * 将配置文件同步到服务器
  24. */
  25. const onSyncServer = async () => {
  26. if (!nginx?.id){
  27. return
  28. }
  29. const serverData = await onSubmitData();
  30. if (!serverData){
  31. return
  32. }
  33. setLoading(true);
  34. const postData = createServerHost(nginx,serverData)
  35. NginxApis.refreshServer(postData)
  36. .then(()=>{
  37. Message.success("sync success!");
  38. const updateData: Partial<INginxServer>={
  39. ...serverData,
  40. confData: postData.serverConf,
  41. }
  42. if (upstream){
  43. dispatch(NginxActions.updateUpstream(updateData))
  44. }else {
  45. dispatch(NginxActions.updateServer(updateData))
  46. }
  47. })
  48. .finally(()=>{
  49. setLoading(false)
  50. })
  51. }
  52. if (!nginx?.id){
  53. return null
  54. }
  55. return (
  56. <>
  57. <Button danger loading={loading} onClick={onSyncServer}>
  58. 同步
  59. <Tooltip placement="left" title="同步配置文件到服务器,如果该server为禁用状态,将从服务器删除该配置文件">
  60. <QuestionCircleOutlined />
  61. </Tooltip>
  62. </Button>
  63. </>
  64. )
  65. }