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

質問編集履歴

1

pages/users/_id.vueの他に、store/index.js, store/posts.js, store/users.jsを追記させていただきました。

2019/10/23 05:35

投稿

toshihirokato
toshihirokato

スコア20

title CHANGED
File without changes
body CHANGED
@@ -86,6 +86,198 @@
86
86
 
87
87
  ```
88
88
 
89
+ ### 該当のソースコード
90
+
91
+ ```indexjs
92
+ import moment from '~/plugins/moment'
93
+
94
+ export const state = () => ({
95
+ isLoggedIn: false,
96
+ user: null
97
+ })
98
+
99
+ export const getters = {
100
+ isLoggedIn: state => state.isLoggedIn,
101
+ user: state =>
102
+ (state.user ? Object.assign({ likes: [] }, state.user) :null)
103
+ }
104
+
105
+ export const mutations = {
106
+ setUser(state, { user }) {
107
+ if (user.id.match(/_|@|./)) {
108
+ throw new TypeError('invalid username')
109
+ }
110
+ state.user = user
111
+ state.isLoggedIn = true
112
+ },
113
+ updateUser(state, { user }) {
114
+ state.user = user
115
+ }
116
+ }
117
+
118
+ export const actions = {
119
+ async login({ commit }, { id }) {
120
+ if (id.match(/_|@|./)) {
121
+ throw new TypeError('invalid username')
122
+ }
123
+ const user = await this.$axios.$get(`/users/${id}.json`)
124
+ console.log(user)
125
+ if (!user.id) throw new Error('Invalid user')
126
+ commit('setUser', { user })
127
+ },
128
+ async register({ commit }, { id }) {
129
+ const payload = {}
130
+ payload[id] = { id }
131
+ await this.$axios.$patch(`/users.json`, payload)
132
+ const user = await this.$axios.$get(`/users/${id}.json`)
133
+ if (!user.id) throw new Error('Invalid user')
134
+ commit('setUser', { user })
135
+ },
136
+ async addLikeLogToUser({ commit }, { user, post }) {
137
+ user.likes.push({
138
+ created_at: moment().format(),
139
+ user_id: user.id,
140
+ post_id: post.id
141
+ })
142
+ const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
143
+ commit('updateUser', { user: newUser })
144
+ },
145
+ async removeLikeLogToUser({ commit }, { user, post }) {
146
+ user.likes = post.likes.filter(like => like.user_id !== user.id) || []
147
+ const newUser = await this.$axios.$put(`/users/${user.id}.json`, user)
148
+ commit('updateUser', { user: newUser })
149
+ }
150
+ }
151
+
152
+ ```
153
+ ###
154
+
155
+ ###該当のソースコード
156
+ ```postsjs
157
+ import moment from '~/plugins/moment'
158
+
159
+ export const state = () => ({
160
+ posts: []
161
+ })
162
+
163
+ export const getters = {
164
+ posts: state =>
165
+ state.posts.map(post => Object.assign({ likes: [] }, post))
166
+ }
167
+
168
+ export const mutations = {
169
+ addPost(state, { post }) {
170
+ state.posts.push(post)
171
+ },
172
+ updatePost(state, { post }) {
173
+ state.posts = state.posts.map((p) => (p.id === post.id ? post: p))
174
+ },
175
+ clearPosts(state) {
176
+ state.posts = []
177
+ }
178
+ }
179
+
180
+ export const actions = {
181
+ async fetchPost({ commit }, { id }) {
182
+ const post = await this.$axios.$get(`/posts/${id}.json`)
183
+ commit('addPost', { post: { ...post, id } })
184
+ },
185
+ async fetchPosts({ commit }) {
186
+ const posts = await this.$axios.$get(`/posts.json`)
187
+ commit('clearPosts')
188
+ Object.entries(posts || [])
189
+ .reverse()
190
+ .forEach(([id, content]) =>
191
+ commit('addPost', {
192
+ post: {
193
+ id,
194
+ ...content
195
+ }
196
+ })
197
+ )
198
+ },
199
+ async publishPost({ commit }, { payload }) {
200
+ const user = await this.$axios.$get(`/users/${payload.user.id}.json`)
201
+ const created_at = moment().format()
202
+ payload = {
203
+ created_at,
204
+ ...payload
205
+ }
206
+ const post_id = (await this.$axios.$get('/posts.json', payload)).name
207
+ const post = { id: post_id, ...payload, created_at }
208
+ const putData = { id: post_id, ...payload, created_at }
209
+ delete putData.user
210
+ await this.$axios.$put(`/users/${user.id}/posts.json`, [
211
+ ...(user.posts || []),
212
+ putData
213
+ ])
214
+ commit('addPost', { post })
215
+ },
216
+ async addLikeToPost({ commit }, { user, post }) {
217
+ post.likes.push({
218
+ created_at: moment().format(),
219
+ user_id: user.id,
220
+ post_id: post.id
221
+ })
222
+ const newPost = await this.$axios.$put(`/posts/${post.id}.json`, post)
223
+ commit('updatePost', { post: newPost })
224
+ },
225
+ async removeLikeToPost({ commit }, { user, post }) {
226
+ post.likes = post.likes.filter(like => like.user_id !== user.id) || []
227
+ const newPost = await this.$axios.$get(`/posts/${post.id}.json`, post)
228
+ commit('updatePost', { post: newPost })
229
+ }
230
+ }
231
+
232
+ ```
233
+ ###
234
+
235
+ ###該当のソースコード
236
+ ```usersjs
237
+ export const state = () => ({
238
+ users: []
239
+ })
240
+
241
+ export const getters = {
242
+ users: state => state.users
243
+ }
244
+
245
+ export const mutations = {
246
+ addUser(state, { user }) {
247
+ state.users.push(user)
248
+ },
249
+ addUserPost(state, { user, post }) {
250
+ state.userPosts[user.id].push(post)
251
+ },
252
+ clearUserPosts(state, { user }) {
253
+ state.userPosts[user.id] = []
254
+ }
255
+ }
256
+
257
+ export const actions = {
258
+ async fetchUser({ commit }, { id }) {
259
+ const user = await this.$axios.$get(`/users/${id}.json`)
260
+ commit('addUser', { user })
261
+ },
262
+ async addLikeLogToUser({ commit }, { user, post }) {
263
+ post.likes.push({
264
+ created_at: moment().format(),
265
+ user_id: user.id,
266
+ post_id: post.id
267
+ })
268
+ const newPost = await this.$axios.$get(`/posts/${post.id}.json`, post)
269
+ commit('updatePost', { post: newPost })
270
+ },
271
+ async removeLikeLogToUser({ commit }, { user, post }) {
272
+ post.likes = post.likes.filter(like => like.user_id !== user.id) || []
273
+ const newPost = await this.$axios.$put(`/posts/${post.id}.json`, post)
274
+ commit('updatePost', { post: newPost })
275
+ }
276
+ }
277
+
278
+ ```
279
+ ###
280
+
89
281
  ### 試したこと
90
282
 
91
283
  yarn run lint --fix等のツールでエラー解析したが、原因を見つけることができませんでした。