apps/web: add Form overlay

This commit is contained in:
vchikalkin 2023-11-21 20:28:57 +03:00
parent d625353a19
commit 8cb4d9c6e5
10 changed files with 90 additions and 21 deletions

View File

@ -6,7 +6,7 @@ import { Button } from 'ui';
export function Buttons() {
const { reset, setValidation, values } = useFormStore();
const { createUrl } = useContext(FormContext);
const { createUrl, setFormStatus } = useContext(FormContext);
return (
<div className="grid grid-cols-1 gap-2 gap-x-4 md:grid-cols-3">
@ -18,7 +18,14 @@ export function Buttons() {
>
Отмена
</Button>
<Button intent="secondary">Возврат на доработку</Button>
<Button
intent="secondary"
onClick={() => {
setFormStatus('pending');
}}
>
Возврат на доработку
</Button>
<Button
onClick={() => {
apiIus.validate({ createUrl, payload: values }).then((res) => {

View File

@ -0,0 +1,33 @@
'use client';
import { FormContext } from './context/form-context';
import { CheckCircleIcon } from '@heroicons/react/24/solid';
import type { PropsWithChildren } from 'react';
import { useContext } from 'react';
import { Background, LoadingSpinner } from 'ui';
function OverlayWrapper({ children }: PropsWithChildren) {
return (
<Background className="lg:w-standard absolute grid h-full w-full items-center justify-center border-none bg-opacity-80 backdrop-blur-sm">
<div className="flex flex-row items-center gap-2">{children} </div>
</Background>
);
}
export function Overlay() {
const { formStatus } = useContext(FormContext);
if (formStatus === 'pending')
return (
<OverlayWrapper>
{LoadingSpinner} <p className="font-medium">Загрузка...</p>
</OverlayWrapper>
);
if (formStatus === 'success')
return (
<OverlayWrapper>
<CheckCircleIcon className="h-10 w-10 fill-green-500" title="OK" />{' '}
<p className="font-medium">Данные сохранены</p>
</OverlayWrapper>
);
return false;
}

View File

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

View File

@ -3,6 +3,7 @@ import { Buttons } from './Buttons';
import { FormContext, FormContextProvider } from './context/form-context';
import { Elements } from './Elements';
import { Header } from './Header';
import { Overlay } from './Overlay';
import type { Props } from './types';
import { makeCreateUrl } from '@/utils/url';
import type { FC } from 'react';
@ -14,7 +15,8 @@ function Content(props: Props) {
const { createUrl } = useContext(FormContext);
return (
<Background className="lg:w-standard grid w-full gap-2 p-5">
<Background className="lg:w-standard relative grid w-full gap-2 p-5">
<Overlay />
<Header title={title} link={'/ius' + createUrl('/conditions')} />
<Elements {...props} />
<Divider />

View File

@ -1,4 +1,5 @@
'use client';
import { ArrowLongLeftIcon } from '@heroicons/react/24/solid';
import { useRouter } from 'next/navigation';
export function Controls() {
@ -14,20 +15,7 @@ export function Controls() {
router.back();
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth="1.5"
stroke="currentColor"
className="h-6 w-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6.75 15.75L3 12m0 0l3.75-3.75M3 12h18"
/>
</svg>
<ArrowLongLeftIcon className="h-5 w-5" />
Назад
</button>
</div>

View File

@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@heroicons/react": "^2.0.18",
"next": "^14.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",

View File

@ -6,7 +6,7 @@ type Props = {
export function Header({ children }: Props) {
return (
<header className="sticky top-0 flex h-16 w-full justify-center border-b border-slate-100 bg-white bg-opacity-80 backdrop-blur-sm">
<header className="sticky top-0 z-50 flex h-16 w-full justify-center border-b border-slate-100 bg-white bg-opacity-80 backdrop-blur-sm">
<div className="lg:w-standard m-0 flex w-full items-center justify-between px-5">
{children}
</div>

21
packages/ui/icons.tsx Normal file
View File

@ -0,0 +1,21 @@
export const LoadingSpinner = (
<div role="status">
<svg
aria-hidden="true"
className="fill-primary-500 h-8 w-8 animate-spin text-gray-200"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);

View File

@ -9,5 +9,6 @@ export * from './divider';
export * from './header';
export * from './heading';
export * from './http-error';
export * from './icons';
export * from './input';
export * from './select';

View File

@ -510,6 +510,11 @@
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.2.0.tgz#5f3d96ec6b2354ad6d8a28bf216a1d97b5426861"
integrity sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==
"@heroicons/react@^2.0.18":
version "2.0.18"
resolved "https://registry.yarnpkg.com/@heroicons/react/-/react-2.0.18.tgz#f80301907c243df03c7e9fd76c0286e95361f7c1"
integrity sha512-7TyMjRrZZMBPa+/5Y8lN0iyvUU/01PeMGX2+RE7cQWpEUIcb4QotzUObFkJDejj/HUH4qjP/eQ0gzzKs2f+6Yw==
"@humanwhocodes/config-array@^0.11.13":
version "0.11.13"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.13.tgz#075dc9684f40a531d9b26b0822153c1e832ee297"