Skip to content

Commit

Permalink
chore(frontend): adapt to normalized gql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Aug 28, 2024
1 parent 9115647 commit ecefef7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 53 deletions.
4 changes: 1 addition & 3 deletions apps/frontend/app/components/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ import { useQuery } from "@tanstack/react-query";
import type { ReactNode, Ref } from "react";
import type { DeepPartial } from "ts-essentials";
import { match } from "ts-pattern";
import { withQuery, withoutHost } from "ufo";
import { withQuery } from "ufo";
import { MEDIA_DETAILS_HEIGHT } from "~/components/common";
import { confirmWrapper } from "~/components/confirmation";
import {
clientGqlService,
dayjsLib,
getPartialMetadataDetailsQuery,
queryFactory,
redirectToQueryParam,
} from "~/lib/generals";
import {
useConfirmSubmit,
Expand Down Expand Up @@ -188,7 +187,6 @@ export const ReviewItemDisplay = (props: {
if (conf)
deleteReviewFetcher.submit(
{
[redirectToQueryParam]: withoutHost(location.href),
shouldDelete: "true",
reviewId: props.review.id || null,
},
Expand Down
14 changes: 0 additions & 14 deletions apps/frontend/app/lib/generals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
mergeQueryKeys,
} from "@lukemorales/query-key-factory";
import {
EntityLot,
MediaLot,
MediaSource,
MetadataDetailsDocument,
Expand Down Expand Up @@ -326,19 +325,6 @@ export const queryFactory = mergeQueryKeys(
miscellaneousQueryKeys,
);

export const convertEntityToIndividualId = (
entityId: string,
entityLot: EntityLot,
) => {
const metadataId = entityLot === EntityLot.Metadata ? entityId : undefined;
const metadataGroupId =
entityLot === EntityLot.MetadataGroup ? entityId : undefined;
const personId = entityLot === EntityLot.Person ? entityId : undefined;
const exerciseId = entityLot === EntityLot.Exercise ? entityId : undefined;
const workoutId = entityLot === EntityLot.Workout ? entityId : undefined;
return { metadataId, metadataGroupId, personId, exerciseId, workoutId };
};

export const getPartialMetadataDetailsQuery = (metadataId: string) =>
queryOptions({
queryKey: queryFactory.media.metadataPartialDetails(metadataId).queryKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
import { DisplayCollectionEntity, ReviewItemDisplay } from "~/components/media";
import {
clientGqlService,
convertEntityToIndividualId,
dayjsLib,
queryClient,
queryFactory,
Expand Down Expand Up @@ -128,18 +127,14 @@ export const action = unstable_defineAction(async ({ request }) => {
bulkRemove: async () => {
const submission = processSubmission(formData, bulkRemoveSchema);
for (const item of submission.items) {
const input = convertEntityToIndividualId(
item.entityId,
item.entityLot,
);
await serverGqlService.authenticatedRequest(
request,
RemoveEntityFromCollectionDocument,
{
input: {
...item,
collectionName: submission.collectionName,
creatorUserId: submission.creatorUserId,
...input,
},
},
);
Expand Down
12 changes: 3 additions & 9 deletions apps/frontend/app/routes/_dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import {
} from "@remix-run/react";
import {
CollectionExtraInformationLot,
EntityLot,
MediaLot,
type MetadataDetailsQuery,
type UserCollectionsListQuery,
Expand Down Expand Up @@ -1188,16 +1187,11 @@ const ReviewEntityForm = ({
closeReviewEntityModal();
}}
>
<input hidden name="entityId" value={entityToReview.entityId} readOnly />
<input
hidden
name={match(entityToReview.entityLot)
.with(EntityLot.Metadata, () => "metadataId")
.with(EntityLot.MetadataGroup, () => "metadataGroupId")
.with(EntityLot.Person, () => "personId")
.with(EntityLot.Collection, () => "collection")
.with(EntityLot.Exercise, () => "exerciseId")
.run()}
value={entityToReview.entityId}
name="entityLot"
value={entityToReview.entityLot}
readOnly
/>
{entityToReview.existingReview?.id ? (
Expand Down
33 changes: 12 additions & 21 deletions apps/frontend/app/routes/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ import invariant from "tiny-invariant";
import { match } from "ts-pattern";
import { z } from "zod";
import { zx } from "zodix";
import {
convertEntityToIndividualId,
redirectToQueryParam,
} from "~/lib/generals";
import { redirectToQueryParam } from "~/lib/generals";
import {
MetadataIdSchema,
MetadataSpecificsSchema,
Expand Down Expand Up @@ -154,8 +151,7 @@ export const action = unstable_defineAction(async ({ request }) => {
})
.with("addEntityToCollection", async () => {
removeCachedUserCollectionsList(request);
const [submission, input] =
getChangeCollectionToEntityVariables(formData);
const [submission] = getChangeCollectionToEntityVariables(formData);
const addTo = [submission.collectionName];
if (submission.collectionName === "Watchlist") addTo.push("Monitoring");
for (const co of addTo) {
Expand All @@ -164,7 +160,7 @@ export const action = unstable_defineAction(async ({ request }) => {
AddEntityToCollectionDocument,
{
input: {
...input,
...submission,
collectionName: co,
creatorUserId: submission.creatorUserId,
information: omitBy(submission.information || {}, isEmpty),
Expand All @@ -182,14 +178,13 @@ export const action = unstable_defineAction(async ({ request }) => {
})
.with("removeEntityFromCollection", async () => {
removeCachedUserCollectionsList(request);
const [submission, input] =
getChangeCollectionToEntityVariables(formData);
const [submission] = getChangeCollectionToEntityVariables(formData);
await serverGqlService.authenticatedRequest(
request,
RemoveEntityFromCollectionDocument,
{
input: {
...input,
...submission,
collectionName: submission.collectionName,
creatorUserId: submission.creatorUserId,
},
Expand All @@ -213,10 +208,13 @@ export const action = unstable_defineAction(async ({ request }) => {
}),
);
} else {
const entityId = submission.entityId;
const entityLot = submission.entityLot;
invariant(entityId && entityLot);
await serverGqlService.authenticatedRequest(
request,
PostReviewDocument,
{ input: submission },
{ input: { ...submission, entityId, entityLot } },
);
extendResponseHeaders(
headers,
Expand Down Expand Up @@ -464,11 +462,8 @@ const reviewSchema = z
text: z.string().optional(),
visibility: z.nativeEnum(Visibility).optional(),
isSpoiler: zx.CheckboxAsString.optional(),
metadataId: z.string().optional(),
metadataGroupId: z.string().optional(),
collectionId: z.string().optional(),
exerciseId: z.string().optional(),
personId: z.string().optional(),
entityId: z.string().optional(),
entityLot: z.nativeEnum(EntityLot).optional(),
reviewId: z.string().optional(),
})
.merge(MetadataSpecificsSchema);
Expand All @@ -478,11 +473,7 @@ const getChangeCollectionToEntityVariables = (formData: FormData) => {
formData,
changeCollectionToEntitySchema.passthrough(),
);
const individualIds = convertEntityToIndividualId(
submission.entityId,
submission.entityLot,
);
return [submission, individualIds] as const;
return [submission] as const;
};

const progressUpdateSchema = z
Expand Down

0 comments on commit ecefef7

Please sign in to comment.