-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add privacy-first lending history and invite flow #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
niklhut
wants to merge
9
commits into
main
Choose a base branch
from
feat/lending
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
09cc6da
Refine lending navigation and loan history views
niklhut 0c11250
Merge branch 'main' into feat/lending
niklhut 0c67adf
fix: harden lending invite acceptance
niklhut d5262cd
fix: open loan cards and show claimed borrower names
niklhut e7172eb
fix: stop nested card keydowns and clear due dates
niklhut b287e2a
fix: refine loan card keyboard handling and schema default
niklhut 3b86e19
refactor: split loans view and remove loan note
niklhut 3443cae
fix: guard returned labels against missing dates
niklhut 6e6bdcf
refactor: share loan date helpers
niklhut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| <script setup lang="ts"> | ||
| interface Props { | ||
| open: boolean | ||
| userBookId: string | ||
| saving?: boolean | ||
| } | ||
|
|
||
| const props = defineProps<Props>() | ||
|
|
||
| const emit = defineEmits<{ | ||
| 'update:open': [open: boolean] | ||
| 'saved': [loan: OwnerLoan] | ||
| }>() | ||
|
|
||
| const toast = useToast() | ||
|
|
||
| const borrowerDisplayName = ref('') | ||
| const borrowerEmail = ref('') | ||
| const dueAt = ref('') | ||
| const isSaving = ref(false) | ||
| const inviteUrl = ref('') | ||
| const copiedAutomatically = ref(false) | ||
|
|
||
| const tomorrowDate = computed(() => { | ||
| const date = new Date() | ||
| date.setDate(date.getDate() + 1) | ||
| return [ | ||
| date.getFullYear(), | ||
| String(date.getMonth() + 1).padStart(2, '0'), | ||
| String(date.getDate()).padStart(2, '0') | ||
| ].join('-') | ||
| }) | ||
|
|
||
| const modalOpen = computed({ | ||
| get: () => props.open, | ||
| set: value => emit('update:open', value) | ||
| }) | ||
|
|
||
| const isSaved = computed(() => Boolean(inviteUrl.value)) | ||
| const canSave = computed(() => borrowerDisplayName.value.trim().length > 0 && !isSaving.value && !isSaved.value) | ||
|
|
||
| watch( | ||
| () => props.open, | ||
| (open) => { | ||
| if (!open) return | ||
| borrowerDisplayName.value = '' | ||
| borrowerEmail.value = '' | ||
| dueAt.value = '' | ||
| inviteUrl.value = '' | ||
| copiedAutomatically.value = false | ||
| } | ||
| ) | ||
|
|
||
| async function lendBook() { | ||
| if (!canSave.value) return | ||
|
|
||
| isSaving.value = true | ||
| try { | ||
| const result = await $fetch<{ loan: OwnerLoan, inviteUrl: string }>(`/api/books/${props.userBookId}/loans`, { | ||
| method: 'POST', | ||
| body: { | ||
| borrowerDisplayName: borrowerDisplayName.value, | ||
| borrowerEmail: borrowerEmail.value || null, | ||
| dueAt: dueAt.value || null | ||
| } | ||
| }) | ||
|
|
||
| inviteUrl.value = `${window.location.origin}${result.inviteUrl}` | ||
| copiedAutomatically.value = await copyInvite({ showToast: false }) | ||
| emit('saved', { ...result.loan, inviteUrl: result.inviteUrl }) | ||
| toast.add({ | ||
| title: 'Book marked as lent out', | ||
| description: copiedAutomatically.value | ||
| ? 'The share link was copied to your clipboard.' | ||
| : 'The share link is ready below.', | ||
| color: 'success' | ||
| }) | ||
| } catch (err: unknown) { | ||
| const message = (err as { data?: { message?: string } })?.data?.message | ||
| ?? (err instanceof Error ? err.message : 'Unable to lend this book') | ||
| toast.add({ | ||
| title: 'Could not lend book', | ||
| description: message, | ||
| color: 'error' | ||
| }) | ||
| } finally { | ||
| isSaving.value = false | ||
| } | ||
| } | ||
|
|
||
| async function copyInvite(options: { showToast?: boolean } = {}) { | ||
| if (!inviteUrl.value) return false | ||
|
|
||
| try { | ||
| await navigator.clipboard.writeText(inviteUrl.value) | ||
| if (options.showToast !== false) { | ||
| toast.add({ | ||
| title: 'Invite link copied', | ||
| color: 'success' | ||
| }) | ||
| } | ||
| return true | ||
| } catch { | ||
| if (options.showToast !== false) { | ||
| toast.add({ | ||
| title: 'Could not copy link', | ||
| description: 'The share link is ready below.', | ||
| color: 'warning' | ||
| }) | ||
| } | ||
| return false | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <UModal | ||
| v-model:open="modalOpen" | ||
| title="Record a book loan" | ||
| description="Save who has this book." | ||
| :ui="{ content: 'sm:max-w-xl', footer: 'justify-end gap-2' }" | ||
| > | ||
| <template #body> | ||
| <div class="space-y-4"> | ||
| <UAlert | ||
| v-if="!isSaved" | ||
| color="neutral" | ||
| variant="subtle" | ||
| icon="i-lucide-link" | ||
| title="A share link will be created" | ||
| description="Email is optional. After you save, Libroo copies a private link you can send however you like." | ||
| /> | ||
|
|
||
| <UFormField | ||
| label="Borrower name" | ||
| required | ||
| > | ||
| <UInput | ||
| v-model="borrowerDisplayName" | ||
| placeholder="Who has the book?" | ||
| icon="i-lucide-user" | ||
| class="w-full" | ||
| :disabled="isSaved" | ||
| /> | ||
| </UFormField> | ||
|
|
||
| <UFormField | ||
| label="Email (optional)" | ||
| help="Only for your private reference." | ||
| > | ||
| <UInput | ||
| v-model="borrowerEmail" | ||
| type="email" | ||
| placeholder="Optional" | ||
| icon="i-lucide-mail" | ||
| class="w-full" | ||
| :disabled="isSaved" | ||
| /> | ||
| </UFormField> | ||
|
|
||
| <UFormField label="Due date"> | ||
| <UInput | ||
| v-model="dueAt" | ||
| type="date" | ||
| icon="i-lucide-calendar" | ||
| :min="tomorrowDate" | ||
| class="w-full" | ||
| :disabled="isSaved" | ||
| /> | ||
| </UFormField> | ||
|
|
||
| <UAlert | ||
| v-if="inviteUrl" | ||
| color="neutral" | ||
| variant="subtle" | ||
| icon="i-lucide-link" | ||
| title="Share link copied" | ||
| :description="copiedAutomatically ? 'Send the copied link so they can add this book to borrowed books.' : 'They can use this link to add the book to borrowed books.'" | ||
| > | ||
| <template #actions> | ||
| <UButton | ||
| color="neutral" | ||
| variant="outline" | ||
| icon="i-lucide-copy" | ||
| @click="copyInvite()" | ||
| > | ||
| Copy link | ||
| </UButton> | ||
| </template> | ||
| </UAlert> | ||
| </div> | ||
| </template> | ||
|
|
||
| <template #footer> | ||
| <UButton | ||
| color="neutral" | ||
| variant="soft" | ||
| @click="modalOpen = false" | ||
| > | ||
| Close | ||
| </UButton> | ||
| <UButton | ||
| v-if="!isSaved" | ||
| icon="i-lucide-handshake" | ||
| :loading="isSaving" | ||
| :disabled="!canSave" | ||
| @click="lendBook" | ||
| > | ||
| Save loan | ||
| </UButton> | ||
| </template> | ||
| </UModal> | ||
| </template> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.