From ce0b82acb74ba3fec53832a597340d9e27ad87de Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Tue, 26 Nov 2024 15:10:17 -0500 Subject: [PATCH] Fix custom default category --- .../0011_category_adventure_category.py | 2 +- backend/server/adventures/models.py | 4 +- backend/server/adventures/serializers.py | 2 +- backend/server/adventures/views.py | 2 +- documentation/.vitepress/config.mts | 5 ++ .../src/lib/components/AdventureModal.svelte | 12 +++++ .../src/lib/components/CategoryModal.svelte | 51 ++++++++++++++----- frontend/src/routes/adventures/+page.svelte | 3 +- .../src/routes/adventures/[id]/+page.svelte | 7 +-- frontend/src/routes/map/+page.svelte | 4 +- 10 files changed, 66 insertions(+), 26 deletions(-) diff --git a/backend/server/adventures/migrations/0011_category_adventure_category.py b/backend/server/adventures/migrations/0011_category_adventure_category.py index 84ae013..6413454 100644 --- a/backend/server/adventures/migrations/0011_category_adventure_category.py +++ b/backend/server/adventures/migrations/0011_category_adventure_category.py @@ -19,7 +19,7 @@ class Migration(migrations.Migration): ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)), ('name', models.CharField(max_length=200)), ('display_name', models.CharField(max_length=200)), - ('icon', models.CharField(default='🌎', max_length=200)), + ('icon', models.CharField(default='🌍', max_length=200)), ('user_id', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], options={ diff --git a/backend/server/adventures/models.py b/backend/server/adventures/models.py index 7225dce..62f73ec 100644 --- a/backend/server/adventures/models.py +++ b/backend/server/adventures/models.py @@ -111,7 +111,7 @@ def save(self, force_insert: bool = False, force_update: bool = False, using: st if force_insert and force_update: raise ValueError("Cannot force both insert and updating in model saving.") if not self.category: - self.category = Category.objects.get_or_create(user_id=self.user_id, name='general', display_name='General', icon='🌎')[0] + self.category = Category.objects.get_or_create(user_id=self.user_id, name='general', display_name='General', icon='🌍')[0] return super().save(force_insert, force_update, using, update_fields) def __str__(self): @@ -260,7 +260,7 @@ class Category(models.Model): User, on_delete=models.CASCADE, default=default_user_id) name = models.CharField(max_length=200) display_name = models.CharField(max_length=200) - icon = models.CharField(max_length=200, default='🌎') + icon = models.CharField(max_length=200, default='🌍') class Meta: verbose_name_plural = 'Categories' diff --git a/backend/server/adventures/serializers.py b/backend/server/adventures/serializers.py index 87a26a5..c4ce818 100644 --- a/backend/server/adventures/serializers.py +++ b/backend/server/adventures/serializers.py @@ -90,7 +90,7 @@ def get_or_create_category(self, category_data): if isinstance(category_data, dict): name = category_data.get('name', '').lower() display_name = category_data.get('display_name', name) - icon = category_data.get('icon', '🌎') + icon = category_data.get('icon', '�') else: name = category_data.name.lower() display_name = category_data.display_name diff --git a/backend/server/adventures/views.py b/backend/server/adventures/views.py index fb85e37..348c142 100644 --- a/backend/server/adventures/views.py +++ b/backend/server/adventures/views.py @@ -644,7 +644,7 @@ def destroy(self, request, *args, **kwargs): general_category = Category.objects.filter(user_id=request.user, name='general').first() if not general_category: - general_category = Category.objects.create(user_id=request.user, name='general', icon='🌎', display_name='General') + general_category = Category.objects.create(user_id=request.user, name='general', icon='🌍', display_name='General') Adventure.objects.filter(category=instance).update(category=general_category) diff --git a/documentation/.vitepress/config.mts b/documentation/.vitepress/config.mts index 57b254b..8757709 100644 --- a/documentation/.vitepress/config.mts +++ b/documentation/.vitepress/config.mts @@ -6,6 +6,7 @@ export default defineConfig({ title: "AdventureLog", description: "The ultimate travel companion.", lang: "en-US", + // lastUpdated: true, ignoreDeadLinks: [ // ignore exact url "/playground" @@ -33,6 +34,10 @@ export default defineConfig({ search: { provider: "local", }, + editLink: { + pattern: + "https://github.com/seanmorley15/AdventureLog/edit/main/documentation/:path", + }, footer: { message: "AdventureLog", diff --git a/frontend/src/lib/components/AdventureModal.svelte b/frontend/src/lib/components/AdventureModal.svelte index 4148582..0dfc951 100644 --- a/frontend/src/lib/components/AdventureModal.svelte +++ b/frontend/src/lib/components/AdventureModal.svelte @@ -404,6 +404,18 @@ event.preventDefault(); console.log(adventure); if (adventure.id === '') { + console.log(categories); + if (categories.some((category) => category.name === 'general')) { + adventure.category = categories.find((category) => category.name === 'general') as Category; + } else { + adventure.category = { + id: '', + name: 'general', + display_name: 'General', + icon: '🌍', + user_id: '' + }; + } let res = await fetch('/api/adventures', { method: 'POST', headers: { diff --git a/frontend/src/lib/components/CategoryModal.svelte b/frontend/src/lib/components/CategoryModal.svelte index b71d6d6..c997a6b 100644 --- a/frontend/src/lib/components/CategoryModal.svelte +++ b/frontend/src/lib/components/CategoryModal.svelte @@ -10,6 +10,8 @@ let category_to_edit: Category | null = null; + let is_changed: boolean = false; + onMount(async () => { modal = document.getElementById('my_modal_1') as HTMLDialogElement; if (modal) { @@ -39,6 +41,7 @@ } return c; }); + is_changed = true; } } } @@ -60,6 +63,7 @@ }); if (response.ok) { categories = categories.filter((c) => c.id !== category.id); + is_changed = true; } }; } @@ -87,21 +91,44 @@ {/if} {#if category_to_edit} - - +

Edit Category

+
+ + + +
{/if} + + {#if is_changed} + + {/if} diff --git a/frontend/src/routes/adventures/+page.svelte b/frontend/src/routes/adventures/+page.svelte index 38b41cc..5ed3be5 100644 --- a/frontend/src/routes/adventures/+page.svelte +++ b/frontend/src/routes/adventures/+page.svelte @@ -361,8 +361,7 @@
-
-

{$t('adventures.sources')}

+

{$t('adventures.sources')}

{#if data.props.collection} @@ -339,8 +337,7 @@
{adventure.name}

- {typeof adventure.category === 'object' && adventure.category.display_name} - {typeof adventure.category === 'object' && adventure.category.icon} + {adventure.category?.display_name + ' ' + adventure.category?.icon}

{#if adventure.visits.length > 0}

diff --git a/frontend/src/routes/map/+page.svelte b/frontend/src/routes/map/+page.svelte index 80a361f..773860e 100644 --- a/frontend/src/routes/map/+page.svelte +++ b/frontend/src/routes/map/+page.svelte @@ -126,7 +126,7 @@ on:click={togglePopup} > - {typeof adventure.category === 'object' ? adventure.category.icon : adventure.category} + {adventure.category?.display_name + ' ' + adventure.category?.icon} {#if isPopupOpen} (isPopupOpen = false)}> @@ -138,7 +138,7 @@ {adventure.is_visited ? $t('adventures.visited') : $t('adventures.planned')}

- {adventure.category.display_name + ' ' + adventure.category.icon} + {adventure.category?.display_name + ' ' + adventure.category?.icon}

{#if adventure.visits && adventure.visits.length > 0}