Skip to content

Commit db3819a

Browse files
committed
refactor(tabs): Clicking on edit button on a certain tab will open the corresponding edit tab
1 parent f019888 commit db3819a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/app/[locale]/projects/detail/[id]/components/ProjectDetailTab.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AdministrationDataType, HttpStatus, SummaryDataType, ActionType } from
1313
import { ApiUtils } from '@/utils'
1414
import { signOut, useSession } from 'next-auth/react'
1515
import { useTranslations } from 'next-intl'
16-
import { notFound, useRouter } from 'next/navigation'
16+
import { notFound, useRouter, useSearchParams } from 'next/navigation'
1717
import { useEffect, useState } from 'react'
1818
import { Button, Col, Dropdown, ListGroup, Row, Spinner, Tab } from 'react-bootstrap'
1919
import LinkProjects from '../../../components/LinkProjects'
@@ -36,6 +36,19 @@ export default function ViewProjects({ projectId }: { projectId: string }) {
3636
const [administrationData, setAdministrationData] = useState<AdministrationDataType | undefined>(undefined)
3737
const [show, setShow] = useState(false)
3838
const router = useRouter()
39+
const searchParams = useSearchParams()
40+
const DEFAULT_ACTIVE_TAB = 'summary'
41+
const [activeKey, setActiveKey] = useState(DEFAULT_ACTIVE_TAB)
42+
43+
useEffect(() => {
44+
const fragment = searchParams.get('tab') || DEFAULT_ACTIVE_TAB
45+
setActiveKey(fragment)
46+
}, [searchParams])
47+
48+
const handleSelect = (key: string | null) => {
49+
setActiveKey(key ?? DEFAULT_ACTIVE_TAB)
50+
router.push(`?tab=${key}`)
51+
}
3952

4053
useEffect(() => {
4154
if (status !== 'authenticated') return
@@ -71,19 +84,19 @@ export default function ViewProjects({ projectId }: { projectId: string }) {
7184
const handleEditProject = (projectId: string) => {
7285
if (session.user.email === summaryData['_embedded']['createdBy']['email']){
7386
MessageService.success(t('You are editing the original document'))
74-
router.push(`/projects/edit/${projectId}`)
87+
router.push(`/projects/edit/${projectId}?tab=${activeKey}`)
7588
}
7689
else {
7790
MessageService.success(t('You will create a moderation request if you update'))
78-
router.push(`/projects/edit/${projectId}`)
91+
router.push(`/projects/edit/${projectId}?tab=${activeKey}`)
7992
}
8093
}
8194

8295
return (
8396
<>
8497
<LinkProjects show={show} setShow={setShow} projectId={projectId} />
8598
<div className='container page-content'>
86-
<Tab.Container defaultActiveKey='summary' mountOnEnter={true} unmountOnExit={true}>
99+
<Tab.Container activeKey={activeKey} onSelect={(k) => handleSelect(k)} mountOnEnter={true} unmountOnExit={true}>
87100
<Row>
88101
<Col sm='auto' className='me-3'>
89102
<ListGroup>

src/app/[locale]/projects/edit/[id]/components/EditProject.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ApiUtils, CommonUtils } from '@/utils'
1818
import { ENABLE_FLEXIBLE_PROJECT_RELEASE_RELATIONSHIP } from '@/utils/env'
1919
import { signOut, getSession } from 'next-auth/react'
2020
import { useTranslations } from 'next-intl'
21-
import { notFound, useRouter } from 'next/navigation'
21+
import { notFound, useRouter, useSearchParams } from 'next/navigation'
2222
import { useCallback, useEffect, useState } from 'react'
2323
import { Button, Col, ListGroup, Row, Tab } from 'react-bootstrap'
2424
import Obligations from '../../../components/Obligations/Obligations'
@@ -38,6 +38,24 @@ function EditProject({ projectId }: { projectId: string }) {
3838
fullName: '',
3939
})
4040

41+
const searchParams = useSearchParams()
42+
const TABS = ['summary', 'administration', 'linkedProjectsAndReleases', 'attachments', 'obligations']
43+
const DEFAULT_ACTIVE_TAB = 'summary'
44+
const [activeKey, setActiveKey] = useState(DEFAULT_ACTIVE_TAB)
45+
46+
useEffect(() => {
47+
let tab = searchParams.get('tab')
48+
if (!tab || TABS.indexOf(tab) === -1) {
49+
tab = DEFAULT_ACTIVE_TAB
50+
}
51+
setActiveKey(tab as string)
52+
}, [searchParams])
53+
54+
const handleSelect = (key: string | null) => {
55+
setActiveKey(key ?? DEFAULT_ACTIVE_TAB)
56+
router.push(`?tab=${key}`)
57+
}
58+
4159
const [externalUrls, setExternalUrls] = useState<InputKeyValue[]>([])
4260

4361
const [externalIds, setExternalIds] = useState<InputKeyValue[]>([])
@@ -319,7 +337,7 @@ function EditProject({ projectId }: { projectId: string }) {
319337
}}
320338
>
321339
<div>
322-
<Tab.Container defaultActiveKey='summary' mountOnEnter={true} unmountOnExit={true}>
340+
<Tab.Container activeKey={activeKey} onSelect={(k) => handleSelect(k)} mountOnEnter={true} unmountOnExit={true}>
323341
<Row>
324342
<Col sm='auto' className='me-3'>
325343
<ListGroup>

0 commit comments

Comments
 (0)