Skip to content

Commit

Permalink
implement guard clause variant
Browse files Browse the repository at this point in the history
  • Loading branch information
pmattmann committed Dec 25, 2024
1 parent 11bc27e commit 4b2dc35
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,59 +704,53 @@ async function requireChecklist(to, from, next) {
}

export function campFromRoute(route) {
if (route.params.campId) {
return apiStore.get().camps({ id: route.params.campId })
} else {
if (!route.params.campId) {
return undefined
}
return apiStore.get().camps({ id: route.params.campId })
}

export function invitationFromInviteKey(inviteKey) {
return apiStore.get().invitations({ action: 'find', id: inviteKey })
}

export function periodFromRoute(route) {
if (route.params.periodId) {
return apiStore.get().periods({ id: route.params.periodId })
} else {
if (!route.params.periodId) {
return undefined
}
return apiStore.get().periods({ id: route.params.periodId })
}

function scheduleEntryFromRoute(route) {
if (route.params.scheduleEntryId) {
return apiStore.get().scheduleEntries({ id: route.params.scheduleEntryId })
} else {
if (!route.params.scheduleEntryId) {
return undefined
}
return apiStore.get().scheduleEntries({ id: route.params.scheduleEntryId })
}

function categoryFromRoute(route) {
if (route.params.categoryId) {
return campFromRoute(route)
.categories()
.allItems.find((c) => c.id === route.params.categoryId)
} else {
if (!route.params.categoryId) {
return undefined
}
return campFromRoute(route)
.categories()
.allItems.find((c) => c.id === route.params.categoryId)
}

export function materialListFromRoute(route) {
if (route.params.materialId) {
return apiStore.get().materialLists({ id: route.params.materialId })
} else {
if (!route.params.materialId) {
return undefined
}
return apiStore.get().materialLists({ id: route.params.materialId })
}

export function checklistFromRoute(route) {
if (route.params.checklistId) {
return campFromRoute(route)
.checklists()
.allItems.find((c) => c.id === route.params.checklistId)
} else {
if (!route.params.checklistId) {
return undefined
}
return campFromRoute(route)
.checklists()
.allItems.find((c) => c.id === route.params.checklistId)
}

function getContentLayout(route) {
Expand All @@ -779,11 +773,10 @@ function getContentLayout(route) {
}

function dayFromScheduleEntryInRoute(route) {
if (route.params.scheduleEntryId) {
return apiStore.get().scheduleEntries({ id: route.params.scheduleEntryId }).day()
} else {
if (!route.params.scheduleEntryId) {
return undefined
}
return apiStore.get().scheduleEntries({ id: route.params.scheduleEntryId }).day()
}

/**
Expand Down Expand Up @@ -962,17 +955,16 @@ export function checklistOverviewRoute(camp, checklist, query = {}) {
}

async function firstFuturePeriod(route) {
if (route.params.campId) {
const periods = await apiStore.get().camps({ id: route.params.campId }).periods()
._meta.load
// Return the first period that hasn't ended, or if no such period exists, return the first period
return (
periods.items.find((period) => new Date(period.end) >= new Date()) ||
periods.items.find((_) => true)
)
} else {
if (!route.params.campId) {
return undefined
}
const periods = await apiStore.get().camps({ id: route.params.campId }).periods()._meta
.load
// Return the first period that hasn't ended, or if no such period exists, return the first period
return (
periods.items.find((period) => new Date(period.end) >= new Date()) ||
periods.items.find((_) => true)
)
}

async function redirectToPeriod(to, from, next, routeName) {
Expand Down

0 comments on commit 4b2dc35

Please sign in to comment.