Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(read-only): add read only mode to webforms #329

Merged
merged 9 commits into from
Jan 26, 2024
4 changes: 2 additions & 2 deletions src/services/ui/src/components/Webform/footer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const Footer = () => {
return (
<div className="flex flex-col gap-2 my-6 text-sm text-slate-500">
<footer className="flex flex-col gap-2 my-6 text-sm text-slate-500">
daniel-belcher marked this conversation as resolved.
Show resolved Hide resolved
<b>{"PRA Disclosure Statement"}</b>
<p>
{
Expand Down Expand Up @@ -31,6 +31,6 @@ export const Footer = () => {
"CMS, 7500 Security Boulevard, Attn: PRA Reports Clearance Officer, Mail Stop C4-26-05, Baltimore, Maryland 21244-1850."
}
</p>
</div>
</footer>
);
};
74 changes: 53 additions & 21 deletions src/services/ui/src/components/Webform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useGetForm } from "@/api";
import { LoadingSpinner } from "@/components";
import { Footer } from "./footer";
import { Link, useParams } from "../Routing";
import { useReadOnlyUser } from "./useReadOnlyUser";

export const Webforms = () => {
return (
Expand Down Expand Up @@ -53,15 +54,23 @@ export const Webforms = () => {
);
};

export function Webform() {
const { id, version } = useParams("/webform/:id/:version");

const { data, isLoading, error } = useGetForm(id as string, version);
interface WebformBodyProps {
id: string;
version: string;
data: any;
readonly: boolean;
values: any;
}

const defaultValues = data ? documentInitializer(data) : {};
const savedData = localStorage.getItem(`${id}v${version}`);
function WebformBody({
version,
id,
data,
values,
readonly,
}: WebformBodyProps) {
const form = useForm({
defaultValues: savedData ? JSON.parse(savedData) : defaultValues,
defaultValues: values,
});

const onSave = () => {
Expand All @@ -88,6 +97,36 @@ export function Webform() {
}
);

return (
<div className="max-w-screen-xl mx-auto p-4 py-8 lg:px-8">
<Form {...form}>
<form onSubmit={onSubmit} className="space-y-6">
<fieldset disabled={readonly}>
<RHFDocument document={data} {...form} />
{!readonly && (
<div className="flex justify-between text-blue-700 underline">
<Button type="button" onClick={onSave} variant="ghost">
Save draft
</Button>
<Button type="submit">Submit</Button>
</div>
)}
</fieldset>
</form>
</Form>
<Footer />
</div>
);
}

export function Webform() {
const { id, version } = useParams("/webform/:id/:version");

const { data, isLoading, error } = useGetForm(id as string, version);
const readonly = useReadOnlyUser();
const defaultValues = data ? documentInitializer(data) : {};
const savedData = localStorage.getItem(`${id}v${version}`);

if (isLoading) return <LoadingSpinner />;
if (error || !data) {
return (
Expand All @@ -98,19 +137,12 @@ export function Webform() {
}

return (
<div className="max-w-screen-xl mx-auto p-4 py-8 lg:px-8">
<Form {...form}>
<form onSubmit={onSubmit} className="space-y-6">
<RHFDocument document={data} {...form} />
<div className="flex justify-between text-blue-700 underline">
<Button type="button" onClick={onSave} variant="ghost">
Save draft
</Button>
<Button type="submit">Submit</Button>
</div>
</form>
</Form>
<Footer />
</div>
<WebformBody
data={data}
readonly={readonly}
id={id}
version={version}
values={savedData ? JSON.parse(savedData) : defaultValues}
/>
);
}
8 changes: 8 additions & 0 deletions src/services/ui/src/components/Webform/useReadOnlyUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useGetUser } from "@/api/useGetUser";
import { UserRoles } from "shared-types";

export const useReadOnlyUser = () => {
const { data } = useGetUser();
const role = data?.user?.["custom:cms-roles"];
return role !== UserRoles.STATE_SUBMITTER;
};
Loading