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

Resolve TS errors: TreeMapper #1860

Merged
merged 37 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f7a4b6d
refactor: TreeMapper.d.ts
prachigarg19 Sep 6, 2023
9a8a922
refactor: MySpecies.tsx
prachigarg19 Sep 6, 2023
5d114c9
refactor:
prachigarg19 Sep 6, 2023
3539ff9
refactor: TreeMapper/index.tsx
prachigarg19 Sep 6, 2023
105f711
refactor: SpeciesAutoComplete
prachigarg19 Sep 6, 2023
0a41636
refactor: SampleTrees.tsx
prachigarg19 Sep 6, 2023
60b661a
refactor: TreeMapperList.tsx
prachigarg19 Sep 6, 2023
343dbf7
refactor: change types to match TreeMapperList Props
prachigarg19 Sep 6, 2023
6297a25
refactor: SampleTreeCard.tsx
prachigarg19 Sep 6, 2023
5245ac3
remove string[] type
prachigarg19 Sep 7, 2023
a6244e4
refactor: TreePlanted/index.tsx
prachigarg19 Sep 7, 2023
76786aa
refactor: PlantingLocation
prachigarg19 Sep 11, 2023
99fa843
Merge branch 'develop' into feature/treemapper-import-errors
prachigarg19 Sep 13, 2023
01f747f
rename `MySpecies` to `MySpeciesForm`
prachigarg19 Sep 13, 2023
1857aa2
refactor: Import/index.tsx
prachigarg19 Sep 13, 2023
777c880
resolve ReviewSubmit
prachigarg19 Sep 13, 2023
d64dbc0
Changes made:
prachigarg19 Sep 13, 2023
81f25d7
update `Links` type in `TreeMapperList.tsx`
prachigarg19 Sep 13, 2023
7040876
fix: import of sass file
prachigarg19 Sep 13, 2023
8b4f539
refactor: remove MySpecies file
prachigarg19 Sep 18, 2023
d85bdcb
Merge branch 'develop' into feature/treemapper-import-errors
prachigarg19 Sep 20, 2023
93544d3
fix: resolves minor TS warnings (PlantingLocation)
mohitb35 Sep 22, 2023
838bb0a
refcator: remove type for `index`
prachigarg19 Sep 25, 2023
9d7e66a
remove duplicate interface
prachigarg19 Sep 25, 2023
7021f5d
rename `ExtendedPlantLocations`
prachigarg19 Sep 25, 2023
d51db6a
Changes made:
prachigarg19 Sep 25, 2023
e1ebd69
add new interface
prachigarg19 Sep 25, 2023
947315a
Remove `namespace TreeMapper`
prachigarg19 Sep 25, 2023
bbf8b13
Replace all namespace imports with regular imports
prachigarg19 Sep 25, 2023
166b547
add types for props in `SampleTreeCard`
prachigarg19 Sep 25, 2023
af6a22d
Changes:
prachigarg19 Sep 25, 2023
6b537b5
fix `formatter` error
prachigarg19 Sep 25, 2023
e75e7c7
Changes made:
prachigarg19 Sep 26, 2023
d41558e
Changes made:
prachigarg19 Sep 26, 2023
85a3b72
undo changes for logic `sampleTrees in location`
prachigarg19 Sep 26, 2023
8fb8ea2
refactor : move `MySpeciesAutoComplete`
prachigarg19 Sep 26, 2023
7db8fe0
fix: minor cleanup
mohitb35 Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/features/common/types/plantLocation.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DateString } from './common';
import { Polygon, Point } from 'geojson';
import { Links } from './payments';

