質問編集履歴
1
pages/users/_id.vueの他に、store/index.js, store/posts.js, store/users.jsを追記させていただきました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -174,6 +174,390 @@
|
|
174
174
|
|
175
175
|
|
176
176
|
|
177
|
+
### 該当のソースコード
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
```indexjs
|
182
|
+
|
183
|
+
import moment from '~/plugins/moment'
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
export const state = () => ({
|
188
|
+
|
189
|
+
isLoggedIn: false,
|
190
|
+
|
191
|
+
user: null
|
192
|
+
|
193
|
+
})
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
export const getters = {
|
198
|
+
|
199
|
+
isLoggedIn: state => state.isLoggedIn,
|
200
|
+
|
201
|
+
user: state =>
|
202
|
+
|
203
|
+
(state.user ? Object.assign({ likes: [] }, state.user) :null)
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
export const mutations = {
|
210
|
+
|
211
|
+
setUser(state, { user }) {
|
212
|
+
|
213
|
+
if (user.id.match(/_|@|./)) {
|
214
|
+
|
215
|
+
throw new TypeError('invalid username')
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
state.user = user
|
220
|
+
|
221
|
+
state.isLoggedIn = true
|
222
|
+
|
223
|
+
},
|
224
|
+
|
225
|
+
updateUser(state, { user }) {
|
226
|
+
|
227
|
+
state.user = user
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
export const actions = {
|
236
|
+
|
237
|
+
async login({ commit }, { id }) {
|
238
|
+
|
239
|
+
if (id.match(/_|@|./)) {
|
240
|
+
|
241
|
+
throw new TypeError('invalid username')
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
const user = await this.$axios.$get(`/users/${id}.json`)
|
246
|
+
|
247
|
+
console.log(user)
|
248
|
+
|
249
|
+
if (!user.id) throw new Error('Invalid user')
|
250
|
+
|
251
|
+
commit('setUser', { user })
|
252
|
+
|
253
|
+
},
|
254
|
+
|
255
|
+
async register({ commit }, { id }) {
|
256
|
+
|
257
|
+
const payload = {}
|
258
|
+
|
259
|
+
payload[id] = { id }
|
260
|
+
|
261
|
+
await this.$axios.$patch(`/users.json`, payload)
|
262
|
+
|
263
|
+
const user = await this.$axios.$get(`/users/${id}.json`)
|
264
|
+
|
265
|
+
if (!user.id) throw new Error('Invalid user')
|
266
|
+
|
267
|
+
commit('setUser', { user })
|
268
|
+
|
269
|
+
},
|
270
|
+
|
271
|
+
async addLikeLogToUser({ commit }, { user, post }) {
|
272
|
+
|
273
|
+
user.likes.push({
|
274
|
+
|
275
|
+
created_at: moment().format(),
|
276
|
+
|
277
|
+
user_id: user.id,
|
278
|
+
|
279
|
+
post_id: post.id
|
280
|
+
|
281
|
+
})
|
282
|
+
|
283
|
+
const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
|
284
|
+
|
285
|
+
commit('updateUser', { user: newUser })
|
286
|
+
|
287
|
+
},
|
288
|
+
|
289
|
+
async removeLikeLogToUser({ commit }, { user, post }) {
|
290
|
+
|
291
|
+
user.likes = post.likes.filter(like => like.user_id !== user.id) || []
|
292
|
+
|
293
|
+
const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
|
294
|
+
|
295
|
+
commit('updateUser', { user: newUser })
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
```
|
304
|
+
|
305
|
+
###
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
###該当のソースコード
|
310
|
+
|
311
|
+
```postsjs
|
312
|
+
|
313
|
+
import moment from '~/plugins/moment'
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
export const state = () => ({
|
318
|
+
|
319
|
+
posts: []
|
320
|
+
|
321
|
+
})
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
export const getters = {
|
326
|
+
|
327
|
+
posts: state =>
|
328
|
+
|
329
|
+
state.posts.map(post => Object.assign({ likes: [] }, post))
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
export const mutations = {
|
336
|
+
|
337
|
+
addPost(state, { post }) {
|
338
|
+
|
339
|
+
state.posts.push(post)
|
340
|
+
|
341
|
+
},
|
342
|
+
|
343
|
+
updatePost(state, { post }) {
|
344
|
+
|
345
|
+
state.posts = state.posts.map((p) => (p.id === post.id ? post: p))
|
346
|
+
|
347
|
+
},
|
348
|
+
|
349
|
+
clearPosts(state) {
|
350
|
+
|
351
|
+
state.posts = []
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
export const actions = {
|
360
|
+
|
361
|
+
async fetchPost({ commit }, { id }) {
|
362
|
+
|
363
|
+
const post = await this.$axios.$get(`/posts/${id}.json`)
|
364
|
+
|
365
|
+
commit('addPost', { post: { ...post, id } })
|
366
|
+
|
367
|
+
},
|
368
|
+
|
369
|
+
async fetchPosts({ commit }) {
|
370
|
+
|
371
|
+
const posts = await this.$axios.$get(`/posts.json`)
|
372
|
+
|
373
|
+
commit('clearPosts')
|
374
|
+
|
375
|
+
Object.entries(posts || [])
|
376
|
+
|
377
|
+
.reverse()
|
378
|
+
|
379
|
+
.forEach(([id, content]) =>
|
380
|
+
|
381
|
+
commit('addPost', {
|
382
|
+
|
383
|
+
post: {
|
384
|
+
|
385
|
+
id,
|
386
|
+
|
387
|
+
...content
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
})
|
392
|
+
|
393
|
+
)
|
394
|
+
|
395
|
+
},
|
396
|
+
|
397
|
+
async publishPost({ commit }, { payload }) {
|
398
|
+
|
399
|
+
const user = await this.$axios.$get(`/users/${payload.user.id}.json`)
|
400
|
+
|
401
|
+
const created_at = moment().format()
|
402
|
+
|
403
|
+
payload = {
|
404
|
+
|
405
|
+
created_at,
|
406
|
+
|
407
|
+
...payload
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
const post_id = (await this.$axios.$get('/posts.json', payload)).name
|
412
|
+
|
413
|
+
const post = { id: post_id, ...payload, created_at }
|
414
|
+
|
415
|
+
const putData = { id: post_id, ...payload, created_at }
|
416
|
+
|
417
|
+
delete putData.user
|
418
|
+
|
419
|
+
await this.$axios.$put(`/users/${user.id}/posts.json`, [
|
420
|
+
|
421
|
+
...(user.posts || []),
|
422
|
+
|
423
|
+
putData
|
424
|
+
|
425
|
+
])
|
426
|
+
|
427
|
+
commit('addPost', { post })
|
428
|
+
|
429
|
+
},
|
430
|
+
|
431
|
+
async addLikeToPost({ commit }, { user, post }) {
|
432
|
+
|
433
|
+
post.likes.push({
|
434
|
+
|
435
|
+
created_at: moment().format(),
|
436
|
+
|
437
|
+
user_id: user.id,
|
438
|
+
|
439
|
+
post_id: post.id
|
440
|
+
|
441
|
+
})
|
442
|
+
|
443
|
+
const newPost = await this.$axios.$put(`/posts/${post.id}.json`, post)
|
444
|
+
|
445
|
+
commit('updatePost', { post: newPost })
|
446
|
+
|
447
|
+
},
|
448
|
+
|
449
|
+
async removeLikeToPost({ commit }, { user, post }) {
|
450
|
+
|
451
|
+
post.likes = post.likes.filter(like => like.user_id !== user.id) || []
|
452
|
+
|
453
|
+
const newPost = await this.$axios.$get(`/posts/${post.id}.json`, post)
|
454
|
+
|
455
|
+
commit('updatePost', { post: newPost })
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
```
|
464
|
+
|
465
|
+
###
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
###該当のソースコード
|
470
|
+
|
471
|
+
```usersjs
|
472
|
+
|
473
|
+
export const state = () => ({
|
474
|
+
|
475
|
+
users: []
|
476
|
+
|
477
|
+
})
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
export const getters = {
|
482
|
+
|
483
|
+
users: state => state.users
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
export const mutations = {
|
490
|
+
|
491
|
+
addUser(state, { user }) {
|
492
|
+
|
493
|
+
state.users.push(user)
|
494
|
+
|
495
|
+
},
|
496
|
+
|
497
|
+
addUserPost(state, { user, post }) {
|
498
|
+
|
499
|
+
state.userPosts[user.id].push(post)
|
500
|
+
|
501
|
+
},
|
502
|
+
|
503
|
+
clearUserPosts(state, { user }) {
|
504
|
+
|
505
|
+
state.userPosts[user.id] = []
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
export const actions = {
|
514
|
+
|
515
|
+
async fetchUser({ commit }, { id }) {
|
516
|
+
|
517
|
+
const user = await this.$axios.$get(`/users/${id}.json`)
|
518
|
+
|
519
|
+
commit('addUser', { user })
|
520
|
+
|
521
|
+
},
|
522
|
+
|
523
|
+
async addLikeLogToUser({ commit }, { user, post }) {
|
524
|
+
|
525
|
+
post.likes.push({
|
526
|
+
|
527
|
+
created_at: moment().format(),
|
528
|
+
|
529
|
+
user_id: user.id,
|
530
|
+
|
531
|
+
post_id: post.id
|
532
|
+
|
533
|
+
})
|
534
|
+
|
535
|
+
const newPost = await this.$axios.$get(`/posts/${post.id}.json`, post)
|
536
|
+
|
537
|
+
commit('updatePost', { post: newPost })
|
538
|
+
|
539
|
+
},
|
540
|
+
|
541
|
+
async removeLikeLogToUser({ commit }, { user, post }) {
|
542
|
+
|
543
|
+
post.likes = post.likes.filter(like => like.user_id !== user.id) || []
|
544
|
+
|
545
|
+
const newPost = await this.$axios.$put(`/posts/${post.id}.json`, post)
|
546
|
+
|
547
|
+
commit('updatePost', { post: newPost })
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
```
|
556
|
+
|
557
|
+
###
|
558
|
+
|
559
|
+
|
560
|
+
|
177
561
|
### 試したこと
|
178
562
|
|
179
563
|
|