Skip to content

Commit

Permalink
feat(OnlyOffice): Manage redirectLink for share file
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Oct 9, 2023
1 parent 7cd29f3 commit dda671b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/drive/web/modules/views/OnlyOffice/Editor.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ jest.mock('cozy-ui/transpiled/react/providers/Breakpoints', () => ({
jest.mock('cozy-client/dist/hooks/useQuery', () => jest.fn())

jest.mock('cozy-flags')
jest.mock(
'drive/web/modules/views/OnlyOffice/Toolbar/HomeLinker',
() =>
({ children }) =>
<div>{children}</div>
)
jest.mock('drive/web/modules/views/OnlyOffice/Toolbar', () => () => (
<div>Toolbar</div>
))

const client = createMockClient({})
client.plugins = {
Expand Down
26 changes: 23 additions & 3 deletions src/drive/web/modules/views/OnlyOffice/Toolbar/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react'
import React, { useContext, useState, useEffect } from 'react'
import { useSearchParams } from 'react-router-dom'

import {
Expand Down Expand Up @@ -26,6 +26,26 @@ const Toolbar = () => {
const { fileId, isPublic, isReadOnly, isEditorReady } =
useContext(OnlyOfficeContext)
const client = useClient()
const [fetchStatus, setFetchStatus] = useState('pending')
const [instance, setInstance] = useState(client.getStackClient().uri)

useEffect(() => {
const fetch = async () => {
try {
setFetchStatus('loading')
const permissions = await client
.collection('io.cozy.permissions')
.fetchOwnPermissions()
if (permissions.included.length > 0) {
setInstance(permissions.included[0].attributes.instance)
}
setFetchStatus('loaded')
} catch {
setFetchStatus('error')
}
}
fetch()
}, [client])

const [searchParams] = useSearchParams()
const params = new URLSearchParams(location.search)
Expand All @@ -47,7 +67,7 @@ const Toolbar = () => {
const { subdomain: subDomainType } = client.getInstanceOptions()

link = generateWebLink({
cozyUrl: client.getStackClient().uri,
cozyUrl: instance,
subDomainType,
slug,
pathname,
Expand All @@ -56,7 +76,7 @@ const Toolbar = () => {
})
}

const showBackButton = link !== undefined
const showBackButton = redirectLink !== null && fetchStatus === 'loaded'

const handleOnClick = () => {
window.location = link
Expand Down
41 changes: 37 additions & 4 deletions src/drive/web/modules/views/OnlyOffice/Toolbar/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ client.plugins = {
unsubscribe: () => {}
}
}
client.collection = () => ({
fetchOwnPermissions: () =>
Promise.resolve({
included: []
})
})

const setup = ({
isReadOnly = false,
Expand Down Expand Up @@ -232,13 +238,13 @@ describe('Toolbar', () => {
expect(backButton).toBeNull()
})

it('should redirect to previous folder with #/hash?searchParam ', () => {
it('should redirect to previous folder with #/hash?searchParam ', async () => {
makeNewLocation('#/onlyoffice/123?redirectLink=drive%23%2Ffolder%2F321')
useQuery.mockReturnValue(officeDocParam)

setup()

const button = screen.getByRole('button', {
const button = await screen.findByRole('button', {
name: 'Back'
})

Expand All @@ -247,19 +253,46 @@ describe('Toolbar', () => {
expect(window.location).toBe('http://cozy-drive.tools/#/folder/321')
})

it('should redirect to previous folder with ?searchParam#/hash', () => {
it('should redirect to previous folder with ?searchParam#/hash', async () => {
makeNewLocation('?redirectLink=drive%23%2Ffolder%2F321#/onlyoffice/123')
useQuery.mockReturnValue(officeDocParam)

setup()

const button = screen.getByRole('button', {
const button = await screen.findByRole('button', {
name: 'Back'
})

fireEvent.click(button)

expect(window.location).toBe('http://cozy-drive.tools/#/folder/321')
})

it('should redirect to previous folder from current cozy user', async () => {
client.collection = () => ({
fetchOwnPermissions: () =>
Promise.resolve({
included: [
{
attributes: {
instance: 'http://other.tools/'
}
}
]
})
})
makeNewLocation('?redirectLink=drive%23%2Ffolder%2F321#/onlyoffice/123')
useQuery.mockReturnValue(officeDocParam)

setup()

const button = await screen.findByRole('button', {
name: 'Back'
})

fireEvent.click(button)

expect(window.location).toBe('http://other-drive.tools/#/folder/321')
})
})
})
11 changes: 10 additions & 1 deletion src/drive/web/modules/views/OnlyOffice/useConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
makeName
} from 'drive/web/modules/views/OnlyOffice/helpers'
import { OnlyOfficeContext } from 'drive/web/modules/views/OnlyOffice'
import { useSearchParams } from 'react-router-dom'

const useConfig = () => {
const {
Expand All @@ -23,6 +24,7 @@ const useConfig = () => {
} = useContext(OnlyOfficeContext)
const client = useClient()
const instanceUri = client.getStackClient().uri
const [currentSearchParams] = useSearchParams()

const [config, setConfig] = useState()
const [status, setStatus] = useState('loading')
Expand Down Expand Up @@ -54,6 +56,12 @@ const useConfig = () => {
const searchParams = [['sharecode', sharecode]]
searchParams.push(['isOnlyOfficeDocShared', true])
searchParams.push(['onlyOfficeDocId', document_id])
if (currentSearchParams.get('redirectLink')) {
searchParams.push([
'redirectLink',
currentSearchParams.get('redirectLink')
])
}
if (public_name) searchParams.push(['username', public_name])

const link = generateWebLink({
Expand Down Expand Up @@ -112,7 +120,8 @@ const useConfig = () => {
username,
isFromSharing,
instanceUri,
isDesktop
isDesktop,
currentSearchParams
])

return { config, status }
Expand Down

0 comments on commit dda671b

Please sign in to comment.