Skip to content

Commit

Permalink
More typescript migration
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Dec 21, 2023
1 parent ab102dc commit c7c1139
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"@types/react-autocomplete": "^1.8.9",
"@types/react-color": "^3.0.10",
"@types/react-dom": "^16.9.24",
"@types/react-file-reader-input": "^2.0.4",
"@types/react-icon-base": "^2.1.6",
"@types/uuid": "^9.0.7",
"@vitejs/plugin-react": "^4.2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/components/InputUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export type FieldUrlProps = {
multi?: boolean
required?: boolean
'aria-label'?: string
type?: string
className?: string
};

type FieldUrlState = {
Expand Down
22 changes: 11 additions & 11 deletions src/components/ModalLoading.jsx → src/components/ModalLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react'
import PropTypes from 'prop-types'

import InputButton from './InputButton'
import Modal from './Modal'


export default class ModalLoading extends React.Component {
static propTypes = {
isOpen: PropTypes.bool.isRequired,
onCancel: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
message: PropTypes.node.isRequired,
}
type ModalLoadingProps = {
isOpen: boolean
onCancel(...args: unknown[]): unknown
title: string
message: React.ReactNode
};


underlayOnClick(e) {
export default class ModalLoading extends React.Component<ModalLoadingProps> {
underlayOnClick(e: Event) {
// This stops click events falling through to underlying modals.
e.stopPropagation();
}
Expand All @@ -24,9 +24,9 @@ export default class ModalLoading extends React.Component {
isOpen={this.props.isOpen}
underlayClickExits={false}
underlayProps={{
onClick: (e) => underlayProps(e)
// @ts-ignore
onClick: (e: Event) => underlayProps(e)
}}
closeable={false}
title={this.props.title}
onOpenToggle={() => this.props.onCancel()}
>
Expand Down
70 changes: 38 additions & 32 deletions src/components/ModalOpen.jsx → src/components/ModalOpen.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import React from 'react'
import PropTypes from 'prop-types'
import React, { FormEvent } from 'react'
import {MdFileUpload} from 'react-icons/md'
import {MdAddCircleOutline} from 'react-icons/md'
import FileReaderInput, { Result } from 'react-file-reader-input'

import ModalLoading from './ModalLoading'
import Modal from './Modal'
import InputButton from './InputButton'
import FileReaderInput from 'react-file-reader-input'
import InputUrl from './InputUrl'

import {MdFileUpload} from 'react-icons/md'
import {MdAddCircleOutline} from 'react-icons/md'

import style from '../libs/style'
import publicStyles from '../config/styles.json'

class PublicStyle extends React.Component {
static propTypes = {
url: PropTypes.string.isRequired,
thumbnailUrl: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
onSelect: PropTypes.func.isRequired,
}
type PublicStyleProps = {
url: string
thumbnailUrl: string
title: string
onSelect(...args: unknown[]): unknown
};

class PublicStyle extends React.Component<PublicStyleProps> {
render() {
return <div className="maputnik-public-style">
<InputButton
Expand All @@ -43,14 +42,21 @@ class PublicStyle extends React.Component {
}
}

export default class ModalOpen extends React.Component {
static propTypes = {
isOpen: PropTypes.bool.isRequired,
onOpenToggle: PropTypes.func.isRequired,
onStyleOpen: PropTypes.func.isRequired,
}
type ModalOpenProps = {
isOpen: boolean
onOpenToggle(...args: unknown[]): unknown
onStyleOpen(...args: unknown[]): unknown
};

type ModalOpenState = {
styleUrl: string
error?: string | null
activeRequest?: any
activeRequestUrl?: string | null
};

constructor(props) {
export default class ModalOpen extends React.Component<ModalOpenProps, ModalOpenState> {
constructor(props: ModalOpenProps) {
super(props);
this.state = {
styleUrl: ""
Expand All @@ -63,7 +69,7 @@ export default class ModalOpen extends React.Component {
})
}

onCancelActiveRequest(e) {
onCancelActiveRequest(e: Event) {
// Else the click propagates to the underlying modal
if(e) e.stopPropagation();

Expand All @@ -76,12 +82,12 @@ export default class ModalOpen extends React.Component {
}
}

onStyleSelect = (styleUrl) => {
onStyleSelect = (styleUrl: string) => {
this.clearError();

let canceled;
let canceled: boolean = false;

const activeRequest = fetch(styleUrl, {
fetch(styleUrl, {
mode: 'cors',
credentials: "same-origin"
})
Expand Down Expand Up @@ -123,13 +129,13 @@ export default class ModalOpen extends React.Component {
})
}

onSubmitUrl = (e) => {
onSubmitUrl = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
this.onStyleSelect(this.state.styleUrl);
}

onUpload = (_, files) => {
const [e, file] = files[0];
onUpload = (_: any, files: Result[]) => {
const [_e, file] = files[0];
const reader = new FileReader();

this.clearError();
Expand All @@ -138,11 +144,11 @@ export default class ModalOpen extends React.Component {
reader.onload = e => {
let mapStyle;
try {
mapStyle = JSON.parse(e.target.result)
mapStyle = JSON.parse(e.target?.result as string)
}
catch(err) {
this.setState({
error: err.toString()
error: (err as Error).toString()
});
return;
}
Expand All @@ -161,7 +167,7 @@ export default class ModalOpen extends React.Component {
this.props.onOpenToggle();
}

onChangeUrl = (url) => {
onChangeUrl = (url: string) => {
this.setState({
styleUrl: url,
});
Expand Down Expand Up @@ -200,7 +206,7 @@ export default class ModalOpen extends React.Component {
<section className="maputnik-modal-section">
<h1>Upload Style</h1>
<p>Upload a JSON style from your computer.</p>
<FileReaderInput onChange={this.onUpload} tabIndex="-1" aria-label="Style file">
<FileReaderInput onChange={this.onUpload} tabIndex={-1} aria-label="Style file">
<InputButton className="maputnik-upload-button"><MdFileUpload /> Upload</InputButton>
</FileReaderInput>
</section>
Expand Down Expand Up @@ -246,7 +252,7 @@ export default class ModalOpen extends React.Component {
<ModalLoading
isOpen={!!this.state.activeRequest}
title={'Loading style'}
onCancel={(e) => this.onCancelActiveRequest(e)}
onCancel={(e: Event) => this.onCancelActiveRequest(e)}
message={"Loading: "+this.state.activeRequestUrl}
/>
</div>
Expand Down

0 comments on commit c7c1139

Please sign in to comment.