Skip to content

Commit 98eaf95

Browse files
authored
Merge pull request instructlab#737 from vishnoianil/save-draft
feat:Auto save the in- progress skill and knowledge contribution
2 parents 1fbc5d9 + 9577593 commit 98eaf95

File tree

18 files changed

+682
-81
lines changed

18 files changed

+682
-81
lines changed

src/app/api/github/pr/knowledge/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async function createPullRequest(headers: HeadersInit, username: string, branchN
9494
method: 'POST',
9595
headers,
9696
body: JSON.stringify({
97-
title: `Knowledge: ${prTitle}`,
97+
title: prTitle,
9898
head: `${username}:${branchName}`,
9999
body: prBody,
100100
base: BASE_BRANCH

src/app/api/github/pr/skill/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async function createPullRequest(headers: HeadersInit, username: string, branchN
9292
method: 'POST',
9393
headers,
9494
body: JSON.stringify({
95-
title: `Skill: ${prTitle}`,
95+
title: prTitle,
9696
body: prBody,
9797
head: `${username}:${branchName}`,
9898
base: BASE_BRANCH

src/app/edit-submission/knowledge/github/[id]/page.tsx renamed to src/app/edit-submission/knowledge/github/[...slug]/page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { AppLayout } from '@/components/AppLayout';
44
import EditKnowledge from '@/components/Contribute/EditKnowledge/github/EditKnowledge';
55

66
type PageProps = {
7-
params: Promise<{ id: string }>;
7+
params: Promise<{ slug: string[] }>;
88
};
99

1010
const EditKnowledgePage = async ({ params }: PageProps) => {
1111
const resolvedParams = await params;
12-
const prNumber = parseInt(resolvedParams.id, 10);
1312

1413
return (
1514
<AppLayout className="contribute-page">
16-
<EditKnowledge prNumber={prNumber} />
15+
<EditKnowledge prNumber={resolvedParams.slug[0]} isDraft={resolvedParams.slug[1] != null ? true : false} />
1716
</AppLayout>
1817
);
1918
};

src/app/edit-submission/knowledge/native/[id]/page.tsx renamed to src/app/edit-submission/knowledge/native/[...slug]/page.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
// src/app/edit-submission/knowledge/[id]/page.tsx
2-
import * as React from 'react';
1+
// src/app/edit-submission/knowledge/[...slug]/page.tsx
32
import { AppLayout } from '@/components/AppLayout';
43
import EditKnowledgeNative from '@/components/Contribute/EditKnowledge/native/EditKnowledge';
4+
import * as React from 'react';
55

66
type PageProps = {
7-
params: Promise<{ id: string }>;
7+
params: Promise<{ slug: string[] }>;
88
};
99

1010
const EditKnowledgePage = async ({ params }: PageProps) => {
11-
const branchName = await params;
12-
11+
const contribution = await params;
1312
return (
1413
<AppLayout className="contribute-page">
15-
<EditKnowledgeNative branchName={branchName.id} />
14+
<EditKnowledgeNative branchName={contribution.slug[0]} isDraft={contribution.slug[1] != null ? true : false} />
1615
</AppLayout>
1716
);
1817
};

src/app/edit-submission/skill/github/[id]/page.tsx renamed to src/app/edit-submission/skill/github/[...slug]/page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { AppLayout } from '@/components/AppLayout';
44
import EditSkill from '@/components/Contribute/EditSkill/github/EditSkill';
55

66
type PageProps = {
7-
params: Promise<{ id: string }>;
7+
params: Promise<{ slug: string[] }>;
88
};
99

1010
const EditSkillPage = async ({ params }: PageProps) => {
1111
const resolvedParams = await params;
12-
const prNumber = parseInt(resolvedParams.id, 10);
1312

1413
return (
1514
<AppLayout className="contribute-page">
16-
<EditSkill prNumber={prNumber} />
15+
<EditSkill prNumber={resolvedParams.slug[0]} isDraft={resolvedParams.slug[1] != null ? true : false} />
1716
</AppLayout>
1817
);
1918
};

src/app/edit-submission/skill/native/[id]/page.tsx renamed to src/app/edit-submission/skill/native/[...slug]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { AppLayout } from '@/components/AppLayout';
44
import EditSkillNative from '@/components/Contribute/EditSkill/native/EditSkill';
55

66
type PageProps = {
7-
params: Promise<{ id: string }>;
7+
params: Promise<{ slug: string[] }>;
88
};
99

1010
const EditSkillPage = async ({ params }: PageProps) => {
11-
const branchName = await params;
11+
const contribution = await params;
1212

1313
return (
1414
<AppLayout className="contribute-page">
15-
<EditSkillNative branchName={branchName.id} />
15+
<EditSkillNative branchName={contribution.slug[0]} isDraft={contribution.slug[1] != null ? true : false} />
1616
</AppLayout>
1717
);
1818
};

src/components/Contribute/ContributionWizard/ContributionWizard.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from '@patternfly/react-core';
2222
import { getGitHubUserInfo } from '@/utils/github';
2323
import ContributionWizardFooter from '@/components/Contribute/ContributionWizard/ContributionWizardFooter';
24+
import { deleteDraftData } from '@/components/Contribute/Utils/autoSaveUtils';
2425

2526
import './contribute-page.scss';
2627

@@ -144,6 +145,8 @@ export const ContributionWizard: React.FunctionComponent<Props> = ({
144145
};
145146

146147
const handleCancel = () => {
148+
//If there is any draft saved, delete it.
149+
deleteDraftData(formData.branchName);
147150
router.push('/dashboard');
148151
};
149152

src/components/Contribute/EditKnowledge/github/EditKnowledge.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,32 @@ import { useRouter } from 'next/navigation';
1414
import KnowledgeFormGithub from '../../Knowledge/Github';
1515
import { ValidatedOptions, Modal, ModalVariant, ModalBody } from '@patternfly/react-core';
1616
import { fetchExistingKnowledgeDocuments } from '@/components/Contribute/Utils/documentUtils';
17+
import { fetchDraftKnowledgeChanges } from '@/components/Contribute/Utils/autoSaveUtils';
1718

1819
interface EditKnowledgeClientComponentProps {
19-
prNumber: number;
20+
prNumber: string;
21+
isDraft: boolean;
2022
}
2123

22-
const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }) => {
24+
const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber, isDraft }) => {
2325
const { data: session } = useSession();
2426
const [isLoading, setIsLoading] = useState<boolean>(true);
2527
const [loadingMsg, setLoadingMsg] = useState<string>('');
2628
const [knowledgeEditFormData, setKnowledgeEditFormData] = useState<KnowledgeEditFormData>();
2729
const router = useRouter();
2830

2931
useEffect(() => {
32+
if (isDraft) {
33+
fetchDraftKnowledgeChanges({ branchName: prNumber, setIsLoading, setLoadingMsg, setKnowledgeEditFormData });
34+
return;
35+
}
36+
3037
setLoadingMsg('Fetching knowledge data from PR : ' + prNumber);
3138
const fetchPRData = async () => {
3239
if (session?.accessToken) {
3340
try {
34-
const prData = await fetchPullRequest(session.accessToken, prNumber);
41+
const prNum = parseInt(prNumber, 10);
42+
const prData = await fetchPullRequest(session.accessToken, prNum);
3543

3644
// Create KnowledgeFormData from existing form.
3745
const knowledgeExistingFormData: KnowledgeFormData = {
@@ -55,16 +63,17 @@ const EditKnowledge: React.FC<EditKnowledgeClientComponentProps> = ({ prNumber }
5563

5664
const knowledgeEditFormData: KnowledgeEditFormData = {
5765
isEditForm: true,
66+
isSubmitted: true,
5867
version: KnowledgeSchemaVersion,
5968
formData: knowledgeExistingFormData,
60-
pullRequestNumber: prNumber,
69+
pullRequestNumber: prNum,
6170
oldFilesPath: ''
6271
};
6372

6473
knowledgeExistingFormData.submissionSummary = prData.title;
6574
knowledgeExistingFormData.branchName = prData.head.ref; // Store the branch name from the pull request
6675

67-
const prFiles: PullRequestFile[] = await fetchPullRequestFiles(session.accessToken, prNumber);
76+
const prFiles: PullRequestFile[] = await fetchPullRequestFiles(session.accessToken, prNum);
6877

6978
const foundYamlFile = prFiles.find((file: PullRequestFile) => file.filename.endsWith('.yaml'));
7079
if (!foundYamlFile) {

src/components/Contribute/EditKnowledge/native/EditKnowledge.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useRouter } from 'next/navigation';
1212
import { ValidatedOptions, Modal, ModalVariant, ModalBody } from '@patternfly/react-core';
1313
import KnowledgeFormNative from '../../Knowledge/Native';
1414
import { fetchExistingKnowledgeDocuments } from '@/components/Contribute/Utils/documentUtils';
15+
import { fetchDraftKnowledgeChanges } from '@/components/Contribute/Utils/autoSaveUtils';
1516

1617
interface ChangeData {
1718
file: string;
@@ -22,16 +23,22 @@ interface ChangeData {
2223

2324
interface EditKnowledgeClientComponentProps {
2425
branchName: string;
26+
isDraft: boolean;
2527
}
2628

27-
const EditKnowledgeNative: React.FC<EditKnowledgeClientComponentProps> = ({ branchName }) => {
29+
const EditKnowledgeNative: React.FC<EditKnowledgeClientComponentProps> = ({ branchName, isDraft }) => {
2830
const { data: session } = useSession();
2931
const [isLoading, setIsLoading] = useState<boolean>(true);
3032
const [loadingMsg, setLoadingMsg] = useState<string>('');
3133
const [knowledgeEditFormData, setKnowledgeEditFormData] = useState<KnowledgeEditFormData>();
3234
const router = useRouter();
3335

3436
useEffect(() => {
37+
if (isDraft) {
38+
fetchDraftKnowledgeChanges({ branchName, setIsLoading, setLoadingMsg, setKnowledgeEditFormData });
39+
return;
40+
}
41+
3542
setLoadingMsg('Fetching knowledge data from branch : ' + branchName);
3643
const fetchBranchChanges = async () => {
3744
try {
@@ -65,6 +72,7 @@ const EditKnowledgeNative: React.FC<EditKnowledgeClientComponentProps> = ({ bran
6572

6673
const knowledgeEditFormData: KnowledgeEditFormData = {
6774
isEditForm: true,
75+
isSubmitted: true,
6876
version: KnowledgeSchemaVersion,
6977
formData: knowledgeExistingFormData,
7078
pullRequestNumber: 0,

src/components/Contribute/EditSkill/github/EditSkill.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@ import axios from 'axios';
1212
import { SkillYamlData, AttributionData, PullRequestFile, SkillFormData, SkillEditFormData, SkillSeedExample } from '@/types';
1313
import { SkillSchemaVersion } from '@/types/const';
1414
import { ValidatedOptions, Modal, ModalVariant, ModalBody } from '@patternfly/react-core';
15+
import { fetchDraftSkillChanges } from '@/components/Contribute/Utils/autoSaveUtils';
1516

1617
interface EditSkillClientComponentProps {
17-
prNumber: number;
18+
prNumber: string;
19+
isDraft: boolean;
1820
}
1921

20-
const EditSkill: React.FC<EditSkillClientComponentProps> = ({ prNumber }) => {
22+
const EditSkill: React.FC<EditSkillClientComponentProps> = ({ prNumber, isDraft }) => {
2123
const { data: session } = useSession();
2224
const [isLoading, setIsLoading] = useState<boolean>(true);
2325
const [loadingMsg, setLoadingMsg] = useState<string>('');
2426
const [skillEditFormData, setSkillEditFormData] = useState<SkillEditFormData>();
2527
const router = useRouter();
2628

2729
useEffect(() => {
30+
if (isDraft) {
31+
fetchDraftSkillChanges({ branchName: prNumber, setIsLoading, setLoadingMsg, setSkillEditFormData });
32+
return;
33+
}
34+
2835
const fetchPRData = async () => {
2936
setLoadingMsg('Fetching skill data from PR: ' + prNumber);
3037
if (session?.accessToken) {
3138
try {
32-
const prData = await fetchPullRequest(session.accessToken, prNumber);
39+
const prNum = parseInt(prNumber, 10);
40+
const prData = await fetchPullRequest(session.accessToken, prNum);
3341

3442
const skillExistingFormData: SkillFormData = {
3543
branchName: '',
@@ -45,15 +53,16 @@ const EditSkill: React.FC<EditSkillClientComponentProps> = ({ prNumber }) => {
4553

4654
const skillEditFormData: SkillEditFormData = {
4755
isEditForm: true,
56+
isSubmitted: true,
4857
version: SkillSchemaVersion,
4958
formData: skillExistingFormData,
50-
pullRequestNumber: prNumber,
59+
pullRequestNumber: prNum,
5160
oldFilesPath: ''
5261
};
5362

5463
skillExistingFormData.branchName = prData.head.ref; // Store the branch name from the pull request
5564

56-
const prFiles: PullRequestFile[] = await fetchPullRequestFiles(session.accessToken, prNumber);
65+
const prFiles: PullRequestFile[] = await fetchPullRequestFiles(session.accessToken, prNum);
5766

5867
const foundYamlFile = prFiles.find((file: PullRequestFile) => file.filename.endsWith('.yaml'));
5968
if (!foundYamlFile) {

0 commit comments

Comments
 (0)