Adapt react-client to use the real backend API (#1545)

Co-authored-by: Philip Molares <philip.molares@udo.edu>
Co-authored-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Erik Michelson 2022-04-15 23:03:15 +02:00 committed by GitHub
parent 3399ed2023
commit 26f90505ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 4726 additions and 2310 deletions

View file

@ -19,8 +19,8 @@ describe('build state from first heading update', () => {
})
it('generates a new state with the given first heading', () => {
const startState = { ...initialState, firstHeading: 'heading', noteTitle: 'noteTitle' }
const startState = { ...initialState, firstHeading: 'heading', title: 'noteTitle' }
const actual = buildStateFromFirstHeadingUpdate(startState, 'new first heading')
expect(actual).toStrictEqual({ ...initialState, firstHeading: 'new first heading', noteTitle: 'generated title' })
expect(actual).toStrictEqual({ ...initialState, firstHeading: 'new first heading', title: 'generated title' })
})
})

View file

@ -17,6 +17,6 @@ export const buildStateFromFirstHeadingUpdate = (state: NoteDetails, firstHeadin
return {
...state,
firstHeading: firstHeading,
noteTitle: generateNoteTitle(state.frontmatter, firstHeading)
title: generateNoteTitle(state.frontmatter, firstHeading)
}
}

View file

@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { initialState } from '../initial-state'
import type { NotePermissions } from '../../../api/notes/types'
import { buildStateFromServerPermissions } from './build-state-from-server-permissions'
import type { NoteDetails } from '../types/note-details'
describe('build state from server permissions', () => {
it('creates a new state with the given permissions', () => {
const state: NoteDetails = { ...initialState }
const permissions: NotePermissions = {
owner: 'test-owner',
sharedToUsers: [
{
username: 'test-user',
canEdit: true
}
],
sharedToGroups: [
{
groupName: 'test-group',
canEdit: false
}
]
}
expect(buildStateFromServerPermissions(state, permissions)).toStrictEqual({ ...state, permissions: permissions })
})
})

View file

@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { NoteDetails } from '../types/note-details'
import type { NotePermissions } from '../../../api/notes/types'
/**
* Builds the updated state from a given previous state and updated NotePermissions data.
* @param state The previous note details state.
* @param serverPermissions The updated NotePermissions data.
*/
export const buildStateFromServerPermissions = (
state: NoteDetails,
serverPermissions: NotePermissions
): NoteDetails => {
return {
...state,
permissions: serverPermissions
}
}

View file

