Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. class RSVP extends Component {
  2. constructor(props) {
  3. super(props);
  4.  
  5. this.state = {
  6. chosenParty: null,
  7. allParties: null,
  8. showConfirmation: false,
  9. isLoading: false,
  10. loadingPartiesErrorId: null
  11. };
  12. }
  13.  
  14. componentDidMount() {
  15. this.loadParties();
  16. }
  17.  
  18. goToHome = () => { /* Omitted */ };
  19.  
  20. loadParties = () => { /* Omitted */ };
  21.  
  22. getPartyWithGuestName = name => { /* Omitted */ };
  23.  
  24. /**
  25. * Search for a party containing a guest with a matching name.
  26. */
  27. onNameFormSubmit = values => {
  28. const { name } = values;
  29. const foundParty = this.getPartyWithGuestName(name);
  30.  
  31. const { partyNotFoundAlert } = this.state;
  32. if (!foundParty && !partyNotFoundAlert) {
  33. const notFoundAlert = alerts.showError(
  34. "We could not find your invite. Please check your spelling and try again.",
  35. { onClose: () => this.setState({ partyNotFoundAlert: null }) }
  36. );
  37. this.setState({ partyNotFoundAlert: notFoundAlert });
  38. } else if (foundParty && foundParty.hasResponded) {
  39. if (partyNotFoundAlert) {
  40. alerts.close(partyNotFoundAlert);
  41. }
  42.  
  43. // Already RSVP'd, show confirmation
  44. this.setState({
  45. chosenParty: foundParty,
  46. showConfirmation: true,
  47. partyNotFoundAlert: null
  48. });
  49. alerts.showSuccess(
  50. "You have already responded. Please find your details below."
  51. );
  52. } else if (foundParty) {
  53. if (partyNotFoundAlert) {
  54. alerts.close(partyNotFoundAlert);
  55. }
  56. this.setState({ chosenParty: foundParty, partyNotFoundAlert: null });
  57. }
  58. };
  59.  
  60. renderNameForm = () => {
  61. return (
  62. <NameForm onSubmit={this.onNameFormSubmit} onCancel={this.goToHome} />
  63. );
  64. };
  65.  
  66. shouldRenderNameForm = () => {
  67. const { allParties, chosenParty, isLoading } = this.state;
  68.  
  69. return allParties && allParties.length && !chosenParty && !isLoading;
  70. };
  71.  
  72. shouldRenderGuestsForm = () => {
  73. const { chosenParty, showConfirmation, isLoading } = this.state;
  74.  
  75. return !!chosenParty && !showConfirmation && !isLoading;
  76. };
  77.  
  78. shouldRenderConfirmation = () => {
  79. const { showConfirmation, isLoading } = this.state;
  80. return showConfirmation && !isLoading;
  81. };
  82.  
  83. onUpdateGuests = updatedGuests => {
  84. const { chosenParty } = this.state;
  85. const updatedParty = {
  86. ...chosenParty,
  87. guests: updatedGuests,
  88. hasResponded: true
  89. };
  90.  
  91. this.setState({ isLoading: true });
  92.  
  93. const partyRef = dbRef.ref(`parties/${updatedParty.id}`);
  94. partyRef.set(updatedParty, error => {
  95. if (error) {
  96. console.error(error);
  97. alerts.showError(
  98. "An error has occurred submitting your response. Please try again"
  99. );
  100. } else {
  101. this.setState({
  102. chosenParty: updatedParty,
  103. showConfirmation: true,
  104. isLoading: false
  105. });
  106. }
  107. });
  108. };
  109.  
  110. renderGuestsForm = () => {
  111. const { chosenParty } = this.state;
  112. return (
  113. <GuestsForm
  114. guests={chosenParty.guests}
  115. updateGuests={this.onUpdateGuests}
  116. onCancel={this.goToHome}
  117. />
  118. );
  119. };
  120.  
  121. renderConfirmationScreen() {
  122. const { chosenParty } = this.state;
  123. return (
  124. <Confirmation guests={chosenParty.guests} goToHome={this.goToHome} />
  125. );
  126. }
  127.  
  128. render() {
  129. const { isLoading } = this.state;
  130. return (
  131. <PageWithNav>
  132. <Fragment>
  133. <AlertContainer
  134. template={AlertTemplate}
  135. closeButton={AlertCloseButton}
  136. />
  137. {isLoading && <LoadingIndicator />}
  138. {this.shouldRenderNameForm() && this.renderNameForm()}
  139. {this.shouldRenderGuestsForm() && this.renderGuestsForm()}
  140. {this.shouldRenderConfirmation() && this.renderConfirmationScreen()}
  141. </Fragment>
  142. </PageWithNav>
  143. );
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement