Skip to content

Commit

Permalink
consolidated report-page elements, renamed report-edit-link
Browse files Browse the repository at this point in the history
  • Loading branch information
varCepheid committed Jul 5, 2024
1 parent ee45d65 commit 50d9762
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 156 deletions.
66 changes: 66 additions & 0 deletions src/components/report-pages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { useJWT } from '@/jwt'
import Card from './card'
import Page from './page'
import { ReportViewer } from './report-viewer'
import { OfflineReport, Report } from '@/api/report'
import { css } from '@linaria/core'
import { ReportEditor } from './report-editor'

const reportPageStyle = css`
display: flex;
padding: 2rem;
justify-content: center;
`

const reportViewerCardStyle = css`
padding: 2rem;
display: grid;
justify-items: center;
grid-gap: 1rem;
max-width: 30rem;
`

export const ReportPage = ({ report }: { report: Report }) => {
const { jwt } = useJWT()
const canEdit =
jwt &&
(report.reporterId === Number.parseInt(jwt.sub) ||
(jwt.peregrineRoles.isAdmin && report.realmId === jwt.peregrineRealm) ||
jwt.peregrineRoles.isSuperAdmin)
return (
<Page name="Report" class={reportPageStyle}>
{/* shows the report */}
<Card class={reportViewerCardStyle}>
<ReportViewer
report={report}
reportEditorLink={
canEdit ? `/saved-reports/${report.key}/edit` : undefined
}
/>
</Card>
</Page>
)
}

export const ReportEditPage = ({
report,
onSaveSuccess,
onSaveLocally,
onDelete,
}: {
report: Report
onSaveSuccess: (report: Report) => void
onSaveLocally: (report: OfflineReport) => void
onDelete: () => void
}) => {
return (
<Page name="Edit Report" class={reportPageStyle}>
<ReportEditor
initialReport={report}
onSaveSuccess={onSaveSuccess}
onSaveLocally={onSaveLocally}
onDelete={onDelete}
/>
</Page>
)
}
7 changes: 2 additions & 5 deletions src/components/report-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ const fieldValuesStyle = css`
`