export interface PlantLocationBase {
hid: string;
Expand Down Expand Up @@ -144,3 +145,30 @@ export interface Species {
scientificName: string;
scientificSpecies: string;
}

interface Filters {
all: string;
'location-partial': string;
'location-complete': string;
'location-single': string;
'location-multi': string;
'location-sample': string;
'revision-pending': string;
}

export interface ExtendedPlantLocations {
items: PlantLocation[] | SamplePlantLocation[];
total: number;
count: number;
_links: Links;
_filters: Filters;
}
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved

export interface Species {
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
aliases: string;
description: string | null;
id: string;
image: string | null;
scientificName: string;
scientificSpecies: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,8 @@ export const TreePlanted = () => {
xaxis: {
labels: {
rotate: -90,
formatter: function (
value:
| DaysCategories
| WeeksCategories
| MonthsCategories
| YearsCategories
) {
return value ? value.label : '';
formatter: function (value: string) {
return value ? value : '';
},
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
},
categories: [],
Expand Down
63 changes: 42 additions & 21 deletions src/features/user/TreeMapper/Import/components/PlantingLocation.tsx
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { ReactElement } from 'react';
import { Controller, useForm, useFieldArray, Control } from 'react-hook-form';
import {
Controller,
useForm,
useFieldArray,
FieldErrors,
FieldArrayWithId,
Control,
} from 'react-hook-form';
import styles from '../Import.module.scss';
import { useTranslation } from 'next-i18next';
import { localeMapForDate } from '../../../../../utils/language/getLanguageName';
Expand All @@ -23,7 +30,10 @@
import { handleError, APIError } from '@planet-sdk/common';
import { ErrorHandlingContext } from '../../../../common/Layout/ErrorHandlingContext';
import { MapProject } from '../../../../common/types/ProjectPropsContextInterface';
import { Treemapper } from '../../Treemapper';
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
import { FeatureCollection, GeometryObject } from 'geojson';
import { Species } from '../../../../common/types/plantLocation';
import { Geometry } from '@turf/turf';

const dialogSx: SxProps = {
'& .MuiButtonBase-root.MuiPickersDay-root.Mui-selected': {
Expand All @@ -46,9 +56,9 @@
index: number;
t: Function;
remove: Function;
errors: any;
item: Record<'id', string>;
control: Control<any, any>;
errors: FieldErrors<Treemapper.PlantLocation>;
item: FieldArrayWithId<Treemapper.PlantLocation, 'plantedSpecies', 'id'>;
control: Control<Treemapper.PlantLocation, any>;

Check warning on line 61 in src/features/user/TreeMapper/Import/components/PlantingLocation.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/features/user/TreeMapper/Import/components/PlantingLocation.tsx#L61

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
}

function PlantedSpecies({
Expand Down Expand Up @@ -79,12 +89,12 @@
value={value}
error={
errors.plantedSpecies &&
errors.plantedSpecies[index]?.otherSpecies
errors.plantedSpecies[index]?.otherSpecies !== undefined
}
helperText={
errors.plantedSpecies &&
errors.plantedSpecies[index]?.otherSpecies &&
errors.plantedSpecies[index]?.otherSpecies.message
(errors.plantedSpecies[index]?.otherSpecies?.message ?? '')
}
/>
)}
Expand All @@ -96,7 +106,7 @@
control={control}
rules={{
required: index > 0 ? false : t('treemapper:treesRequired'),
validate: (value: any) => {
validate: (value) => {
return parseInt(value, 10) >= 1
? true
: t('treemapper:treesRequired');
Expand All @@ -106,19 +116,20 @@
<TextField
label={t('treemapper:count')}
variant="outlined"
onChange={(e: any) => {
onChange={(e) => {
e.target.value = e.target.value.replace(/[^0-9]/g, '');
onChange(e.target.value);
}}
value={value > 0 ? value : ''}
onBlur={onBlur}
error={
errors.plantedSpecies && errors.plantedSpecies[index]?.treeCount
errors.plantedSpecies &&
errors.plantedSpecies[index]?.treeCount !== undefined
}
helperText={
errors.plantedSpecies &&
errors.plantedSpecies[index]?.treeCount &&
errors.plantedSpecies[index]?.treeCount.message
(errors.plantedSpecies[index]?.treeCount?.message ?? '')
}
/>
)}
Expand All @@ -141,9 +152,9 @@
interface Props {
handleNext: () => void;
userLang: string;
plantLocation: any;
plantLocation: Treemapper.PlantLocation | null;
setPlantLocation: Function;
geoJson: any;
geoJson: Geometry | null;
setGeoJson: Function;
activeMethod: string;
setActiveMethod: Function;
Expand All @@ -165,12 +176,12 @@
const [projects, setProjects] = React.useState<MapProject[]>([]);
const importMethods = ['import', 'editor'];
const [geoJsonError, setGeoJsonError] = React.useState(false);
const [mySpecies, setMySpecies] = React.useState<Species[] | null>(null);
const [mySpecies, setMySpecies] = React.useState<Species | null>(null);
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
const { setErrors } = React.useContext(ErrorHandlingContext);

const { t } = useTranslation(['treemapper', 'common', 'maps']);
const defaultValues = {
plantDate: new Date(),
plantDate: '',
plantProject: '',
geometry: {},
plantedSpecies: [
Expand Down Expand Up @@ -227,8 +238,12 @@
}
}, [contextLoaded]);

const normalizeGeoJson = (geoJson: any) => {
if (gjv.isGeoJSONObject(geoJson) && geoJson.features?.length > 0) {
const normalizeGeoJson = (geoJson: GeometryObject | FeatureCollection) => {
if (
gjv.isGeoJSONObject(geoJson) &&
'features' in geoJson &&
geoJson.features?.length > 0
) {
const flattened = flatten(geoJson);
if (flattened.features[0]?.geometry?.type === 'Polygon') {
setGeoJsonError(false);
Expand All @@ -246,8 +261,8 @@
}
};

const onDrop = React.useCallback((acceptedFiles: File[]) => {
acceptedFiles.forEach((file) => {
const onDrop = React.useCallback((acceptedFiles) => {
acceptedFiles.forEach((file: File) => {
const reader = new FileReader();
reader.readAsText(file);
reader.onabort = () => console.log('file reading was aborted');
Expand Down Expand Up @@ -304,15 +319,15 @@
onFileDialogCancel: () => setIsUploadingData(false),
});

const onSubmit = async (data: any) => {
const onSubmit = async (data: Treemapper.PlantLocation) => {
if (geoJson) {
setIsUploadingData(true);
const submitData = {
type: 'multi',
captureMode: 'external',
geometry: geoJson,
plantedSpecies: data.plantedSpecies,
plantDate: data.plantDate.toISOString(),
plantDate: new Date(data.plantDate).toISOString(),
registrationDate: new Date().toISOString(),
plantProject: data.plantProject,
};
Expand Down Expand Up @@ -437,7 +452,7 @@
onChange={onChange}
onBlur={onBlur}
value={value}
error={errors.plantProject}
error={errors.plantProject !== undefined}
helperText={errors.plantProject && errors.plantProject.message}
>
{projects.map((option) => (
Expand Down Expand Up @@ -492,6 +507,12 @@
append({
otherSpecies: '',
treeCount: 0,
// Set to default or empty value for type match
scientificName: '',
created: '',
scientificSpecies: '',
id: '',
updated: '',
});
}}
className={styles.addSpeciesButton}
Expand Down
12 changes: 5 additions & 7 deletions src/features/user/TreeMapper/Import/components/ReviewSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import styles from '../Import.module.scss';
import { useTranslation } from 'next-i18next';
import formatDate from '../../../../../utils/countryCurrency/getFormattedDate';
import { useRouter } from 'next/router';
import { PlantLocation } from '../../Treemapper';
import { Button } from '@mui/material';
import { Treemapper } from '../../Treemapper';

interface Props {
plantLocation: PlantLocation | null;
plantLocation: Treemapper.PlantLocation | null;
handleBack: () => void;
errorMessage: string;
setErrorMessage: (errorMessage: string) => void;
Expand All @@ -16,8 +16,6 @@ interface Props {
export default function ReviewSubmit({
plantLocation,
handleBack,
errorMessage,
setErrorMessage,
}: Props): ReactElement {
const router = useRouter();
const { t } = useTranslation(['treemapper', 'common']);
Expand Down Expand Up @@ -80,7 +78,7 @@ export default function ReviewSubmit({
<div className={styles.gridItemValue}>
<span>
{plantLocation.plantedSpecies
? plantLocation.plantedSpecies.map((species: any) => {
? plantLocation.plantedSpecies.map((species) => {
return (
<p key={species.id}>
{species.treeCount}{' '}
Expand All @@ -103,12 +101,12 @@ export default function ReviewSubmit({
<div className={styles.gridItemValue}>
{plantLocation.samplePlantLocations &&
plantLocation.samplePlantLocations.map(
(spl: any, index: number) => {
(spl, index: number) => {
return (
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
<div key={index} className={styles.value}>
{index + 1}.{' '}
<span className={styles.link}>
{spl.otherSpecies}
{'otherSpecies' in spl && spl.otherSpecies}
</span>
<br />
{spl.tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MobileDatePicker as MuiDatePicker } from '@mui/x-date-pickers/MobileDat
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import themeProperties from '../../../../../theme/themeProperties';
import { PlantLocation, PlantedSpecies } from '../../Treemapper';
import { Treemapper } from '../../Treemapper';

const dialogSx: SxProps = {
'& .MuiButtonBase-root.MuiPickersDay-root.Mui-selected': {
Expand All @@ -36,7 +36,7 @@ interface Props {
control: any;
userLang: string;
item: any;
plantLocation: PlantLocation;
plantLocation: Treemapper.PlantLocation | null;
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
errors: any;
key: string;
}
Expand All @@ -52,7 +52,7 @@ export default function SampleTreeCard({
errors,
}: Props): ReactElement {
const sampleTrees = getValues();
const { t, ready } = useTranslation(['treemapper', 'common']);
const { t } = useTranslation(['treemapper', 'common']);
return (
<div className={styles.sampleTreeFieldGroup}>
<div className={styles.sampleTreeName}>
Expand Down Expand Up @@ -251,9 +251,10 @@ export default function SampleTreeCard({
value={value}
select
>
{plantLocation.plantedSpecies.map(
(species: PlantedSpecies, index: number) => {
if (plantLocation.plantedSpecies.length === 1) {
{plantLocation?.plantedSpecies.map(
(species: Treemapper.PlantedSpecies, index: number) => {
if (!species.otherSpecies) return;
if (plantLocation?.plantedSpecies.length === 1) {
return (
<MenuItem
key={index}
Expand Down