Skip to content

Commit

Permalink
refactor: internalize view within the module
Browse files Browse the repository at this point in the history
  • Loading branch information
adbayb committed Dec 25, 2024
1 parent f875d7a commit ee0fbf2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
16 changes: 6 additions & 10 deletions hosts/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {
DependencyInjection as CatalogDependencyInjection,
QuoteEntityGateway,
} from "@clean-architecture/catalog";

import { GetQuote } from "./GetQuote";
// eslint-disable-next-line import-x/no-namespace
import * as Catalog from "@clean-architecture/catalog";

export const App = () => {
return (
<CatalogDependencyInjection
quoteEntityGateway={new QuoteEntityGateway()}
<Catalog.DependencyInjection
quoteEntityGateway={new Catalog.QuoteEntityGateway()} // TODO: move it internally?
>
<GetQuote />
</CatalogDependencyInjection>
<Catalog.GetQuoteView />
</Catalog.DependencyInjection>
);
};
24 changes: 0 additions & 24 deletions hosts/web/src/GetQuote.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";

import { GetQuoteUseCase } from "../../useCases/GetQuoteUseCase";
import type { GetQuoteViewModel } from "../../adapters/GetQuoteViewModel";
import { GetQuotePresenter } from "../../adapters/GetQuotePresenter";
import { GetQuoteController } from "../../adapters/GetQuoteController";
import { useDependencyInjection } from "./useDependencyInjection";

export const useGetQuote = () => {
export const GetQuoteView = () => {
const { controller, viewModel } = useGetQuote();

useEffect(() => {
void controller.execute({ id: "todo" });
}, [controller]);

if (viewModel.error) {
return <p>Error: {String(viewModel.error)}</p>;
}

if (viewModel.data) {
return (
<section>
<h3>{viewModel.data}</h3>
</section>
);
}

return null;
};

const useGetQuote = () => {
const { quoteEntityGateway } = useDependencyInjection();
const [viewModel, setViewModel] = useState<GetQuoteViewModel>({});
const presenter = useMemo(() => new GetQuotePresenter(setViewModel), []);
Expand Down
2 changes: 1 addition & 1 deletion modules/catalog/src/frameworks/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DependencyInjection } from "./useDependencyInjection";
export { useGetQuote } from "./useGetQuote";
export { GetQuoteView } from "./GetQuoteView";
2 changes: 1 addition & 1 deletion modules/catalog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { GetQuotePresenter } from "./adapters/GetQuotePresenter";
export { GetQuoteController } from "./adapters/GetQuoteController";
export { QuoteEntityGateway } from "./adapters/QuoteEntityGateway";
export { GetQuoteUseCase } from "./useCases/GetQuoteUseCase";
export { DependencyInjection, useGetQuote } from "./frameworks/web";
export { DependencyInjection, GetQuoteView } from "./frameworks/web";
11 changes: 5 additions & 6 deletions modules/shared-kernel/src/Presenter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Result } from "@open-vanilla/result";

import type { ViewModel } from "./ViewModel";
import type { UseCaseOutputPort } from "./UseCase";
import type { ResponseModel } from "./ResponseModel";

/**
* A presenter maps data structures returned by the use case interactor into data structures most convenient for the view.
Expand All @@ -10,11 +9,13 @@ import type { UseCaseOutputPort } from "./UseCase";
* @example
*/
export abstract class Presenter<
RM extends Result<unknown>,
RM extends ResponseModel<unknown>,
VM extends ViewModel,
> implements UseCaseOutputPort<RM>
{
public constructor(protected onViewModelChange: (vm: VM) => void) {}
public constructor(private readonly onViewModelChange: (vm: VM) => void) {}

public abstract toViewModel(responseModel: RM): VM;

private setViewModel(responseModel: RM) {
this.onViewModelChange(this.toViewModel(responseModel));
Expand All @@ -39,6 +40,4 @@ export abstract class Presenter<

this.setViewModel(responseModel);
}

public abstract toViewModel(responseModel: RM): VM;
}

0 comments on commit ee0fbf2

Please sign in to comment.