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

Use mudder to rank form elements and related pub values #996

Merged
merged 48 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7e96f51
Add ranks to form elements
kalilsn Feb 25, 2025
6e01fce
Add rank to pub values
kalilsn Feb 25, 2025
027853a
Set rank for form elements with mudder when creating new forms
kalilsn Feb 25, 2025
d2fe54b
Merge branch 'main' into kalilsn/sorting
kalilsn Feb 25, 2025
68406d1
Use mudder rank instead of order in form builder
kalilsn Feb 25, 2025
6571462
Fix test expectation
kalilsn Feb 25, 2025
f43c255
Sort pub values by rank
kalilsn Feb 25, 2025
69f0f95
Fix bugs in related value ranking
kalilsn Feb 25, 2025
6437e39
Merge branch 'main' into kalilsn/sorting
kalilsn Feb 25, 2025
ee6700e
Fix form builder sort bugs
kalilsn Feb 26, 2025
aaf0f0a
Pass transaction to getPubType
kalilsn Feb 26, 2025
1e0850b
Fix off by one error
kalilsn Feb 26, 2025
f3b06bf
Condense migrations
kalilsn Feb 26, 2025
c021a34
Set collation order at field level rather than on query
kalilsn Feb 26, 2025
0ff5ea0
Improve form builder readability
kalilsn Feb 26, 2025
91c7dcf
Cleanup
kalilsn Feb 26, 2025
0344bb2
Merge branch 'main' into kalilsn/sorting
kalilsn Feb 26, 2025
31e4822
Make keyboard interactions work on sortable form elements
kalilsn Mar 3, 2025
059d430
Fix missing hover state on form elements delete/restore buttons
kalilsn Mar 3, 2025
abfbdef
Add test for form element sorting
kalilsn Mar 3, 2025
58ce93e
Set config.label on element creation for proper default label
kalilsn Mar 3, 2025
9693c08
Actually show label changes in form builder
kalilsn Mar 3, 2025
ebe6b10
Set default label correctly for relationship elements
kalilsn Mar 3, 2025
4c19037
Set correct input component for relationship fields on form creation
kalilsn Mar 3, 2025
594105e
Set default config.label when adding a new element to a form
kalilsn Mar 3, 2025
c4d8159
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 3, 2025
b8096b8
Make sure config.relationshipConfig.component is set for relationship…
kalilsn Mar 3, 2025
4e486f7
Update test now that label updates are actually reflected in the form…
kalilsn Mar 3, 2025
61d5372
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 3, 2025
5c0536a
Improve UX for adding relationship form element
kalilsn Mar 3, 2025
cfa81e2
Prevent fallback to bad slug value when rendering unconfigured relati…
kalilsn Mar 3, 2025
e19947e
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 3, 2025
536c87b
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 4, 2025
ce13fed
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 4, 2025
72a7c8f
Make sure mudder table in migrations is 0 indexed
kalilsn Mar 4, 2025
f993aa5
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 4, 2025
e8ff6b9
chore: PR preview fixes (#1021)
3mcd Mar 4, 2025
b3a3698
Sort get pubs result in js
kalilsn Mar 4, 2025
328ef0d
Fix merge mistake
kalilsn Mar 4, 2025
62fadeb
Add ranks to values in datacite test
kalilsn Mar 4, 2025
585e6cc
Allow tabbing to form element buttons
kalilsn Mar 5, 2025
9207292
Fix arcadia seed
kalilsn Mar 5, 2025
2121ea8
Remove unnecessary sorting
kalilsn Mar 5, 2025
d5fedc6
Sort pub values in the database
kalilsn Mar 5, 2025
8ba8fd6
Remove rank from returned values
kalilsn Mar 5, 2025
eb4aee6
Update tests to ignore return order of pub values
kalilsn Mar 5, 2025
7f74fc6
Merge branch 'main' into kalilsn/sorting
kalilsn Mar 5, 2025
1d0cda0
Update sort test to account for new drag handle setup
kalilsn Mar 5, 2025
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
6 changes: 5 additions & 1 deletion .github/workflows/on_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}

deploy-preview:
permissions: write-all
permissions:
contents: read
deployments: write
pull-requests: write
statuses: write
runs-on: ubuntu-latest
timeout-minutes: 30
needs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import type { ColumnDef } from "@tanstack/react-table";

import Link from "next/link";

import type { PubsId } from "db/public";
import { Badge } from "ui/badge";
import { DataTableColumnHeader } from "ui/data-table";
import { HoverCard, HoverCardContent, HoverCardTrigger } from "ui/hover-card";

import type { PubTitleProps } from "~/lib/pubs";
import { PubTitle } from "~/app/components/PubTitle";

