Skip to content

Commit

Permalink
refactor!: change the posts structure to be equal to saved posts stru…
Browse files Browse the repository at this point in the history
…cture
  • Loading branch information
AlejandroAkbal committed Jun 26, 2021
1 parent 9b74c9b commit b9b326c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
17 changes: 4 additions & 13 deletions pages/premium/saved-posts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ export default {
let SAVED_POSTS = null
if (this.selectedBooru === '<All Boorus>') {
//
SAVED_POSTS = JSON.parse(JSON.stringify(this.getSavedPosts))
}
//
else {
} else {
//
const FILTERED_SAVED_POSTS = this.getSavedPosts.filter(
(POST) => POST.meta_data.booru_domain === this.selectedBooru
)
Expand All @@ -85,15 +84,7 @@ export default {
const SORTED_SAVED_POSTS = this.sortPostsByDate(SAVED_POSTS)
// Transform the structure
const SIMPLE_SAVED_POSTS = SORTED_SAVED_POSTS.map((SAVED_POST) => {
return {
...SAVED_POST.data,
_saved_post_meta_data: SAVED_POST.meta_data,
}
})
return SIMPLE_SAVED_POSTS
return SORTED_SAVED_POSTS
},
},
Expand Down
20 changes: 17 additions & 3 deletions store/booru.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,12 @@ export const actions = {
return urlToFetch.toString()
},

async fetchPosts({ dispatch }, mode) {
async fetchPosts({ getters, dispatch }, mode) {
// Tip: Actions that return a value have to be awaited
const url = await dispatch('createApiUrl', { mode: 'posts' })

const ACTIVE_BOORU_DOMAIN = getters.getActiveBooru.domain

try {
const response = await dispatch(
'simpleFetch',
Expand All @@ -395,10 +397,22 @@ export const actions = {
{ root: true }
)

// This is how a final booru object looks like
const POSTS = response.map((POST) => {
return {
id: `${ACTIVE_BOORU_DOMAIN}-${POST.id}`,
data: POST,
meta_data: {
booru_domain: ACTIVE_BOORU_DOMAIN,
created_at: null,
},
}
})

if (mode === 'concat') {
dispatch('postsManager', { operation: 'concat', value: response })
dispatch('postsManager', { operation: 'concat', value: POSTS })
} else {
dispatch('postsManager', { operation: 'set', value: response })
dispatch('postsManager', { operation: 'set', value: POSTS })
}

//
Expand Down
45 changes: 20 additions & 25 deletions store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ export const state = () => ({
savedPosts: [
// 1st default post
{
meta_data: {
booru_domain: 'rule34.xxx',

// :')
created_at: new Date(1997, 8 - 1, 22, '21'),
},
id: 'rule34.xxx-1',

data: {
id: 1,
Expand Down Expand Up @@ -97,16 +92,18 @@ export const state = () => ({
rating: 'safe',
media_type: 'image',
},
},

// 2nd default post
{
meta_data: {
booru_domain: 'gelbooru.com',
booru_domain: 'rule34.xxx',

// :')
created_at: new Date(2000, 1 - 1, 31, '0'),
created_at: new Date(1997, 8 - 1, 22, '21'),
},
},

// 2nd default post
{
id: 'gelbooru.com-5',

data: {
id: 5,
Expand Down Expand Up @@ -161,6 +158,13 @@ export const state = () => ({
rating: 'safe',
media_type: 'image',
},

meta_data: {
booru_domain: 'gelbooru.com',

// :')
created_at: new Date(2000, 1 - 1, 31, '0'),
},
},
],
},
Expand Down Expand Up @@ -327,33 +331,24 @@ export const actions = {
}
},

addPostToSavedPosts({ getters, commit }, { domain, post }) {
addPostToSavedPosts({ getters, commit }, { post }) {
const SAVED_POSTS = JSON.parse(JSON.stringify(getters.getSavedPosts))

const NEW_POST_DATA = {
meta_data: { booru_domain: domain, created_at: new Date().toJSON() },
data: post,
}
const NEW_POST_DATA = post
NEW_POST_DATA.meta_data.created_at = new Date().toJSON()

// Add new Post
SAVED_POSTS.push(NEW_POST_DATA)

commit('setSavedPosts', SAVED_POSTS)
},

removePostFromSavedPosts({ getters, commit }, { domain, post }) {
removePostFromSavedPosts({ getters, commit }, { postId }) {
const SAVED_POSTS = JSON.parse(JSON.stringify(getters.getSavedPosts))

// Remove post with the same ID
const FILTERED_SAVED_POSTS = SAVED_POSTS.filter((SAVED_POST) => {
return !(
// Post Domain
(
SAVED_POST.meta_data.booru_domain === domain &&
// Post ID
SAVED_POST.data.id === post.id
)
)
return SAVED_POST.id !== postId
})

commit('setSavedPosts', FILTERED_SAVED_POSTS)
Expand Down

0 comments on commit b9b326c

Please sign in to comment.