This repository was archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: added json validation and import error modal #25
Open
vincendiary
wants to merge
2
commits into
main
Choose a base branch
from
vinh/15-add-data-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,17 @@ | ||||||||||||||||||||||||||
| /* eslint @typescript-eslint/no-var-requires: "off" */ | ||||||||||||||||||||||||||
| import React, { useCallback, useEffect, useState } from 'react'; | ||||||||||||||||||||||||||
| import { FeatureCollection, Feature } from 'geojson'; | ||||||||||||||||||||||||||
| import Map from './Map'; | ||||||||||||||||||||||||||
| import styled, { | ||||||||||||||||||||||||||
| createGlobalStyle // TODO: Establish a theme and hook this up to everything | ||||||||||||||||||||||||||
| } from 'styled-components'; | ||||||||||||||||||||||||||
| import Left from './Left'; | ||||||||||||||||||||||||||
| import Properties from './Properties'; | ||||||||||||||||||||||||||
| import type { IpcRendererEvent } from 'electron'; | ||||||||||||||||||||||||||
| import { MapEvent } from 'react-map-gl'; | ||||||||||||||||||||||||||
| import { partition } from 'lodash'; | ||||||||||||||||||||||||||
| import { isFeatureCollection } from './util/validateJson'; | ||||||||||||||||||||||||||
| import Map from './Map'; | ||||||||||||||||||||||||||
| import Left from './Left'; | ||||||||||||||||||||||||||
| import Properties from './Properties'; | ||||||||||||||||||||||||||
| import Modal from './Modal'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const { ipcRenderer } = window.require('electron'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -18,6 +21,30 @@ const StyledDiv = styled.div` | |||||||||||||||||||||||||
| height: 100vh; | ||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ErrorHeader = styled.h2` | ||||||||||||||||||||||||||
| font-size: 1rem; | ||||||||||||||||||||||||||
| line-height: 1.5rem; | ||||||||||||||||||||||||||
| margin: 0px; | ||||||||||||||||||||||||||
| margin-bottom: 1em; | ||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ErrorText = styled.p` | ||||||||||||||||||||||||||
| font-size: 0.875rem; | ||||||||||||||||||||||||||
| line-height: 1.25rem; | ||||||||||||||||||||||||||
| margin: 0px; | ||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ErrorList = styled.ul` | ||||||||||||||||||||||||||
| padding-left: 1em; | ||||||||||||||||||||||||||
| margin: 0px; | ||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ErrorItem = styled.li` | ||||||||||||||||||||||||||
| font-size: 0.875rem; | ||||||||||||||||||||||||||
| line-height: 1.25rem; | ||||||||||||||||||||||||||
| margin: 0px; | ||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||
vincendiary marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const GlobalStyle = createGlobalStyle` | ||||||||||||||||||||||||||
| body { | ||||||||||||||||||||||||||
| margin: 0; | ||||||||||||||||||||||||||
|
|
@@ -29,20 +56,32 @@ const carleton = { | |||||||||||||||||||||||||
| longitude: -75.69608 | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| interface FileData { | ||||||||||||||||||||||||||
| filepath: string; | ||||||||||||||||||||||||||
| content: FeatureCollection; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const fileChannel = 'file-content'; | ||||||||||||||||||||||||||
| const App: React.FC = () => { | ||||||||||||||||||||||||||
| const [feature, setFeature] = useState<Feature[] | undefined>(undefined); | ||||||||||||||||||||||||||
| const [mapData, setData] = useState<FeatureCollection[] | null>(null); | ||||||||||||||||||||||||||
| const [errorFiles, setErrorFiles] = useState<string[]>([]); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const displayFeature = useCallback(({ features }: MapEvent) => { | ||||||||||||||||||||||||||
| setFeature(features); | ||||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||||
| const [mapData, setData] = useState<FeatureCollection[] | null>(null); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const onFileOpen = useCallback( | ||||||||||||||||||||||||||
| (event: IpcRendererEvent, contents: FeatureCollection[]) => { | ||||||||||||||||||||||||||
| (event: IpcRendererEvent, contents: FileData[]) => { | ||||||||||||||||||||||||||
| console.log(`RENDERER: ${fileChannel}`, contents); | ||||||||||||||||||||||||||
| setData(contents); | ||||||||||||||||||||||||||
| // TODO: Add file validation | ||||||||||||||||||||||||||
| const [validFiles, invalidFiles] = partition(contents, (f) => | ||||||||||||||||||||||||||
| isFeatureCollection(f.content) | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| // if a bad file was found, num of validated files will be less than input contents | ||||||||||||||||||||||||||
| if (validFiles.length < contents.length) { | ||||||||||||||||||||||||||
| setErrorFiles(invalidFiles.map((f) => f.filepath)); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| setData(validFiles.map((f) => f.content)); | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| [] | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
@@ -67,6 +106,17 @@ const App: React.FC = () => { | |||||||||||||||||||||||||
| displayFeature={displayFeature} | ||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||
| <Properties features={feature} /> | ||||||||||||||||||||||||||
| {errorFiles.length > 0 ? ( | ||||||||||||||||||||||||||
| <Modal onClose={() => setErrorFiles([])}> | ||||||||||||||||||||||||||
| <ErrorHeader>There was an error importing some files.</ErrorHeader> | ||||||||||||||||||||||||||
| <ErrorText>Files that could not be imported:</ErrorText> | ||||||||||||||||||||||||||
| <ErrorList> | ||||||||||||||||||||||||||
| {errorFiles.map((file, i) => ( | ||||||||||||||||||||||||||
| <ErrorItem key={`${file}-${i}`}>{file}</ErrorItem> | ||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||
| </ErrorList> | ||||||||||||||||||||||||||
| </Modal> | ||||||||||||||||||||||||||
| ) : null} | ||||||||||||||||||||||||||
|
Comment on lines
+85
to
+90
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| </StyledDiv> | ||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import React from 'react'; | ||
| import styled from 'styled-components'; | ||
|
|
||
| const ModalContainer = styled.div` | ||
| position: fixed; | ||
| top: 0px; | ||
| right: 0px; | ||
| bottom: 0px; | ||
| left: 0px; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| z-index: 10; | ||
| `; | ||
|
|
||
| const ModalBackground = styled.div` | ||
| position: absolute; | ||
| top: 0px; | ||
| right: 0px; | ||
| bottom: 0px; | ||
| left: 0px; | ||
| background-color: #000; | ||
| opacity: 0.8; | ||
| `; | ||
|
|
||
| const ModalContent = styled.div` | ||
| position: relative; | ||
| display: flex; | ||
| flex-direction: column; | ||
| background-color: #fff; | ||
| opacity: 1; | ||
| padding: 2em; | ||
| padding-bottom: 3em; | ||
| border-radius: 0.25rem; | ||
| `; | ||
|
|
||
| const ModalHeader = styled.div` | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| width: 100%; | ||
| `; | ||
|
|
||
| const ModalCloseButton = styled.button` | ||
| margin: 0px; | ||
| padding: 0px; | ||
| cursor: pointer; | ||
| background: none; | ||
| border: none; | ||
| `; | ||
vincendiary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface ModalProps { | ||
| onClose: () => void; | ||
| children?: React.ReactNode; | ||
| } | ||
|
|
||
| const Modal: React.FC<ModalProps> = ({ onClose, children }: ModalProps) => { | ||
| return ( | ||
| <ModalContainer> | ||
| <ModalBackground onClick={() => onClose()} /> | ||
| <ModalContent> | ||
| <ModalHeader> | ||
| <ModalCloseButton onClick={() => onClose()}> | ||
| <svg | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| style={{ width: '1.25rem' }} | ||
| fill='none' | ||
| viewBox='0 0 24 24' | ||
| stroke='currentColor' | ||
| > | ||
| <path | ||
| strokeLinecap='round' | ||
| strokeLinejoin='round' | ||
| strokeWidth={2} | ||
| d='M6 18L18 6M6 6l12 12' | ||
| /> | ||
| </svg> | ||
| </ModalCloseButton> | ||
| </ModalHeader> | ||
| {children} | ||
| </ModalContent> | ||
| </ModalContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default Modal; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* eslint @typescript-eslint/no-explicit-any: "off" */ | ||
| /* eslint @typescript-eslint/explicit-module-boundary-types: "off" */ | ||
| import type { | ||
| FeatureCollection, | ||
| Feature, | ||
| GeoJsonProperties, | ||
| Geometry | ||
| } from 'geojson'; | ||
|
|
||
| export function isFeatureCollection(object: any): object is FeatureCollection { | ||
vincendiary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ( | ||
| typeof object !== 'undefined' && | ||
| typeof object === 'object' && | ||
| object.type === 'FeatureCollection' && | ||
| Array.isArray(object.features) && | ||
| object.features.every(isFeature) | ||
| ); | ||
| } | ||
|
|
||
| export function isFeature(object: any): object is Feature { | ||
| return ( | ||
| typeof object !== 'undefined' && | ||
| typeof object === 'object' && | ||
| object.type === 'Feature' && | ||
| (typeof object.id === 'string' || | ||
| typeof object.id == 'number' || | ||
vincendiary marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| typeof object.id === 'undefined') && | ||
| isGeoJsonProperties(object.properties) && | ||
| isGeometry(object.geometry) | ||
| ); | ||
| } | ||
|
|
||
| export function isGeoJsonProperties(object: any): object is GeoJsonProperties { | ||
| return typeof object === 'object' || object === null; | ||
| } | ||
|
|
||
| export function isGeometry(object: any): object is Geometry { | ||
| return ( | ||
| typeof object !== 'undefined' && | ||
| typeof object === 'object' && | ||
| (object.type === 'Point' || | ||
| object.type === 'MultiPoint' || | ||
| object.type === 'LineString' || | ||
| object.type === 'MultiLineString' || | ||
| object.type === 'Polygon' || | ||
| object.type === 'MultiPolygon') && | ||
vincendiary marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Array.isArray(object.coordinates) && | ||
| (typeof object.coordinates[0] === 'number' || | ||
| (Array.isArray(object.coordinates[0]) && | ||
| (typeof object.coordinates[0][0] === 'number' || | ||
| (Array.isArray(object.coordinates[0][0]) && | ||
| typeof object.coordinates[0][0][0] === 'number')))) | ||
| ); | ||
vincendiary marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.