Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Fix route issue and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar committed May 12, 2017
1 parent 0eb5343 commit 24ea552
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 118 deletions.
10 changes: 5 additions & 5 deletions dist/build.js

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions functions.php
Expand Up @@ -94,9 +94,9 @@ function rt_vue_title( $title, $sep, $seplocation ) {
}

// Extend rest response
add_action( 'rest_api_init', 'rt_add_thumbnail' );
add_action( 'rest_api_init', 'rt_extend_rest_post_response' );

function rt_add_thumbnail() {
function rt_extend_rest_post_response() {

// Add featured image
register_rest_field( 'post',
Expand All @@ -118,13 +118,13 @@ function rt_add_thumbnail() {
);

register_rest_field( 'post',
'tag_name',
array(
'get_callback' => 'rt_get_tag_name',
'update_callback' => null,
'schema' => null,
)
);
'tag_name',
array(
'get_callback' => 'rt_get_tag_name',
'update_callback' => null,
'schema' => null,
)
);

}
// Get featured image
Expand Down Expand Up @@ -159,23 +159,23 @@ function rt_get_cat_name( $object, $field_name, $request ) {

}

function rt_get_tag_name($object, $field_name, $request) {

$tags = $object['tags'];
$res = [];
$ob = [];

foreach ( $tags as $x ) {
$tag_id = (int) $x;
$tag = get_tag( $tag_id );
if( is_wp_error( $tag ) ) {
$res[] = '';
} else {
$ob['name'] = isset( $tag->name ) ? $tag->name : '';
$ob['id'] = isset( $tag->term_id ) ? $tag->term_id : '';
$ob['slug'] = isset( $tag->slug ) ? $tag->slug : '';
$res[] = $ob;
}
}
return $res;
function rt_get_tag_name( $object, $field_name, $request ) {

$tags = $object['tags'];
$res = [];
$ob = [];

foreach ( $tags as $x ) {
$tag_id = (int) $x;
$tag = get_tag( $tag_id );
if ( is_wp_error( $tag ) ) {
$res[] = '';
} else {
$ob['name'] = isset( $tag->name ) ? $tag->name : '';
$ob['id'] = isset( $tag->term_id ) ? $tag->term_id : '';
$ob['slug'] = isset( $tag->slug ) ? $tag->slug : '';
$res[] = $ob;
}
}
return $res;
}
7 changes: 6 additions & 1 deletion sass/_posts-and-pages.scss
Expand Up @@ -62,8 +62,13 @@
margin: rem-calc(5);
padding: rem-calc(2);
border-radius: rem-calc(5);
background-color: #666;
background-color: #bcbcbc;
color: $black;

&:hover{
opacity: 0.8;
}

}
}

Expand Down
11 changes: 1 addition & 10 deletions src/components/category.vue
Expand Up @@ -81,8 +81,6 @@ export default {
vm.posts = res.data;
vm.totalPages = res.headers[ 'x-wp-totalpages' ];
vm.loaded = 'true';
vm.pageTitle = 'Category' + ' - ' + vm.catName;
Expand Down Expand Up @@ -129,14 +127,7 @@ export default {
}
}
},
watch: {
'$route'( to, from ) {
this.getPosts( this.$route.params.page );
}
},
}
};
</script>
133 changes: 62 additions & 71 deletions src/components/tag.vue
Expand Up @@ -41,102 +41,93 @@
</template>

<script>
export default {
export default {
mounted: function() {
mounted: function() {
const vm = this;
const vm = this;
if ( vm.$route.params.name ) {
if ( vm.$route.params.name ) {
vm.getTagId( vm.$route.params.name );
vm.getTagId( vm.$route.params.name );
}
}
},
data() {
},
data() {
return {
return {
posts: {},
loaded: 'false',
pageTitle: '',
totalCount: '',
tagName: ''
posts: {},
loaded: 'false',
pageTitle: '',
totalCount: '',
tagName: ''
};
};
},
},
methods: {
methods: {
getPosts: function( tagId ) {
getPosts: function( tagId ) {
const vm = this;
vm.loaded = 'false';
vm.$http.get( 'wp/v2/posts', {
params: { tags: tagId }
} )
.then( ( res ) => {
vm.posts = res.data;
vm.totalPages = res.headers[ 'x-wp-totalpages' ];
vm.loaded = 'true';
const vm = this;
vm.loaded = 'false';
vm.$http.get( 'wp/v2/posts', {
params: { tags: tagId }
} )
.then( ( res ) => {
vm.pageTitle = 'Tag' + ' - ' + vm.tagName;
vm.posts = res.data;
vm.$store.commit( 'rtChangeTitle', vm.pageTitle );
vm.loaded = 'true';
} )
.catch( ( res ) => {
//console.log( `Something went wrong : ${ res }` );
} );
vm.pageTitle = 'Tag' + ' - ' + vm.tagName;
},
getTagId: function( name ) {
const vm = this;
vm.tagName = name;
vm.loaded = 'false';
vm.$http.get( 'wp/v2/tags/?slug=' + name )
.then( ( res ) => {
vm.$store.commit( 'rtChangeTitle', vm.pageTitle );
res = res.data[ 0 ];
vm.totalCount = ( res.data );
vm.getPosts( res.id );
} )
.catch( ( res ) => {
//console.log( `Something went wrong : ${ res }` );
} );
} )
.catch( ( res ) => {
//console.log( `Something went wrong : ${ res }` );
} );
},
formatDate: function( value ) {
},
getTagId: function( name ) {
const vm = this;
vm.tagName = name;
vm.loaded = 'false';
vm.$http.get( 'wp/v2/tags/?slug=' + name )
.then( ( res ) => {
value = value.date;
if ( value ) {
const date = new Date( value );
const monthNames = [ "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" ];
res = res.data[ 0 ];
vm.totalCount = ( res.data );
vm.getPosts( res.id );
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
} )
.catch( ( res ) => {
//console.log( `Something went wrong : ${ res }` );
} );
},
formatDate: function( value ) {
return monthNames[ monthIndex ] + ',' + day + ' ' + year;
}
value = value.date;
if ( value ) {
const date = new Date( value );
const monthNames = [ "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" ];
}
},
watch: {
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
'$route'( to, from ) {
this.getPosts( this.$route.params.page );
return monthNames[ monthIndex ] + ',' + day + ' ' + year;
}
},
}
}
};
};
</script>
2 changes: 1 addition & 1 deletion src/main.js
Expand Up @@ -42,7 +42,7 @@ const App = Vue.extend( {
//Define route for vue app
//ref : http://router.vuejs.org/en/
const router = new VueRouter( {

mode: 'history',
routes: [

{ path: '/blog/:page(\\d+)?', name: 'home', component: posts },
Expand Down

0 comments on commit 24ea552

Please sign in to comment.