parsley/app/javascript/components/TheRecipeList.vue

183 lines
5.1 KiB
Vue
Raw Normal View History

2018-03-29 01:57:00 -05:00
<template>
<div>
2018-03-30 14:31:09 -05:00
<h1 class="title">Recipes</h1>
2018-04-03 18:31:20 -05:00
<router-link v-if="isLoggedIn" :to="{name: 'new_recipe'}" class="button is-primary">Create Recipe</router-link>
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="recipe" @changePage="changePage"></app-pager>
2018-03-30 14:31:09 -05:00
<table class="table">
<thead>
<tr>
2018-04-01 21:43:23 -05:00
<th v-for="h in tableHeader" :key="h.name">
<a v-if="h.sort" href="#" @click.prevent="setSort(h.name)">
{{h.label}}
2018-04-04 19:46:02 -05:00
<app-icon v-if="search.sortColumn === h.name" size="sm" :icon="search.sortDirection === 'asc' ? 'caret-bottom' : 'caret-top'"></app-icon>
2018-04-01 21:43:23 -05:00
</a>
<span v-else>{{h.label}}</span>
</th>
<th></th>
</tr>
<tr>
<td>
<div class="field">
<div class="control">
<input type="text" class="input" placeholder="search names" v-model="search.name">
</div>
</div>
</td>
<td>
<div class="field">
<div class="control">
<input type="text" class="input" placeholder="search tags" v-model="search.tags">
</div>
</div>
</td>
<td colspan="5"></td>
2018-03-30 14:31:09 -05:00
</tr>
</thead>
<tbody>
<tr v-for="r in recipes" :key="r.id">
2018-04-01 21:43:23 -05:00
<td><router-link :to="{name: 'recipe', params: { id: r.id } }">{{r.name}}</router-link></td>
<td>
<div class="tags">
<span class="tag" v-for="tag in r.tags" :key="tag">{{tag}}</span>
</div>
</td>
2018-04-03 10:29:57 -05:00
<td>
<app-rating v-if="r.rating !== null" :rating="r.rating" ></app-rating>
<span v-else>--</span>
</td>
<td>{{ r.yields }}</td>
<td>{{ formatRecipeTime(r.total_time, r.active_time) }}</td>
2018-04-04 19:46:02 -05:00
<td><app-date-time :date-time="r.created_at" :show-time="false"></app-date-time></td>
2018-04-01 21:43:23 -05:00
<td>
2018-04-13 10:25:18 -05:00
<router-link v-if="isLoggedIn" :to="{name: 'new_log', params: { recipeId: r.id } }" class="button is-primary">
<app-icon icon="star" size="md"></app-icon>
</router-link>
2018-04-04 19:46:02 -05:00
<router-link v-if="isLoggedIn" :to="{name: 'edit_recipe', params: { id: r.id } }" class="button is-primary">
2018-04-03 18:31:20 -05:00
<app-icon icon="pencil" size="md"></app-icon>
</router-link>
<button v-if="isLoggedIn" type="button" class="button is-danger">
<app-icon icon="x" size="md"></app-icon>
</button>
2018-04-01 21:43:23 -05:00
</td>
2018-03-30 14:31:09 -05:00
</tr>
</tbody>
</table>
2018-04-03 18:31:20 -05:00
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="recipe" @changePage="changePage"></app-pager>
2018-03-29 01:57:00 -05:00
</div>
</template>
<script>
2018-03-30 14:31:09 -05:00
import api from "../lib/Api";
2018-04-01 21:43:23 -05:00
import debounce from "lodash/debounce";
2018-03-30 14:31:09 -05:00
2018-03-29 01:57:00 -05:00
export default {
2018-03-30 14:31:09 -05:00
data() {
return {
2018-04-01 21:43:23 -05:00
recipeData: null,
search: {
2018-04-04 19:46:02 -05:00
sortColumn: 'created_at',
2018-04-01 21:43:23 -05:00
sortDirection: 'desc',
page: 1,
per: 25,
name: null,
tags: null
}
2018-03-30 14:31:09 -05:00
};
},
computed: {
recipes() {
if (this.recipeData) {
return this.recipeData.recipes;
} else {
return [];
}
2018-04-01 21:43:23 -05:00
},
tableHeader() {
return [
{name: 'name', label: 'Name', sort: true},
{name: 'tags', label: 'Tags', sort: false},
{name: 'rating', label: 'Rating', sort: true},
{name: 'yields', label: 'Yields', sort: false},
{name: 'total_time', label: 'Time', sort: true},
{name: 'created_at', label: 'Created', sort: true}
]
2018-04-03 18:31:20 -05:00
},
totalPages() {
if (this.recipeData) {
return this.recipeData.total_pages;
}
return 0;
},
currentPage() {
if (this.recipeData) {
return this.recipeData.current_page;
}
return 0;
2018-03-30 14:31:09 -05:00
}
2018-04-01 21:43:23 -05:00
},
methods: {
2018-04-03 18:31:20 -05:00
changePage(idx) {
this.search.page = idx;
},
2018-04-01 21:43:23 -05:00
setSort(col) {
if (this.search.sortColumn === col) {
this.search.sortDirection = this.search.sortDirection === "desc" ? "asc" : "desc";
} else {
this.search.sortColumn = col;
2018-04-04 19:46:02 -05:00
this.search.sortDirection = "asc";
2018-04-01 21:43:23 -05:00
}
},
getList: debounce(function() {
this.loadResource(
api.getRecipeList(this.search.page, this.search.per, this.search.sortColumn, this.search.sortDirection, this.search.name, this.search.tags)
.then(data => this.recipeData = data)
);
2018-04-03 10:29:57 -05:00
}, 500, {leading: true, trailing: true}),
formatRecipeTime(total, active) {
let str = "";
if (total && total > 0) {
str += total;
}
if (active && active > 0) {
if (str.length) {
str += " (" + active + ")";
} else {
str += active;
}
}
return str;
}
2018-04-01 21:43:23 -05:00
},
created() {
this.$watch("search",
() => this.getList(),
{
deep: true,
immediate: true
}
);
},
components: {
2018-03-30 14:31:09 -05:00
}
2018-03-29 01:57:00 -05:00
}
</script>