Skip to content

Commit

Permalink
Merge pull request #135 from seanmorley15/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
seanmorley15 authored Jul 19, 2024
2 parents 91f6a5c + c6633a1 commit 7c33b9e
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 209 deletions.
18 changes: 18 additions & 0 deletions backend/server/adventures/migrations/0011_adventure_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-07-19 12:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0010_adventure_created_at_collection_created_at'),
]

operations = [
migrations.AddField(
model_name='adventure',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
1 change: 1 addition & 0 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Adventure(models.Model):
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def clean(self):
if self.collection:
Expand Down
12 changes: 6 additions & 6 deletions backend/server/adventures/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class AdventureViewSet(viewsets.ModelViewSet):
pagination_class = StandardResultsSetPagination

def apply_sorting(self, queryset):
order_by = self.request.query_params.get('order_by', 'created_at')
order_by = self.request.query_params.get('order_by', 'updated_at')
order_direction = self.request.query_params.get('order_direction', 'asc')
include_collections = self.request.query_params.get('include_collections', 'true')

valid_order_by = ['name', 'type', 'date', 'rating', 'created_at']
valid_order_by = ['name', 'type', 'date', 'rating', 'updated_at']
if order_by not in valid_order_by:
order_by = 'name'

Expand All @@ -52,12 +52,12 @@ def apply_sorting(self, queryset):
if order_direction == 'desc':
ordering = f'-{ordering}'

# reverse ordering for created_at field
if order_by == 'created_at':
# reverse ordering for updated_at field
if order_by == 'updated_at':
if order_direction == 'asc':
ordering = '-created_at'
ordering = '-updated_at'
else:
ordering = 'created_at'
ordering = 'updated_at'

print(f"Ordering by: {ordering}") # For debugging

Expand Down
6 changes: 5 additions & 1 deletion frontend/src/lib/components/ActivityComplete.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@
{#each activities as activity}
<li class="flex items-center justify-between bg-base-200 p-2 rounded">
{activity}
<button class="btn btn-sm btn-error" on:click={() => removeActivity(activity)}>
<button
type="button"
class="btn btn-sm btn-error"
on:click={() => removeActivity(activity)}
>
Remove
</button>
</li>
Expand Down
119 changes: 70 additions & 49 deletions frontend/src/lib/components/AdventureCard.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { goto } from '$app/navigation';
import type { Adventure } from '$lib/types';
import type { Adventure, User } from '$lib/types';
const dispatch = createEventDispatcher();
import Launch from '~icons/mdi/launch';
Expand All @@ -20,10 +20,25 @@
export let type: string;
export let user: User | null;
let isCollectionModalOpen: boolean = false;
export let adventure: Adventure;
let activityTypes: string[] = [];
// makes it reactivty to changes so it updates automatically
$: {
if (adventure.activity_types) {
activityTypes = adventure.activity_types;
if (activityTypes.length > 3) {
activityTypes = activityTypes.slice(0, 3);
let remaining = adventure.activity_types.length - 3;
activityTypes.push('+' + remaining);
}
}
}
async function deleteAdventure() {
let res = await fetch(`/adventures/${adventure.id}?/delete`, {
method: 'POST',
Expand Down Expand Up @@ -129,14 +144,14 @@
<h2 class="text-2xl font-semibold -mt-2 break-words text-wrap">
{adventure.name}
</h2>
<div>
{#if adventure.type == 'visited'}
<div class="badge badge-primary">Visited</div>
{:else}
<div class="badge badge-secondary">Planned</div>
{/if}
<div class="badge badge-neutral">{adventure.is_public ? 'Public' : 'Private'}</div>
</div>
</div>
<div>
{#if adventure.type == 'visited' && user?.pk == adventure.user_id}
<div class="badge badge-primary">Visited</div>
{:else if user?.pk == adventure.user_id}
<div class="badge badge-secondary">Planned</div>
{/if}
<div class="badge badge-neutral">{adventure.is_public ? 'Public' : 'Private'}</div>
</div>
{#if adventure.location && adventure.location !== ''}
<div class="inline-flex items-center">
Expand All @@ -152,7 +167,7 @@
{/if}
{#if adventure.activity_types && adventure.activity_types.length > 0}
<ul class="flex flex-wrap">
{#each adventure.activity_types as activity}
{#each activityTypes as activity}
<div class="badge badge-primary mr-1 text-md font-semibold pb-2 pt-1 mb-1">
{activity}
</div>
Expand All @@ -162,48 +177,54 @@
<div class="card-actions justify-end mt-2">
<!-- action options dropdown -->
{#if type != 'link'}
<div class="dropdown dropdown-end">
<div tabindex="0" role="button" class="btn btn-neutral">
<DotsHorizontal class="w-6 h-6" />
</div>
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<ul
tabindex="0"
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow"
>
<button
class="btn btn-neutral mb-2"
on:click={() => goto(`/adventures/${adventure.id}`)}
><Launch class="w-6 h-6" />Open Details</button
{#if user?.pk == adventure.user_id}
<div class="dropdown dropdown-end">
<div tabindex="0" role="button" class="btn btn-neutral">
<DotsHorizontal class="w-6 h-6" />
</div>
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<ul
tabindex="0"
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow"
>
<button class="btn btn-neutral mb-2" on:click={editAdventure}>
<FileDocumentEdit class="w-6 h-6" />Edit Adventure
</button>
{#if adventure.type == 'visited'}
<button class="btn btn-neutral mb-2" on:click={changeType('planned')}
><FormatListBulletedSquare class="w-6 h-6" />Change to Plan</button
>
{/if}
{#if adventure.type == 'planned'}
<button class="btn btn-neutral mb-2" on:click={changeType('visited')}
><CheckBold class="w-6 h-6" />Mark Visited</button
<button
class="btn btn-neutral mb-2"
on:click={() => goto(`/adventures/${adventure.id}`)}
><Launch class="w-6 h-6" />Open Details</button
>
{/if}
{#if adventure.collection}
<button class="btn btn-neutral mb-2" on:click={removeFromCollection}
><LinkVariantRemove class="w-6 h-6" />Remove from Collection</button
<button class="btn btn-neutral mb-2" on:click={editAdventure}>
<FileDocumentEdit class="w-6 h-6" />Edit Adventure
</button>
{#if adventure.type == 'visited'}
<button class="btn btn-neutral mb-2" on:click={changeType('planned')}
><FormatListBulletedSquare class="w-6 h-6" />Change to Plan</button
>
{/if}
{#if adventure.type == 'planned'}
<button class="btn btn-neutral mb-2" on:click={changeType('visited')}
><CheckBold class="w-6 h-6" />Mark Visited</button
>
{/if}
{#if adventure.collection}
<button class="btn btn-neutral mb-2" on:click={removeFromCollection}
><LinkVariantRemove class="w-6 h-6" />Remove from Collection</button
>
{/if}
{#if !adventure.collection}
<button class="btn btn-neutral mb-2" on:click={() => (isCollectionModalOpen = true)}
><Plus class="w-6 h-6" />Add to Collection</button
>
{/if}
<button class="btn btn-warning" on:click={deleteAdventure}
><TrashCan class="w-6 h-6" />Delete</button
>
{/if}
{#if !adventure.collection}
<button class="btn btn-neutral mb-2" on:click={() => (isCollectionModalOpen = true)}
><Plus class="w-6 h-6" />Add to Collection</button
>
{/if}
<button class="btn btn-warning" on:click={deleteAdventure}
><TrashCan class="w-6 h-6" />Delete</button
>
</ul>
</div>
</ul>
</div>
{:else}
<button class="btn btn-neutral mb-2" on:click={() => goto(`/adventures/${adventure.id}`)}
><Launch class="w-6 h-6" /></button
>
{/if}
{/if}
{#if type == 'link'}
<button class="btn btn-primary" on:click={link}><Link class="w-6 h-6" /></button>
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/lib/components/AdventureLink.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { deserialize } from '$app/forms';
import type { Adventure } from '$lib/types';
import type { Adventure, User } from '$lib/types';
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
import type { ActionResult } from '@sveltejs/kit';
Expand All @@ -10,6 +10,8 @@
let adventures: Adventure[] = [];
export let user: User | null;
onMount(async () => {
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
if (modal) {
Expand Down Expand Up @@ -53,7 +55,7 @@
<h1 class="text-center font-bold text-4xl mb-6">My Adventures</h1>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard type="link" {adventure} on:link={add} />
<AdventureCard user={user ?? null} type="link" {adventure} on:link={add} />
{/each}
{#if adventures.length === 0}
<p class="text-center text-lg">
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/lib/components/EditAdventure.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@
latitude={adventureToEdit.latitude}
on:close={() => (isPointModalOpen = false)}
on:submit={setLongLat}
query={adventureToEdit.name}
/>
{/if}

{#if isImageFetcherOpen}
<ImageFetcher on:image={handleImageFetch} on:close={() => (isImageFetcherOpen = false)} />
<ImageFetcher
on:image={handleImageFetch}
name={adventureToEdit.name}
on:close={() => (isImageFetcherOpen = false)}
/>
{/if}

<dialog id="my_modal_1" class="modal">
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/lib/components/ImageFetcher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
let modal: HTMLDialogElement;
let url: string = '';
let query: string = '';
export let name: string | null = null;
let error = '';
Expand All @@ -30,13 +31,13 @@
}
async function fetchWikiImage() {
let res = await fetch(`/api/generate/img/?name=${query}`);
let res = await fetch(`/api/generate/img/?name=${name}`);
let data = await res.json();
if (data.source) {
let imageUrl = data.source;
let res = await fetch(imageUrl);
let blob = await res.blob();
let file = new File([blob], `${query}.jpg`, { type: 'image/jpeg' });
let file = new File([blob], `${name}.jpg`, { type: 'image/jpeg' });
close();
dispatch('image', { file });
} else {
Expand Down Expand Up @@ -75,7 +76,7 @@
<input
type="text"
class="input input-bordered w-full max-w-xs"
bind:value={query}
bind:value={name}
placeholder="Enter a Wikipedia Article Name"
/>
<button class="btn btn-primary" on:click={fetchWikiImage}>Submit</button>
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/lib/components/NewAdventure.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@
</script>

{#if isPointModalOpen}
<PointSelectionModal on:close={() => (isPointModalOpen = false)} on:submit={setLongLat} />
<PointSelectionModal
query={newAdventure.name}
on:close={() => (isPointModalOpen = false)}
on:submit={setLongLat}
/>
{/if}

{#if isImageFetcherOpen}
<ImageFetcher on:image={handleImageFetch} on:close={() => (isImageFetcherOpen = false)} />
<ImageFetcher
on:image={handleImageFetch}
name={newAdventure.name}
on:close={() => (isImageFetcherOpen = false)}
/>
{/if}

<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/lib/components/PointSelectionModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
let markers: Point[] = [];
let query: string = '';
export let query: string | null = null;
if (query) {
geocode();
}
export let longitude: number | null = null;
export let latitude: number | null = null;
Expand Down Expand Up @@ -43,8 +47,10 @@
let places: OpenStreetMapPlace[] = [];
async function geocode(e: Event) {
e.preventDefault();
async function geocode(e: Event | null) {
if (e) {
e.preventDefault();
}
if (!query) {
alert('Please enter a location');
return;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export type Adventure = {
latitude: number | null;
longitude: number | null;
is_public: boolean;
created_at?: string;
created_at?: string | null;
updated_at?: string | null;
};

export type Country = {
Expand Down
Loading

0 comments on commit 7c33b9e

Please sign in to comment.