Skip to content

Commit

Permalink
Set up front-end to read and render actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Haube committed Oct 31, 2023
1 parent c0edbc5 commit 7eda16b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/packages/shared-types/opensearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SeaToolTransform } from "./seatool";
import { OneMacTransform } from "./onemac";
import { Action } from "./actions";

export type OsHit<T> = {
_index: string;
Expand Down Expand Up @@ -34,6 +35,10 @@ export type OsResponse<T> = {
export type OsMainSourceItem = OneMacTransform & SeaToolTransform;
export type OsMainSearchResponse = OsResponse<OsMainSourceItem>;
export type SearchData = OsHits<OsMainSourceItem>;
export type ItemResult = OsHit<OsMainSourceItem> & {
found: boolean;
actions: Action[];
};

export type OsFilterType =
| "term"
Expand Down
14 changes: 2 additions & 12 deletions src/services/api/handlers/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,21 @@ import {
getStateFilter,
lookupUserAttributes,
} from "../libs/auth/user";
import {
OsHit,
OsMainSourceItem,
Action,
CognitoUserAttributes,
} from "shared-types";
import { Action, CognitoUserAttributes, ItemResult } from "shared-types";
import { isCmsUser } from "shared-utils";

if (!process.env.osDomain) {
throw "ERROR: osDomain env variable is required,";
}

type ItemResult = OsHit<OsMainSourceItem> & {
found: boolean;
actions: Action[];
};

/** Generates an array of allowed actions from a combination of user attributes
* and OS result data */
const packageActionsForResult = (
user: CognitoUserAttributes,
result: ItemResult
): Action[] => {
const actions = [];
if (isCmsUser(user)) {
if (isCmsUser(user) && result._source.raiReceivedDate) {
actions.push(Action.ENABLE_RAI_WITHDRAW);
}
return actions;
Expand Down
13 changes: 9 additions & 4 deletions src/services/ui/src/api/useGetItem.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { OsHit, OsMainSourceItem, ReactQueryApiError } from "shared-types";
import {
ItemResult,
OsHit,
OsMainSourceItem,
ReactQueryApiError,
} from "shared-types";

export const getItem = async (id: string): Promise<OsHit<OsMainSourceItem>> => {
export const getItem = async (id: string): Promise<ItemResult> => {
const record = await API.post("os", "/item", { body: { id } });

return record;
};

export const useGetItem = (
id: string,
options?: UseQueryOptions<OsHit<OsMainSourceItem>, ReactQueryApiError>
options?: UseQueryOptions<ItemResult, ReactQueryApiError>
) => {
return useQuery<OsHit<OsMainSourceItem>, ReactQueryApiError>(
return useQuery<ItemResult, ReactQueryApiError>(
["record", id],
() => getItem(id),
options
Expand Down
59 changes: 40 additions & 19 deletions src/services/ui/src/pages/detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import {
SubmissionInfo,
} from "@/components";
import { useGetUser } from "@/api/useGetUser";
import { OsHit, OsMainSourceItem } from "shared-types";
import { Action, ItemResult, OsHit, OsMainSourceItem } from "shared-types";
import { useQuery } from "@/hooks";
import { useGetItem } from "@/api";
import { DetailNav } from "./detailNav";
import { BreadCrumbs } from "@/components/BreadCrumb";
import { BREAD_CRUMB_CONFIG_PACKAGE_DETAILS } from "@/components/BreadCrumb/bread-crumb-config";
import { mapActionLabel } from "@/utils";
import { Link } from "react-router-dom";
import { ROUTES } from "@/routes";

export const DetailsContent = ({
data,
}: {
data?: OsHit<OsMainSourceItem>;
}) => {
export const DetailsContent = ({ data }: { data?: ItemResult }) => {
const { data: user } = useGetUser();
if (!data?._source) return <LoadingSpinner />;
return (
Expand All @@ -46,20 +45,42 @@ export const DetailsContent = ({
))}
</aside>
<div className="flex-1">
<section id="package-overview" className="block md:flex mb-8 gap-8">
<CardWithTopBorder>
<div className="p-4">
<p className="text-gray-600 font-semibold mb-2">Status</p>
<div>
<h2 className="text-xl font-semibold mb-2">
{user?.isCms
? data._source.cmsStatus
: data._source.stateStatus}
</h2>
<div className="flex gap-8">
<section id="package-overview" className="block md:flex mb-8 gap-8">
<CardWithTopBorder>
<div className="p-4">
<p className="text-gray-600 font-semibold mb-2">Status</p>
<div>
<h2 className="text-xl font-semibold mb-2">
{user?.isCms
? data._source.cmsStatus
: data._source.stateStatus}
</h2>
</div>
</div>
</div>
</CardWithTopBorder>
</section>
</CardWithTopBorder>
</section>
<section id="package-actions" className="block md:flex mb-8 gap-8">
<CardWithTopBorder>
<div className="p-4">
<p className="text-gray-600 font-semibold mb-2">Actions</p>
<div>
<ul>
{data.actions.map((action, idx) => (
<Link
className="text-sky-500 underline"
to={ROUTES.DASHBOARD}
key={`${idx}-${action}`}
>
<li>{mapActionLabel(action)}</li>
</Link>
))}
</ul>
</div>
</div>
</CardWithTopBorder>
</section>
</div>
<DetailsSection id="package-details" title="Package Details">
<ChipSpaPackageDetails {...data?._source} />
</DetailsSection>
Expand Down
9 changes: 9 additions & 0 deletions src/services/ui/src/utils/actionLabelMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Action } from "shared-types";
export const mapActionLabel = (a: Action) => {
switch (a) {
case Action.ENABLE_RAI_WITHDRAW:
return "Enable RAI Response Withdraw";
default:
return null;
}
};
1 change: 1 addition & 0 deletions src/services/ui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./user";
export * from "./date";
export * from "./textHelpers";
export * from "./createContextProvider";
export * from "./actionLabelMapper";

0 comments on commit 7eda16b

Please sign in to comment.