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

Add missing type annotations & catch unhandled exceptions #2382

Merged
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
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import ZUIFuture from 'zui/ZUIFuture';
import ZUISection from 'zui/ZUISection';
import ZUITimeline from 'zui/ZUITimeline';
import { scaffold, ScaffoldedGetServerSideProps } from 'utils/next';
import { ZetkinJourneyInstance, ZetkinOrganization } from 'utils/types/zetkin';
import { ZetkinJourneyInstance } from 'utils/types/zetkin';

export const scaffoldOptions = {
authLevelRequired: 2,
@@ -31,34 +31,28 @@ export const getJourneyInstanceScaffoldProps: ScaffoldedGetServerSideProps =
async (ctx) => {
const { orgId, instanceId, journeyId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const journeyInstance = await apiClient.get<ZetkinJourneyInstance>(
`/api/orgs/${orgId}/journey_instances/${instanceId}`
);
const organization = await apiClient.get<ZetkinOrganization>(
`/api/orgs/${orgId}`
);
try {
const apiClient = new BackendApiClient(ctx.req.headers);
const journeyInstance = await apiClient.get<ZetkinJourneyInstance>(
`/api/orgs/${orgId}/journey_instances/${instanceId}`
);

if (
journeyInstance &&
journeyInstance.journey.id.toString() !== (journeyId as string)
) {
return {
redirect: {
destination: `/organize/${orgId}/journeys/${journeyInstance.journey.id}/${instanceId}`,
permanent: false,
},
};
}

if (organization && journeyInstance) {
return {
props: {
instanceId,
orgId,
},
};
} else {
if (journeyInstance.journey.id.toString() !== (journeyId as string)) {
return {
redirect: {
destination: `/organize/${orgId}/journeys/${journeyInstance.journey.id}/${instanceId}`,
permanent: false,
},
};
} else {
return {
props: {
instanceId,
orgId,
},
};
}
} catch {
return {
notFound: true,
};
13 changes: 6 additions & 7 deletions src/pages/organize/[orgId]/journeys/[journeyId]/closed.tsx
Original file line number Diff line number Diff line change
@@ -21,16 +21,15 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId, journeyId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const journey = await apiClient.get<ZetkinJourney>(
`/api/orgs/${orgId}/journeys/${journeyId}`
);

if (journey) {
try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get<ZetkinJourney>(
`/api/orgs/${orgId}/journeys/${journeyId}`
);
return {
props: {},
};
} else {
} catch {
return {
notFound: true,
};
13 changes: 6 additions & 7 deletions src/pages/organize/[orgId]/journeys/[journeyId]/index.tsx
Original file line number Diff line number Diff line change
@@ -21,16 +21,15 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId, journeyId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const journey = await apiClient.get<ZetkinJourney>(
`/api/orgs/${orgId}/journeys/${journeyId}`
);

if (journey) {
try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get<ZetkinJourney>(
`/api/orgs/${orgId}/journeys/${journeyId}`
);
return {
props: {},
};
} else {
} catch {
return {
notFound: true,
};
15 changes: 7 additions & 8 deletions src/pages/organize/[orgId]/journeys/[journeyId]/new.tsx
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ import ZUIEditTextinPlace from 'zui/ZUIEditTextInPlace';
import ZUIFuture from 'zui/ZUIFuture';
import ZUISubmitCancelButtons from 'zui/ZUISubmitCancelButtons';
import { Msg, useMessages } from 'core/i18n';
import { ZetkinPerson, ZetkinTag } from 'utils/types/zetkin';
import { ZetkinJourney, ZetkinPerson, ZetkinTag } from 'utils/types/zetkin';

const scaffoldOptions = {
authLevelRequired: 2,
@@ -34,20 +34,19 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId, journeyId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const journey = await apiClient.get(
`/api/orgs/${orgId}/journeys/${journeyId}`
);
const organization = await apiClient.get(`/api/orgs/${orgId}`);
try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get<ZetkinJourney>(
`/api/orgs/${orgId}/journeys/${journeyId}`
);

if (organization && journey) {
return {
props: {
journeyId,
orgId,
},
};
} else {
} catch {
return {
notFound: true,
};
11 changes: 5 additions & 6 deletions src/pages/organize/[orgId]/journeys/index.tsx
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import { scaffold } from 'utils/next';
import useJourneys from 'features/journeys/hooks/useJourneys';
import { useMessages } from 'core/i18n';
import { useNumericRouteParams } from 'core/hooks';
import { ZetkinJourney } from 'utils/types/zetkin';
import { ZetkinJourney, ZetkinOrganization } from 'utils/types/zetkin';
import ZUISection from 'zui/ZUISection';

const scaffoldOptions = {
@@ -22,17 +22,16 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const journeys = await apiClient.get(`/api/orgs/${orgId}/journeys`);
const organization = await apiClient.get(`/api/orgs/${orgId}`);
try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get<ZetkinOrganization>(`/api/orgs/${orgId}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This request should not be necessary.


if (organization && journeys) {
return {
props: {
orgId,
},
};
} else {
} catch {
return {
notFound: true,
};
3 changes: 2 additions & 1 deletion src/pages/organize/[orgId]/people/[personId]/index.tsx
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import useTagging from 'features/tags/hooks/useTagging';
import ZUIFuture from 'zui/ZUIFuture';
import ZUISnackbarContext from 'zui/ZUISnackbarContext';
import { scaffold, ScaffoldedGetServerSideProps } from 'utils/next';
import { ZetkinPerson } from 'utils/types/zetkin';

export const scaffoldOptions = {
authLevelRequired: 2,
@@ -34,7 +35,7 @@ export const getPersonScaffoldProps: ScaffoldedGetServerSideProps = async (

try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get(`/api/orgs/${orgId}/people/${personId}`);
await apiClient.get<ZetkinPerson>(`/api/orgs/${orgId}/people/${personId}`);
return {
props: {
orgId,
16 changes: 9 additions & 7 deletions src/pages/organize/[orgId]/people/folders/[folderId].tsx
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { scaffold } from 'utils/next';
import { useMessages } from 'core/i18n';
import ViewBrowser from 'features/views/components/ViewBrowser';
import messageIds from 'features/views/l10n/messageIds';
import { ZetkinViewFolder } from 'features/views/components/types';

const scaffoldOptions = {
authLevelRequired: 2,
@@ -17,19 +18,20 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId, folderId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const folder = await apiClient.get(
`/api/orgs/${orgId}/people/view_folders/${folderId}`
);

if (folder) {
try {
const apiClient = new BackendApiClient(ctx.req.headers);
// Note: We don't actually care for the returned folder, but we still want to perform
// the api request to know if this user may access this particular folder.
await apiClient.get<ZetkinViewFolder>(
`/api/orgs/${orgId}/people/view_folders/${folderId}`
);
return {
props: {
folderId,
orgId,
},
};
} else {
} catch {
return {
notFound: true,
};
10 changes: 5 additions & 5 deletions src/pages/organize/[orgId]/people/index.tsx
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import { scaffold } from 'utils/next';
import { useMessages } from 'core/i18n';
import useServerSide from 'core/useServerSide';
import ViewBrowser from 'features/views/components/ViewBrowser';
import { ZetkinView } from 'features/views/components/types';

const scaffoldOptions = {
authLevelRequired: 2,
@@ -18,16 +19,15 @@ const scaffoldOptions = {
export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const views = await apiClient.get(`/api/orgs/${orgId}/people/views`);

if (views) {
try {
const apiClient = new BackendApiClient(ctx.req.headers);
await apiClient.get<ZetkinView[]>(`/api/orgs/${orgId}/people/views`);
return {
props: {
orgId,
},
};
} else {
} catch {
return {
notFound: true,
};
14 changes: 11 additions & 3 deletions src/pages/organize/[orgId]/people/lists/[viewId]/index.tsx
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import useView from 'features/views/hooks/useView';
import useViewGrid from 'features/views/hooks/useViewGrid';
import ViewDataTable from 'features/views/components/ViewDataTable';
import ZUIFutures from 'zui/ZUIFutures';
import { ZetkinView } from 'features/views/components/types';

const scaffoldOptions = {
allowNonOfficials: true,
@@ -23,9 +24,16 @@ export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
const { orgId, viewId } = ctx.params!;

const apiClient = new BackendApiClient(ctx.req.headers);
const view = await apiClient.get(`/api/orgs/${orgId}/people/views/${viewId}`);

if (view) {
// Try to fetch the view as the current user. If this is unsuccessful, and error object will
// be returned and the apiClient will throw an error for us to catch.
try {
// Note: We don't actually care for the returned view, but we still want to perform
// the api request to know if this user may access this particular view.
await apiClient.get<ZetkinView>(
`/api/orgs/${orgId}/people/views/${viewId}`
);

// Check if user is an official
// TODO: Consider moving this to some more general-purpose utility
const officialMemberships = await getUserMemberships(ctx, false);
@@ -51,7 +59,7 @@ export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
},
};
}
} else {
} catch {
return {
notFound: true,
};
5 changes: 4 additions & 1 deletion src/pages/organize/[orgId]/people/lists/[viewId]/shared.tsx
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import ViewDataTable from 'features/views/components/ViewDataTable';
import { ZetkinMembership } from 'utils/types/zetkin';
import { ZetkinObjectAccess } from 'core/api/types';
import ZUIFutures from 'zui/ZUIFutures';
import { ZetkinView } from 'features/views/components/types';

const scaffoldOptions = {
allowNonOfficials: true,
@@ -63,7 +64,9 @@ export const getServerSideProps: GetServerSideProps = scaffold(async (ctx) => {
);

try {
await apiClient.get(`/api/orgs/${orgId}/people/views/${viewId}`);
await apiClient.get<ZetkinView>(
`/api/orgs/${orgId}/people/views/${viewId}`
);

return {
props: {