Skip to content
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

fix errors on signup page #1529

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
21 changes: 15 additions & 6 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface ErrorData {
error: string
}

export class NetworkError extends Error {
name = 'NetworkError'
}

export const request = <Expected>(
method: HTTPMethod,
endpoint: string,
Expand All @@ -33,12 +37,17 @@ export const request = <Expected>(

onCancel(() => controller.abort())
const jwt = await getWorkingJWT()
const resp = await fetch(apiUrl + endpoint + qs(params), {
method,
body: JSON.stringify(body),
headers: jwt ? { Authorization: `Bearer ${jwt.raw}` } : {},
signal,
})
let resp: Response
try {
resp = await fetch(apiUrl + endpoint + qs(params), {
method,
body: JSON.stringify(body),
headers: jwt ? { Authorization: `Bearer ${jwt.raw}` } : {},
signal,
})
} catch (error_) {
throw new NetworkError(error_.message)
}

const text = await resp.text()

Expand Down
8 changes: 4 additions & 4 deletions src/api/user/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EditableUser } from '.'
import { request } from '../base'

// Anyone can create a user. For admins the users will be verified
// automatically, for non-admins or non-authenticated users the user will not be
// verified and will require admin approval. Super-admins can create verified
// users in any realm, admins can only do so in their own realm.
/** Anyone can create a user. For admins the users will be verified
* automatically, for non-admins or non-authenticated users the user will not be
* verified and will require admin approval. Super-admins can create verified
* users in any realm, admins can only do so in their own realm. */
export const createUser = (user: EditableUser) =>
request<number | false>('POST', 'users', {}, user)
6 changes: 5 additions & 1 deletion src/components/analysis-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { usePromise } from '@/utils/use-promise'
import { getEventTeams } from '@/api/event-team-info/get-event-teams'
import { eventTeamUrl } from '@/utils/urls/event-team'
import { EventTeamInfo } from '@/api/event-team-info'
import { isData } from '@/utils/is-data'

interface Props {
eventKey: string
Expand Down Expand Up @@ -224,7 +225,10 @@ const AnalysisTable = ({
const rankColumn: Column<EventTeamInfo | undefined, RowType> = {
title: 'Rank',
key: 'Rank',
getCell: (row) => rankingInfo?.find((r) => r.team === 'frc' + row.team),
getCell: (row) =>
isData(rankingInfo)
? rankingInfo.find((r) => r.team === 'frc' + row.team)
: undefined,
getCellValue: (cell) => cell?.rank ?? Infinity,
renderCell: (cell) => (
<td class={rankCellStyle}>
Expand Down
9 changes: 7 additions & 2 deletions src/components/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { cleanFieldName } from '@/utils/clean-field-name'
import { getFieldKey } from '@/utils/get-field-key'
import { getReports } from '@/api/report/get-reports'
import { GetReport } from '@/api/report'
import { isData } from '@/utils/is-data'

const commentsDisplayStyle = css`
grid-column: 1 / -1;
Expand Down Expand Up @@ -106,7 +107,7 @@ export const ChartCard = ({
return matchesAutoFieldName || matchesTeleopFieldName
})?.name

const matchesWithSelectedStat = (matchesStats || [])
const matchesWithSelectedStat = (isData(matchesStats) ? matchesStats : [])
.map(({ matchKey, stats }) => {
const matchingStat = stats.find((f) => f.name === fullFieldName)
if (matchingStat) return { matchKey, matchingStat }
Expand Down Expand Up @@ -190,7 +191,11 @@ export const ChartCard = ({
</div>
{selectedMatchKey && (
<CommentsDisplay
reports={allReports.filter((r) => r.matchKey === selectedMatchKey)}
reports={
isData(allReports)
? allReports.filter((r) => r.matchKey === selectedMatchKey)
: []
}
/>
)}
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/components/comment-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Icon from './icon'
import { mdiMessageReplyText } from '@mdi/js'
import { formatUserName } from '@/utils/format-user-name'
import { getFastestUser } from '@/cache/users/get-fastest'
import { isData } from '@/utils/is-data'

const commentCardStyle = css`
display: grid;
Expand Down Expand Up @@ -52,7 +53,11 @@ export const CommentCard = ({
class={commentCardStyle}
>
<Icon icon={mdiMessageReplyText} />
{showReporter && <span>{formatUserName(reporter, reporterId)}</span>}
{showReporter && (
<span>
{formatUserName(isData(reporter) ? reporter : undefined, reporterId)}
</span>
)}
<p>{report.comment}</p>
</Card>
)
Expand Down
3 changes: 2 additions & 1 deletion src/components/profile-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { css } from 'linaria'
import { mdiAccountCircle } from '@mdi/js'
import { pigmicePurple } from '@/colors'
import { getFastestUser } from '@/cache/users/get-fastest'
import { isData } from '@/utils/is-data'

export const ProfileLink = ({
reporterId,
Expand All @@ -26,7 +27,7 @@ export const ProfileLink = ({
class={reporterStyle}
>
<Icon icon={mdiAccountCircle} />
{formatUserName(reporter, reporterId)}
{formatUserName(isData(reporter) ? reporter : undefined, reporterId)}
</El>
)
}
Expand Down
46 changes: 35 additions & 11 deletions src/components/report-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { createAlert } from '@/router'
import { AlertType } from './alert'
import Loader from './loader'
import Card from './card'
import { isData } from '@/utils/is-data'

const reportEditorStyle = css`
padding: 1.5rem 2rem;
Expand Down Expand Up @@ -71,6 +72,7 @@ const userDropdownStyle = css`
align-items: center;
`

// eslint-disable-next-line complexity
export const ReportEditor = ({
initialReport,
onMatchSelect,
Expand All @@ -84,10 +86,16 @@ export const ReportEditor = ({
const [isDeleting, setIsDeleting] = useState<boolean>(false)
const [matchKey, setMatchKey] = useState(initialReport.matchKey)
const [comment, setComment] = useState(initialReport.comment || '')
const schemaId = useEventInfo(eventKey)?.schemaId
const schema = useSchema(schemaId)?.schema

const eventInfo = useEventInfo(eventKey)
const schemaId = isData(eventInfo) ? eventInfo.schemaId : undefined
const testSchema = useSchema(schemaId)
const schema = isData(testSchema) ? testSchema.schema : undefined

const eventMatches = useEventMatches(eventKey)
const match = eventMatches?.find((match) => matchKey === match.key)
const match = isData(eventMatches)
? eventMatches.find((match) => matchKey === match.key)
: undefined
const { jwt } = useJWT()
const isAdmin = jwt?.peregrineRoles.isAdmin
const users = usePromise(() => getUsers(), [])
Expand Down Expand Up @@ -228,14 +236,18 @@ export const ReportEditor = ({
return eventMatches && match && visibleFields ? (
<Card as="form" class={reportEditorStyle} onSubmit={submit}>
<Dropdown
options={eventMatches}
options={isData(eventMatches) ? eventMatches : []}
onChange={(match) => {
onMatchSelect?.(match.key)
setMatchKey(match.key)
}}
getKey={(match) => match.key}
getText={(match) => formatMatchKeyShort(match.key)}
value={eventMatches.find((match) => match.key === matchKey)}
value={
isData(eventMatches)
? eventMatches.find((match) => match.key === matchKey)
: undefined
}
/>
<TeamPicker
onChange={setTeam}
Expand Down Expand Up @@ -271,20 +283,32 @@ export const ReportEditor = ({
<div class={userDropdownStyle}>
<Icon icon={mdiAccountCircle} />
<Dropdown
options={users.sort((a, b) =>
a.firstName.toLowerCase() > b.firstName.toLowerCase() ? 1 : -1,
)}
options={
isData(users)
? users.sort((a, b) =>
a.firstName.toLowerCase() > b.firstName.toLowerCase()
? 1
: -1,
)
: []
}
onChange={(user) => {
setReporterId(user.id)
setRealmId(user.realmId)
}}
getKey={(user) => user.id}
getText={(user) => `${user.firstName} ${user.lastName}`}
getGroup={(user) =>
realms?.find((realm) => realm.id === user.realmId)?.name ||
String(user.realmId)
isData(realms)
? realms.find((realm) => realm.id === user.realmId)?.name ||
String(user.realmId)
: null
}
value={
isData(users)
? users.find((user) => user.id === reporterId)
: undefined
}
value={users.find((user) => user.id === reporterId)}
/>
</div>
)}
Expand Down
13 changes: 7 additions & 6 deletions src/components/report-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CommentCard } from './comment-card'
import { css } from 'linaria'
import { cleanFieldName } from '@/utils/clean-field-name'
import { ProfileLink } from './profile-link'
import { isData } from '@/utils/is-data'

interface Props {
report: Report
Expand Down Expand Up @@ -66,10 +67,10 @@ export const ReportViewer = ({ report, onEditClick }: Props) => {
const reporterId = report.reporterId
const eventInfo = useEventInfo(report.eventKey)
const matchInfo = useMatchInfo(report.eventKey, report.matchKey)
const schema = useSchema(eventInfo?.schemaId)
const displayableFields = schema?.schema.filter(
(field) => field.reportReference !== undefined,
)
const schema = useSchema(isData(eventInfo) ? eventInfo.schemaId : undefined)
const displayableFields = isData(schema)
? schema.schema.filter((field) => field.reportReference !== undefined)
: undefined
const autoFields = displayableFields?.filter(
(field) => field.period === 'auto',
)
Expand Down Expand Up @@ -99,8 +100,8 @@ export const ReportViewer = ({ report, onEditClick }: Props) => {
<div>
{matchInfo && (
<TeamPicker
redAlliance={matchInfo.redAlliance}
blueAlliance={matchInfo.blueAlliance}
redAlliance={isData(matchInfo) ? matchInfo.redAlliance : []}
blueAlliance={isData(matchInfo) ? matchInfo.blueAlliance : []}
value={report.teamKey}
editable={false}
/>
Expand Down
7 changes: 4 additions & 3 deletions src/routes/event-analysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
tablePageTableStyle,
} from '@/utils/table-page-style'
import Card from '@/components/card'
import { isData } from '@/utils/is-data'

interface Props {
eventKey: string
Expand All @@ -26,7 +27,7 @@ const EventAnalysis: FunctionComponent<Props> = ({ eventKey }) => {
const eventStats = usePromise(() => getEventStats(eventKey), [eventKey])
const eventInfo = useEventInfo(eventKey)

const schema = useSchema(eventInfo?.schemaId)
const schema = useSchema(isData(eventInfo) ? eventInfo.schemaId : undefined)

return (
<Page
Expand All @@ -35,15 +36,15 @@ const EventAnalysis: FunctionComponent<Props> = ({ eventKey }) => {
class={tablePageStyle}
wrapperClass={tablePageWrapperStyle}
>
{eventStats && schema ? (
{isData(eventStats) && schema ? (
eventStats.length === 0 ? (
'No Event Data'
) : (
<Card class={tablePageTableStyle}>
<AnalysisTable
eventKey={eventKey}
teams={eventStats}
schema={schema}
schema={isData(schema) ? schema : { id: -1, schema: [] }}
renderTeam={(team, link) => (
<a class={teamStyle} href={link}>
{team}
Expand Down
Loading