Skip to content

Commit 51db8a1

Browse files
Merge pull request #564 from shapehq/enhancement/getDefaultSpecification
Introduce getDefaultSpecification for #558
2 parents 6af91b5 + 3a7a864 commit 51db8a1

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

src/features/projects/data/useProjectSelection.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import NProgress from "nprogress"
44
import { useRouter, usePathname } from "next/navigation"
55
import { useContext } from "react"
66
import { ProjectsContext } from "@/common"
7-
import { Project, ProjectNavigator, getProjectSelectionFromPath } from "../domain"
7+
import {
8+
Project,
9+
ProjectNavigator,
10+
getProjectSelectionFromPath,
11+
getDefaultSpecification
12+
13+
} from "../domain"
814

915
export default function useProjectSelection() {
1016
const router = useRouter()
@@ -29,7 +35,7 @@ export default function useProjectSelection() {
2935
},
3036
selectProject: (project: Project) => {
3137
const version = project.versions[0]
32-
const specification = version.specifications.find(spec => spec.isDefault) || version.specifications[0]
38+
const specification = getDefaultSpecification(version)
3339
NProgress.start()
3440
projectNavigator.navigate(
3541
project.owner,

src/features/projects/domain/ProjectNavigator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Project from "./Project"
2+
import { getDefaultSpecification } from "./Version"
23

34
interface IPathnameReader {
45
readonly pathname: string
@@ -36,7 +37,7 @@ export default class ProjectNavigator {
3637
if (candidateSpecification) {
3738
this.router.push(`/${project.owner}/${project.name}/${newVersion.id}/${candidateSpecification.id}`)
3839
} else {
39-
const defaultOrFirstSpecification = newVersion.specifications.find(spec => spec.isDefault) || newVersion.specifications[0]
40+
const defaultOrFirstSpecification = getDefaultSpecification(newVersion)
4041
this.router.push(`/${project.owner}/${project.name}/${newVersion.id}/${defaultOrFirstSpecification.id}`)
4142
}
4243
}

src/features/projects/domain/Version.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ export const VersionSchema = z.object({
1212
type Version = z.infer<typeof VersionSchema>
1313

1414
export default Version
15+
16+
export function getDefaultSpecification(version: Version) {
17+
return version.specifications.find((spec) => spec.isDefault) || version.specifications[0]
18+
}

src/features/projects/domain/getProjectSelectionFromPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Project from "./Project"
2-
import Version from "./Version"
2+
import Version, { getDefaultSpecification } from "./Version"
33
import OpenApiSpecification from "./OpenApiSpecification"
44

55
export default function getProjectSelectionFromPath({
@@ -61,7 +61,7 @@ export default function getProjectSelectionFromPath({
6161
if (specificationId && !didMoveSpecificationIdToVersionId) {
6262
specification = version.specifications.find(e => e.id == specificationId)
6363
} else if (version.specifications.length > 0) {
64-
specification = version.specifications.find(spec => spec.isDefault) || version.specifications[0]
64+
specification = getDefaultSpecification(version)
6565
}
6666
return { project, version, specification }
6767
}

src/features/projects/domain/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export { default as ProjectNavigator } from "./ProjectNavigator"
1616
export { default as ProjectRepository } from "./ProjectRepository"
1717
export { default as updateWindowTitle } from "./updateWindowTitle"
1818
export type { default as Version } from "./Version"
19+
export { getDefaultSpecification } from "./Version"

0 commit comments

Comments
 (0)