parsley/app/javascript/components/TheFoodList.vue

131 lines
3.9 KiB
Vue
Raw Normal View History

2018-03-30 14:31:09 -05:00
<template>
2018-04-02 00:10:06 -05:00
<div>
<h1 class="title">Ingredients</h1>
2018-03-30 14:31:09 -05:00
2018-04-07 10:54:56 -05:00
<div class="buttons">
2024-10-02 14:34:50 -05:00
<router-link v-if="appConfig.isLoggedIn" :to="{name: 'new_food'}" class="button is-primary">Create Ingredient</router-link>
2018-04-07 10:54:56 -05:00
</div>
2018-04-03 18:31:20 -05:00
2018-09-11 22:56:26 -05:00
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="food" @changePage="changePage"></app-pager>
2018-04-02 00:10:06 -05:00
2018-04-07 10:54:56 -05:00
<table class="table is-fullwidth is-narrow">
2018-04-02 00:10:06 -05:00
<thead>
<tr>
<th>Name</th>
<th>USDA</th>
<th>KCal per 100g</th>
2018-04-03 18:31:20 -05:00
<th>Density (oz/cup)</th>
2018-04-02 00:10:06 -05:00
<th></th>
</tr>
<tr>
<th>
<div class="field">
<div class="control">
<input type="text" class="input" placeholder="search names" v-model="search.name">
</div>
</div>
</th>
<th colspan="4"></th>
</tr>
</thead>
<transition-group tag="tbody" name="fade" mode="out-in">
2018-09-11 22:56:26 -05:00
<tr v-for="i in foods" :key="i.id">
<td><router-link :to="{name: 'food', params: { id: i.id } }">{{i.name}}</router-link></td>
2018-04-02 00:10:06 -05:00
<td><app-icon v-if="i.usda" icon="check"></app-icon></td>
<td>{{i.kcal}}</td>
<td>{{i.density}}</td>
<td>
2024-10-02 14:34:50 -05:00
<template v-if="appConfig.isLoggedIn">
<router-link class="button" :to="{name: 'edit_food', params: { id: i.id } }">
<app-icon icon="pencil"></app-icon>
</router-link>
<button type="button" class="button is-danger" @click="deleteFood(i)">
<app-icon icon="x"></app-icon>
</button>
</template>
2018-04-02 00:10:06 -05:00
</td>
</tr>
</transition-group>
2018-04-02 00:10:06 -05:00
</table>
2018-09-11 22:56:26 -05:00
<app-pager :current-page="currentPage" :total-pages="totalPages" paged-item-name="food" @changePage="changePage"></app-pager>
2018-04-03 18:31:20 -05:00
2018-04-07 10:54:56 -05:00
<div class="buttons">
2024-10-02 14:34:50 -05:00
<router-link v-if="appConfig.isLoggedIn" :to="{name: 'new_food'}" class="button is-primary">Create Ingredient</router-link>
2018-04-07 10:54:56 -05:00
</div>
2024-10-02 14:34:50 -05:00
<app-confirm :open="showConfirmFoodDelete" title="Delete Ingredient?" :message="confirmFoodDeleteMessage" @cancel="foodDeleteCancel" @confirm="foodDeleteConfirm"></app-confirm>
2018-05-01 10:55:57 -05:00
2018-04-02 00:10:06 -05:00
</div>
2018-03-30 14:31:09 -05:00
</template>
2024-10-02 14:34:50 -05:00
<script setup>
2018-03-30 14:31:09 -05:00
2024-10-02 14:34:50 -05:00
import { computed, reactive, ref, watch } from "vue";
2018-04-02 00:10:06 -05:00
import api from "../lib/Api";
import debounce from "lodash/debounce";
2024-10-02 14:34:50 -05:00
import { useAppConfigStore } from "../stores/appConfig";
import { useLoadResource } from "../lib/useLoadResource";
const appConfig = useAppConfigStore();
const { loadResource } = useLoadResource();
const foodData = ref(null);
const foodForDeletion = ref(null);
const search = reactive({
page: 1,
per: 25,
name: null
});
const foods = computed(() => foodData.value?.foods || []);
const totalPages = computed(() => foodData.value?.total_pages || 0);
const currentPage = computed(() => foodData.value?.current_page || 0);
const showConfirmFoodDelete = computed(() => foodForDeletion.value !== null);
const confirmFoodDeleteMessage = computed(() => {
if (foodForDeletion.value !== null) {
return `Are you sure you want to delete ${foodForDeletion.value.name}?`;
} else {
return "??";
}
});
const getList = debounce(function() {
return loadResource(
api.getFoodList(search.page, search.per, search.name)
.then(data => foodData.value = data)
);
}, 500, {leading: true, trailing: true});
watch(search,
() => getList(),
{
deep: true,
immediate: true
2018-05-01 10:55:57 -05:00
}
2024-10-02 14:34:50 -05:00
);
function changePage(idx) {
search.page = idx;
}
2018-04-02 00:10:06 -05:00
2024-10-02 14:34:50 -05:00
function deleteFood(food) {
foodForDeletion.value = food;
}
function foodDeleteCancel() {
foodForDeletion.value = null;
}
function foodDeleteConfirm() {
if (foodForDeletion.value !== null) {
loadResource(
api.deleteFood(foodForDeletion.value.id).then(res => {
foodForDeletion.value = null;
return getList();
})
);
2018-04-02 00:10:06 -05:00
}
}
2018-03-30 14:31:09 -05:00
2018-04-02 00:10:06 -05:00
</script>