Display.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, {Component} from 'react';
  2. import {serverbyprops, servicebyprops} from "../../gql";
  3. import {Spin} from 'antd';
  4. import gql from "graphql-tag";
  5. import {Query} from "react-apollo";
  6. import Server from './Server';
  7. import Service from './Service';
  8. import './index.css'
  9. class Display extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. display: 'server',
  14. serverID: ''
  15. }
  16. }
  17. pageSwitchToService = (serverID) => {
  18. return () => {
  19. this.setState ({
  20. serverID,
  21. display: 'service'
  22. })
  23. }
  24. };
  25. pageSwitchToServer = () => {
  26. this.setState ({
  27. display: 'server',
  28. serverID: ''
  29. })
  30. };
  31. render() {
  32. return (
  33. <div>
  34. {
  35. this.state.display === 'server' ?
  36. <Query query={gql(serverbyprops)} variables={{}}>
  37. {
  38. ({loading, error, data}) => {
  39. if (loading) {
  40. return <Spin className={'spin'}/>
  41. }
  42. if (error) {
  43. return 'error!';
  44. }
  45. let servers = data.serverbyprops;
  46. let tip = '';
  47. if (servers.length === 0) {
  48. servers = [];
  49. tip = '还没有服务'
  50. }
  51. return (
  52. <Server
  53. servers={servers}
  54. tip={tip}
  55. pageSwitchToService={this.pageSwitchToService}
  56. />
  57. )
  58. }
  59. }
  60. </Query>
  61. :
  62. <Query query={gql(servicebyprops)} variables={{server_id: this.state.serverID}}>
  63. {
  64. ({loading, error, data}) => {
  65. if (loading) {
  66. return <Spin className={'spin'}/>
  67. }
  68. if (error) {
  69. return 'error!';
  70. }
  71. let services = data.servicebyprops;
  72. let tip = '';
  73. if (services.length === 0) {
  74. services = [];
  75. tip = '本人休息'
  76. }
  77. return (
  78. <Service
  79. services={services}
  80. tip={tip}
  81. pageSwitchToServer={this.pageSwitchToServer}
  82. />
  83. )
  84. }
  85. }
  86. </Query>
  87. }
  88. </div>
  89. );
  90. }
  91. }
  92. export default Display;