aboutsummaryrefslogtreecommitdiff
path: root/src/components/forum-element.js
blob: 1c2585bbb347d6f0055dba75d71492e5e5876b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
@license
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/

import { LitElement, html } from '@polymer/lit-element';
import { connect } from 'pwa-helpers/connect-mixin.js';

// This element is connected to the Redux store.
import { store } from '../store.js';

//These are the actions needed by this element.
import { createComment } from '../actions/firebaseFirestore';

// Import button styles
import { ButtonSharedStyles } from './button-shared-styles.js';

class ForumElement extends connect(store)(LitElement) {
  _render(props) {
    return html`
      ${ButtonSharedStyles}

      <style>

      paper-card {
        display: block;
        padding-bottom: 3px;
      }

      .button-grid {
        display: grid;
        grid-template-rows: 1fr;
      }

      .card-content > h4 {
        text-align:   right;
        font-weight:  lighter;
        font-style:   italic;
        word-break:   break-all;
      }

      .card-content > h3 {
        word-break: break-all;
      }

      .card-content > h3, p {
        text-align: center;
      }

      .success {
          width:        100%;
          margin-top:   5px;
      }

      @media (min-width: 460px) {
        .button-grid {
          grid-template-columns: 1fr 1fr;
        }

        #comment-subject-field {
            margin-left:    auto;
            margin-right:   auto;
            text-align:     center;
            width:          50%;
        }
      }


      </style>

      <paper-card
      elevation="0">
        <div class="card-content">
            <h4>${props.author}</h4>
            <h3>${props.subject}</h3>
            <p>${props.content}</p>
        </div>
        <div class="card-actions">
            <div class="button-grid">
                <paper-button class="info" onclick="${() => this.showComments = !this.showComments}">Show Comments</paper-button>
                <paper-button class="alert" onclick="${() => this.postComment  = !this.postComment}">${this.postComment? "Hide Draft" : "Post Comment"}</paper-button>
            </div>

            <div hidden="${!props.postComment}">
                <hr>
                <paper-input label="Comment Subject" id="comment-subject-field"></paper-input>
                <paper-textarea label="Comment Content" id="comment-content-field"></paper-textarea>
                <paper-button raised class="success" onclick="${() => this.submitComment()}">Submit Comment</paper-comment>
            </div>

            <div hidden="${!props.showComments}">
                <hr hidden="${!props.showComments && !props.postComment}">
                <h4>Comments</h4>

                </div>  
            </div>
        </div>
      </paper-card>
    `;
  }

  static get properties() { return {
    signedIn:   Boolean,

    author:     String,
    subject:    String,
    content:    String,
    postId:     String,

    comments:   Array,

    showComments:   Boolean,
    postComment:    Boolean
  }};

  _stateChanged(state) {
    this.signedIn = state.firebaseAuth.signedIn;
  }

  constructor() {
    super();

    this.signedIn   = false;

    this.author     = "";
    this.subject    = "";
    this.content    = "";
    this.postId     = "";

    this.comments   = [];

    this.showComments   = false;
    this.postComment    = false;
  }
  
    submitComment() {
        var commentSubject = this.shadowRoot.getElementById('comment-subject-field'); 
        var commentContent = this.shadowRoot.getElementById('comment-content-field'); 
        //commentContent.value.replace('\n','<br>')
        if( commentSubject                          &&
            commentSubject.value.trim().length > 0  &&
            commentContent.value.trim().length > 0) {
                if(confirm('Are you sure you want to submit this comment on ' + this.author + "'s post about " + this.subject + '?')) {
                    store.dispatch(createComment(this.postId, commentSubject.value, commentContent.value));

                    commentSubject.value = "";
                    commentContent.value = "";
                }
            } else {
                console.log(this.comments);
                alert('Please fill out comment subject and content fields.');
            }
            
    }
}

window.customElements.define('forum-element', ForumElement);