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

Research: Add stricter type checking #2336

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const config = {
},
sourceType: 'module',
allowImportExportEverywhere: true,
project: ['tsconfig.json'],
},
rules: {
strict: 1,
Expand All @@ -53,6 +54,8 @@ const config = {
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,

'@typescript-eslint/no-unnecessary-condition': 'warn',

'no-console': 1,
indent: ['error', 4, { SwitchCase: 1 }],

Expand Down
2 changes: 1 addition & 1 deletion app/Base/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function Navbar(props: Props) {
});

const handleCloseNotificationClick = useCallback(() => {
notificationRef?.current?.setShowPopup(false);
notificationRef.current?.setShowPopup(false);
}, []);

useEffect(
Expand Down
11 changes: 6 additions & 5 deletions app/Base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '#base/utils/restRequest';
import 'mapbox-gl/dist/mapbox-gl.css';

import { find } from '#base/utils/common';
import localforageInstance from '#base/configs/localforage';
import { mapboxToken } from '#base/configs/env';

Expand Down Expand Up @@ -119,7 +120,7 @@ function Base() {
setAlerts((prevAlerts) => unique(
[...prevAlerts, alert],
(a) => a.name,
) ?? prevAlerts);
));
},
[setAlerts],
);
Expand All @@ -144,18 +145,18 @@ function Base() {
const updateAlertContent = React.useCallback(
(name: string, children: React.ReactNode) => {
setAlerts((prevAlerts) => {
const i = prevAlerts.findIndex((a) => a.name === name);
if (i === -1) {
const prevAlert = find(prevAlerts, (a) => a.name === name);
if (prevAlert.index === undefined) {
return prevAlerts;
}

const updatedAlert = {
...prevAlerts[i],
...prevAlert.value,
children,
};

const newAlerts = [...prevAlerts];
newAlerts.splice(i, 1, updatedAlert);
newAlerts.splice(prevAlert.index, 1, updatedAlert);

return newAlerts;
});
Expand Down
2 changes: 1 addition & 1 deletion app/Base/utils/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function isArrayEqual<T>(foo: readonly T[], bar: T[]) {

export function checkErrorCode(errors: ApolloError['graphQLErrors'], path: (string | number)[], code: string) {
return errors.some((error) => (
error.path && error.extensions?.code
error.path && error.extensions.code
&& isArrayEqual(error.path, path) && code === error.extensions.code
));
}
14 changes: 14 additions & 0 deletions app/Base/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// eslint-disable-next-line import/prefer-default-export
export function find<T>(list: T[], selector: (value: T, index: number, obj: T[]) => boolean) {
const index = list.findIndex(selector);
if (index === -1) {
return {
index: undefined,
value: undefined,
};
}
return {
index,
value: list[index] as T,
};
}
10 changes: 4 additions & 6 deletions app/Base/utils/restRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type FormDataCompatibleObj = Record<string, Literal | Literal[] | null | undefin

function getFormData(jsonData: FormDataCompatibleObj) {
const formData = new FormData();
Object.keys(jsonData || {}).forEach(
Object.keys(jsonData).forEach(
(key) => {
const value = jsonData?.[key];
const value = jsonData[key];
if (value && Array.isArray(value)) {
value.forEach((v) => {
formData.append(key, v instanceof Blob ? v : String(v));
Expand Down Expand Up @@ -178,9 +178,7 @@ export const processDeepOptions: DeepContextInterface['transformOptions'] = (
const csrftoken = getCookie(`deep-${deepEnvironment}-csrftoken`);

finalOptions.credentials = 'include';
if (finalOptions.headers) {
finalOptions.headers['X-CSRFToken'] = csrftoken;
}
finalOptions.headers['X-CSRFToken'] = csrftoken;
}

return finalOptions;
Expand Down Expand Up @@ -250,7 +248,7 @@ export const processDeepError = (
const faramErrors = alterResponse(res.errors);

const messageForNotification = (
faramErrors?.$internal
faramErrors.$internal
?? 'Some error occurred while performing this action.'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,24 @@ function GeoLocationMapInput(props: Props) {
skip: !projectId,
variables,
onCompleted: (data) => {
const [topRegion] = data.project?.regions?.filter(
if (!data.project?.regions) {
return;
}

const regions = data.project.regions.filter(
(region) => region.isPublished,
) ?? [];
const topAdminLevel = topRegion?.adminLevels?.find((v) => v.level === 0)
?? topRegion?.adminLevels?.[0];
);
const topRegion = regions[0];
if (!topRegion) {
// eslint-disable-next-line no-console
console.error('There must be at least on region');
return;
}

const topAdminLevel = topRegion.adminLevels?.find((v) => v.level === 0)
?? topRegion.adminLevels?.[0];

setSelectedRegion(topRegion?.id);
setSelectedRegion(topRegion.id);
setActiveAdminLevel(topAdminLevel?.id);
},
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/NonFieldError/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function NonFieldError(props: Props) {
);
}

const internalError = error?.[internal];
const internalError = error[internal];

if (!internalError) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions app/components/Notifications/NotificationContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ function NotificationContainer(props: Props) {
{
refetchQueries: ['UserNotificationsCount'],
onCompleted: (response) => {
if (response?.notificationStatusUpdate?.ok) {
const newStatus = response.notificationStatusUpdate?.result?.statusDisplay;
if (response.notificationStatusUpdate?.ok) {
const newStatus = response.notificationStatusUpdate.result?.statusDisplay;
alert.show(
`Successfully updated notification status as ${newStatus?.toLowerCase() ?? 'required'}.`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function ProjectJoinRequestItem(props: Props) {
{
refetchQueries: ['UserNotificationsCount'],
onCompleted: (response) => {
if (response?.project?.acceptRejectProject?.ok) {
const status = response.project.acceptRejectProject?.result?.status;
if (response.project?.acceptRejectProject?.ok) {
const status = response.project.acceptRejectProject.result?.status;
alert.show(
status === 'ACCEPTED'
? 'Successfully added user to the project.'
Expand Down Expand Up @@ -129,20 +129,20 @@ function ProjectJoinRequestItem(props: Props) {
<NotificationContainer
className={_cs(className, styles.projectJoinItem)}
notification={notification}
userName={data?.requested_by?.display_name}
userName={data.requested_by.display_name}
descriptionLabel="Reason"
description={data?.reason}
description={data.reason}
pendingRequests={loading}
content={
generateString(
'{requestorName} requested to join the project {projectTitle}.',
{
requestorName: (<b>{data?.requested_by?.display_name}</b>),
projectTitle: (<b>{data?.project?.title}</b>),
requestorName: (<b>{data.requested_by.display_name}</b>),
projectTitle: (<b>{data.project.title}</b>),
},
)
}
actions={data?.status === 'pending' && (
actions={data.status === 'pending' && (
<>
<Button
name={undefined}
Expand Down
Loading