Skip to content

feat: display specific errors if templates page fails #4023

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

Merged
merged 10 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Format
  • Loading branch information
presleyp committed Sep 12, 2022
commit 302b8534ba34b99288622589225965fd0d47c7bb
2 changes: 1 addition & 1 deletion site/src/i18n/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export const en = {
workspacePage,
auditLog,
templatePage,
templatesPage
templatesPage,
}
4 changes: 3 additions & 1 deletion site/src/pages/TemplatesPage/TemplatesPageView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ export const EmptyCannotCreate = Template.bind({})
EmptyCannotCreate.args = {}

export const Error = Template.bind({})
Error.args = { getTemplatesError: makeMockApiError({ message: "Something went wrong fetching templates." }) }
Error.args = {
getTemplatesError: makeMockApiError({ message: "Something went wrong fetching templates." }),
}
19 changes: 14 additions & 5 deletions site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProp
const navigate = useNavigate()
const { t } = useTranslation("templatesPage")
const theme: Theme = useTheme()
const empty = !props.loading && !props.getOrganizationsError && !props.getTemplatesError && !props.templates?.length
const empty =
!props.loading &&
!props.getOrganizationsError &&
!props.getTemplatesError &&
!props.templates?.length

return (
<Margins>
Expand Down Expand Up @@ -120,11 +124,16 @@ export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProp
</PageHeader>

{props.getOrganizationsError ? (
<ErrorSummary error={props.getOrganizationsError} defaultMessage={t("errors.getOrganizationsError")} />
<ErrorSummary
error={props.getOrganizationsError}
defaultMessage={t("errors.getOrganizationsError")}
/>
) : props.getTemplatesError ? (
<ErrorSummary
error={props.getTemplatesError}
defaultMessage={t("errors.getTemplatesError")}
/>
) : (
props.getTemplatesError ? (
<ErrorSummary error={props.getTemplatesError} defaultMessage={t("errors.getTemplatesError")} />
) :
<TableContainer>
Copy link
Member

Choose a reason for hiding this comment

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

I find this triple, JSX ternary a little confusing and personally think this is a good candidate for early return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you do the early return? I'm not seeing a good way of doing it since the margins and page header need to be there in each case. I could put those in a const but at that point I'd rather put the content (error or data) in a const and still use the conditional so you can easily see what's definitely there vs what's conditional.

Copy link
Member

Choose a reason for hiding this comment

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

I might move the margins and page header into TemplatesPage.tsx - they seem like page scaffolding to me. Then, one could decompose the TemplatesPageView file:

export const TemplatesPageView = () => {
  if (props.getOrganizationsError) {
    return <ErrorSummary />
  }

  if (props.getTemplatesError) {
    return <ErrorSummary />
  }

  if (empty) {
    return (
      <TableContainer>
        <LoadingTableBody />
      </TableContainer>
    )
  }

  return (
    <TableContainer>
      <TemplateTableBody templates={templates} />
    </TableContainer>
  )
}

I think the benefit here is we are writing state in a declarative way - it is apparent at a glance what states the developer should be aware of. Let me know what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this looks nice and readable, but the scaffolding is in the view for storybook reasons. I'm open to a refactor but I think I'll leave it for future work so I can get the error handling in.

Copy link
Member

Choose a reason for hiding this comment

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

sounds good!

<Table>
<TableHead>
Expand Down