Skip to content

Commit

Permalink
Merge pull request #253 from seanmorley15/development
Browse files Browse the repository at this point in the history
Transportation card improvements
  • Loading branch information
seanmorley15 authored Aug 19, 2024
2 parents 1d65ecb + a9f2ff6 commit 7728be7
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.8 on 2024-08-19 20:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0003_adventure_end_date'),
]

operations = [
migrations.AddField(
model_name='transportation',
name='end_date',
field=models.DateTimeField(blank=True, null=True),
),
]
10 changes: 10 additions & 0 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Adventure(models.Model):
updated_at = models.DateTimeField(auto_now=True)

def clean(self):
if self.date and self.end_date and self.date > self.end_date:
raise ValidationError('The start date must be before the end date. Start date: ' + str(self.date) + ' End date: ' + str(self.end_date))
if self.end_date and not self.date:
raise ValidationError('Adventures must have an end date. Adventure: ' + self.name)
if self.collection:
if self.collection.is_public and not self.is_public:
raise ValidationError('Adventures associated with a public collection must be public. Collection: ' + self.trip.name + ' Adventure: ' + self.name)
Expand Down Expand Up @@ -100,6 +104,7 @@ class Transportation(models.Model):
rating = models.FloatField(blank=True, null=True)
link = models.URLField(blank=True, null=True)
date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
flight_number = models.CharField(max_length=100, blank=True, null=True)
from_location = models.CharField(max_length=200, blank=True, null=True)
to_location = models.CharField(max_length=200, blank=True, null=True)
Expand All @@ -109,6 +114,11 @@ class Transportation(models.Model):
updated_at = models.DateTimeField(auto_now=True)

def clean(self):
print(self.date)
if self.date and self.end_date and self.date > self.end_date:
raise ValidationError('The start date must be before the end date. Start date: ' + str(self.date) + ' End date: ' + str(self.end_date))
if self.end_date and self.date is 'null':
raise ValidationError('Transportations must have an end date. Transportation: ' + self.name)
if self.collection:
if self.collection.is_public and not self.is_public:
raise ValidationError('Transportations associated with a public collection must be public. Collection: ' + self.collection.name + ' Transportation: ' + self.name)
Expand Down
2 changes: 1 addition & 1 deletion backend/server/adventures/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Meta:
fields = [
'id', 'user_id', 'type', 'name', 'description', 'rating',
'link', 'date', 'flight_number', 'from_location', 'to_location',
'is_public', 'collection', 'created_at', 'updated_at'
'is_public', 'collection', 'created_at', 'updated_at', 'end_date'
]
read_only_fields = ['id', 'created_at', 'updated_at', 'user_id']

Expand Down
33 changes: 20 additions & 13 deletions frontend/src/lib/components/AdventureModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@
}
}

if (adventure.end_date && !adventure.date) {
adventure.end_date = null;
adventure.date = null;
}

