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

Sign up to event #104

Merged
merged 25 commits into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
451c9bd
Add sign-up functionality for event
kristofferlarberg Mar 25, 2021
6c32cd1
Refactor fetch functions, change name for DELETE function
kristofferlarberg Mar 30, 2021
786eaf8
Remove undefined from prop types and move non-null assertion to org p…
kristofferlarberg Mar 30, 2021
b19d8df
Remove eslint-disable from EventList
kristofferlarberg Mar 30, 2021
82da026
Update cache when response PUT/DELETE and conditionally render signup…
kristofferlarberg Apr 1, 2021
2a336c8
Refactor functionality and general structure for conditional sign-up …
kristofferlarberg Apr 12, 2021
c85776c
Add login procedure in button test
kristofferlarberg Apr 14, 2021
c83f3fe
Remove unnecessary spies
kristofferlarberg Apr 14, 2021
db23b2c
Refactor useMutation functionality to reusable hook
kristofferlarberg Apr 15, 2021
f75a42e
Make small changes in integration and component tests for events page…
kristofferlarberg Apr 16, 2021
cdd458a
Include eventResponses in useOnEventResponse
kristofferlarberg Apr 16, 2021
3dea616
Add TODO
kristofferlarberg Apr 16, 2021
7d82701
Apply useEventResponses on event page
kristofferlarberg Apr 16, 2021
748c496
Add space before block
kristofferlarberg Apr 16, 2021
2f21830
Merge branch 'main' into issue-93/sign-up-to-event
kristofferlarberg Apr 16, 2021
6e5a17d
Make integration-tests for events/event pages less flaky
kristofferlarberg Apr 16, 2021
2ee9245
Merge branch 'issue-93/sign-up-to-event' of https://github.com/zetkin…
kristofferlarberg Apr 16, 2021
01a5889
Add user attribute to context
kristofferlarberg Apr 19, 2021
0949317
Refactor events/event page related fetch functions and conditionally …
kristofferlarberg Apr 19, 2021
8970016
Adjust integration tests for sign-up button
kristofferlarberg Apr 19, 2021
3e94a79
Move user attribute to after reqWithSession, remove else stement for …
kristofferlarberg Apr 20, 2021
876a5b1
Make defaultFetch shared module, remove fetch injection in put- and d…
kristofferlarberg Apr 20, 2021
79a4b05
Extract user data object as ctx.user and reuse with augmentProps
kristofferlarberg Apr 20, 2021
6423f59
Refactor back putEventResponse and deleteEventResponse to not returni…
kristofferlarberg Apr 21, 2021
8f02e54
Add type assertion to user object, remove augmentProps function
kristofferlarberg Apr 21, 2021
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
12 changes: 3 additions & 9 deletions src/fetching/deleteEventResponse.ts
@@ -1,26 +1,20 @@
import apiUrl from '../utils/apiUrl';

import { ZetkinMembership } from '../types/zetkin';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

interface MutationVariables {
eventId: number;
orgId: number;
}

