Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : #216 Mark as complete button UI sync #221

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import { useRouter } from 'next/navigation';
import { usePathname, useRouter } from 'next/navigation';
import {
Accordion,
AccordionContent,
Expand All @@ -13,6 +13,7 @@ import { useRecoilState } from 'recoil';
import { sidebarOpen as sidebarOpenAtom } from '@/store/atoms/sidebar';
import { useEffect, useState } from 'react';
import { handleMarkAsCompleted } from '@/lib/utils';
import { markAsCompleteAtom } from '@/store/atoms/markAsComplete';

export function Sidebar({
courseId,
Expand Down Expand Up @@ -61,6 +62,12 @@ export function Sidebar({
router.push(path);
}
};
const getContentPath = (contentId: any) => {
const pathArray = findPathToContent(fullCourseContent, contentId);
if (pathArray) {
return `/courses/${courseId}/${pathArray.join('/')}`;
}
};

const renderContent = (contents: any) => {
return contents.map((content: any) => {
Expand Down Expand Up @@ -101,7 +108,7 @@ export function Sidebar({
</div>
{content.type === 'video' ? (
<div className="flex flex-col justify-center ml-2">
<Check content={content} />
<Check content={content} pathCheck = {getContentPath}/>
</div>
) : null}
</div>
Expand Down Expand Up @@ -232,17 +239,25 @@ function NotionIcon() {
}

// Todo: Fix types here
function Check({ content }: { content: any }) {
function Check({ content , pathCheck } : { content: any , pathCheck : any}) {
const [completed, setCompleted] = useState(
content?.videoProgress?.markAsCompleted || false,
);
);
const [currentPath] = useState(usePathname());
const [markAsComplete , setMarkAsComplete] = useRecoilState(markAsCompleteAtom);

return (
<>
<input
defaultChecked={completed}
onClick={async (e) => {
checked={markAsComplete.isValid && markAsComplete?.path === pathCheck(content.id) ? markAsComplete.isCompleted : completed}
onChange={async (e) => {
setCompleted(!completed);
handleMarkAsCompleted(!completed, content.id);
setMarkAsComplete({
isValid: true,
path: currentPath,
isCompleted: !completed
});
e.stopPropagation();
}}
type="checkbox"
Expand Down
18 changes: 14 additions & 4 deletions src/components/admin/ContentRendererClient.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use client';
import { useSearchParams, useRouter } from 'next/navigation';
import { useSearchParams, useRouter, usePathname } from 'next/navigation';
// import { QualitySelector } from '../QualitySelector';
import { VideoPlayerSegment } from '@/components/VideoPlayerSegment';
import VideoContentChapters from '../VideoContentChapters';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { handleMarkAsCompleted } from '@/lib/utils';
import { useRecoilState } from 'recoil';
import { markAsCompleteAtom } from '@/store/atoms/markAsComplete';

export const ContentRendererClient = ({
metadata,
Expand Down Expand Up @@ -70,7 +72,10 @@ export const ContentRendererClient = ({
type: 'video/mp4',
};
}
const [currentPath] = useState(usePathname());
const [markAsComplete , setMarkAsComplete] = useRecoilState(markAsCompleteAtom);


const toggleShowChapters = () => {
setShowChapters((prev) => !prev);
};
Expand All @@ -86,8 +91,12 @@ export const ContentRendererClient = ({
setContentCompleted((prev) => !prev);
}
setLoadingMarkAs(false);
setMarkAsComplete({
isValid : true,
path : currentPath,
isCompleted : contentCompleted
})
};

return (
<div className="flex gap-2 items-start flex-col lg:flex-row">
<div className="flex-1 w-full">
Expand Down Expand Up @@ -130,7 +139,8 @@ export const ContentRendererClient = ({
disabled={loadingMarkAs}
onClick={handleMarkCompleted}
>
{contentCompleted ? 'Mark as Incomplete' : 'Mark as completed'}
{markAsComplete.isValid && markAsComplete?.path === currentPath ? (markAsComplete?.isCompleted ? "Mark as Incomplete" : "Mark as completed") : (contentCompleted ? "Mark as Incomplete" : "Mark as completed")}
{/* {contentCompleted ? 'Mark as Incomplete' : 'Mark as completed'} */}
</button>
</div>

Expand Down
12 changes: 12 additions & 0 deletions src/store/atoms/markAsComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {atom} from 'recoil';

interface markAsCompleteParams {
isValid : boolean
path? : string,
isCompleted? : boolean
}

export const markAsCompleteAtom = atom<markAsCompleteParams>({
key: 'markAsCompleteAtom',
default: {isValid: false}
});
Loading