export type ActionRun = {
id: string;
createdAt: Date;
actionInstance: { name: string; action: string } | null;
stage: { id: string; name: string } | null;
pub: {
id: string;
values: { field: { slug: string }; value: unknown }[] | Record<string, unknown>;
createdAt: Date;
pubType: { name: string };
title: string | null;
} | null;
pub: PubTitleProps & { id: PubsId };
result: unknown;
} & (
| {
Expand Down
16 changes: 1 addition & 15 deletions core/app/c/[communitySlug]/activity/actions/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Metadata } from "next";

import { notFound, redirect } from "next/navigation";
import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/postgres";
import { jsonObjectFrom } from "kysely/helpers/postgres";

import { Capabilities, MembershipType } from "db/public";

Expand Down Expand Up @@ -76,20 +76,6 @@ export default async function Page(props: {
.selectFrom("pubs")
.select(["pubs.id", "pubs.createdAt", "pubs.title"])
.whereRef("pubs.id", "=", "action_runs.pubId")
.select((eb) =>
jsonArrayFrom(
eb
.selectFrom("pub_values")
.leftJoin("pub_fields", "pub_values.fieldId", "pub_fields.id")
.select([
"pub_values.value",
"pub_fields.name as fieldName",
"pub_fields.schemaName as schemaName",
"pub_fields.slug as fieldSlug",
])
.whereRef("pub_values.pubId", "=", "pubs.id")
).as("values")
)
.select((eb) => pubType({ eb, pubTypeIdRef: "pubs.pubTypeId" }))
).as("pub"),
jsonObjectFrom(
Expand Down
4 changes: 3 additions & 1 deletion core/app/c/[communitySlug]/forms/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assert } from "utils";

import { isUniqueConstraintError } from "~/kysely/errors";
import { getLoginData } from "~/lib/authentication/loginData";
import { getPubType } from "~/lib/server";
import { autoRevalidate } from "~/lib/server/cache/autoRevalidate";
import { findCommunityBySlug } from "~/lib/server/community";
import { defineServerAction } from "~/lib/server/defineServerAction";
Expand All @@ -27,8 +28,9 @@ export const createForm = defineServerAction(async function createForm(
}

try {
const pubType = await getPubType(pubTypeId).executeTakeFirstOrThrow();
await autoRevalidate(
insertForm(pubTypeId, name, slug, communityId, false)
insertForm(pubType, name, slug, communityId, false)
).executeTakeFirstOrThrow();
} catch (error) {
if (isUniqueConstraintError(error)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ const memberFields = (pubId: Expression<string>) =>
.whereRef("pub_values.pubId", "=", pubId)
.where("pub_fields.schemaName", "=", CoreSchemaType.MemberId)
.distinctOn("pub_fields.id")
.orderBy(["pub_fields.id", "pub_values.createdAt desc"])
.orderBy(["pub_fields.id", "pub_values.updatedAt desc"])
);

const pubType = (pubTypeId: Expression<string>) =>
jsonObjectFrom(getPubTypeBase().whereRef("pub_types.id", "=", pubTypeId));

export const getPubChildrenTable = (parentId: PubsId, selectedPubTypeId?: PubTypesId) => {
return autoCache(
db
Expand Down
8 changes: 5 additions & 3 deletions core/app/c/[communitySlug]/types/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isUniqueConstraintError } from "~/kysely/errors";
import { getLoginData } from "~/lib/authentication/loginData";
import { userCan } from "~/lib/authorization/capabilities";
import { defaultFormName, defaultFormSlug } from "~/lib/form";
import { ApiError } from "~/lib/server";
import { ApiError, getPubType } from "~/lib/server";
import { autoRevalidate } from "~/lib/server/cache/autoRevalidate";
import { findCommunityBySlug } from "~/lib/server/community";
import { defineServerAction } from "~/lib/server/defineServerAction";
Expand Down Expand Up @@ -157,7 +157,7 @@ export const createPubType = defineServerAction(async function createPubType(
}
try {
await db.transaction().execute(async (trx) => {
const pubType = await autoRevalidate(
const { id: pubTypeId } = await autoRevalidate(
trx
.with("newType", (db) =>
db
Expand All @@ -180,9 +180,11 @@ export const createPubType = defineServerAction(async function createPubType(
.returning("B as id")
).executeTakeFirstOrThrow();

const pubType = await getPubType(pubTypeId, trx).executeTakeFirstOrThrow();

await autoRevalidate(
insertForm(
pubType.id,
pubType,
defaultFormName(name),
defaultFormSlug(name),
communityId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import mudder from "mudder";
import { useForm, useFormContext } from "react-hook-form";
import { z } from "zod";

Expand Down Expand Up @@ -27,7 +28,7 @@ import { ChevronDown } from "ui/icon";
import { Input } from "ui/input";
import { cn } from "utils";

import type { ButtonElement, FormBuilderSchema } from "../types";
import type { FormBuilderSchema } from "../types";
import { useCommunity } from "../../providers/CommunityProvider";
import { useFormBuilder } from "../FormBuilderContext";
import { ButtonOption } from "../SubmissionSettings";
Expand All @@ -48,7 +49,7 @@ export const ButtonConfigurationForm = ({
// This uses the parent's form context to get the most up to date version of 'elements'
const { getValues } = useFormContext<FormBuilderSchema>();
// Derive some initial values based on the state of the parent form when this panel was opened
const { button, buttonIndex, otherButtons, numElements } = useMemo(() => {
const { button, buttonIndex, otherButtons, numElements, elements } = useMemo(() => {
const elements = getValues()["elements"];
// Because a button might not have an ID yet (if it wasn't saved to the db yet) fall back to its label as an identifier
const buttonIndex = buttonIdentifier
Expand All @@ -66,7 +67,7 @@ export const ButtonConfigurationForm = ({
e.elementId !== buttonIdentifier &&
e.label !== buttonIdentifier
);
return { button, buttonIndex, otherButtons, numElements: elements.length };
return { button, buttonIndex, otherButtons, numElements: elements.length, elements };
}, []);

const schema = z.object({
Expand Down Expand Up @@ -102,7 +103,11 @@ export const ButtonConfigurationForm = ({
const onSubmit = (values: z.infer<typeof schema>) => {
const index = buttonIndex === -1 ? numElements : buttonIndex;
update(index, {
order: null,
rank: mudder.base62.mudder(
elements[index - 1]?.rank ?? "",
elements[index + 1]?.rank ?? "",
1
)[0],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generates a rank at the end of the form elements. It's not used right now since button elements aren't ordered in the form. But maybe the should/will be eventually? In any case, I did this because I wanted to make the rank column non-nullable. Should I use a constant string here instead of the mudder calculation?

type: ElementType.button,
elementId: button?.elementId,
label: values.label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export const InputComponentConfigurationForm = ({ index, fieldInputElement }: Pr
data-testid="save-configuration-button"
type="submit"
className="bg-blue-500 hover:bg-blue-600"
disabled={!form.formState.isDirty}
disabled={!form.formState.isDirty && fieldInputElement.configured}
>
Save
</Button>
Expand Down
25 changes: 18 additions & 7 deletions core/app/components/FormBuilder/ElementPanel/SelectElement.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mudder from "mudder";
import { useFormContext } from "react-hook-form";
import { defaultComponent, SCHEMA_TYPES_WITH_ICONS } from "schemas";
import { defaultComponent } from "schemas";

import { ElementType, StructuralFormElement } from "db/public";
import { ElementType, InputComponent, StructuralFormElement } from "db/public";
import { Button } from "ui/button";
import { Type } from "ui/icon";
import { Input } from "ui/input";
import { usePubFieldContext } from "ui/pubFields";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "ui/tabs";
Expand All @@ -18,9 +18,9 @@ export const SelectElement = ({ panelState }: { panelState: PanelState }) => {

const { elementsCount, dispatch, addElement } = useFormBuilder();
const { getValues } = useFormContext();
const elements: FormElementData[] = getValues()["elements"];

const fieldButtons = Object.values(fields).map((field) => {
const elements: FormElementData[] = getValues()["elements"];
const usedFields = elements.map((e) => e.fieldId);
if (
usedFields.includes(field.id) ||
Expand Down Expand Up @@ -49,9 +49,16 @@ export const SelectElement = ({ panelState }: { panelState: PanelState }) => {
fieldId: field.id,
required: true,
type: ElementType.pubfield,
order: elementsCount,
rank: mudder.base62.mudder(elements[elementsCount - 1].rank, "", 1)[0],
configured: false,
label: field.name,
config: field.isRelation
? {
relationshipConfig: {
label: field.name,
component: InputComponent.relationBlock,
},
}
: { label: field.name },
component,
schemaName,
isRelation: field.isRelation,
Expand Down Expand Up @@ -130,7 +137,11 @@ export const SelectElement = ({ panelState }: { panelState: PanelState }) => {
addElement({
element: elementType,
type: ElementType.structural,
order: elementsCount,
rank: mudder.base62.mudder(
elements[elementsCount - 1].rank,
"",
1
)[0],
configured: false,
});
dispatch({
Expand Down
Loading
Loading