apps/web: remove makeCreateUrl function

This commit is contained in:
vchikalkin 2023-11-22 00:37:49 +03:00
parent 8cb4d9c6e5
commit 3fb32b548c
7 changed files with 39 additions and 42 deletions

View File

@ -1,17 +1,20 @@
'use server'; 'use server';
import type * as t from './types'; import type * as t from './types';
import { getUrls } from '@/config/urls'; import { getUrls } from '@/config/urls';
import type { CreateUrl } from '@/utils/url'; import { createUrl, type PageUrlParams } from '@/utils/url';
import type { WretchError } from 'wretch'; import type { WretchError } from 'wretch';
import wretch from 'wretch'; import wretch from 'wretch';
const urls = getUrls(); const urls = getUrls();
const api = wretch(urls.URL_UIS).errorType('json'); const api = wretch(urls.URL_UIS).errorType('json');
type Input = { createUrl: CreateUrl; payload?: unknown }; type Input = { pageUrlParams: PageUrlParams; payload?: unknown };
export async function getData({ createUrl }: Input) { export async function getData({ pageUrlParams }: Input) {
const url = createUrl(''); const url = createUrl({
...pageUrlParams,
route: '',
});
return api return api
.get(url) .get(url)
@ -19,8 +22,8 @@ export async function getData({ createUrl }: Input) {
.then((res) => res); .then((res) => res);
} }
export async function getMetaData({ createUrl }: Input) { export async function getMetaData({ pageUrlParams }: Input) {
const url = createUrl('/meta'); const url = createUrl({ ...pageUrlParams, route: '/meta' });
return api return api
.get(url) .get(url)
@ -28,8 +31,8 @@ export async function getMetaData({ createUrl }: Input) {
.then((res) => res); .then((res) => res);
} }
export async function getConfig({ createUrl }: Input) { export async function getConfig({ pageUrlParams }: Input) {
const url = createUrl('/config'); const url = createUrl({ ...pageUrlParams, route: '/config' });
return api return api
.get(url) .get(url)
@ -37,8 +40,8 @@ export async function getConfig({ createUrl }: Input) {
.then((res) => res); .then((res) => res);
} }
export async function getConditions({ createUrl }: Input) { export async function getConditions({ pageUrlParams }: Input) {
const url = createUrl('/conditions'); const url = createUrl({ ...pageUrlParams, route: '/conditions' });
return api return api
.get(url) .get(url)
@ -46,8 +49,8 @@ export async function getConditions({ createUrl }: Input) {
.then((res) => res); .then((res) => res);
} }
export async function validate({ createUrl, payload }: Input) { export async function validate({ pageUrlParams, payload }: Input) {
const url = createUrl('/validate'); const url = createUrl({ ...pageUrlParams, route: '/validate' });
return api return api
.post(payload, url) .post(payload, url)

View File

@ -2,13 +2,12 @@ import * as apiIUS from '@/api/ius/query';
import { Conditions } from '@/components/Conditions'; import { Conditions } from '@/components/Conditions';
import type { PageProps } from '@/types/page'; import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error'; import { withError } from '@/utils/error';
import { getPageUrlParams, makeCreateUrl } from '@/utils/url'; import { getPageUrlParams } from '@/utils/url';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
export async function generateMetadata(pageProps: PageProps): Promise<Metadata> { export async function generateMetadata(pageProps: PageProps): Promise<Metadata> {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
const createUrl = makeCreateUrl(pageUrlParams); const { title } = await apiIUS.getConfig({ pageUrlParams });
const { title } = await apiIUS.getConfig({ createUrl });
const text = `Условия: ${title} | Эволюция`; const text = `Условия: ${title} | Эволюция`;
return { return {
@ -25,8 +24,7 @@ export default async function Page(pageProps: PageProps) {
return withError({ return withError({
render: async () => { render: async () => {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
const createUrl = makeCreateUrl(pageUrlParams); const conditions = await apiIUS.getConditions({ pageUrlParams });
const conditions = await apiIUS.getConditions({ createUrl });
return <Conditions html={conditions} />; return <Conditions html={conditions} />;
}, },

View File

@ -2,13 +2,12 @@ import * as apiIUS from '@/api/ius/query';
import { Form } from '@/components/Form'; import { Form } from '@/components/Form';
import type { PageProps } from '@/types/page'; import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error'; import { withError } from '@/utils/error';
import { getPageUrlParams, makeCreateUrl } from '@/utils/url'; import { getPageUrlParams } from '@/utils/url';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
export async function generateMetadata(pageProps: PageProps): Promise<Metadata> { export async function generateMetadata(pageProps: PageProps): Promise<Metadata> {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
const createUrl = makeCreateUrl(pageUrlParams); const { title } = await apiIUS.getConfig({ pageUrlParams });
const { title } = await apiIUS.getConfig({ createUrl });
const text = `${title} | Эволюция`; const text = `${title} | Эволюция`;
return { return {
@ -25,12 +24,11 @@ export default async function Page(pageProps: PageProps) {
return withError({ return withError({
render: async () => { render: async () => {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
const createUrl = makeCreateUrl(pageUrlParams);
return Promise.all([ return Promise.all([
apiIUS.getData({ createUrl }), apiIUS.getData({ pageUrlParams }),
apiIUS.getMetaData({ createUrl }), apiIUS.getMetaData({ pageUrlParams }),
apiIUS.getConfig({ createUrl }), apiIUS.getConfig({ pageUrlParams }),
]).then(([data, metaData, { title }]) => { ]).then(([data, metaData, { title }]) => {
const props = { data, metaData, pageUrlParams, title }; const props = { data, metaData, pageUrlParams, title };

View File

@ -6,7 +6,7 @@ import { Button } from 'ui';
export function Buttons() { export function Buttons() {
const { reset, setValidation, values } = useFormStore(); const { reset, setValidation, values } = useFormStore();
const { createUrl, setFormStatus } = useContext(FormContext); const { pageUrlParams, setFormStatus } = useContext(FormContext);
return ( return (
<div className="grid grid-cols-1 gap-2 gap-x-4 md:grid-cols-3"> <div className="grid grid-cols-1 gap-2 gap-x-4 md:grid-cols-3">
@ -28,7 +28,7 @@ export function Buttons() {
</Button> </Button>
<Button <Button
onClick={() => { onClick={() => {
apiIus.validate({ createUrl, payload: values }).then((res) => { apiIus.validate({ pageUrlParams, payload: values }).then((res) => {
if (typeof res !== 'boolean') { if (typeof res !== 'boolean') {
Object.keys(res.errors).forEach((name) => { Object.keys(res.errors).forEach((name) => {
const elementValidation = res?.errors?.[name]; const elementValidation = res?.errors?.[name];

View File

@ -1,13 +1,12 @@
import type { CreateUrl, PageUrlParams } from '@/utils/url'; import type { PageUrlParams } from '@/utils/url';
import type { PropsWithChildren } from 'react'; import type { PropsWithChildren } from 'react';
import { createContext, useMemo, useState } from 'react'; import { createContext, useMemo, useState } from 'react';
type FormStatus = 'pending' | 'edit' | 'success'; type FormStatus = 'pending' | 'edit' | 'success';
type ContextType = { type ContextType = {
readonly createUrl: CreateUrl;
readonly formStatus: FormStatus; readonly formStatus: FormStatus;
readonly pageUrlParams: PageUrlParams | undefined; readonly pageUrlParams: PageUrlParams;
readonly setFormStatus: (status: FormStatus) => void; readonly setFormStatus: (status: FormStatus) => void;
}; };
@ -16,7 +15,7 @@ export const FormContext = createContext<ContextType>({} as ContextType);
export function FormContextProvider({ export function FormContextProvider({
children, children,
...initialData ...initialData
}: PropsWithChildren & Pick<ContextType, 'createUrl' | 'pageUrlParams'>) { }: PropsWithChildren & Pick<ContextType, 'pageUrlParams'>) {
const [formStatus, setFormStatus] = useState<FormStatus>('edit'); const [formStatus, setFormStatus] = useState<FormStatus>('edit');
const value = useMemo( const value = useMemo(
() => ({ ...initialData, formStatus, setFormStatus }), () => ({ ...initialData, formStatus, setFormStatus }),

View File

@ -5,19 +5,19 @@ import { Elements } from './Elements';
import { Header } from './Header'; import { Header } from './Header';
import { Overlay } from './Overlay'; import { Overlay } from './Overlay';
import type { Props } from './types'; import type { Props } from './types';
import { makeCreateUrl } from '@/utils/url'; import { createUrl } from '@/utils/url';
import type { FC } from 'react'; import type { FC } from 'react';
import { useContext } from 'react'; import { useContext } from 'react';
import { Background, Divider } from 'ui'; import { Background, Divider } from 'ui';
function Content(props: Props) { function Content(props: Props) {
const { title } = props; const { title } = props;
const { createUrl } = useContext(FormContext); const { pageUrlParams } = useContext(FormContext);
return ( return (
<Background className="lg:w-standard relative grid w-full gap-2 p-5"> <Background className="lg:w-standard relative grid w-full gap-2 p-5">
<Overlay /> <Overlay />
<Header title={title} link={'/ius' + createUrl('/conditions')} /> <Header title={title} link={'/ius' + createUrl({ ...pageUrlParams, route: '/conditions' })} />
<Elements {...props} /> <Elements {...props} />
<Divider /> <Divider />
<Buttons /> <Buttons />
@ -28,10 +28,9 @@ function Content(props: Props) {
function withContext<T extends Props>(Component: FC<T>) { function withContext<T extends Props>(Component: FC<T>) {
return (props: T) => { return (props: T) => {
const { pageUrlParams } = props; const { pageUrlParams } = props;
const createUrl = makeCreateUrl(pageUrlParams);
return ( return (
<FormContextProvider pageUrlParams={pageUrlParams} createUrl={createUrl}> <FormContextProvider pageUrlParams={pageUrlParams}>
<Component {...props} /> <Component {...props} />
</FormContextProvider> </FormContextProvider>
); );

View File

@ -6,12 +6,12 @@ export function getPageUrlParams({ params, searchParams }: PageProps) {
export type PageUrlParams = ReturnType<typeof getPageUrlParams>; export type PageUrlParams = ReturnType<typeof getPageUrlParams>;
export function makeCreateUrl({ path, urlSearchParams }: ReturnType<typeof getPageUrlParams>) { export function createUrl({
return function (route: string) { path,
if (urlSearchParams) return `${path}${route}?${new URLSearchParams(urlSearchParams)}`; route = '',
urlSearchParams,
}: PageUrlParams & { route: string }) {
if (urlSearchParams) return `${path}${route}?${new URLSearchParams(urlSearchParams)}`;
return path + route; return path + route;
};
} }
export type CreateUrl = ReturnType<typeof makeCreateUrl>;