Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
presleyp merged 10 commits intomainfrompresleyp/template-errors
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
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
2 changes: 1 addition & 1 deletionsite/src/AppRouter.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ import { NotFoundPage } from "./pages/404Page/404Page"
import{CliAuthenticationPage}from"./pages/CliAuthPage/CliAuthPage"
import{HealthzPage}from"./pages/HealthzPage/HealthzPage"
import{LoginPage}from"./pages/LoginPage/LoginPage"
importTemplatesPagefrom"./pages/TemplatesPage/TemplatesPage"
import{TemplatesPage}from"./pages/TemplatesPage/TemplatesPage"
import{AccountPage}from"./pages/UserSettingsPage/AccountPage/AccountPage"
import{SecurityPage}from"./pages/UserSettingsPage/SecurityPage/SecurityPage"
import{SSHKeysPage}from"./pages/UserSettingsPage/SSHKeysPage/SSHKeysPage"
Expand Down
2 changes: 2 additions & 0 deletionssite/src/i18n/en/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
importauditLogfrom"./auditLog.json"
importcommonfrom"./common.json"
importtemplatePagefrom"./templatePage.json"
importtemplatesPagefrom"./templatesPage.json"
importworkspacePagefrom"./workspacePage.json"

exportconsten={
common,
workspacePage,
auditLog,
templatePage,
templatesPage,
}
6 changes: 6 additions & 0 deletionssite/src/i18n/en/templatesPage.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
{
"errors": {
"getOrganizationError": "Something went wrong fetching organizations.",
"getTemplatesError": "Something went wrong fetching templates."
}
}
2 changes: 1 addition & 1 deletionsite/src/pages/TemplatesPage/TemplatesPage.test.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@ import * as CreateDayString from "util/createDayString"
import{MockTemplate}from"../../testHelpers/entities"
import{history,render}from"../../testHelpers/renderHelpers"
import{server}from"../../testHelpers/server"
importTemplatesPagefrom"./TemplatesPage"
import{TemplatesPage}from"./TemplatesPage"
import{Language}from"./TemplatesPageView"

