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

Fix: Bugs in valueset page #10738

Merged
merged 15 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,7 @@
"save_and_continue": "Save and Continue",
"save_investigation": "Save Investigation",
"saving": "Saving...",
"retired":"Retired",
"scan_asset_qr": "Scan Asset QR!",
"schedule": "Schedule",
"schedule_an_appointment_or_create_a_new_encounter": "Schedule an appointment or create a new encounter",
Expand Down Expand Up @@ -2341,6 +2342,7 @@
"valid_to": "Valid Till",
"valid_year_of_birth": "Please enter a valid year of birth (YYYY)",
"value_set": "Value Set",
"save_valueset":"Save ValueSet",
"valuesets": "Valuesets",
"vehicle_preference": "Vehicle preference",
"vendor_name": "Vendor Name",
Expand Down
10 changes: 5 additions & 5 deletions src/components/ValueSet/ValueSetEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "raviger";
import { toast } from "sonner";

Expand All @@ -9,7 +9,6 @@ import query from "@/Utils/request/query";
import {
CreateValuesetModel,
UpdateValuesetModel,
ValuesetBase,
ValuesetFormType,
} from "@/types/valueset/valueset";
import valuesetApi from "@/types/valueset/valuesetApi";
Expand All @@ -22,7 +21,7 @@ interface ValueSetEditorProps {

export function ValueSetEditor({ slug }: ValueSetEditorProps) {
const navigate = useNavigate();

const queryClient = useQueryClient();
// Fetch existing valueset if we're editing
const { data: existingValueset, isLoading } = useQuery({
queryKey: ["valueset", slug],
Expand All @@ -35,9 +34,9 @@ export function ValueSetEditor({ slug }: ValueSetEditorProps) {
// Create mutation
const createMutation = useMutation({
mutationFn: mutate(valuesetApi.create),
onSuccess: (data: ValuesetBase) => {
onSuccess: () => {
toast.success("ValueSet created successfully");
navigate(`/valuesets/${data.slug}`);
navigate(`/admin/valuesets`);
},
});

Expand All @@ -49,6 +48,7 @@ export function ValueSetEditor({ slug }: ValueSetEditorProps) {
onSuccess: () => {
toast.success("ValueSet updated successfully");
navigate(`/admin/valuesets`);
queryClient.invalidateQueries({ queryKey: ["valueset"] });
},
});

Expand Down
127 changes: 66 additions & 61 deletions src/components/ValueSet/ValueSetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { PlusIcon, TrashIcon, UpdateIcon } from "@radix-ui/react-icons";
import { useMutation } from "@tanstack/react-query";
import { useFieldArray, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import * as z from "zod";

Expand Down Expand Up @@ -34,59 +35,6 @@ import {
import valuesetApi from "@/types/valueset/valuesetApi";

// Create a schema for form validation
const valuesetFormSchema = z.object({
name: z.string().min(1, "Name is required"),
slug: z.string().min(1, "Slug is required"),
description: z.string(),
status: z.enum(["active", "inactive"]),
is_system_defined: z.boolean(),
compose: z.object({
include: z.array(
z.object({
system: z.string(),
concept: z
.array(
z.object({
code: z.string(),
display: z.string(),
}),
)
.optional(),
filter: z
.array(
z.object({
property: z.string(),
op: z.string(),
value: z.string(),
}),
)
.optional(),
}),
),
exclude: z.array(
z.object({
system: z.string(),
concept: z
.array(
z.object({
code: z.string(),
display: z.string(),
}),
)
.optional(),
filter: z
.array(
z.object({
property: z.string(),
op: z.string(),
value: z.string(),
}),
)
.optional(),
}),
),
}),
});

interface ValueSetFormProps {
initialData?: ValuesetFormType;
Expand Down Expand Up @@ -396,6 +344,61 @@ export function ValueSetForm({
onSubmit,
isSubmitting,
}: ValueSetFormProps) {
const { t } = useTranslation();

const valuesetFormSchema = z.object({
name: z.string().min(1, t("field_required")),
slug: z.string().min(1, t("field_required")),
description: z.string(),
status: z.enum(["active", "draft", "retired", "unknown"]),
is_system_defined: z.boolean(),
compose: z.object({
include: z.array(
z.object({
system: z.string(),
concept: z
.array(
z.object({
code: z.string(),
display: z.string(),
}),
)
.optional(),
filter: z
.array(
z.object({
property: z.string(),
op: z.string(),
value: z.string(),
}),
)
.optional(),
}),
),
exclude: z.array(
z.object({
system: z.string(),
concept: z
.array(
z.object({
code: z.string(),
display: z.string(),
}),
)
.optional(),
filter: z
.array(
z.object({
property: z.string(),
op: z.string(),
value: z.string(),
}),
)
.optional(),
}),
),
}),
});
const form = useForm<ValuesetFormType>({
resolver: zodResolver(valuesetFormSchema),
defaultValues: {
Expand All @@ -419,7 +422,7 @@ export function ValueSetForm({
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormLabel required>{t("name")}</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
Expand All @@ -433,7 +436,7 @@ export function ValueSetForm({
name="slug"
render={({ field }) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<FormLabel required>{t("slug")}</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
Expand All @@ -447,7 +450,7 @@ export function ValueSetForm({
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormLabel>{t("description")}</FormLabel>
<FormControl>
<Textarea {...field} />
</FormControl>
Expand All @@ -461,16 +464,18 @@ export function ValueSetForm({
name="status"
render={({ field }) => (
<FormItem>
<FormLabel>Status</FormLabel>
<FormLabel required>{t("status")}</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select status" />
<SelectValue placeholder="Select Status" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="active">Active</SelectItem>
<SelectItem value="inactive">Inactive</SelectItem>
<SelectItem value="active">{t("active")}</SelectItem>
<SelectItem value="draft">{t("draft")}</SelectItem>
<SelectItem value="retired">{t("retired")}</SelectItem>
<SelectItem value="unknown">{t("unknown")}</SelectItem>
</SelectContent>
</Select>
<FormMessage />
Expand All @@ -484,7 +489,7 @@ export function ValueSetForm({
</div>

<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Saving..." : "Save ValueSet"}
{isSubmitting ? t("saving") : t("save_valueset")}
</Button>
</form>
</Form>
Expand Down
Loading
Loading