From c1b7d4210e38eb8ea941f59d146a550ecc029ed2 Mon Sep 17 00:00:00 2001
From: jableader
Date: Tue, 15 Oct 2024 19:40:06 +1100
Subject: [PATCH] Consolodated date formatting
---
src/components/meals/EditMealPage.vue | 29 +++--------------
src/components/meals/MealCard.vue | 25 +++------------
.../shopping/PurchasedShoppingListPage.vue | 7 ++++-
src/components/shopping/ShoppingListItem.vue | 31 ++-----------------
src/dateformats.js | 20 ++++++++++++
5 files changed, 37 insertions(+), 75 deletions(-)
create mode 100644 src/dateformats.js
diff --git a/src/components/meals/EditMealPage.vue b/src/components/meals/EditMealPage.vue
index 0c9cc9d..f85fd09 100644
--- a/src/components/meals/EditMealPage.vue
+++ b/src/components/meals/EditMealPage.vue
@@ -53,8 +53,7 @@
- Purchased {{ formatDate(meal.purchase_date) }}
-
+ Purchased {{ ago(meal.purchase_date) }}
@@ -63,6 +62,8 @@
import data from '@/data.js';
import alert from '@/alert.js';
+import { ago } from '@/dateformats.js';
+
import RecipeSearchBox from '@/components/recipes/RecipeSearchBox.vue';
import RecipeCard from '@/components/recipes/RecipeCard.vue';
import DatePicker from './DatePicker.vue';
@@ -103,6 +104,7 @@ export default {
}
},
methods: {
+ ago,
async selectRecipe(recipe) {
// Refetch to get additional details
recipe = await data.getRecipe(recipe.id);
@@ -121,29 +123,6 @@ export default {
selectDate(date) {
this.meal.suggested_date = date;
},
- formatDate(date) {
- // If it's today, show 'x hours ago'
- // If it's within the last week, show 'x days ago'
- // If it's within the last month weeks, show 'x weeks ago'
- // Otherwise, show the date as dd/mm
- const now = new Date();
- const diff = now - date;
- const days = Math.floor(diff / (1000 * 60 * 60 * 24));
- if (days === 0) {
- const hours = Math.floor(diff / (1000 * 60 * 60));
- return `${hours} hour${hours === 1 ? '' : 's'} ago`;
- }
-
- if (days < 7) {
- return `${days} day${days === 1 ? '' : 's'} ago`;
- }
-
- if (days < 30) {
- return `${Math.floor(days / 7)} week${days === 7 ? '' : 's'} ago`;
- }
-
- return date.toLocaleDateString('en-au', { day: 'numeric', month: 'numeric' });
- },
removeRecipe(mealRecipe) {
if (confirm(`Are you sure you want to remove ${mealRecipe.servings} servings of ${mealRecipe.recipe.name} from this meal?`)) {
this.meal.recipes = this.meal.recipes.filter(r => r != mealRecipe);
diff --git a/src/components/meals/MealCard.vue b/src/components/meals/MealCard.vue
index ce72a01..d3479d2 100644
--- a/src/components/meals/MealCard.vue
+++ b/src/components/meals/MealCard.vue
@@ -40,7 +40,7 @@
somebody?
- Purchased {{ formatDate(meal.purchase_date) }}
+ Purchased {{ ago(meal.purchase_date) }}
@@ -49,6 +49,9 @@
\ No newline at end of file
diff --git a/src/components/shopping/PurchasedShoppingListPage.vue b/src/components/shopping/PurchasedShoppingListPage.vue
index 5673659..125b2e0 100644
--- a/src/components/shopping/PurchasedShoppingListPage.vue
+++ b/src/components/shopping/PurchasedShoppingListPage.vue
@@ -1,5 +1,5 @@
- Purchased {{ shoppingList?.purchased_date?.toLocaleDateString({ year: 'numeric', month: '2-digit', day: '2-digit' }) || '' }}
+ Purchased {{ shoppingList ? ago(shoppingList.created_date) : '' }}
Included Meals
@@ -29,6 +29,8 @@
\ No newline at end of file
diff --git a/src/components/shopping/ShoppingListItem.vue b/src/components/shopping/ShoppingListItem.vue
index 2243776..93f8db1 100644
--- a/src/components/shopping/ShoppingListItem.vue
+++ b/src/components/shopping/ShoppingListItem.vue
@@ -108,6 +108,7 @@