mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
collections v3
This commit is contained in:
parent
e533dda328
commit
7e5e4edd4d
9 changed files with 266 additions and 158 deletions
|
@ -24,7 +24,7 @@ class CollectionSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = Collection
|
||||
# fields are all plus the adventures field
|
||||
fields = ['id', 'user_id', 'name', 'is_public', 'adventures']
|
||||
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures']
|
||||
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ class AdventureViewSet(viewsets.ModelViewSet):
|
|||
def apply_sorting(self, queryset):
|
||||
order_by = self.request.query_params.get('order_by', 'name')
|
||||
order_direction = self.request.query_params.get('order_direction', 'asc')
|
||||
include_collections = self.request.query_params.get('include_collections', 'false')
|
||||
include_collections = self.request.query_params.get('include_collections', 'true')
|
||||
|
||||
valid_order_by = ['name', 'type', 'date', 'rating']
|
||||
if order_by not in valid_order_by:
|
||||
|
|
|
@ -49,6 +49,7 @@
|
|||
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
||||
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
||||
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
|
||||
<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} />
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
|
||||
import Calendar from '~icons/mdi/calendar';
|
||||
import MapMarker from '~icons/mdi/map-marker';
|
||||
import Launch from '~icons/mdi/launch';
|
||||
import TrashCanOutline from '~icons/mdi/trash-can-outline';
|
||||
|
||||
import FileDocumentEdit from '~icons/mdi/file-document-edit';
|
||||
|
||||
import { goto } from '$app/navigation';
|
||||
import type { Collection } from '$lib/types';
|
||||
import { addToast } from '$lib/toasts';
|
||||
|
@ -13,6 +13,10 @@
|
|||
|
||||
// export let type: String;
|
||||
|
||||
function editAdventure() {
|
||||
dispatch('edit', collection);
|
||||
}
|
||||
|
||||
export let collection: Collection;
|
||||
|
||||
async function deleteCollection() {
|
||||
|
@ -42,6 +46,9 @@
|
|||
<button on:click={deleteCollection} class="btn btn-secondary"
|
||||
><TrashCanOutline class="w-5 h-5 mr-1" /></button
|
||||
>
|
||||
<button class="btn btn-primary" on:click={editAdventure}>
|
||||
<FileDocumentEdit class="w-6 h-6" />
|
||||
</button>
|
||||
<button class="btn btn-primary" on:click={() => goto(`/collections/${collection.id}`)}
|
||||
><Launch class="w-5 h-5 mr-1" /></button
|
||||
>
|
||||
|
|
162
frontend/src/lib/components/EditCollection.svelte
Normal file
162
frontend/src/lib/components/EditCollection.svelte
Normal file
|
@ -0,0 +1,162 @@
|
|||
<script lang="ts">
|
||||
export let collectionToEdit: Collection;
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import type { Adventure, Collection } from '$lib/types';
|
||||
const dispatch = createEventDispatcher();
|
||||
import { onMount } from 'svelte';
|
||||
import { addToast } from '$lib/toasts';
|
||||
let modal: HTMLDialogElement;
|
||||
|
||||
console.log(collectionToEdit.id);
|
||||
|
||||
let originalName = collectionToEdit.name;
|
||||
|
||||
let isPointModalOpen: boolean = false;
|
||||
|
||||
import MapMarker from '~icons/mdi/map-marker';
|
||||
import Calendar from '~icons/mdi/calendar';
|
||||
import Notebook from '~icons/mdi/notebook';
|
||||
import ClipboardList from '~icons/mdi/clipboard-list';
|
||||
import Image from '~icons/mdi/image';
|
||||
import Star from '~icons/mdi/star';
|
||||
import Attachment from '~icons/mdi/attachment';
|
||||
import PointSelectionModal from './PointSelectionModal.svelte';
|
||||
import Earth from '~icons/mdi/earth';
|
||||
|
||||
onMount(async () => {
|
||||
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
|
||||
if (modal) {
|
||||
modal.showModal();
|
||||
}
|
||||
});
|
||||
|
||||
function submit() {}
|
||||
|
||||
function close() {
|
||||
dispatch('close');
|
||||
}
|
||||
|
||||
function handleKeydown(event: KeyboardEvent) {
|
||||
if (event.key === 'Escape') {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit(event: Event) {
|
||||
event.preventDefault();
|
||||
const form = event.target as HTMLFormElement;
|
||||
const formData = new FormData(form);
|
||||
|
||||
const response = await fetch(form.action, {
|
||||
method: form.method,
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
const data = JSON.parse(result.data);
|
||||
console.log(data);
|
||||
|
||||
if (data) {
|
||||
addToast('success', 'Adventure edited successfully!');
|
||||
dispatch('saveEdit', collectionToEdit);
|
||||
close();
|
||||
} else {
|
||||
addToast('warning', 'Error editing adventure');
|
||||
console.log('Error editing adventure');
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<dialog id="my_modal_1" class="modal">
|
||||
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
||||
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
||||
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
|
||||
<h3 class="font-bold text-lg">Edit Collection: {originalName}</h3>
|
||||
<div
|
||||
class="modal-action items-center"
|
||||
style="display: flex; flex-direction: column; align-items: center; width: 100%;"
|
||||
>
|
||||
<form method="post" style="width: 100%;" on:submit={handleSubmit} action="/collections?/edit">
|
||||
<div class="mb-2">
|
||||
<input
|
||||
type="text"
|
||||
id="adventureId"
|
||||
name="adventureId"
|
||||
hidden
|
||||
readonly
|
||||
bind:value={collectionToEdit.id}
|
||||
class="input input-bordered w-full max-w-xs mt-1"
|
||||
/>
|
||||
<label for="name">Name</label><br />
|
||||
<input
|
||||
type="text"
|
||||
name="name"
|
||||
id="name"
|
||||
bind:value={collectionToEdit.name}
|
||||
class="input input-bordered w-full max-w-xs mt-1"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label for="date">Description <Notebook class="inline-block -mt-1 mb-1 w-6 h-6" /></label
|
||||
><br />
|
||||
<div class="flex">
|
||||
<input
|
||||
type="text"
|
||||
id="description"
|
||||
name="description"
|
||||
bind:value={collectionToEdit.description}
|
||||
class="input input-bordered w-full max-w-xs mt-1 mb-2"
|
||||
/>
|
||||
<!-- <button
|
||||
class="btn btn-neutral ml-2"
|
||||
type="button"
|
||||
on:click={generate}
|
||||
><iconify-icon icon="mdi:wikipedia" class="text-xl -mb-1"
|
||||
></iconify-icon>Generate Description</button
|
||||
> -->
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label for="is_public">Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label><br
|
||||
/>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="toggle toggle-primary"
|
||||
id="is_public"
|
||||
name="is_public"
|
||||
bind:checked={collectionToEdit.is_public}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{#if collectionToEdit.is_public}
|
||||
<div class="bg-neutral p-4 rounded-md shadow-sm">
|
||||
<p class=" font-semibold">Share this Adventure!</p>
|
||||
<div class="flex items-center justify-between">
|
||||
<p class="text-card-foreground font-mono">
|
||||
{window.location.origin}/collections/{collectionToEdit.id}
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
on:click={() => {
|
||||
navigator.clipboard.writeText(
|
||||
`${window.location.origin}/collections/${collectionToEdit.id}`
|
||||
);
|
||||
}}
|
||||
class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 h-10 px-4 py-2"
|
||||
>
|
||||
Copy Link
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<button type="submit" class="btn btn-primary mr-4 mt-4" on:click={submit}>Edit</button>
|
||||
<!-- if there is a button in form, it will close the modal -->
|
||||
<button class="btn mt-4" on:click={close}>Close</button>
|
||||
</form>
|
||||
<div class="flex items-center justify-center flex-wrap gap-4 mt-4"></div>
|
||||
</div>
|
||||
</div>
|
||||
</dialog>
|
|
@ -18,7 +18,7 @@ export const load = (async (event) => {
|
|||
let count = 0;
|
||||
let adventures: Adventure[] = [];
|
||||
let initialFetch = await fetch(
|
||||
`${serverEndpoint}/api/adventures/filtered?types=visited,planned`,
|
||||
`${serverEndpoint}/api/adventures/filtered?types=visited,planned&include_collections=false`,
|
||||
{
|
||||
headers: {
|
||||
Cookie: `${event.cookies.get('auth')}`
|
||||
|
@ -369,6 +369,7 @@ export const actions: Actions = {
|
|||
} else {
|
||||
include_collections = 'false';
|
||||
}
|
||||
|
||||
const order_direction = formData.get('order_direction') as string;
|
||||
const order_by = formData.get('order_by') as string;
|
||||
|
||||
|
|
|
@ -129,164 +129,96 @@ export const actions: Actions = {
|
|||
|
||||
return { id, user_id };
|
||||
},
|
||||
// edit: async (event) => {
|
||||
// const formData = await event.request.formData();
|
||||
edit: async (event) => {
|
||||
const formData = await event.request.formData();
|
||||
|
||||
// const adventureId = formData.get('adventureId') as string;
|
||||
// const type = formData.get('type') as string;
|
||||
// const name = formData.get('name') as string;
|
||||
// const location = formData.get('location') as string | null;
|
||||
// let date = (formData.get('date') as string | null) ?? null;
|
||||
// const description = formData.get('description') as string | null;
|
||||
// let activity_types = formData.get('activity_types')
|
||||
// ? (formData.get('activity_types') as string).split(',')
|
||||
// : null;
|
||||
// const rating = formData.get('rating') ? Number(formData.get('rating')) : null;
|
||||
// let link = formData.get('link') as string | null;
|
||||
// let latitude = formData.get('latitude') as string | null;
|
||||
// let longitude = formData.get('longitude') as string | null;
|
||||
// let is_public = formData.get('is_public') as string | null | boolean;
|
||||
const collectionId = formData.get('adventureId') as string;
|
||||
const name = formData.get('name') as string;
|
||||
const description = formData.get('description') as string | null;
|
||||
let is_public = formData.get('is_public') as string | null | boolean;
|
||||
|
||||
// if (is_public) {
|
||||
// is_public = true;
|
||||
// } else {
|
||||
// is_public = false;
|
||||
// }
|
||||
if (is_public) {
|
||||
is_public = true;
|
||||
} else {
|
||||
is_public = false;
|
||||
}
|
||||
|
||||
// // check if latitude and longitude are valid
|
||||
// if (latitude && longitude) {
|
||||
// if (isNaN(Number(latitude)) || isNaN(Number(longitude))) {
|
||||
// return {
|
||||
// status: 400,
|
||||
// body: { error: 'Invalid latitude or longitude' }
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
if (!name) {
|
||||
return {
|
||||
status: 400,
|
||||
body: { error: 'Missing name.' }
|
||||
};
|
||||
}
|
||||
|
||||
// // round latitude and longitude to 6 decimal places
|
||||
// if (latitude) {
|
||||
// latitude = Number(latitude).toFixed(6);
|
||||
// }
|
||||
// if (longitude) {
|
||||
// longitude = Number(longitude).toFixed(6);
|
||||
// }
|
||||
const formDataToSend = new FormData();
|
||||
formDataToSend.append('name', name);
|
||||
formDataToSend.append('description', description || '');
|
||||
formDataToSend.append('is_public', is_public.toString());
|
||||
|
||||
// const image = formData.get('image') as File;
|
||||
let auth = event.cookies.get('auth');
|
||||
|
||||
// // console.log(activity_types);
|
||||
if (!auth) {
|
||||
const refresh = event.cookies.get('refresh');
|
||||
if (!refresh) {
|
||||
return {
|
||||
status: 401,
|
||||
body: { message: 'Unauthorized' }
|
||||
};
|
||||
}
|
||||
let res = await tryRefreshToken(refresh);
|
||||
if (res) {
|
||||
auth = res;
|
||||
event.cookies.set('auth', auth, {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
expires: new Date(Date.now() + 60 * 60 * 1000), // 60 minutes
|
||||
path: '/'
|
||||
});
|
||||
} else {
|
||||
return {
|
||||
status: 401,
|
||||
body: { message: 'Unauthorized' }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// if (!type || !name) {
|
||||
// return {
|
||||
// status: 400,
|
||||
// body: { error: 'Missing required fields' }
|
||||
// };
|
||||
// }
|
||||
if (!auth) {
|
||||
return {
|
||||
status: 401,
|
||||
body: { message: 'Unauthorized' }
|
||||
};
|
||||
}
|
||||
|
||||
// if (date == null || date == '') {
|
||||
// date = null;
|
||||
// }
|
||||
const csrfToken = await fetchCSRFToken();
|
||||
|
||||
// if (link) {
|
||||
// link = checkLink(link);
|
||||
// }
|
||||
if (!csrfToken) {
|
||||
return {
|
||||
status: 500,
|
||||
body: { message: 'Failed to fetch CSRF token' }
|
||||
};
|
||||
}
|
||||
|
||||
// const formDataToSend = new FormData();
|
||||
// formDataToSend.append('type', type);
|
||||
// formDataToSend.append('name', name);
|
||||
// formDataToSend.append('location', location || '');
|
||||
// formDataToSend.append('date', date || '');
|
||||
// formDataToSend.append('description', description || '');
|
||||
// formDataToSend.append('latitude', latitude || '');
|
||||
// formDataToSend.append('longitude', longitude || '');
|
||||
// formDataToSend.append('is_public', is_public.toString());
|
||||
// if (activity_types) {
|
||||
// // Filter out empty and duplicate activity types, then trim each activity type
|
||||
// const cleanedActivityTypes = Array.from(
|
||||
// new Set(
|
||||
// activity_types
|
||||
// .map((activity_type) => activity_type.trim())
|
||||
// .filter((activity_type) => activity_type !== '' && activity_type !== ',')
|
||||
// )
|
||||
// );
|
||||
const res = await fetch(`${serverEndpoint}/api/collections/${collectionId}/`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'X-CSRFToken': csrfToken,
|
||||
Cookie: auth
|
||||
},
|
||||
body: formDataToSend
|
||||
});
|
||||
|
||||
// // Append each cleaned activity type to formDataToSend
|
||||
// cleanedActivityTypes.forEach((activity_type) => {
|
||||
// formDataToSend.append('activity_types', activity_type);
|
||||
// });
|
||||
// }
|
||||
// formDataToSend.append('rating', rating ? rating.toString() : '');
|
||||
// formDataToSend.append('link', link || '');
|
||||
if (!res.ok) {
|
||||
const errorBody = await res.json();
|
||||
return {
|
||||
status: res.status,
|
||||
body: { error: errorBody }
|
||||
};
|
||||
}
|
||||
|
||||
// if (image && image.size > 0) {
|
||||
// formDataToSend.append('image', image);
|
||||
// }
|
||||
|
||||
// let auth = event.cookies.get('auth');
|
||||
|
||||
// if (!auth) {
|
||||
// const refresh = event.cookies.get('refresh');
|
||||
// if (!refresh) {
|
||||
// return {
|
||||
// status: 401,
|
||||
// body: { message: 'Unauthorized' }
|
||||
// };
|
||||
// }
|
||||
// let res = await tryRefreshToken(refresh);
|
||||
// if (res) {
|
||||
// auth = res;
|
||||
// event.cookies.set('auth', auth, {
|
||||
// httpOnly: true,
|
||||
// sameSite: 'lax',
|
||||
// expires: new Date(Date.now() + 60 * 60 * 1000), // 60 minutes
|
||||
// path: '/'
|
||||
// });
|
||||
// } else {
|
||||
// return {
|
||||
// status: 401,
|
||||
// body: { message: 'Unauthorized' }
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!auth) {
|
||||
// return {
|
||||
// status: 401,
|
||||
// body: { message: 'Unauthorized' }
|
||||
// };
|
||||
// }
|
||||
|
||||
// const csrfToken = await fetchCSRFToken();
|
||||
|
||||
// if (!csrfToken) {
|
||||
// return {
|
||||
// status: 500,
|
||||
// body: { message: 'Failed to fetch CSRF token' }
|
||||
// };
|
||||
// }
|
||||
|
||||
// const res = await fetch(`${serverEndpoint}/api/adventures/${adventureId}/`, {
|
||||
// method: 'PATCH',
|
||||
// headers: {
|
||||
// 'X-CSRFToken': csrfToken,
|
||||
// Cookie: auth
|
||||
// },
|
||||
// body: formDataToSend
|
||||
// });
|
||||
|
||||
// if (!res.ok) {
|
||||
// const errorBody = await res.json();
|
||||
// return {
|
||||
// status: res.status,
|
||||
// body: { error: errorBody }
|
||||
// };
|
||||
// }
|
||||
|
||||
// let adventure = await res.json();
|
||||
|
||||
// let image_url = adventure.image;
|
||||
// let link_url = adventure.link;
|
||||
// return { image_url, link_url };
|
||||
// },
|
||||
return {
|
||||
status: 200
|
||||
};
|
||||
},
|
||||
get: async (event) => {
|
||||
if (!event.locals.user) {
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||
import CollectionCard from '$lib/components/CollectionCard.svelte';
|
||||
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
||||
import EditCollection from '$lib/components/EditCollection.svelte';
|
||||
import NewAdventure from '$lib/components/NewAdventure.svelte';
|
||||
import NewCollection from '$lib/components/NewCollection.svelte';
|
||||
import NotFound from '$lib/components/NotFound.svelte';
|
||||
|
@ -94,7 +95,7 @@
|
|||
isShowingCreateModal = false;
|
||||
}
|
||||
|
||||
function editAdventure(event: CustomEvent<Collection>) {
|
||||
function editCollection(event: CustomEvent<Collection>) {
|
||||
collectionToEdit = event.detail;
|
||||
isEditModalOpen = true;
|
||||
}
|
||||
|
@ -120,13 +121,13 @@
|
|||
<NewCollection on:create={createAdventure} on:close={() => (isShowingCreateModal = false)} />
|
||||
{/if}
|
||||
|
||||
<!-- {#if isEditModalOpen}
|
||||
<EditAdventure
|
||||
adventureToEdit={collectionToEdit}
|
||||
{#if isEditModalOpen}
|
||||
<EditCollection
|
||||
{collectionToEdit}
|
||||
on:close={() => (isEditModalOpen = false)}
|
||||
on:saveEdit={saveEdit}
|
||||
/>
|
||||
{/if} -->
|
||||
{/if}
|
||||
|
||||
<div class="fixed bottom-4 right-4 z-[999]">
|
||||
<div class="flex flex-row items-center justify-center gap-4">
|
||||
|
@ -178,7 +179,7 @@
|
|||
{#if currentView == 'cards'}
|
||||
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
|
||||
{#each collections as collection}
|
||||
<CollectionCard {collection} on:delete={deleteCollection} />
|
||||
<CollectionCard {collection} on:delete={deleteCollection} on:edit={editCollection} />
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
|
|
@ -27,6 +27,10 @@
|
|||
}
|
||||
});
|
||||
|
||||
function deleteAdventure(event: CustomEvent<number>) {
|
||||
adventures = adventures.filter((a) => a.id !== event.detail);
|
||||
}
|
||||
|
||||
async function addAdventure(event: CustomEvent<Adventure>) {
|
||||
console.log(event.detail);
|
||||
if (adventures.find((a) => a.id === event.detail.id)) {
|
||||
|
@ -123,7 +127,7 @@
|
|||
<h1 class="text-center font-semibold text-2xl mt-4 mb-2">Linked Adventures</h1>
|
||||
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
|
||||
{#each adventures as adventure}
|
||||
<AdventureCard type={adventure.type} {adventure} />
|
||||
<AdventureCard on:delete={deleteAdventure} type={adventure.type} {adventure} />
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue