Submit Tab Comment -- working code

PHOTO EMBED

Thu Sep 19 2024 04:36:22 GMT+0000 (Coordinated Universal Time)

Saved by @alfred.roshan

handleSubmitTabComment = async (e, type) => {
    e.preventDefault();
    this.setState({ loading: true });

    const allComments = Array.from(this.state.comments);

    const newComment = {
      date: new Date(),
      user: {
        name: this.state.user.username || this.state.user.name || 'N/A',
        companyId: this.state.user.companyId || 'N/A',
        id: this.state.user.id || 'N/A',
        email: this.state.user.email || 'N/A',
      },
      comment: this.state.tabcommentvalue,
      type: type,
    };

    allComments.push(newComment);

    allComments.sort((a, b) => new Date(b.date) - new Date(a.date));

    const { surveyId, currentSurveyId, consultantId } = this.state;
    // axios(this.postOptions(postUrl, newComment))
    addComment(
      {
        surveyId,
        consultantId,
      },
      newComment,
    )
      .then(response => {
        console.log('submitted response', response);
        this.setState({
          loading: false,
          showtabcommentmodal: {
            open: false,
            type: '',
          },
          tabcommentvalue: '',
          tabComments: allComments,
        });
        const summary = `${this.state.user.name} Added new comment ${newComment.comment}`;
        this.handleAddAuditLog(
          'add',
          this.state.assessmentType,
          'Assessment',
          summary,
          'Tab Comment',
        );
      })
      .catch(error => {
        this.setState({
          loading: false,
          showtabcommentmodal: {
            open: false,
            type: '',
          },
          tabcommentvalue: '',
          tabComments: allComments,
        });
      });

   
  };
content_copyCOPY