Skip to content

Commit

Permalink
💄 improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Mar 6, 2024
1 parent 49d0330 commit bbc1f17
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
4 changes: 2 additions & 2 deletions adminSiteClient/ChartEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type FieldWithDetailReferences =

export type DetailReferences = Record<FieldWithDetailReferences, string[]>

export interface DimensionErrorMessages {
export interface DimensionErrorMessage {
displayName?: string
}

Expand All @@ -111,7 +111,7 @@ export interface ChartEditorManager {
errorMessages: Partial<Record<FieldWithDetailReferences, string>>
errorMessagesForDimensions: Record<
DimensionProperty,
DimensionErrorMessages[]
DimensionErrorMessage[]
>
}

Expand Down
21 changes: 8 additions & 13 deletions adminSiteClient/ChartEditorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,14 @@ export class ChartEditorPage

@computed
get invalidDetailReferences(): ChartEditorManager["invalidDetailReferences"] {
const { currentDetailReferences } = this
const keys = getIndexableKeys(currentDetailReferences)

const invalidDetailReferences = Object.fromEntries(
keys.map((key: FieldWithDetailReferences) => [
key,
currentDetailReferences[key].filter(
(term: string) => !this.details[term]
),
])
) as DetailReferences

return invalidDetailReferences
const { subtitle, note, axisLabelX, axisLabelY } =
this.currentDetailReferences
return {
subtitle: subtitle.filter((term) => !this.details[term]),
note: note.filter((term) => !this.details[term]),
axisLabelX: axisLabelX.filter((term) => !this.details[term]),
axisLabelY: axisLabelY.filter((term) => !this.details[term]),
}
}

@computed get errorMessages(): ChartEditorManager["errorMessages"] {
Expand Down
6 changes: 3 additions & 3 deletions adminSiteClient/DimensionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import { observable, computed, action } from "mobx"
import { observer } from "mobx-react"
import { ChartDimension } from "@ourworldindata/grapher"
import { ChartEditor, DimensionErrorMessages } from "./ChartEditor.js"
import { ChartEditor, DimensionErrorMessage } from "./ChartEditor.js"
import { Toggle, BindAutoString, BindAutoFloat, ColorBox } from "./Forms.js"
import { Link } from "./Link.js"
import {
Expand All @@ -23,7 +23,7 @@ export class DimensionCard extends React.Component<{
onChange: (dimension: ChartDimension) => void
onEdit?: () => void
onRemove?: () => void
errorMessages?: DimensionErrorMessages
errorMessage?: DimensionErrorMessage
}> {
@observable.ref isExpanded: boolean = false

Expand Down Expand Up @@ -150,7 +150,7 @@ export class DimensionCard extends React.Component<{
store={dimension.display}
auto={column.displayName}
onBlur={this.onChange}
errorMessage={this.props.errorMessages?.displayName}
errorMessage={this.props.errorMessage?.displayName}
/>
<BindAutoString
label="Unit of measurement"
Expand Down
2 changes: 1 addition & 1 deletion adminSiteClient/EditorBasicTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class DimensionSlotView extends React.Component<{
isDndEnabled={
this.isDndEnabled
}
errorMessages={
errorMessage={
this.errorMessages[
slot.property
][index]
Expand Down
20 changes: 6 additions & 14 deletions adminSiteClient/SaveButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import { ChartEditor } from "./ChartEditor.js"
import { action, computed } from "mobx"
import { observer } from "mobx-react"
import { isEmpty } from "@ourworldindata/utils"

@observer
export class SaveButtons extends React.Component<{ editor: ChartEditor }> {
Expand All @@ -23,21 +24,12 @@ export class SaveButtons extends React.Component<{ editor: ChartEditor }> {
const { editor } = this.props
const { errorMessages, errorMessagesForDimensions } = editor.manager

for (const message of Object.values(errorMessages)) {
if (message) return true
}
if (!isEmpty(errorMessages)) return true

for (const slot of Object.values(errorMessagesForDimensions)) {
for (const dimension of slot) {
if (!dimension) continue
const messages = Object.values(dimension).filter(
(message) => message
)
if (messages.length > 0) return true
}
}

return false
const allErrorMessagesForDimensions = Object.values(
errorMessagesForDimensions
).flat()
return allErrorMessagesForDimensions.some((error) => error)
}

render() {
Expand Down
3 changes: 2 additions & 1 deletion packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { observer } from "mobx-react"
import {
Bounds,
DEFAULT_BOUNDS,
isEmpty,
triggerDownloadFromBlob,
triggerDownloadFromUrl,
} from "@ourworldindata/utils"
Expand Down Expand Up @@ -204,7 +205,7 @@ export class DownloadModal extends React.Component<DownloadModalProps> {
}

@computed private get hasDetails(): boolean {
return (this.manager.detailsOrderedByReference ?? []).length > 0
return !isEmpty(this.manager.detailsOrderedByReference)
}

@computed private get showExportControls(): boolean {
Expand Down

0 comments on commit bbc1f17

Please sign in to comment.