Skip to content

Commit

Permalink
ar(feat) [DPCP-38] [DPCP-39] [DPCP-46]: Abstract Views
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloreale committed Aug 5, 2024
1 parent 18318b1 commit 12ac054
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/app/components/client/blocks/topnav-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export const VTopNav = ({ user }: VTopNavProps) => {
<DPTypo variant={TypographyVariant.SMALL}>
Welcome, {coercedName}
</DPTypo>
<InternalLink href="/services/rickmorty">
<InternalLink className="block" href="/services/rickmorty">
Rick Morty
</InternalLink>
<InternalLink href="/services/hypnos">
<InternalLink className="block" href="/services/hypnos">
hypnos
</InternalLink>
</div>
Expand Down
64 changes: 37 additions & 27 deletions src/app/components/client/elements/calendar-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,87 @@ import { useContext, useEffect, useRef, useMemo } from 'react';

import { clsx } from 'clsx';
import Image from 'next/image';
import { HypnosPublicContext, AuthContext, GlobalContext } from '@state';
import { ALoadPublicListings, AUnloadPublicListings, ADecoratePublicListings, AAddToFavoritePublicListings } from '@actions';
import { navigate, addToFavorites, loadHypnosPublicListings } from '@gateway';
import { CardGrid as DPCardGrid } from "@dreampipcom/oneiros";
import { AuthContext, GlobalContext } from '@state';
import { navigate, addToFavorites } from '@gateway';
import { CalendarView } from "@dreampipcom/oneiros";

// to-do: character type annotations
interface VListingListProps {
interface VCalendarProps {
listings: ICard[];
addToFavorites?: () => void;
fetchListings?: () => void;
loadListings?: () => void;
decListings?: () => void;
unloadListings?: () => void;
favoriteType?: string;
listingContext?: any;
}

type VHPNPCalendarProps = VListingListProps;
type VHPNPCalendarProps = VCalendarProps;

export const VHPNPCalendar = ({ listings }: VHPNPCalendarProps) => {
const hypnosPublicContext = useContext(HypnosPublicContext);
export const VHPNPCalendar = ({ listings, fetchListings, favListing, loadListings, decListings, unloadListings, isListingsLoaded, listingContext, favoriteType }: VHPNPCalendarProps) => {
const [_isListingsLoaded, _loadListings] = loadListings({});
const [, _decListings] = decListings({});
const [, _unloadListings] = unloadListings({});
const [, _favListing] = favListing({});

const _listingContext = useContext(listingContext);

const { authd, email, user } = useContext(AuthContext);

const globalContext = useContext(GlobalContext);
const { theme } = globalContext;

const [isListingsLoaded, loadListings] = ALoadPublicListings({});
const [, decListings] = ADecoratePublicListings({});
const [, unloadListings] = AUnloadPublicListings({});
const [, favListing] = AAddToFavoritePublicListings({});
const initd = useRef(false);

const { listings: currentListings }: { listings?: ICard[] } = hypnosPublicContext;
if (!listingContext) return;

const { listings: currentListings }: { listings?: ICard[] } = _listingContext;


const dispatchAddToFavorites = async (cid?: number) => {
const func = async (payload: IDPayload) => {
await addToFavorites({ listings: [cid] });
const op_2 = await loadHypnosPublicListings();
loadListings({ listings: op_2 });
const res = await addToFavorites({ listings: [cid], type: favoriteType });
const op_2 = await fetchListings();
_loadListings({ listings: op_2 });
};
favListing({ email, cid }, func);
_favListing({ email, cid }, func);
};

useEffect(() => {
if (authd && listings && !isListingsLoaded && !initd.current) {
loadListings({
if (authd && listings && !_isListingsLoaded && !initd.current) {
_loadListings({
listings: listings as ICard[],
});
initd.current = true;
}
}, [listings, isListingsLoaded, loadListings, authd]);
}, [listings, _isListingsLoaded, _loadListings, authd]);

useEffect(() => {
if (authd && isListingsLoaded) {
decListings({ action: {} });
if (authd && _isListingsLoaded) {
_decListings({ action: {} });

return () => {
// to-do decorate clean up
};
}
}, [isListingsLoaded, authd]);
}, [_isListingsLoaded, authd]);

useEffect(() => {
if (!isListingsLoaded) return;
if (!_isListingsLoaded) return;
return () => {
unloadListings();
_unloadListings();
};
}, []);

if (!authd || !listings) return;

if (!isListingsLoaded && !listings) return <span>Loading...</span>;
if (!_isListingsLoaded && !listings) return <span>Loading...</span>;

if (authd) {
return (
<article>
<DPCardGrid cards={currentListings} theme={theme} onLikeCard={dispatchAddToFavorites} />
<CalendarView cards={currentListings} theme={theme} />
</article>
);
}
Expand Down
1 change: 0 additions & 1 deletion src/app/components/client/elements/list-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const VHPNPList = ({ listings, fetchListings, favListing, loadListings, d
const dispatchAddToFavorites = async (cid?: number) => {
const func = async (payload: IDPayload) => {
const res = await addToFavorites({ listings: [cid], type: favoriteType });
console.log({ res })
const op_2 = await fetchListings();
_loadListings({ listings: op_2 });
};
Expand Down
64 changes: 37 additions & 27 deletions src/app/components/client/elements/map-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,87 @@ import { useContext, useEffect, useRef, useMemo } from 'react';

import { clsx } from 'clsx';
import Image from 'next/image';
import { HypnosPublicContext, AuthContext, GlobalContext } from '@state';
import { ALoadPublicListings, AUnloadPublicListings, ADecoratePublicListings, AAddToFavoritePublicListings } from '@actions';
import { navigate, addToFavorites, loadHypnosPublicListings } from '@gateway';
import { CardGrid as DPCardGrid } from "@dreampipcom/oneiros";
import { AuthContext, GlobalContext } from '@state';
import { navigate, addToFavorites } from '@gateway';
import { MapView } from "@dreampipcom/oneiros";

// to-do: character type annotations
interface VListingListProps {
interface VMapProps {
listings: ICard[];
addToFavorites?: () => void;
fetchListings?: () => void;
loadListings?: () => void;
decListings?: () => void;
unloadListings?: () => void;
favoriteType?: string;
listingContext?: any;
}

type VHPNPMapProps = VListingListProps;
type VHPNPMapProps = VMapProps;

export const VHPNPMap = ({ listings }: VHPNPMapProps) => {
const hypnosPublicContext = useContext(HypnosPublicContext);
export const VHPNPMap = ({ listings, fetchListings, favListing, loadListings, decListings, unloadListings, isListingsLoaded, listingContext, favoriteType }: VHPNPMapProps) => {
const [_isListingsLoaded, _loadListings] = loadListings({});
const [, _decListings] = decListings({});
const [, _unloadListings] = unloadListings({});
const [, _favListing] = favListing({});

const _listingContext = useContext(listingContext);

const { authd, email, user } = useContext(AuthContext);

const globalContext = useContext(GlobalContext);
const { theme } = globalContext;

const [isListingsLoaded, loadListings] = ALoadPublicListings({});
const [, decListings] = ADecoratePublicListings({});
const [, unloadListings] = AUnloadPublicListings({});
const [, favListing] = AAddToFavoritePublicListings({});
const initd = useRef(false);

const { listings: currentListings }: { listings?: ICard[] } = hypnosPublicContext;
if (!listingContext) return;

const { listings: currentListings }: { listings?: ICard[] } = _listingContext;


const dispatchAddToFavorites = async (cid?: number) => {
const func = async (payload: IDPayload) => {
await addToFavorites({ listings: [cid] });
const op_2 = await loadHypnosPublicListings();
loadListings({ listings: op_2 });
const res = await addToFavorites({ listings: [cid], type: favoriteType });
const op_2 = await fetchListings();
_loadListings({ listings: op_2 });
};
favListing({ email, cid }, func);
_favListing({ email, cid }, func);
};

useEffect(() => {
if (authd && listings && !isListingsLoaded && !initd.current) {
loadListings({
if (authd && listings && !_isListingsLoaded && !initd.current) {
_loadListings({
listings: listings as ICard[],
});
initd.current = true;
}
}, [listings, isListingsLoaded, loadListings, authd]);
}, [listings, _isListingsLoaded, _loadListings, authd]);

useEffect(() => {
if (authd && isListingsLoaded) {
decListings({ action: {} });
if (authd && _isListingsLoaded) {
_decListings({ action: {} });

return () => {
// to-do decorate clean up
};
}
}, [isListingsLoaded, authd]);
}, [_isListingsLoaded, authd]);

useEffect(() => {
if (!isListingsLoaded) return;
if (!_isListingsLoaded) return;
return () => {
unloadListings();
_unloadListings();
};
}, []);

if (!authd || !listings) return;

if (!isListingsLoaded && !listings) return <span>Loading...</span>;
if (!_isListingsLoaded && !listings) return <span>Loading...</span>;

if (authd) {
return (
<article>
<DPCardGrid cards={currentListings} theme={theme} onLikeCard={dispatchAddToFavorites} />
<MapView events={currentListings} theme={theme} mapBoxToken={process.env.NEXT_PUBLIC_MAPBOX_TOKEN} />
</article>
);
}
Expand Down
12 changes: 9 additions & 3 deletions src/app/components/server/blocks/hypnos-public-list.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// hypnos-public-list.tsx
'use server';
import type { ICard } from '@dreampipcom/oneiros';
import { ListView } from '@elements/client';
import { ListView, CalendarView, MapView } from '@elements/client';
import { loadHypnosPublicListings } from '@gateway';
import { HypnosPublicProvider, HypnosPublicContext } from '@state';
import { ALoadPublicListings, AUnloadPublicListings, ADecoratePublicListings, AAddToFavoritePublicListings } from '@actions';

export const CHPNPList = async () => {
export interface IServiceViewProps {
mode?: "calendar" | "list" | "map";
}

export const CHPNPList = async ({ mode = "list" }) => {
const listings: ICard[] = await loadHypnosPublicListings();

return (
<HypnosPublicProvider>
<ListView listings={listings} fetchListings={loadHypnosPublicListings} loadListings={ALoadPublicListings} decListings={ADecoratePublicListings} unloadListings={AUnloadPublicListings} listingContext={HypnosPublicContext} favListing={AAddToFavoritePublicListings} />
{mode === "list" ? <ListView listings={listings} fetchListings={loadHypnosPublicListings} loadListings={ALoadPublicListings} decListings={ADecoratePublicListings} unloadListings={AUnloadPublicListings} listingContext={HypnosPublicContext} favListing={AAddToFavoritePublicListings} /> : undefined }
{mode === "map" ? <MapView listings={listings} fetchListings={loadHypnosPublicListings} loadListings={ALoadPublicListings} decListings={ADecoratePublicListings} unloadListings={AUnloadPublicListings} listingContext={HypnosPublicContext} favListing={AAddToFavoritePublicListings} /> : undefined }
{mode === "calendar" ? <CalendarView listings={listings} fetchListings={loadHypnosPublicListings} loadListings={ALoadPublicListings} decListings={ADecoratePublicListings} unloadListings={AUnloadPublicListings} listingContext={HypnosPublicContext} favListing={AAddToFavoritePublicListings} /> : undefined }
</HypnosPublicProvider>
);
};
12 changes: 9 additions & 3 deletions src/app/components/server/blocks/rm-list.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// list-controller.tsx
'use server';
import type { ICard } from '@dreampipcom/oneiros';
import { ListView } from '@elements/client';
import { ListView, CalendarView, MapView } from '@elements/client';
import { loadChars, getChars } from '@gateway';
import { RickMortyProvider, RMContext } from '@state';
import { ALoadChars, AUnloadChars, ADecorateChars, AAddToFavoriteChars} from '@actions';

export const CRMList = async () => {
export interface IServiceViewProps {
mode?: "calendar" | "list" | "map";
}

export const CRMList = async ({ mode = "list" }: IServiceViewProps) => {
const characters: ICard[] = await loadChars();

return (
<RickMortyProvider>
<ListView listings={characters} favoriteType="string" fetchListings={getChars} loadListings={ALoadChars} decListings={ADecorateChars} unloadListings={AUnloadChars} listingContext={RMContext} favListing={AAddToFavoriteChars} />
{mode === "list" ? <ListView listings={characters} favoriteType="string" fetchListings={getChars} loadListings={ALoadChars} decListings={ADecorateChars} unloadListings={AUnloadChars} listingContext={RMContext} favListing={AAddToFavoriteChars} /> : undefined }
{mode === "map" ? <MapView listings={characters} favoriteType="string" fetchListings={getChars} loadListings={ALoadChars} decListings={ADecorateChars} unloadListings={AUnloadChars} listingContext={RMContext} favListing={AAddToFavoriteChars} /> : undefined }
{mode === "calendar" ? <CalendarView listings={characters} favoriteType="string" fetchListings={getChars} loadListings={ALoadChars} decListings={ADecorateChars} unloadListings={AUnloadChars} listingContext={RMContext} favListing={AAddToFavoriteChars} /> : undefined }
</RickMortyProvider>
);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// page.tsx
import { DPPublicListings } from '@blocks/server';
export default function Home() {

export default function Home({ mode = 'list' }) {

return (
<main>
<section>
<DPPublicListings />
<DPPublicListings mode={mode} />
</section>
</main>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// page.tsx
import { DPRMList } from '@blocks/server';

export default function Home() {
return (
<main>
Expand Down

0 comments on commit 12ac054

Please sign in to comment.