Skip to content

Commit 8459911

Browse files
committed
♻️ Extract method (#2020)
1 parent 7c69d3a commit 8459911

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/lib/components/WorkBooks/TitleTableBodyCell.svelte

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { TableBodyCell } from 'svelte-5-ui-lib';
33
44
import type { WorkbookList } from '$lib/types/workbook';
5+
6+
import { getUrlSlugFrom } from '$lib/utils/workbooks';
57
import PublicationStatusLabel from '$lib/components/WorkBooks/PublicationStatusLabel.svelte';
68
79
interface Props {
@@ -10,12 +12,6 @@
1012
}
1113
1214
let { paddingLeft = 'pl-2 xs:pl-4', workbook }: Props = $props();
13-
14-
function getUrlSlugFrom(workbook: WorkbookList): string {
15-
const slug = workbook.urlSlug;
16-
17-
return slug ? slug : workbook.id.toString();
18-
}
1915
</script>
2016

2117
<TableBodyCell class="w-2/5 {paddingLeft} pr-4">

src/lib/components/WorkBooks/WorkBookBaseTable.svelte

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
TableHeadCell,
1111
} from 'svelte-5-ui-lib';
1212
13-
import { WorkBookType, type WorkbooksList, type WorkbookList } from '$lib/types/workbook';
13+
import { WorkBookType, type WorkbooksList } from '$lib/types/workbook';
1414
import { TaskGrade, type TaskResults } from '$lib/types/task';
1515
import type { Roles } from '$lib/types/user';
1616
@@ -22,6 +22,7 @@
2222
import AcceptedCounter from '$lib/components/SubmissionStatus/AcceptedCounter.svelte';
2323
2424
import { canRead, canEdit, canDelete } from '$lib/utils/authorship';
25+
import { getUrlSlugFrom } from '$lib/utils/workbooks';
2526
2627
interface Props {
2728
workbookType: WorkBookType;
@@ -41,12 +42,6 @@
4142
function getTaskResult(workbookId: number): TaskResults {
4243
return taskResults?.get(workbookId) ?? [];
4344
}
44-
45-
function getUrlSlugFrom(workbook: WorkbookList): string {
46-
const slug = workbook.urlSlug;
47-
48-
return slug ? slug : workbook.id.toString();
49-
}
5045
</script>
5146

5247
<!-- FIXME: 問題集の種類別にコンポーネントを分ける -->

src/lib/utils/workbooks.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import { Roles } from '$lib/types/user';
22
import { isAdmin } from '$lib/utils/authorship';
3+
import { type WorkbookList } from '$lib/types/workbook';
34

45
// 管理者 + ユーザ向けに公開されている場合
56
export function canViewWorkBook(role: Roles, isPublished: boolean) {
67
return isAdmin(role) || isPublished;
78
}
9+
10+
/**
11+
* Gets the URL slug for a workbook, falling back to the workbook ID if no slug is available.
12+
*
13+
* @param workbook - The workbook object containing urlSlug and id properties
14+
* @returns The URL slug if available, otherwise the workbook ID as a string
15+
*/
16+
export function getUrlSlugFrom(workbook: WorkbookList): string {
17+
const slug = workbook.urlSlug;
18+
19+
return slug ? slug : workbook.id.toString();
20+
}

0 commit comments

Comments
 (0)