2025-10-29 09:57:29 +01:00
|
|
|
<script lang="ts">
|
2023-08-02 19:35:27 +01:00
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import Modal from '$lib/components/Modal/index.svelte';
|
2023-09-27 05:35:06 +01:00
|
|
|
import { snackbar } from '$lib/components/Snackbar/store';
|
2023-08-02 19:35:27 +01:00
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
2025-10-29 09:57:29 +01:00
|
|
|
import TextArea from '$lib/components/Form/TextArea.svelte';
|
2023-08-16 20:42:58 +00:00
|
|
|
import { copyCourseModalInitialState, copyCourseModal } from '$lib/components/Courses/store';
|
2025-10-29 09:57:29 +01:00
|
|
|
import { classroomio } from '$lib/utils/services/api';
|
|
|
|
|
import generateSlug from '$lib/utils/functions/generateSlug';
|
|
|
|
|
import { currentOrg } from '$lib/utils/store/org';
|
|
|
|
|
import { t } from '$lib/utils/functions/translations';
|
|
|
|
|
|
|
|
|
|
interface CloneCourseResponse {
|
|
|
|
|
success: boolean;
|
|
|
|
|
course: { id: string };
|
|
|
|
|
error?: string;
|
|
|
|
|
}
|
2021-12-30 22:30:30 +02:00
|
|
|
|
|
|
|
|
// clone, show spinner and redirect to new course
|
|
|
|
|
async function createCourse() {
|
2025-10-29 09:57:29 +01:00
|
|
|
// Prevent double submission (Option 1: Early Return Guard)
|
|
|
|
|
if ($copyCourseModal.isSaving) return;
|
|
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
console.log('Create course');
|
|
|
|
|
$copyCourseModal.isSaving = true;
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-29 09:57:29 +01:00
|
|
|
// Generate unique slug from the title
|
|
|
|
|
const slug = generateSlug($copyCourseModal.title);
|
2021-12-30 22:30:30 +02:00
|
|
|
|
2025-10-29 09:57:29 +01:00
|
|
|
const response = await classroomio.course.clone.$post({
|
|
|
|
|
json: {
|
|
|
|
|
id: $copyCourseModal.id,
|
|
|
|
|
title: $copyCourseModal.title,
|
|
|
|
|
description: $copyCourseModal.description,
|
|
|
|
|
slug,
|
|
|
|
|
organizationId: $currentOrg.id
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-12-30 22:30:30 +02:00
|
|
|
|
2025-10-29 09:57:29 +01:00
|
|
|
const result = (await response.json()) as CloneCourseResponse;
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(result.error || $t('courses.copy_course.error.failed_to_clone'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Option 3: Close modal and reset state before navigation
|
2021-12-30 22:30:30 +02:00
|
|
|
copyCourseModal.set(copyCourseModalInitialState);
|
2025-10-29 09:57:29 +01:00
|
|
|
|
|
|
|
|
// Navigate to the new course
|
|
|
|
|
goto(`/courses/${result.course.id}`);
|
|
|
|
|
$copyCourseModal.open = false;
|
2021-12-30 22:30:30 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
|
2023-09-27 05:35:06 +01:00
|
|
|
// IIFI of snackbar to Show notification error
|
2021-12-30 22:30:30 +02:00
|
|
|
(() => {
|
2023-09-27 05:35:06 +01:00
|
|
|
snackbar.error();
|
2021-12-30 22:30:30 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
// Stop loader
|
|
|
|
|
$copyCourseModal.isSaving = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<Modal
|
|
|
|
|
onClose={() => ($copyCourseModal.open = false)}
|
|
|
|
|
bind:open={$copyCourseModal.open}
|
|
|
|
|
width="w-96"
|
2025-10-29 09:57:29 +01:00
|
|
|
modalHeading={$t('courses.copy_course.title')}
|
2021-12-30 22:30:30 +02:00
|
|
|
>
|
|
|
|
|
<form on:submit|preventDefault={createCourse}>
|
|
|
|
|
<TextField
|
2025-10-29 09:57:29 +01:00
|
|
|
label={$t('courses.copy_course.course_name_label')}
|
2021-12-30 22:30:30 +02:00
|
|
|
bind:value={$copyCourseModal.title}
|
|
|
|
|
autoFocus={true}
|
2025-10-29 09:57:29 +01:00
|
|
|
placeholder={$t('courses.copy_course.course_name_placeholder')}
|
2021-12-30 22:30:30 +02:00
|
|
|
className="mb-4"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
autoComplete={false}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-10-29 09:57:29 +01:00
|
|
|
<TextArea
|
|
|
|
|
label={$t('courses.copy_course.course_description_label')}
|
|
|
|
|
bind:value={$copyCourseModal.description}
|
|
|
|
|
placeholder={$t('courses.copy_course.course_description_placeholder')}
|
|
|
|
|
className="mb-4"
|
|
|
|
|
rows={4}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div class="mt-5 flex flex-row-reverse items-center">
|
2021-12-30 22:30:30 +02:00
|
|
|
<PrimaryButton
|
2022-12-30 13:08:54 +01:00
|
|
|
className="px-6 py-3"
|
2025-10-29 09:57:29 +01:00
|
|
|
label={$t('courses.copy_course.create_button')}
|
2021-12-30 22:30:30 +02:00
|
|
|
type="submit"
|
|
|
|
|
isLoading={$copyCourseModal.isSaving}
|
2025-10-29 09:57:29 +01:00
|
|
|
isDisabled={$copyCourseModal.isSaving}
|
2021-12-30 22:30:30 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|