describe("TemplatesPage",()=>{
Expand Down
9 changes: 5 additions & 4 deletionssite/src/pages/TemplatesPage/TemplatesPage.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,23 +6,24 @@ import { XServiceContext } from "../../xServices/StateContext"
import{templatesMachine}from"../../xServices/templates/templatesXService"
import{TemplatesPageView}from"./TemplatesPageView"

constTemplatesPage:React.FC=()=>{
exportconstTemplatesPage:React.FC=()=>{
Copy link
Member

@Kira-PilotKira-PilotSep 12, 2022
edited
Loading

Choose a reason for hiding this comment

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

I think we need to update the import ofTemplatesPage inAppRouter.tsx and inTemplatesPage.test.tsx.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Gah, thanks!

constxServices=useContext(XServiceContext)
const[authState]=useActor(xServices.authXService)
const[templatesState]=useMachine(templatesMachine)
const{ templates, getOrganizationsError, getTemplatesError}=templatesState.context

return(
<>
<Helmet>
<title>{pageTitle("Templates")}</title>
</Helmet>
<TemplatesPageView
templates={templatesState.context.templates}
templates={templates}
canCreateTemplate={authState.context.permissions?.createTemplates}
loading={templatesState.hasTag("loading")}
getOrganizationsError={getOrganizationsError}
getTemplatesError={getTemplatesError}
/>
</>
)
}

exportdefaultTemplatesPage
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
import { ComponentMeta, Story } from "@storybook/react"
import { MockTemplate } from "../../testHelpers/entities"
import {makeMockApiError,MockTemplate } from "../../testHelpers/entities"
import { TemplatesPageView, TemplatesPageViewProps } from "./TemplatesPageView"

export default {
Expand DownExpand Up@@ -49,3 +49,8 @@ EmptyCanCreate.args = {

export const EmptyCannotCreate = Template.bind({})
EmptyCannotCreate.args = {}

export const Error = Template.bind({})
Error.args = {
getTemplatesError: makeMockApiError({ message: "Something went wrong fetching templates." }),
}
192 changes: 109 additions & 83 deletionssite/src/pages/TemplatesPage/TemplatesPageView.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,9 @@ import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight"
import useTheme from "@material-ui/styles/useTheme"
import { ErrorSummary } from "components/ErrorSummary/ErrorSummary"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import { createDayString } from "util/createDayString"
import { formatTemplateActiveDevelopers } from "util/templates"
Expand DownExpand Up@@ -77,12 +79,20 @@ export interface TemplatesPageViewProps {
loading?: boolean
canCreateTemplate?: boolean
templates?: TypesGen.Template[]
getOrganizationsError?: Error | unknown
getTemplatesError?: Error | unknown
}

export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProps>> = (props) => {
const styles = useStyles()
const navigate = useNavigate()
const { t } = useTranslation("templatesPage")
const theme: Theme = useTheme()
const empty =
!props.loading &&
!props.getOrganizationsError &&
!props.getTemplatesError &&
!props.templates?.length

return (
<Margins>
Expand DownExpand Up@@ -114,94 +124,110 @@ export const TemplatesPageView: FC<React.PropsWithChildren<TemplatesPageViewProp
)}
</PageHeader>

<TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell width="50%">{Language.nameLabel}</TableCell>
<TableCell width="16%">{Language.usedByLabel}</TableCell>
<TableCell width="16%">{Language.lastUpdatedLabel}</TableCell>
<TableCell width="16%">{Language.createdByLabel}</TableCell>
<TableCell width="1%"></TableCell>
</TableRow>
</TableHead>
<TableBody>
{props.loading && <TableLoader />}
{!props.loading && !props.templates?.length && (
{props.getOrganizationsError ? (
<ErrorSummary
error={props.getOrganizationsError}
defaultMessage={t("errors.getOrganizationsError")}
/>
) : 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.

jsjoeio reacted with thumbs up emoji
Copy link
ContributorAuthor

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 intoTemplatesPage.tsx - they seem like page scaffolding to me. Then, one could decompose theTemplatesPageView 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!

jsjoeio reacted with thumbs up emoji
Copy link
ContributorAuthor

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>
<TableRow>
<TableCell colSpan={999}>
<EmptyState
message={Language.emptyMessage}
description={
props.canCreateTemplate
? Language.emptyDescription
: Language.emptyViewNoPerms
}
descriptionClassName={styles.emptyDescription}
cta={<CodeExample code="coder templates init" />}
/>
</TableCell>
<TableCell width="50%">{Language.nameLabel}</TableCell>
<TableCell width="16%">{Language.usedByLabel}</TableCell>
<TableCell width="16%">{Language.lastUpdatedLabel}</TableCell>
<TableCell width="16%">{Language.createdByLabel}</TableCell>
<TableCell width="1%"></TableCell>
</TableRow>
)}
{props.templates?.map((template) => {
const templatePageLink = `/templates/${template.name}`
const hasIcon = template.icon && template.icon !== ""

return (
<TableRow
key={template.id}
hover
data-testid={`template-${template.id}`}
tabIndex={0}
onKeyDown={(event) => {
if (event.key === "Enter") {
navigate(templatePageLink)
}
}}
className={styles.clickableTableRow}
>
<TableCellLink to={templatePageLink}>
<AvatarData
title={template.name}
subtitle={template.description}
highlightTitle
avatar={
hasIcon ? (
<div className={styles.templateIconWrapper}>
<img alt="" src={template.icon} />
</div>
) : undefined
</TableHead>
<TableBody>
{props.loading && <TableLoader />}

{empty ? (
<TableRow>
<TableCell colSpan={999}>
<EmptyState
message={Language.emptyMessage}
description={
props.canCreateTemplate
? Language.emptyDescription
: Language.emptyViewNoPerms
}
descriptionClassName={styles.emptyDescription}
cta={<CodeExample code="coder templates init" />}
/>
</TableCellLink>

<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{Language.developerCount(template.active_user_count)}
</span>
</TableCellLink>

<TableCellLink data-chromatic="ignore" to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{createDayString(template.updated_at)}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{template.created_by_name}
</span>
</TableCellLink>
<TableCellLink to={templatePageLink}>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
</div>
</TableCellLink>
</TableCell>
</TableRow>
)
})}
</TableBody>
</Table>
</TableContainer>
) : (
props.templates?.map((template) => {
const templatePageLink = `/templates/${template.name}`
const hasIcon = template.icon && template.icon !== ""

return (
<TableRow
key={template.id}
hover
data-testid={`template-${template.id}`}
tabIndex={0}
onKeyDown={(event) => {
if (event.key === "Enter") {
navigate(templatePageLink)
}
}}
className={styles.clickableTableRow}
>
<TableCellLink to={templatePageLink}>
<AvatarData
title={template.name}
subtitle={template.description}
highlightTitle
avatar={
hasIcon && (
<div className={styles.templateIconWrapper}>
<img alt="" src={template.icon} />
</div>
)
}
/>
</TableCellLink>

<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{Language.developerCount(template.active_user_count)}
</span>
</TableCellLink>

<TableCellLink data-chromatic="ignore" to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{createDayString(template.updated_at)}
</span>
</TableCellLink>

<TableCellLink to={templatePageLink}>
<span style={{ color: theme.palette.text.secondary }}>
{template.created_by_name}
</span>
</TableCellLink>

<TableCellLink to={templatePageLink}>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
</div>
</TableCellLink>
</TableRow>
)
})
)}
</TableBody>
</Table>
</TableContainer>
)}
</Margins>
)
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp