teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

アドバイスを受け無事解決したので、結果を書きました。

2020/05/18 15:35

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -88,4 +88,143 @@
88
88
  }
89
89
 
90
90
  export default Index;
91
+ ```
92
+
93
+ # 解決方法
94
+ アドバイスを受け無事表示できたので結果を載せておきます。
95
+
96
+ ## 【うまくいった方法】
97
+ js側で投稿記事を抽出するロジックを書く
98
+ jsx側では表示のみにする
99
+
100
+ ```jsx
101
+ // js側で抽出
102
+ // auth userの投稿記事のみ抽出
103
+ const result = list.filter((e) => {
104
+ return e.authId === authId;
105
+ });
106
+ console.log(result);
107
+ const length = result.length;
108
+ console.log(length);
109
+
110
+ {/* jsx側で表示 */}
111
+ <div className="post-list user-post-list">
112
+ {length !== 0 ? (
113
+ <React.Fragment>
114
+ {result.map(item => (
115
+ <div key={item.docId + String(new Date())}>
116
+ <div className="auth-inner-post-list">
117
+ <div className="auth-inner-post-text">
118
+ <div className="post-msg">{item.msg}</div>
119
+ <span className="delete" onClick={() => handleDelete(item.docId)}>&gt; Delete...</span>
120
+ </div>
121
+ </div>
122
+ </div>
123
+ ))}
124
+ </React.Fragment>
125
+ ) : (
126
+ <div className="no-user-posts">まだ記事が投稿されていません</div>
127
+ )}
128
+ </div>
129
+ ```
130
+
131
+ ```jsx
132
+ // コード全文
133
+ import React from 'react';
134
+ import { Link } from 'react-router-dom';
135
+ import firebase, { db } from '../../Firebase';
136
+ import SignOut from '../FirebaseAuthHook/SignOut';
137
+ import { useCollectionData } from "react-firebase-hooks/firestore";
138
+ import BackTopIcon from '../../assets/img/backtop.png';
139
+ import UserIcon from '../../assets/img/user.png';
140
+ import styled from 'styled-components';
141
+
142
+ const BackTopStyle = styled.img`
143
+ width: 50px;
144
+ top: 25px;
145
+ left: 25px;
146
+ position: fixed;
147
+ zIndex: 11,
148
+ `;
149
+
150
+ const Index = () => {
151
+ // Firebese Auth uid, email取得
152
+ const user = firebase.auth().currentUser;
153
+ let authId;
154
+ let name;
155
+ let photoURL;
156
+ if (user !== null) {
157
+ user.providerData.forEach(() => {
158
+ authId = user.uid;
159
+ name = user.displayName;
160
+ photoURL = user.photoURL;
161
+ });
162
+ }
163
+
164
+ // Render
165
+ const [ list, loading, error ] = useCollectionData(db.collection('posts').orderBy('createdAt', 'desc'), { idField: 'docId' });
166
+ if (loading) return <div>Loading...</div>;
167
+ if (error) return <div>Error...</div>;
168
+
169
+ // auth userの投稿記事のみ抽出
170
+ const result = list.filter((e) => {
171
+ return e.authId === authId;
172
+ });
173
+ console.log(result);
174
+ const length = result.length;
175
+ console.log(length);
176
+
177
+
178
+ // Delete
179
+ const handleDelete = (uid) => {
180
+ if (window.confirm('削除しますか?')) {
181
+ db.collection('posts').doc(uid).delete();
182
+ }
183
+ }
184
+
185
+ return(
186
+ <React.Fragment>
187
+ <SignOut />
188
+ <Link to="/"><BackTopStyle src={ BackTopIcon } alt="Topに戻る"/></Link>
189
+
190
+ <div className="user-wrapper">
191
+ <div className="user">
192
+ {photoURL ? (
193
+ <img src={ photoURL } className="auth-user-icon" alt="User Thumbnail" />
194
+ ) : (
195
+ <img src={ UserIcon } className="auth-user-icon" alt="Firebase Thumbnail" />
196
+ )}
197
+ <div className="user-datail">
198
+ {name ? (
199
+ <div className="user-name">{name}</div>
200
+ ) : (
201
+ <div className="user-name">Firebaseユーザー</div>
202
+ )}
203
+ </div>
204
+ </div>
205
+
206
+ <div className="post-list user-post-list">
207
+ {length !== 0 ? (
208
+ <React.Fragment>
209
+ {result.map(item => (
210
+ <div key={item.docId + String(new Date())}>
211
+ <div className="auth-inner-post-list">
212
+ <div className="auth-inner-post-text">
213
+ <div className="post-msg">{item.msg}</div>
214
+ <span className="delete" onClick={() => handleDelete(item.docId)}>&gt; Delete...</span>
215
+ </div>
216
+ </div>
217
+ </div>
218
+ ))}
219
+ </React.Fragment>
220
+ ) : (
221
+ <div className="no-user-posts">まだ記事が投稿されていません</div>
222
+ )}
223
+ </div>
224
+ </div>
225
+ </React.Fragment>
226
+ );
227
+ }
228
+
229
+ export default Index;
91
230
  ```