// viewing a report on the report page
export const ReportViewer = ({
report,
reportEditorLink: onEditClick,
}: Props) => {
export const ReportViewer = ({ report, reportEditorLink }: Props) => {
const reporterId = report.reporterId
const eventInfo = useEventInfo(report.eventKey)
const matchInfo = useMatchInfo(report.eventKey, report.matchKey)
Expand Down Expand Up @@ -133,7 +130,7 @@ export const ReportViewer = ({

{/* links to the author of the report */}
<ProfileLink reporterId={reporterId} />
{onEditClick && <Button href={onEditClick}>Edit</Button>}
{reportEditorLink && <Button href={reportEditorLink}>Edit</Button>}
</>
)
}
39 changes: 2 additions & 37 deletions src/routes/report-edit.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
import { Report, OfflineReport } from '@/api/report'
import { Report } from '@/api/report'
import { getReport } from '@/api/report/get-report'
import { AlertType } from '@/components/alert'
import Loader from '@/components/loader'
import Page from '@/components/page'
import { ReportEditor } from '@/components/report-editor'
import { ReportEditPage } from '@/components/report-pages'
import { route } from '@/router'
import { css } from '@linaria/core'
import { useEffect, useState } from 'preact/hooks'

const reportPageStyle = css`
display: flex;
padding: 2rem;
justify-content: center;
`

const ReportEditPage = ({
report,
onSaveSuccess,
onSaveLocally,
onDelete,
}: {
report: Report
onSaveSuccess: (report: Report) => void
onSaveLocally: (report: OfflineReport) => void
onDelete: () => void
}) => {
return (
<Page name="Edit Report" class={reportPageStyle}>
<ReportEditor
initialReport={report}
onSaveSuccess={(report) => {
onSaveSuccess(report)
}}
onSaveLocally={(report) => {
onSaveLocally(report)
}}
onDelete={onDelete}
/>
</Page>
)
}

const ReportEditorRoute = ({ reportId }: { reportId: number }) => {
const [report, setReport] = useState<Report | undefined>(undefined)

Expand Down
39 changes: 1 addition & 38 deletions src/routes/report.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,8 @@
import Page from '@/components/page'
import { ReportViewer } from '@/components/report-viewer'
import Card from '@/components/card'
import { css } from '@linaria/core'
import { getReport } from '@/api/report/get-report'
import Loader from '@/components/loader'
import { useState, useEffect } from 'preact/hooks'
import { useJWT } from '@/jwt'
import { Report } from '@/api/report'

const reportPageStyle = css`
display: flex;
padding: 2rem;
justify-content: center;
`

const reportViewerCardStyle = css`
padding: 2rem;
display: grid;
justify-items: center;
grid-gap: 1rem;
max-width: 30rem;
`

const ReportPage = ({ report }: { report: Report }) => {
const { jwt } = useJWT()
const canEdit =
jwt &&
(report.reporterId === Number.parseInt(jwt.sub) ||
(jwt.peregrineRoles.isAdmin && report.realmId === jwt.peregrineRealm) ||
jwt.peregrineRoles.isSuperAdmin)
return (
<Page name="Report" class={reportPageStyle}>
<Card class={reportViewerCardStyle}>
<ReportViewer
report={report}
reportEditorLink={canEdit ? `/reports/${report.id}/edit` : undefined}
/>
</Card>
</Page>
)
}
import { ReportPage } from '@/components/report-pages'

const ReportRoute = ({ reportId }: { reportId: number }) => {
const [report, setReport] = useState<Report | undefined>(undefined)
Expand Down
39 changes: 4 additions & 35 deletions src/routes/saved-report-edit.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
import { Report, OfflineReport } from '@/api/report'
import { Report } from '@/api/report'
import { getSavedReports } from '@/api/report/submit-report'
import { AlertType } from '@/components/alert'
import Loader from '@/components/loader'
import Page from '@/components/page'
import { ReportEditor } from '@/components/report-editor'
import { ReportEditPage } from '@/components/report-pages'
import { route } from '@/router'
import { css } from '@linaria/core'
import { useEffect, useState } from 'preact/hooks'

const reportPageStyle = css`
display: flex;
padding: 2rem;
justify-content: center;
`

const ReportEditPage = ({
report,
onSaveSuccess,
onSaveLocally,
onDelete,
}: {
report: Report
onSaveSuccess: (report: Report) => void
onSaveLocally: (report: OfflineReport) => void
onDelete: () => void
}) => {
return (
<Page name="Edit Report" class={reportPageStyle}>
<ReportEditor
initialReport={report}
onSaveSuccess={onSaveSuccess}
onSaveLocally={onSaveLocally}
onDelete={onDelete}
/>
</Page>
)
}

const LocalReportEditorRoute = ({ reportKey }: { reportKey: string }) => {
const SavedReportEditorRoute = ({ reportKey }: { reportKey: string }) => {
const [report, setReport] = useState<Report | undefined>(undefined)

useEffect(() => {
Expand Down Expand Up @@ -74,4 +43,4 @@ const LocalReportEditorRoute = ({ reportKey }: { reportKey: string }) => {
)
}

export default LocalReportEditorRoute
export default SavedReportEditorRoute
44 changes: 3 additions & 41 deletions src/routes/saved-report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,14 @@ import Page from '@/components/page'
import { css } from '@linaria/core'
import { useState, useEffect } from 'preact/hooks'
import { Report } from '@/api/report'
import Card from '@/components/card'
import { ReportViewer } from '@/components/report-viewer'
import { useJWT } from '@/jwt'
import { ReportPage } from '@/components/report-pages'

const missingReportStyle = css`
padding: 2rem;
text-align: center;
`

const reportPageStyle = css`
display: flex;
padding: 2rem;
justify-content: center;
`

const reportViewerCardStyle = css`
padding: 2rem;
display: grid;
justify-items: center;
grid-gap: 1rem;
max-width: 30rem;
`

const ReportPage = ({ report }: { report: Report }) => {
const { jwt } = useJWT()
const canEdit =
jwt &&
(report.reporterId === Number.parseInt(jwt.sub) ||
(jwt.peregrineRoles.isAdmin && report.realmId === jwt.peregrineRealm) ||
jwt.peregrineRoles.isSuperAdmin)
return (
<Page name="Report" class={reportPageStyle}>
{/* shows the report */}
<Card class={reportViewerCardStyle}>
<ReportViewer
report={report}
reportEditorLink={
canEdit ? `/saved-reports/${report.key}/edit` : undefined
}
/>
</Card>
</Page>
)
}

const SavedReportsPage = ({ reportKey }: { reportKey: string }) => {
const SavedReportsRoute = ({ reportKey }: { reportKey: string }) => {
const [report, setReport] = useState<undefined | Report>(undefined)

useEffect(() => {
Expand All @@ -64,4 +26,4 @@ const SavedReportsPage = ({ reportKey }: { reportKey: string }) => {
)
}

export default SavedReportsPage
export default SavedReportsRoute

0 comments on commit 50d9762

Please sign in to comment.