refactor: structure of calling usecases to just be from controller

This commit is contained in:
Behnamrhp74 2025-03-08 22:16:29 +03:00
parent 7d9a6e77bd
commit 4306ea1bdc
26 changed files with 74 additions and 36 deletions

View File

@ -1,4 +1,4 @@
import cardController from "@/app/[lang]/dashboard/components/server/card/card.controller";
import cardController from "@/app/[lang]/dashboard/controller/card.controller";
export function Card({
title,

View File

@ -1,5 +1,5 @@
import { Card } from "@/app/[lang]/dashboard/components/server/card/card";
import cardsController from "@/app/[lang]/dashboard/components/server/cards/cards.controller";
import { Card } from "@/app/[lang]/dashboard/components/server/card";
import cardsController from "@/app/[lang]/dashboard/controller/cards.controller";
export default async function CardWrapper() {
const { customersNumber, invoicesNumber, invoicesSummary } =

View File

@ -1,5 +1,5 @@
import CreateRandomInvoiceContainer from "@/app/[lang]/dashboard/components/client/create-random-invoice/create-random-invoice";
import latestInvoicesController from "@/app/[lang]/dashboard/components/server/latest-invoices/latest-invoices.controller";
import latestInvoicesController from "@/app/[lang]/dashboard/controller/latest-invoices.controller";
import { ArrowPathIcon } from "@heroicons/react/24/outline";
import clsx from "clsx";
import { isLeft } from "fp-ts/lib/Either";

View File

@ -1,4 +1,4 @@
import revenueChartController from "@/app/[lang]/dashboard/components/server/revenue-chart/revenue-chart.controller";
import revenueChartController from "@/app/[lang]/dashboard/controller/revenue-chart.controller";
import { CalendarIcon } from "@heroicons/react/24/outline";
export default async function RevenueChart() {

View File

@ -7,6 +7,9 @@ import {
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default function cardController(props: {
type: "invoices" | "customers" | "pending" | "collected";

View File

@ -3,6 +3,9 @@ import { connection } from "next/server";
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default function cardsController() {
connection();

View File

@ -0,0 +1,19 @@
"use server";
import { ApiEither } from "@/feature/common/data/api-task";
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice.param";
import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase";
import { connection } from "next/server";
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default async function createInvoiceController(
params: InvoiceParam,
): Promise<ApiEither<string>> {
connection();
return createInvoiceUsecase(params);
}

View File

@ -3,6 +3,9 @@ import { connection } from "next/server";
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default function latestInvoicesController() {
connection();

View File

@ -3,6 +3,9 @@ import fetchRevenuesUsecase from "@/feature/core/revenue/domain/usecase/fetch-re
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default async function revenueChartController() {
const revenue = await fetchRevenuesUsecase();

View File

@ -1,4 +1,4 @@
import DashboardSkeleton from "@/app/[lang]/dashboard/components/server/skeletons/skeletons";
import DashboardSkeleton from "@/app/[lang]/dashboard/components/server/skeletons";
export default function Loading() {
return <DashboardSkeleton />;

View File

@ -1,6 +1,6 @@
import CreateRandomInvoiceButtonVM from "@/app/[lang]/dashboard/vm/create-random-invoice-button-vm";
import di from "@/bootstrap/di/init-di";
import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase";
// import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase";
/**
* Each page can have its own di to connect all vms, usecases or controllers
@ -8,9 +8,9 @@ import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-i
export default function dashboardAppModule() {
const dashboardDi = di.createChildContainer();
dashboardDi.register(createInvoiceUsecase.name, {
useValue: createInvoiceUsecase,
});
// dashboardDi.register(createInvoiceUsecase.name, {
// useValue: createInvoiceUsecase,
// });
dashboardDi.register(
CreateRandomInvoiceButtonVM,
CreateRandomInvoiceButtonVM,

View File

@ -1,13 +1,13 @@
import {
LatestInvoicesSkeleton,
RevenueChartSkeleton,
} from "@/app/[lang]/dashboard/components/server/skeletons/skeletons";
import CardWrapper from "@/app/[lang]/dashboard/components/server/cards/cards";
import LatestInvoices from "@/app/[lang]/dashboard/components/server/latest-invoices/latest-invoices";
import RevenueChart from "@/app/[lang]/dashboard/components/server/revenue-chart/revenue-chart";
} from "@/app/[lang]/dashboard/components/server/skeletons";
import CardWrapper from "@/app/[lang]/dashboard/components/server/cards";
import RevenueChart from "@/app/[lang]/dashboard/components/server/revenue-chart";
import { Suspense } from "react";
import { getServerTranslation, LANGS } from "@/bootstrap/i18n/i18n";
import langKey from "@/bootstrap/i18n/dictionaries/lang-key";
import LatestInvoices from "@/app/[lang]/dashboard/components/server/latest-invoices";
export default async function Dashboard(props: {
params: Promise<{ lang: LANGS }>;

View File

@ -1,3 +1,4 @@
import createInvoiceController from "@/app/[lang]/dashboard/controller/create-invoice.controller";
import ButtonVm from "@/app/components/button/button.i-vm";
import { useServerAction } from "@/bootstrap/helpers/hooks/use-server-action";
import useThrottle from "@/bootstrap/helpers/hooks/use-throttle";
@ -19,7 +20,7 @@ export default class CreateRandomInvoiceButtonVM extends BaseVM<ButtonVm> {
constructor() {
super();
this.createInvoice = this.di.resolve(createInvoiceUsecase.name);
this.createInvoice = createInvoiceController;
}
useVM(): ButtonVm {

View File

@ -20,7 +20,7 @@ export default async function layout(
) {
const { params, children } = props;
const { lang } = await params;
const { resources } = await getI18n({ lng: lang });
await getI18n({ lng: lang });
return (
<html lang={lang} suppressHydrationWarning>
<body
@ -32,9 +32,7 @@ export default async function layout(
enableSystem
disableTransitionOnChange
>
<TranslationsProvider lng={lang} resources={resources}>
{children}
</TranslationsProvider>
<TranslationsProvider lng={lang}>{children}</TranslationsProvider>
</ThemeProvider>
</body>
</html>

View File

@ -1,17 +1,24 @@
"use client";
import { I18nextProvider } from "react-i18next";
import { i18nInstance, getI18n, LANGS } from "@/bootstrap/i18n/i18n";
import { Resource } from "i18next";
import { PropsWithChildren } from "react";
import { getI18n, LANGS } from "@/bootstrap/i18n/i18n";
import { PropsWithChildren, useEffect, useState } from "react";
import { i18n } from "i18next";
import storeLang from "@/bootstrap/i18n/store-lang-action";
export default function TranslationsProvider({
children,
lng,
resources,
}: PropsWithChildren & { lng: LANGS; resources: Resource }) {
if (!resources) return children;
getI18n({ lng, resources });
}: PropsWithChildren & { lng: LANGS }) {
const [i18n, setI18n] = useState<i18n>();
return <I18nextProvider i18n={i18nInstance}>{children}</I18nextProvider>;
useEffect(() => {
(async () => {
storeLang(lng);
setI18n((await getI18n({ lng })).i18n);
})();
}, [lng]);
if (!i18n) return null;
return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
}

View File

@ -0,0 +1,9 @@
"use server";
import { LANGS } from "@/bootstrap/i18n/i18n";
import { cookieName } from "@/bootstrap/i18n/settings";
import { cookies } from "next/headers";
export default async function storeLang(lng: LANGS) {
(await cookies()).set(cookieName, lng);
}

View File

@ -1,4 +1,3 @@
import "server-only";
import { ApiEither } from "@/feature/common/data/api-task";
import serverDi from "@/feature/common/server.di";
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice.entity";

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di";
import { customerKey } from "@/feature/core/customer/customer-key";
import CustomerRepo, {

View File

@ -1,4 +1,3 @@
import "server-only";
import { ApiEither } from "@/feature/common/data/api-task";
import serverDi from "@/feature/common/server.di";
import { customerKey } from "@/feature/core/customer/customer-key";

View File

@ -1,3 +1,4 @@
import "server-only";
import { sql } from "@/bootstrap/boundaries/db/db";
import ApiTask from "@/feature/common/data/api-task";
import { failureOr } from "@/feature/common/failures/failure-helpers";

View File

@ -1,5 +1,3 @@
"use server";
import { ApiEither } from "@/feature/common/data/api-task";
import ParamsFailure from "@/feature/common/failures/params.failure";
import serverDi from "@/feature/common/server.di";

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di";
import InvoiceRepo, {
invoiceRepoKey,

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di";
import InvoiceRepo, {
invoiceRepoKey,

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di";
import Revenue from "@/feature/core/revenue/domain/entity/revenue.entity";
import RevenueRepo, {

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di";
import fetchCustomersAmountUsecase from "@/feature/core/customer/domain/usecase/fetch-customers-amount-usecase";
import fetchAllInvoicesAmountUsecase from "@/feature/core/invoice/domain/usecase/fetch-all-invoices-amount.usecase";