console.log(adventure);
if (adventure.id === '') {
let res = await fetch('/api/adventures', {
Expand Down Expand Up @@ -383,7 +388,7 @@
</div>

<div>
<label for="date">Date (or start date)</label><br />
<label for="date">{adventure.date ? 'Start Date' : 'Date'}</label><br />
<input
type="date"
id="date"
Expand All @@ -394,18 +399,20 @@
class="input input-bordered w-full"
/>
</div>
<div>
<label for="end_date">End Date</label><br />
<input
type="date"
id="end_date"
name="end_date"
min={startDate || ''}
max={endDate || ''}
bind:value={adventure.end_date}
class="input input-bordered w-full"
/>
</div>
{#if adventure.date}
<div>
<label for="end_date">End Date</label><br />
<input
type="date"
id="end_date"
name="end_date"
min={startDate || ''}
max={endDate || ''}
bind:value={adventure.end_date}
class="input input-bordered w-full"
/>
</div>
{/if}
<div>
<!-- link -->
<div>
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/lib/components/EditTransportation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
if (transportationToEdit.date) {
transportationToEdit.date = transportationToEdit.date.slice(0, 19);
}
if (transportationToEdit.end_date) {
transportationToEdit.end_date = transportationToEdit.end_date.slice(0, 19);
}

function close() {
dispatch('close');
Expand All @@ -55,6 +58,20 @@
event.preventDefault();
const form = event.target as HTMLFormElement;
const formData = new FormData(form);
// make sure end_date is not before start_date
if (
transportationToEdit.end_date &&
transportationToEdit.date &&
transportationToEdit.date > transportationToEdit.end_date
) {
addToast('error', 'End date cannot be before start date');
return;
}
// make sure end_date has a start_date
if (transportationToEdit.end_date && !transportationToEdit.date) {
transportationToEdit.end_date = null;
formData.set('end_date', '');
}

const response = await fetch(`/api/transportations/${transportationToEdit.id}/`, {
method: 'PUT',
Expand Down Expand Up @@ -147,7 +164,9 @@
</div>
<div class="mb-2">
<label for="start_date"
>Date & Time <Calendar class="inline-block mb-1 w-6 h-6" /></label
>{transportationToEdit.date ? 'Start' : ''}Date & Time <Calendar
class="inline-block mb-1 w-6 h-6"
/></label
><br />
<input
type="datetime-local"
Expand All @@ -159,6 +178,22 @@
class="input input-bordered w-full max-w-xs mt-1"
/>
</div>
{#if transportationToEdit.date}
<div class="mb-2">
<label for="end_date"
>End Date & Time <Calendar class="inline-block mb-1 w-6 h-6" /></label
><br />
<input
type="datetime-local"
id="end_date"
name="end_date"
min={fullStartDate || ''}
max={fullEndDate || ''}
bind:value={transportationToEdit.end_date}
class="input input-bordered w-full max-w-xs mt-1"
/>
</div>
{/if}
<div class="mb-2">
<label for="rating">Rating <Star class="inline-block mb-1 w-6 h-6" /></label><br />
<input
Expand Down
23 changes: 22 additions & 1 deletion frontend/src/lib/components/NewTransportation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
const form = event.target as HTMLFormElement;
const formData = new FormData(form);

// make sure there is a start date if there is an end date
if (formData.get('end_date') && !formData.get('date')) {
addToast('error', 'Please provide a start date');
return;
}

const response = await fetch(`/api/transportations/`, {
method: 'POST',
body: formData
Expand Down Expand Up @@ -168,7 +174,7 @@
</div>
<div class="mb-2">
<label for="start_date"
>Date & Time <Calendar class="inline-block mb-1 w-6 h-6" /></label
>Start Date & Time <Calendar class="inline-block mb-1 w-6 h-6" /></label
><br />
<input
type="datetime-local"
Expand All @@ -179,6 +185,21 @@
class="input input-bordered w-full max-w-xs mt-1"
/>
</div>

<div class="mb-2">
<label for="end_date"
>End Date & Time <Calendar class="inline-block mb-1 w-6 h-6" /></label
><br />
<input
type="datetime-local"
id="end_date"
name="end_date"
min={fullStartDate || ''}
max={fullEndDate || ''}
class="input input-bordered w-full max-w-xs mt-1"
/>
</div>

<div class="mb-2">
<label for="rating">Rating <Star class="inline-block mb-1 w-6 h-6" /></label><br />
<input
Expand Down
30 changes: 21 additions & 9 deletions frontend/src/lib/components/TransportationCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { addToast } from '$lib/toasts';

import Plus from '~icons/mdi/plus';
import ArrowDownThick from '~icons/mdi/arrow-down-thick';

const dispatch = createEventDispatcher();

Expand Down Expand Up @@ -39,19 +40,30 @@
</script>

<div
class="card min-w-max lg:w-96 md:w-80 sm:w-60 xs:w-40 bg-primary-content shadow-xl overflow-hidden text-base-content"
class="card w-full max-w-xs sm:max-w-sm md:max-w-md lg:max-w-md xl:max-w-md bg-primary-content shadow-xl text-base-content"
>
<div class="card-body">
<h2 class="card-title overflow-ellipsis">{transportation.name}</h2>
<div class="badge badge-secondary">{transportation.type}</div>
{#if transportation.from_location && transportation.to_location}
<p class="text-sm">
{transportation.from_location} to {transportation.to_location}
</p>
{/if}
{#if transportation.date}
{new Date(transportation.date).toLocaleString(undefined, { timeZone: 'UTC' })}
{/if}
<div>
{#if transportation.from_location}
<p class="break-words text-wrap">{transportation.from_location}</p>
{/if}
{#if transportation.to_location}
<ArrowDownThick class="w-6 h-6" />
<p class="break-words text-wrap">{transportation.to_location}</p>
{/if}
</div>
<div>
{#if transportation.date}
<p>{new Date(transportation.date).toLocaleString(undefined, { timeZone: 'UTC' })}</p>
{/if}
{#if transportation.end_date}
<ArrowDownThick class="w-6 h-6" />
<p>{new Date(transportation.end_date).toLocaleString(undefined, { timeZone: 'UTC' })}</p>
{/if}
</div>

{#if user?.pk === transportation.user_id}
<div class="card-actions justify-end">
<button on:click={deleteTransportation} class="btn btn-secondary"
Expand Down
53 changes: 26 additions & 27 deletions frontend/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,6 @@ export async function exportData() {
visitedRegions
};

async function convertImages() {
const promises = data.adventures.map(async (adventure, i) => {
if (adventure.image) {
const res = await fetch(adventure.image);
const blob = await res.blob();
const base64 = await blobToBase64(blob);
adventure.image = base64;
data.adventures[i].image = adventure.image;
}
});

await Promise.all(promises);
}

function blobToBase64(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => resolve(reader.result as string);
reader.onerror = (error) => reject(error);
});
}

await convertImages();

const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
return URL.createObjectURL(blob);
}
Expand Down Expand Up @@ -92,7 +67,19 @@ export function groupAdventuresByDate(
adventures.forEach((adventure) => {
if (adventure.date) {
const adventureDate = new Date(adventure.date).toISOString().split('T')[0];
if (groupedAdventures[adventureDate]) {
if (adventure.end_date) {
const endDate = new Date(adventure.end_date).toISOString().split('T')[0];
const currentDate = new Date(startDate);
for (let i = 0; i < numberOfDays; i++) {
currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0];
if (dateString >= adventureDate && dateString <= endDate) {
if (groupedAdventures[dateString]) {
groupedAdventures[dateString].push(adventure);
}
}
}
} else if (groupedAdventures[adventureDate]) {
groupedAdventures[adventureDate].push(adventure);
}
}
Expand All @@ -118,7 +105,19 @@ export function groupTransportationsByDate(
transportations.forEach((transportation) => {
if (transportation.date) {
const transportationDate = new Date(transportation.date).toISOString().split('T')[0];
if (groupedTransportations[transportationDate]) {
if (transportation.end_date) {
const endDate = new Date(transportation.end_date).toISOString().split('T')[0];
const currentDate = new Date(startDate);
for (let i = 0; i < numberOfDays; i++) {
currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0];
if (dateString >= transportationDate && dateString <= endDate) {
if (groupedTransportations[dateString]) {
groupedTransportations[dateString].push(transportation);
}
}
}
} else if (groupedTransportations[transportationDate]) {
groupedTransportations[transportationDate].push(transportation);
}
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type Transportation = {
rating: number | null;
link: string | null;
date: string | null; // ISO 8601 date string
end_date: string | null; // ISO 8601 date string
flight_number: string | null;
from_location: string | null;
to_location: string | null;
Expand Down

0 comments on commit 7728be7

Please sign in to comment.