Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/classrooms/_tests_/classrooms.api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { getClassrooms } from '@/classrooms/api/classrooms.api'

describe('classrooms.api', () => {
afterEach(() => {
vi.unstubAllGlobals()
})

it('normalizes owned classrooms that use id instead of classroomId', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => [
{
id: 7,
title: 'Detektivgjengen 6B',
description: '',
pupilCount: 24,
pendingPupilCount: 0,
teacherCount: 2,
taskCount: 5,
},
],
})
vi.stubGlobal('fetch', fetchMock)

await expect(getClassrooms('Bearer token')).resolves.toEqual([
{
classroomId: 7,
title: 'Detektivgjengen 6B',
description: '',
pupilCount: 24,
pendingPupilCount: 0,
teacherCount: 2,
taskCount: 5,
},
])
})
})
5 changes: 3 additions & 2 deletions src/classrooms/api/classrooms.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type CreateClassroomRequest = {
}

export interface OwnedClassroomResponse {
classroomId: number
classroomId?: number
id?: number
description: string
title: string
pupilCount: number
Expand Down Expand Up @@ -368,7 +369,7 @@ export async function getClassrooms(
)

return classrooms.map((c) => ({
classroomId: c.classroomId,
classroomId: c.classroomId ?? c.id ?? 0,
title: c.title,
description: c.description,
pupilCount: c.pupilCount,
Expand Down
29 changes: 27 additions & 2 deletions src/notebook/components/NotebookComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const currentStop = computed(
() =>
mapStops.value.find((stop) => stop.stopId === currentStopId.value) ?? null,
)
const currentStopUnlocked = computed(
() => isTeacherReview.value || currentStop.value?.status !== 'LOCKED',
)
const sectionTitle = computed(() => {
if (currentStop.value?.title) {
return currentStop.value.title
Expand Down Expand Up @@ -229,6 +232,14 @@ function selectSection(index: number) {
return
}
if (
!isTeacherReview.value &&
mapStops.value.find((stop) => stop.stopId === sectionStopIds.value[index])
?.status === 'LOCKED'
) {
return
}
currentSection.value = index
clampCurrentPage()
}
Expand Down Expand Up @@ -329,6 +340,12 @@ async function createEntry() {
return
}
if (!currentStopUnlocked.value) {
status.value = 'error'
errorMessage.value = t('map.lockedMessage')
return
}
if (currentClassroomId.value === null || defaultCreateStopId.value === null) {
status.value = 'error'
errorMessage.value = t('notebook.errors.missingStop')
Expand Down Expand Up @@ -601,6 +618,7 @@ onBeforeUnmount(() => {

<NotebookSidebarComponent
:current-section="currentSection"
:disable-locked-stops="!isTeacherReview"
:entries-by-stop-id="entriesByStopId"
:section-stop-ids="sectionStopIds"
:stops="mapStops"
Expand Down Expand Up @@ -656,7 +674,12 @@ onBeforeUnmount(() => {
{{ errorMessage }}
</p>

<div v-if="visiblePage.hasForm && !isTeacherReview" class="mt-4">
<div
v-if="
visiblePage.hasForm && !isTeacherReview && currentStopUnlocked
"
class="mt-4"
>
<form
ref="formElement"
data-testid="notebook-create-form"
Expand Down Expand Up @@ -748,7 +771,9 @@ onBeforeUnmount(() => {
</footer>

<form
v-if="!isTeacherReview && !visiblePage.hasForm"
v-if="
!isTeacherReview && !visiblePage.hasForm && currentStopUnlocked
"
ref="formElement"
class="pointer-events-none absolute -left-2500 top-0 w-[calc(100%-4rem)] rounded-xl border border-amber-200/80 bg-white/55 p-4 opacity-0"
aria-hidden="true"
Expand Down
16 changes: 16 additions & 0 deletions src/notebook/components/NotebookSidebarComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { t } = useI18n()
const props = defineProps<{
currentSection: number
disableLockedStops?: boolean
entriesByStopId: Record<number, number>
sectionStopIds: number[]
stops: MapStopOverview[]
Expand All @@ -22,6 +23,14 @@ function resolveStopTitle(stops: MapStopOverview[], stopId: number) {
)
}
function isStopLocked(stopId: number) {
if (!props.disableLockedStops) {
return false
}
return props.stops.find((stop) => stop.stopId === stopId)?.status === 'LOCKED'
}
const tabColorClasses = [
'border-rose-300 bg-rose-300/95 text-rose-950 shadow-rose-950/20 hover:bg-rose-200',
'border-orange-300 bg-orange-300/95 text-orange-950 shadow-orange-950/20 hover:bg-orange-200',
Expand All @@ -43,12 +52,17 @@ const tabOffsetClasses = [
]
function resolveTabClasses(index: number) {
const isLocked = isStopLocked(props.sectionStopIds[index] ?? -1)
return [
tabColorClasses[index % tabColorClasses.length],
tabOffsetClasses[index % tabOffsetClasses.length],
index === props.currentSection
? 'z-20 lg:translate-x-2 rotate-0 ring-2 ring-stone-900/20'
: 'z-10 lg:translate-x-0 rotate-[0.5deg] opacity-90 hover:opacity-100',
isLocked
? 'cursor-not-allowed saturate-50 opacity-60 hover:opacity-60'
: '',
]
}
Expand All @@ -74,7 +88,9 @@ function selectSection(index: number) {
v-for="(stopId, index) in props.sectionStopIds"
:key="stopId"
:aria-current="index === props.currentSection ? 'page' : undefined"
:aria-disabled="isStopLocked(stopId) ? 'true' : undefined"
:class="resolveTabClasses(index)"
:disabled="isStopLocked(stopId)"
class="relative flex min-w-44 items-center justify-between rounded-r-md border border-l-0 px-4 py-3 text-left text-sm font-semibold shadow-lg transition duration-200 before:absolute before:inset-y-1 before:left-0 before:w-px before:bg-white/50 lg:absolute lg:left-0 lg:h-16 lg:min-w-0 lg:w-56 lg:rounded-l-sm lg:rounded-r-none lg:pl-14 lg:pr-4 lg:before:inset-y-0 lg:before:left-0 lg:before:w-12 lg:before:bg-linear-to-r lg:before:from-stone-900/25 lg:before:to-white/25"
type="button"
@click="selectSection(index)"
Expand Down
52 changes: 52 additions & 0 deletions src/notebook/components/__tests__/NotebookComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,56 @@ describe('NotebookComponent pupil pagination', () => {
expect(wrapper.text()).toContain('Market note')
expect(wrapper.text()).not.toContain('Side 2 av 2')
})

it('disables locked stops in the sidebar for pupils', async () => {
vi.mocked(requestMapOverview).mockResolvedValue([
{
stopId: 9,
slug: 'intro',
title: 'Intro',
category: null,
displayOrder: 1,
isFinalBoss: false,
status: 'NOT_STARTED',
stars: 0,
medalAwarded: false,
},
{
stopId: 10,
slug: 'market',
title: 'Market',
category: null,
displayOrder: 2,
isFinalBoss: false,
status: 'LOCKED',
stars: 0,
medalAwarded: false,
},
])
vi.mocked(requestNotebookEntries).mockResolvedValue([
notebookEntry({ id: 21, stopId: 9, title: 'First note' }),
])

const wrapper = mount(NotebookComponent, {
global: {
plugins: [pinia, createAppI18n()],
},
})

await flushPromises()

const buttons = wrapper.findAll('button')
const lockedTab = buttons.find((button) => button.text().includes('Market'))
expect(lockedTab).toBeDefined()
expect(lockedTab?.attributes('disabled')).toBeDefined()

await lockedTab?.trigger('click')
await flushPromises()

expect(wrapper.find('h2').text()).toBe('Intro')
expect(visibleEntryText(wrapper)).toContain('First note')
expect(wrapper.find('[data-testid="notebook-create-form"]').exists()).toBe(
true,
)
})
})