Skip to content

Commit aeebbe7

Browse files
committed
fix open access
1 parent 424dc83 commit aeebbe7

3 files changed

Lines changed: 63 additions & 75 deletions

File tree

‎frontend/main/src/components/user/MemberNotValidShow.tsx‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎frontend/main/src/components/user/MemberValidShow.tsx‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@ import { UserInfoExtended } from '@/models/user.model';
22

33
import useIsMember from '@/hooks/useIsMember';
44
import { useAuth } from 'reactfire';
5+
import { Post } from '@/models/post.model';
6+
import { AccessMode } from '@/models/access.model';
57

68
export default function MemberValidShow({
79
user,
810
children,
11+
sectionLesson,
12+
notValidComponent,
913
}: {
1014
user: UserInfoExtended;
1115
children: JSX.Element;
16+
sectionLesson?: Post;
17+
notValidComponent: JSX.Element;
1218
}): JSX.Element {
1319
const auth = useAuth();
14-
1520
const { member, team } = useIsMember(user);
1621

17-
return <>{(member || team) && <section>{children}</section>}</>;
22+
if (!sectionLesson || sectionLesson?.access_mode === AccessMode.free) {
23+
return <section>{children}</section>;
24+
} else if (
25+
sectionLesson?.access_mode === AccessMode.open &&
26+
auth?.currentUser?.uid
27+
) {
28+
return <section>{children}</section>;
29+
} else if (
30+
sectionLesson?.access_mode === AccessMode.closed &&
31+
(member || team)
32+
) {
33+
return <section>{children}</section>;
34+
}
35+
return <>{notValidComponent}</>;
1836
}

‎frontend/main/src/layout/PostLayout.tsx‎

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ const MemberValidShow = dynamic<any>(
3131
ssr: false,
3232
}
3333
);
34-
const MemberNotValidShow = dynamic<any>(
35-
() => import('@/components/user/MemberNotValidShow'),
36-
{
37-
ssr: false,
38-
}
39-
);
4034

4135
import dynamic from 'next/dynamic';
4236
import { isActiveLesson } from '@/utils/basics/links';
@@ -64,27 +58,28 @@ export default function PostLayout({
6458
const [videoProgress, setVideoProgress] = useState(0);
6559
const [playing, setPlaying] = useState(false);
6660
const [currentLesson, setCurrentLesson] = useState<string | null>(null);
61+
const [sectionLesson, setSectionLesson] = useState<Post | undefined>(
62+
undefined
63+
);
6764
useEffect(() => {
6865
setHref(location.href);
6966
setPlaying(false);
7067
setVideoProgress(0);
7168
setCurrentLesson(post.id);
72-
}, [post]);
7369

74-
function isLockedLesson() {
7570
// Only lockdown lessons
7671
if (post._type != PostType.lesson) {
77-
return false;
72+
setSectionLesson(undefined);
7873
}
7974
//If lesson, without assigned course (or course is not yet published)
8075
if (post._type == PostType.lesson && !course) {
81-
return true;
76+
setSectionLesson(undefined);
8277
}
8378
const lessons: Post[] = [];
8479
course?.sections?.map((s) => s?.lessons?.map((l) => lessons.push(l)));
85-
const currentLesson = lessons.find((l) => l.slug === post.slug);
86-
return currentLesson?.access_mode != AccessMode.free;
87-
}
80+
setSectionLesson(lessons.find((l) => l.slug === post.slug));
81+
}, [post, course]);
82+
8883
const pluralType = pluralize(post);
8984

9085
function backButton() {
@@ -308,7 +303,7 @@ export default function PostLayout({
308303
</>
309304
);
310305
};
311-
306+
console.log(sectionLesson);
312307
return (
313308
<>
314309
{/* DIV TO AVOID GRID GAP */}
@@ -317,32 +312,21 @@ export default function PostLayout({
317312
{post._type !== PostType.page && (
318313
<section className="">
319314
{/* MEDIA */}
320-
{!isLockedLesson() ? (
321-
<PostMedia
322-
post={post}
323-
playing={playing}
324-
setPlaying={setPlaying}
325-
videoProgress={videoProgress}
326-
setVideoProgress={setVideoProgress}
327-
/>
328-
) : user && user?.uid ? (
329-
<>
330-
<MemberValidShow user={user}>
331-
<PostMedia
332-
post={post}
333-
playing={playing}
334-
setPlaying={setPlaying}
335-
videoProgress={videoProgress}
336-
setVideoProgress={setVideoProgress}
337-
/>
338-
</MemberValidShow>
339-
<MemberNotValidShow user={user}>
340-
<PostMediaLocked />
341-
</MemberNotValidShow>
342-
</>
343-
) : (
344-
<PostMediaLocked />
345-
)}
315+
<>
316+
<MemberValidShow
317+
user={user}
318+
sectionLesson={sectionLesson}
319+
notValidComponent={<PostMediaLocked />}
320+
>
321+
<PostMedia
322+
post={post}
323+
playing={playing}
324+
setPlaying={setPlaying}
325+
videoProgress={videoProgress}
326+
setVideoProgress={setVideoProgress}
327+
/>
328+
</MemberValidShow>
329+
</>
346330
</section>
347331
)}
348332

@@ -452,26 +436,30 @@ export default function PostLayout({
452436
</section>
453437

454438
{/* BLOG POST */}
455-
{!isLockedLesson() ? (
456-
<div className="flex flex-wrap gap-4 px-4 pb-4 mt-2 xl:flex-nowrap lg:px-10 lg:pb-10">
457-
<section className="flex flex-col w-full gap-4">
458-
<>{showPost()}</>
459-
</section>
460-
</div>
461-
) : user && user?.uid ? (
462-
<>
463-
<MemberValidShow user={user}>
439+
<>
440+
<MemberValidShow
441+
user={user}
442+
sectionLesson={sectionLesson}
443+
notValidComponent={<PostMediaLocked />}
444+
>
445+
<>
464446
<div className="flex flex-wrap gap-4 px-4 pb-4 mt-2 xl:flex-nowrap lg:px-10 lg:pb-10">
465447
<section className="flex flex-col w-full gap-4">
466448
<>{showPost()}</>
467449
</section>
468450
</div>
469-
</MemberValidShow>
470-
<div className="inline-block w-full xl:hidden">{recents()}</div>
471-
</>
472-
) : (
473-
<div className="inline-block w-full">{recents()}</div>
474-
)}
451+
<>
452+
{user && user?.uid ? (
453+
<div className="inline-block w-full xl:hidden">
454+
{recents()}
455+
</div>
456+
) : (
457+
<div className="inline-block w-full">{recents()}</div>
458+
)}
459+
</>
460+
</>
461+
</MemberValidShow>
462+
</>
475463
</div>
476464
<style global jsx>{`
477465
article > * {

0 commit comments

Comments
 (0)