export default function deleteEventResponse(fetch = defaultFetch) {
export default function deleteEventResponse() {
return async ({ eventId, orgId } : MutationVariables) : Promise<void> => {
const mRes = await fetch('/users/me/memberships');
const mRes = await fetch(apiUrl('/users/me/memberships'));
const mData = await mRes.json();
//TODO: Memberships should be cached.
const orgMembership = mData.data.find((m : ZetkinMembership ) => m.organization.id === orgId);

if (orgMembership) {
await fetch(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`, {
await fetch(apiUrl(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`), {
method: 'DELETE',
});
}
Expand Down
7 changes: 1 addition & 6 deletions src/fetching/getEvent.ts
@@ -1,11 +1,6 @@
import apiUrl from '../utils/apiUrl';
import { defaultFetch } from '.';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

export default function getEvent(orgId : string, eventId : string, fetch = defaultFetch) {
return async () : Promise<ZetkinEvent> => {
const cRes = await fetch(`/orgs/${orgId}/campaigns`);
Expand Down
8 changes: 1 addition & 7 deletions src/fetching/getEventResponses.ts
@@ -1,12 +1,6 @@
import apiUrl from '../utils/apiUrl';

import { defaultFetch } from '.';
import { ZetkinEventResponse } from '../types/zetkin';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

export default function getEventResponses(fetch = defaultFetch) {
return async () : Promise<ZetkinEventResponse[]> => {
const rRes = await fetch('/users/me/action_responses');
Expand Down
7 changes: 1 addition & 6 deletions src/fetching/getEvents.ts
@@ -1,11 +1,6 @@
import apiUrl from '../utils/apiUrl';
import { defaultFetch } from '.';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

export default function getEvents(orgId : string, fetch = defaultFetch) {
return async () : Promise<ZetkinEvent[]> => {
const cRes = await fetch(`/orgs/${orgId}/campaigns`);
Expand Down
8 changes: 1 addition & 7 deletions src/fetching/getOrg.ts
@@ -1,12 +1,6 @@
import apiUrl from '../utils/apiUrl';

import { defaultFetch } from '.';
import { ZetkinOrganization } from '../interfaces/ZetkinOrganization';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

export default function getOrg(orgId : string, fetch = defaultFetch) {
return async () : Promise<ZetkinOrganization> => {
const oRes = await fetch(`/orgs/${orgId}`);
Expand Down
6 changes: 6 additions & 0 deletions src/fetching/index.ts
@@ -0,0 +1,6 @@
import apiUrl from '../utils/apiUrl';

export function defaultFetch(path : string, init? : RequestInit) : Promise<Response> {
const url = apiUrl(path);
return fetch(url, init);
}
12 changes: 3 additions & 9 deletions src/fetching/putEventResponse.ts
@@ -1,26 +1,20 @@
import apiUrl from '../utils/apiUrl';

import { ZetkinEventResponse, ZetkinMembership } from '../types/zetkin';

function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}

interface MutationVariables {
eventId: number;
orgId: number;
}

export default function putEventResponse(fetch = defaultFetch) {
export default function putEventResponse() {
Copy link
Member

Choose a reason for hiding this comment

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

Why return a function in this function? It should not be necessary to wrap the actual operations of this function in an inner function which is then returned. Just run the code in the body of putEventResponse and pass around putEventResponse instead of passing around the return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a side-effect of the refactoring for server side rendering. But since neither the put or delete functions are run server side I'll revert these back to their previous state (same as you describe).

return async ({ eventId, orgId } : MutationVariables) : Promise<ZetkinEventResponse> => {
const mRes = await fetch('/users/me/memberships');
const mRes = await fetch(apiUrl('/users/me/memberships'));
const mData = await mRes.json();
//TODO: Memberships should be cached.
const orgMembership = mData.data.find((m : ZetkinMembership ) => m.organization.id === orgId);

if (orgMembership) {
const eventRes = await fetch(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`, {
const eventRes = await fetch(apiUrl(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`), {
method: 'PUT',
});
const eventResData = await eventRes.json();
Expand Down
10 changes: 5 additions & 5 deletions src/utils/next.ts
Expand Up @@ -7,7 +7,7 @@ import { AppSession } from '../types';
import { getMessages } from './locale';
import stringToBool from './stringToBool';
import { ZetkinUser } from '../interfaces/ZetkinUser';
import { ZetkinZ, ZetkinZResult } from '../types/sdk';
import { ZetkinZ } from '../types/sdk';

//TODO: Create module definition and revert to import.
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -26,7 +26,7 @@ export type ScaffoldedProps = RegularProps & {

export type ScaffoldedContext = GetServerSidePropsContext & {
apiFetch: (path : string, init? : RequestInit) => Promise<Response>;
user: ZetkinZResult | null;
user: unknown;
Copy link
Member

Choose a reason for hiding this comment

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

This should be typed as ZetkinUser.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I tried that, but get the following:

Type 'unknown' is not assignable to type 'ZetkinUser'.

z: ZetkinZ;
};

Expand Down Expand Up @@ -76,7 +76,8 @@ export const scaffold = (wrapped : ScaffoldedGetServerSideProps, options? : Scaf
}

try {
ctx.user = await ctx.z.resource('users', 'me').get();
const user = await ctx.z.resource('users', 'me').get();
ctx.user = user.data.data;
}
catch (error) {
ctx.user = null;
Expand Down Expand Up @@ -104,8 +105,7 @@ export const scaffold = (wrapped : ScaffoldedGetServerSideProps, options? : Scaf
};

try {
const user = await ctx.z.resource('users', 'me').get();
augmentProps(user.data.data as ZetkinUser);
augmentProps(ctx.user as ZetkinUser);
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't look right. The try used to be there to verify that the user could be retrieved. If it couldn't, the catch would run augmentProps(null). Now, you are not doing anything here that should raise an exception, so the try is no longer relevant.

The typecast should also not be necessary, as ctx.user should be typed correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, sorry I missed this, looking at it now it's obvious.

}
catch (error) {
augmentProps(null);
Expand Down