Skip to content

Commit

Permalink
fix: clean-up result page rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jbottigliero committed Apr 16, 2024
1 parent 11d5e32 commit df3947a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 54 deletions.
25 changes: 12 additions & 13 deletions src/app/results/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ import { search } from "@globus/sdk";

import { GMetaResult } from "../page";

export default function ResultPage() {
const router = useRouter();
const ClientSideResult = () => {
const params = useSearchParams();
const subject = params.get("subject");

const [result, setResult] = useState<GMetaResult>();
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
async function fetchResult() {
const response = await (
Expand All @@ -37,15 +33,24 @@ export default function ResultPage() {
},
})
).json();
setIsLoading(false);
setResult(response);
}

fetchResult();
}, [subject]);
return <Result result={result} />;
};

export default function ResultPage() {
const router = useRouter();
return (
<Container maxW="container.xl" p={4}>
<Link onClick={() => router.back()}>
<Flex alignItems="center" mb={4}>
<ChevronLeftIcon /> <Text fontSize="sm">Back</Text>
</Flex>
</Link>
<Divider my={2} />
<Suspense
fallback={
<Center>
Expand All @@ -59,13 +64,7 @@ export default function ResultPage() {
</Center>
}
>
<Link onClick={() => router.back()}>
<Flex alignItems="center" mb={4}>
<ChevronLeftIcon /> <Text fontSize="sm">Back</Text>
</Flex>
</Link>
<Divider my={2} />
<Result result={result} isLoading={isLoading} />
<ClientSideResult />
</Suspense>
</Container>
);
Expand Down
52 changes: 11 additions & 41 deletions src/components/Result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import {
DrawerCloseButton,
useDisclosure,
Divider,
Skeleton,
AlertIcon,
AlertTitle,
AlertDescription,
Spinner,
Center,
Alert,
} from "@chakra-ui/react";
import { getAttribute, getAttributeFrom } from "../../static";
Expand Down Expand Up @@ -89,11 +86,9 @@ const FieldValue = ({ value }: { value: unknown }) => {
const Field = ({
field,
gmeta,
isLoading,
}: {
field: FieldDefinition;
gmeta: GMetaResult;
isLoading: boolean;
}) => {
const processedField =
typeof field === "string" ? { label: undefined, property: field } : field;
Expand All @@ -108,34 +103,15 @@ const Field = ({
{processedField.label}
</Heading>
)}
<Skeleton isLoaded={!isLoading}>
<FieldValue value={value} />
</Skeleton>
<FieldValue value={value} />
</Box>
);
};

export default function Result({
result,
isLoading,
}: {
result?: GMetaResult | GError;
isLoading: boolean;
}) {
export default function Result({ result }: { result?: GMetaResult | GError }) {
if (!result) {
return (
<Center>
<Spinner
thickness="4px"
speed="0.65s"
emptyColor="gray.200"
color="blue.500"
size="xl"
/>
</Center>
);
return null;
}

if (result["@datatype"] === "GError") {
return (
<Alert
Expand Down Expand Up @@ -167,11 +143,9 @@ export default function Result({

return (
<>
<Skeleton isLoaded={!isLoading}>
<Heading as="h1" size="md" color="brand">
{heading}
</Heading>
</Skeleton>
<Heading as="h1" size="md" color="brand">
{heading}
</Heading>

<Divider my={2} />

Expand All @@ -182,14 +156,12 @@ export default function Result({
<Heading as="h2" size="sm" my={2}>
Summary
</Heading>
<Skeleton isLoaded={!isLoading}>
<Text as="p">{summary}</Text>
</Skeleton>
<Text as="p">{summary}</Text>
</Box>
)}

{fields.map((field: any, i: number) => (
<Field key={i} field={field} gmeta={result} isLoading={isLoading} />
<Field key={i} field={field} gmeta={result} />
))}

{/*
Expand Down Expand Up @@ -265,11 +237,9 @@ export default function Result({
))}
</Box> */}

{!isLoading && (
<ResponseDrawer>
<Code as="pre">{JSON.stringify(result, null, 2)}</Code>
</ResponseDrawer>
)}
<ResponseDrawer>
<Code as="pre">{JSON.stringify(result, null, 2)}</Code>
</ResponseDrawer>
</Box>
</Flex>
</>
Expand Down

0 comments on commit df3947a

Please sign in to comment.