GroupModal.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {Modal, Button, Tabs, Form, Input, message} from 'antd';
  2. import React, {Component} from "react";
  3. import './add-folder-modal.less'
  4. import {SearchOutlined, UserOutlined} from "@ant-design/icons";
  5. import getCookie from "../../utils/getCookie";
  6. import {apiCreateGroup, apiJoinGroup, apiQuitGroup} from "../../services/API/API";
  7. const {TabPane} = Tabs;
  8. class GroupModal extends Component {
  9. state = {
  10. visible: false,
  11. }
  12. showModal = () => {
  13. this.setState({
  14. visible: true
  15. })
  16. }
  17. setVisible = visible => {
  18. this.setState({
  19. visible: visible
  20. })
  21. }
  22. handleOk = () => {
  23. this.Child.clickButton()
  24. setTimeout(() => {
  25. this.setVisible(false);
  26. }, 1000);
  27. };
  28. handleJoinGroup = values => {
  29. let params = {
  30. username: getCookie('username'),
  31. token: getCookie('token'),
  32. ...values
  33. }
  34. apiJoinGroup(params).then(res => {
  35. if (res.data.code === 200) {
  36. message.success('加入成功');
  37. this.props.getGroupList()
  38. this.handleCancel()
  39. } else if (res.data.code === 401) {
  40. message.error('未登录')
  41. window.location.href = "";
  42. } else if (res.data.code === 402) {
  43. message.error('你已在群内')
  44. } else if (res.data.code === 403) {
  45. message.error('该群不存在')
  46. } else {
  47. message.error('错误')
  48. }
  49. })
  50. }
  51. handleCreateGroup = values => {
  52. let params = {
  53. username: getCookie('username'),
  54. token: getCookie('token'),
  55. ...values
  56. }
  57. apiCreateGroup(params).then(res => {
  58. if (res.data.code === 200) {
  59. message.success('建群成功');
  60. this.props.getGroupList()
  61. this.handleCancel()
  62. } else if (res.data.code === 401) {
  63. message.error('未登录')
  64. window.location.href = "";
  65. } else if (res.data.code === 500) {
  66. message.error('新建失败')
  67. } else {
  68. message.error('错误')
  69. }
  70. })
  71. }
  72. handleQuitGroup = () => {
  73. let params = {
  74. username: getCookie('username'),
  75. token: getCookie('token'),
  76. group_id: this.props.group_id,
  77. }
  78. apiQuitGroup(params).then(res => {
  79. if (res.data.code === 200) {
  80. message.success('退出成功');
  81. this.props.getGroupList()
  82. this.handleCancel()
  83. } else if (res.data.code === 401) {
  84. message.error('未登录')
  85. window.location.href = "";
  86. } else if (res.data.code === 403) {
  87. message.error('该群不存在')
  88. } else if (res.data.code === 421) {
  89. message.error('群主不可退群')
  90. } else {
  91. message.error('错误')
  92. }
  93. })
  94. }
  95. handleCancel = () => {
  96. this.setVisible(false);
  97. };
  98. render() {
  99. return (
  100. <div className={'group-modal'}>
  101. <Button type="primary" shape="circle" icon={<SearchOutlined/>} onClick={this.showModal}/>
  102. <Modal
  103. title='Join / Create Group'
  104. visible={this.state.visible}
  105. footer={null}
  106. onCancel={this.handleCancel}
  107. >
  108. <div>
  109. <Tabs
  110. defaultActiveKey="1"
  111. size={'large'}
  112. animated={true}
  113. centered={true}
  114. tabBarGutter={50}
  115. >
  116. <TabPane
  117. tab='Join'
  118. key="1"
  119. style={{padding: "20px 30px 10px"}}>
  120. <Form onFinish={this.handleJoinGroup} initialValues={{remember: true}}>
  121. <Form.Item
  122. name="group_id"
  123. rules={[{required: true, message: 'Please input Group Id!'}]}
  124. >
  125. <Input
  126. prefix={<UserOutlined id="item-icon"/>}
  127. placeholder="Group Id"
  128. size="large"
  129. />
  130. </Form.Item>
  131. <Form.Item style={{marginBottom: '10px'}}>
  132. <Button size="large" type="primary" htmlType="submit"
  133. style={{width: '100%',}}>
  134. Join
  135. </Button>
  136. </Form.Item>
  137. </Form>
  138. </TabPane>
  139. <TabPane tab="Create" key="2" style={{padding: "20px 30px 0"}}>
  140. <Form onFinish={this.handleCreateGroup} initialValues={{remember: true}}>
  141. <Form.Item
  142. name="group_name"
  143. rules={[{required: true, message: 'Please input Group Name!'}]}
  144. >
  145. <Input
  146. prefix={<UserOutlined id="item-icon"/>}
  147. placeholder="Group Name"
  148. size="large"
  149. />
  150. </Form.Item>
  151. <Form.Item style={{marginBottom: '10px'}}>
  152. <Button size="large" type="primary" htmlType="submit"
  153. style={{width: '100%',}}>
  154. Create
  155. </Button>
  156. </Form.Item>
  157. </Form>
  158. </TabPane>
  159. <TabPane tab="Quit" key="3" style={{padding: "20px 30px 0"}}>
  160. <Form onFinish={this.handleQuitGroup}
  161. initialValues={{remember: true}}>
  162. <Form.Item style={{marginBottom: '10px'}}>
  163. <Button size="large" type="primary" htmlType="submit"
  164. style={{width: '100%',}}>
  165. Quit Group
  166. </Button>
  167. </Form.Item>
  168. </Form>
  169. </TabPane>
  170. </Tabs>
  171. </div>
  172. </Modal>
  173. </div>
  174. )
  175. }
  176. }
  177. export default GroupModal;