@ -3,8 +3,6 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { NoteDto } from '../../../api/notes/types'
import { buildStateFromServerDto } from './build-state-from-set-note-data-from-server'
import * as buildStateFromUpdatedMarkdownContentModule from '../build-state-from-updated-markdown-content'
import { Mock } from 'ts-mockery'
@ -12,6 +10,7 @@ import type { NoteDetails } from '../types/note-details'
import { NoteTextDirection, NoteType } from '../types/note-details'
import { DateTime } from 'luxon'
import { initialSlideOptions } from '../initial-state'
import type { Note } from '../../../api/notes/types'
describe('build state from set note data from server', () => {
const buildStateFromUpdatedMarkdownContentMock = jest.spyOn(
@ -29,54 +28,42 @@ describe('build state from set note data from server', () => {
})
it('builds a new state from the given note dto', () => {
const noteDto: NoteDto = {
const noteDto: Note = {
content: 'line1\nline2',
metadata: {
primaryAddress: 'alias',
version: 5678,
alias: 'alias',
aliases: [
{
noteId: 'id',
primaryAlias: true,
name: 'alias'
}
],
id: 'id',
createTime: '2012-05-25T09:08:34.123',
createdAt: '2012-05-25T09:08:34.123',
description: 'description',
editedBy: ['editedBy'],
permissions: {
owner: {
username: 'username',
photo: 'photo',
email: 'email',
displayName: 'displayName'
},
owner: 'username',
sharedToGroups: [
{
canEdit: true,
group: {
displayName: 'groupdisplayname',
name: 'groupname',
special: true
}
groupName: 'groupName'
}
],
sharedToUsers: [
{
canEdit: true,
user: {
username: 'shareusername',
email: 'shareemail',
photo: 'sharephoto',
displayName: 'sharedisplayname'
}
username: 'shareusername'
}
]
},
viewCount: 987,
tags: ['tag'],
title: 'title',
updateTime: '2020-05-25T09:08:34.123',
updateUser: {
username: 'updateusername',
photo: 'updatephoto',
email: 'updateemail',
displayName: 'updatedisplayname'
}
updatedAt: '2020-05-25T09:08:34.123',
updateUsername: 'updateusername'
},
editedByAtPosition: [
{
@ -84,7 +71,7 @@ describe('build state from set note data from server', () => {
createdAt: 'createdAt',
startPos: 9,
updatedAt: 'updatedAt',
userName: 'userName'
username: 'userName'
}
]
}
@ -117,7 +104,7 @@ describe('build state from set note data from server', () => {
lineOffset: 0,
slideOptions: initialSlideOptions
},
noteTitle: '',
title: 'title',
selection: { from: 0 },
markdownContent: {
plain: 'line1\nline2',
@ -127,14 +114,35 @@ describe('build state from set note data from server', () => {
firstHeading: '',
rawFrontmatter: '',
id: 'id',
createTime: DateTime.fromISO('2012-05-25T09:08:34.123'),
lastChange: {
username: 'updateusername',
timestamp: DateTime.fromISO('2020-05-25T09:08:34.123')
},
createdAt: DateTime.fromISO('2012-05-25T09:08:34.123'),
updatedAt: DateTime.fromISO('2020-05-25T09:08:34.123'),
updateUsername: 'updateusername',
viewCount: 987,
alias: 'alias',
authorship: ['editedBy']
aliases: [
{
name: 'alias',
noteId: 'id',
primaryAlias: true
}
],
primaryAddress: 'alias',
version: 5678,
editedBy: ['editedBy'],
permissions: {
owner: 'username',
sharedToGroups: [
{
canEdit: true,
groupName: 'groupName'
}
],
sharedToUsers: [
{
canEdit: true,
username: 'shareusername'
}
]
}
}
const result = buildStateFromServerDto(noteDto)

View file

@ -4,19 +4,19 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { NoteDto } from '../../../api/notes/types'
import type { NoteDetails } from '../types/note-details'
import { buildStateFromUpdatedMarkdownContent } from '../build-state-from-updated-markdown-content'
import { initialState } from '../initial-state'
import { DateTime } from 'luxon'
import { calculateLineStartIndexes } from '../calculate-line-start-indexes'
import type { Note } from '../../../api/notes/types'
/**
* Builds a {@link NoteDetails} redux state from a DTO received as an API response.
* @param dto The first DTO received from the API containing the relevant information about the note.
* @return An updated {@link NoteDetails} redux state.
*/
export const buildStateFromServerDto = (dto: NoteDto): NoteDetails => {
export const buildStateFromServerDto = (dto: Note): NoteDetails => {
const newState = convertNoteDtoToNoteDetails(dto)
return buildStateFromUpdatedMarkdownContent(newState, newState.markdownContent.plain)
}
@ -27,24 +27,26 @@ export const buildStateFromServerDto = (dto: NoteDto): NoteDetails => {
* @param note The NoteDTO as defined in the backend.
* @return The NoteDetails object corresponding to the DTO.
*/
const convertNoteDtoToNoteDetails = (note: NoteDto): NoteDetails => {
const convertNoteDtoToNoteDetails = (note: Note): NoteDetails => {
const newLines = note.content.split('\n')
return {
...initialState,
updateUsername: note.metadata.updateUsername,
permissions: note.metadata.permissions,
editedBy: note.metadata.editedBy,
primaryAddress: note.metadata.primaryAddress,
id: note.metadata.id,
aliases: note.metadata.aliases,
title: note.metadata.title,
version: note.metadata.version,
viewCount: note.metadata.viewCount,
markdownContent: {
plain: note.content,
lines: newLines,
lineStartIndexes: calculateLineStartIndexes(newLines)
},
rawFrontmatter: '',
id: note.metadata.id,
createTime: DateTime.fromISO(note.metadata.createTime),
lastChange: {
username: note.metadata.updateUser.username,
timestamp: DateTime.fromISO(note.metadata.updateTime)
},
viewCount: note.metadata.viewCount,
alias: note.metadata.alias,
authorship: note.metadata.editedBy
createdAt: DateTime.fromISO(note.metadata.createdAt),
updatedAt: DateTime.fromISO(note.metadata.updatedAt)
}
}