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 #222

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
24 changes: 20 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 { 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,9 @@ export const ContentRendererClient = ({
type: 'video/mp4',
};
}

const [currentPath] = useState(usePathname());
const [markAsComplete , setMarkAsComplete] = useRecoilState(markAsCompleteAtom);

const toggleShowChapters = () => {
setShowChapters((prev) => !prev);
};
Expand All @@ -86,8 +90,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 +138,10 @@ export const ContentRendererClient = ({
disabled={loadingMarkAs}
onClick={handleMarkCompleted}
>
{contentCompleted ? 'Mark as Incomplete' : 'Mark as completed'}
{
markAsComplete.isValid && markAsComplete.path === currentPath ? <DisplayMarkAsComplete isCompleted={markAsComplete?.isCompleted} /> : <DisplayMarkAsComplete isCompleted={contentCompleted} />
}

</button>
</div>

Expand Down Expand Up @@ -193,3 +204,8 @@ export const ContentRendererClient = ({
</div>
);
};
export const DisplayMarkAsComplete = ({isCompleted} : {isCompleted : boolean | undefined}) => {
return (
isCompleted ? 'Mark as Incomplete' : 'Mark as Complete'
);
};
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>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach seems slightly ad-hoc-ish
You're using a single atom for all checkboxes
You probably need an atom family here

key: 'markAsCompleteAtom',
default: {isValid: false}
});
Loading