apps/web: remove makeCreateUrl function
This commit is contained in:
parent
8cb4d9c6e5
commit
3fb32b548c
@ -1,17 +1,20 @@
|
||||
'use server';
|
||||
import type * as t from './types';
|
||||
import { getUrls } from '@/config/urls';
|
||||
import type { CreateUrl } from '@/utils/url';
|
||||
import { createUrl, type PageUrlParams } from '@/utils/url';
|
||||
import type { WretchError } from 'wretch';
|
||||
import wretch from 'wretch';
|
||||
|
||||
const urls = getUrls();
|
||||
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) {
|
||||
const url = createUrl('');
|
||||
export async function getData({ pageUrlParams }: Input) {
|
||||
const url = createUrl({
|
||||
...pageUrlParams,
|
||||
route: '',
|
||||
});
|
||||
|
||||
return api
|
||||
.get(url)
|
||||
@ -19,8 +22,8 @@ export async function getData({ createUrl }: Input) {
|
||||
.then((res) => res);
|
||||
}
|
||||
|
||||
export async function getMetaData({ createUrl }: Input) {
|
||||
const url = createUrl('/meta');
|
||||
export async function getMetaData({ pageUrlParams }: Input) {
|
||||
const url = createUrl({ ...pageUrlParams, route: '/meta' });
|
||||
|
||||
return api
|
||||
.get(url)
|
||||
@ -28,8 +31,8 @@ export async function getMetaData({ createUrl }: Input) {
|
||||
.then((res) => res);
|
||||
}
|
||||
|
||||
export async function getConfig({ createUrl }: Input) {
|
||||
const url = createUrl('/config');
|
||||
export async function getConfig({ pageUrlParams }: Input) {
|
||||
const url = createUrl({ ...pageUrlParams, route: '/config' });
|
||||
|
||||
return api
|
||||
.get(url)
|
||||
@ -37,8 +40,8 @@ export async function getConfig({ createUrl }: Input) {
|
||||
.then((res) => res);
|
||||
}
|
||||
|
||||
export async function getConditions({ createUrl }: Input) {
|
||||
const url = createUrl('/conditions');
|
||||
export async function getConditions({ pageUrlParams }: Input) {
|
||||
const url = createUrl({ ...pageUrlParams, route: '/conditions' });
|
||||
|
||||
return api
|
||||
.get(url)
|
||||
@ -46,8 +49,8 @@ export async function getConditions({ createUrl }: Input) {
|
||||
.then((res) => res);
|
||||
}
|
||||
|
||||
export async function validate({ createUrl, payload }: Input) {
|
||||
const url = createUrl('/validate');
|
||||
export async function validate({ pageUrlParams, payload }: Input) {
|
||||
const url = createUrl({ ...pageUrlParams, route: '/validate' });
|
||||
|
||||
return api
|
||||
.post(payload, url)
|
||||
|
||||
@ -2,13 +2,12 @@ import * as apiIUS from '@/api/ius/query';
|
||||
import { Conditions } from '@/components/Conditions';
|
||||
import type { PageProps } from '@/types/page';
|
||||
import { withError } from '@/utils/error';
|
||||
import { getPageUrlParams, makeCreateUrl } from '@/utils/url';
|
||||
import { getPageUrlParams } from '@/utils/url';
|
||||
import type { Metadata } from 'next';
|
||||
|
||||
export async function generateMetadata(pageProps: PageProps): Promise<Metadata> {
|
||||
const pageUrlParams = getPageUrlParams(pageProps);
|
||||
const createUrl = makeCreateUrl(pageUrlParams);
|
||||
const { title } = await apiIUS.getConfig({ createUrl });
|
||||
const { title } = await apiIUS.getConfig({ pageUrlParams });
|
||||
const text = `Условия: ${title} | Эволюция`;
|
||||
|
||||
return {
|
||||
@ -25,8 +24,7 @@ export default async function Page(pageProps: PageProps) {
|
||||
return withError({
|
||||
render: async () => {
|
||||
const pageUrlParams = getPageUrlParams(pageProps);
|
||||
const createUrl = makeCreateUrl(pageUrlParams);
|
||||
const conditions = await apiIUS.getConditions({ createUrl });
|
||||
const conditions = await apiIUS.getConditions({ pageUrlParams });
|
||||
|
||||
return <Conditions html={conditions} />;
|
||||
},
|
||||
|
||||
@ -2,13 +2,12 @@ import * as apiIUS from '@/api/ius/query';
|
||||
import { Form } from '@/components/Form';
|
||||
import type { PageProps } from '@/types/page';
|
||||
import { withError } from '@/utils/error';
|
||||
import { getPageUrlParams, makeCreateUrl } from '@/utils/url';
|
||||
import { getPageUrlParams } from '@/utils/url';
|
||||
import type { Metadata } from 'next';
|
||||
|
||||
export async function generateMetadata(pageProps: PageProps): Promise<Metadata> {
|
||||
const pageUrlParams = getPageUrlParams(pageProps);
|
||||
const createUrl = makeCreateUrl(pageUrlParams);
|
||||
const { title } = await apiIUS.getConfig({ createUrl });
|
||||
const { title } = await apiIUS.getConfig({ pageUrlParams });
|
||||
const text = `${title} | Эволюция`;
|
||||
|
||||
return {
|
||||
@ -25,12 +24,11 @@ export default async function Page(pageProps: PageProps) {
|
||||
return withError({
|
||||
render: async () => {
|
||||
const pageUrlParams = getPageUrlParams(pageProps);
|
||||
const createUrl = makeCreateUrl(pageUrlParams);
|
||||
|
||||
return Promise.all([
|
||||
apiIUS.getData({ createUrl }),
|
||||
apiIUS.getMetaData({ createUrl }),
|
||||
apiIUS.getConfig({ createUrl }),
|
||||
apiIUS.getData({ pageUrlParams }),
|
||||
apiIUS.getMetaData({ pageUrlParams }),
|
||||
apiIUS.getConfig({ pageUrlParams }),
|
||||
]).then(([data, metaData, { title }]) => {
|
||||
const props = { data, metaData, pageUrlParams, title };
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import { Button } from 'ui';
|
||||
|
||||
export function Buttons() {
|
||||
const { reset, setValidation, values } = useFormStore();
|
||||
const { createUrl, setFormStatus } = useContext(FormContext);
|
||||
const { pageUrlParams, setFormStatus } = useContext(FormContext);
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-1 gap-2 gap-x-4 md:grid-cols-3">
|
||||
@ -28,7 +28,7 @@ export function Buttons() {
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
apiIus.validate({ createUrl, payload: values }).then((res) => {
|
||||
apiIus.validate({ pageUrlParams, payload: values }).then((res) => {
|
||||
if (typeof res !== 'boolean') {
|
||||
Object.keys(res.errors).forEach((name) => {
|
||||
const elementValidation = res?.errors?.[name];
|
||||
|
||||
@ -1,13 +1,12 @@
|
||||
import type { CreateUrl, PageUrlParams } from '@/utils/url';
|
||||
import type { PageUrlParams } from '@/utils/url';
|
||||
import type { PropsWithChildren } from 'react';
|
||||
import { createContext, useMemo, useState } from 'react';
|
||||
|
||||
type FormStatus = 'pending' | 'edit' | 'success';
|
||||
|
||||
type ContextType = {
|
||||
readonly createUrl: CreateUrl;
|
||||
readonly formStatus: FormStatus;
|
||||
readonly pageUrlParams: PageUrlParams | undefined;
|
||||
readonly pageUrlParams: PageUrlParams;
|
||||
readonly setFormStatus: (status: FormStatus) => void;
|
||||
};
|
||||
|
||||
@ -16,7 +15,7 @@ export const FormContext = createContext<ContextType>({} as ContextType);
|
||||
export function FormContextProvider({
|
||||
children,
|
||||
...initialData
|
||||
}: PropsWithChildren & Pick<ContextType, 'createUrl' | 'pageUrlParams'>) {
|
||||
}: PropsWithChildren & Pick<ContextType, 'pageUrlParams'>) {
|
||||
const [formStatus, setFormStatus] = useState<FormStatus>('edit');
|
||||
const value = useMemo(
|
||||
() => ({ ...initialData, formStatus, setFormStatus }),
|
||||
|
||||
@ -5,19 +5,19 @@ import { Elements } from './Elements';
|
||||
import { Header } from './Header';
|
||||
import { Overlay } from './Overlay';
|
||||
import type { Props } from './types';
|
||||
import { makeCreateUrl } from '@/utils/url';
|
||||
import { createUrl } from '@/utils/url';
|
||||
import type { FC } from 'react';
|
||||
import { useContext } from 'react';
|
||||
import { Background, Divider } from 'ui';
|
||||
|
||||
function Content(props: Props) {
|
||||
const { title } = props;
|
||||
const { createUrl } = useContext(FormContext);
|
||||
const { pageUrlParams } = useContext(FormContext);
|
||||
|
||||
return (
|
||||
<Background className="lg:w-standard relative grid w-full gap-2 p-5">
|
||||
<Overlay />
|
||||
<Header title={title} link={'/ius' + createUrl('/conditions')} />
|
||||
<Header title={title} link={'/ius' + createUrl({ ...pageUrlParams, route: '/conditions' })} />
|
||||
<Elements {...props} />
|
||||
<Divider />
|
||||
<Buttons />
|
||||
@ -28,10 +28,9 @@ function Content(props: Props) {
|
||||
function withContext<T extends Props>(Component: FC<T>) {
|
||||
return (props: T) => {
|
||||
const { pageUrlParams } = props;
|
||||
const createUrl = makeCreateUrl(pageUrlParams);
|
||||
|
||||
return (
|
||||
<FormContextProvider pageUrlParams={pageUrlParams} createUrl={createUrl}>
|
||||
<FormContextProvider pageUrlParams={pageUrlParams}>
|
||||
<Component {...props} />
|
||||
</FormContextProvider>
|
||||
);
|
||||
|
||||
@ -6,12 +6,12 @@ export function getPageUrlParams({ params, searchParams }: PageProps) {
|
||||
|
||||
export type PageUrlParams = ReturnType<typeof getPageUrlParams>;
|
||||
|
||||
export function makeCreateUrl({ path, urlSearchParams }: ReturnType<typeof getPageUrlParams>) {
|
||||
return function (route: string) {
|
||||
if (urlSearchParams) return `${path}${route}?${new URLSearchParams(urlSearchParams)}`;
|
||||
export function createUrl({
|
||||
path,
|
||||
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>;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user