parsley/app/javascript/components/TheRecipeList.vue

264 lines
7.1 KiB
Vue
Raw Normal View History

2018-03-29 01:57:00 -05:00
<template>
<div>
2018-04-18 17:04:25 -05:00
<h1 class="title">Recipes</h1>
2018-03-30 14:31:09 -05:00
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
<app-loading v-if="localLoading"></app-loading>
2018-09-07 21:56:13 -05:00
<table class="table is-fullwidth" :class="{ small: mediaQueries.touch }">
2018-03-30 14:31:09 -05:00
<thead>
<tr>
<th v-for="h in tableHeader" :key="h.name">
<a v-if="h.sort" href="#" @click.prevent="setSort(h.name)">
{{h.label}}
<app-icon v-if="search.sortColumn === h.name" size="sm" :icon="search.sortDirection === 'asc' ? 'caret-bottom' : 'caret-top'"></app-icon>
</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">
2018-04-01 21:43:23 -05:00
</div>
</div>
</td>
<td>
<div class="field">
<div class="control">
<input type="text" class="input" placeholder="search tags" v-model="search.tags">
2018-04-01 21:43:23 -05:00
</div>
</div>
</td>
<td colspan="5"></td>
</tr>
2018-03-30 14:31:09 -05:00
</thead>
<transition-group name="fade" tag="tbody">
<tr v-for="r in recipes" :key="r.id">
<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>
<td>
<app-rating v-if="r.rating !== null" :value="r.rating" readonly></app-rating>
<span v-else>--</span>
</td>
<td>{{ r.yields }}</td>
<td class="recipe-time">{{ formatRecipeTime(r.total_time, r.active_time) }}</td>
<td><app-date-time :date-time="r.created_at" :show-time="false"></app-date-time></td>
<td>
<app-dropdown hover v-if="isLoggedIn" class="is-right">
<button slot="button" class="button">
<app-icon icon="menu"></app-icon>
</button>
<div class="dropdown-item">
<router-link :to="{name: 'new_log', params: { recipeId: r.id } }" class="button is-primary is-fullwidth">
<app-icon icon="star" size="md"></app-icon> <span>Add Log Entry</span>
</router-link>
</div>
<div class="dropdown-item">
<router-link :to="{name: 'edit_recipe', params: { id: r.id } }" class="button is-primary is-fullwidth">
<app-icon icon="pencil" size="md"></app-icon> <span>Edit Recipe</span>
</router-link>
2018-04-01 21:43:23 -05:00
</div>
<div class="dropdown-item">
<button type="button" class="button is-danger is-fullwidth" @click="deleteRecipe(r)">
<app-icon icon="x" size="md"></app-icon> <span>Delete Recipe</span>
2018-09-07 21:56:13 -05:00
</button>
</div>
2018-09-07 21:56:13 -05:00
</app-dropdown>
</td>
</tr>
</transition-group>
2018-03-30 14:31:09 -05:00
</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-05-01 10:55:57 -05:00
<app-confirm :open="showConfirmRecipeDelete" :message="confirmRecipeDeleteMessage" :cancel="recipeDeleteCancel" :confirm="recipeDeleteConfirm"></app-confirm>
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";
import { mapMutations, mapState } from "vuex";
import AppLoading from "./AppLoading";
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,
2018-05-01 10:55:57 -05:00
recipeForDeletion: null,
2018-04-01 21:43:23 -05:00
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: {
2018-09-07 21:56:13 -05:00
...mapState([
"mediaQueries"
]),
2018-03-30 14:31:09 -05:00
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-05-01 10:55:57 -05:00
},
showConfirmRecipeDelete() {
return this.recipeForDeletion !== null;
},
confirmRecipeDeleteMessage() {
if (this.showConfirmRecipeDelete) {
return `Are you sure you want to delete ${this.recipeForDeletion.name}?`;
} else {
return "??";
}
2018-03-30 14:31:09 -05:00
}
2018-04-01 21:43:23 -05:00
},
methods: {
...mapMutations([
"setInitialLoad"
]),
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
}
},
2018-05-01 10:55:57 -05:00
deleteRecipe(recipe) {
this.recipeForDeletion = recipe;
},
recipeDeleteConfirm() {
if (this.recipeForDeletion !== null) {
this.loadResource(
api.deleteRecipe(this.recipeForDeletion.id).then(() => {
this.recipeForDeletion = null;
return this.getList();
})
);
}
},
recipeDeleteCancel() {
this.recipeForDeletion = null;
},
2018-04-01 21:43:23 -05:00
getList: debounce(function() {
2018-05-01 10:55:57 -05:00
return this.loadResource(
2018-04-18 17:04:25 -05:00
api.getRecipeList(this.search.page, this.search.per, this.search.sortColumn, this.search.sortDirection, this.search.name, this.search.tags, data => this.recipeData = data)
2018-04-01 21:43:23 -05:00
);
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().then(() => this.setInitialLoad(true)),
2018-04-01 21:43:23 -05:00
{
deep: true,
immediate: true
}
);
},
components: {
AppLoading
2018-03-30 14:31:09 -05:00
}
2018-03-29 01:57:00 -05:00
}
2018-07-15 17:00:25 -05:00
</script>
<style lang="scss" scoped>
.recipe-time {
white-space: nowrap;
}
2018-09-07 21:56:13 -05:00
.table th {
white-space: nowrap;
}
.table.small {
td, th {
&:nth-of-type(3), &:nth-of-type(4), &:nth-of-type(5), &:nth-of-type(6) {
display: none;
}
}
}
2018-07-15 17:00:25 -05:00
</style>