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 12 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
5 changes: 3 additions & 2 deletions cypress/fixtures/dummyEventResponses.json
@@ -1,8 +1,9 @@
{
"data": [
{
"action_id": 22,
"response_date": "1111 11 11, 11:11",
"action_id": 25,
"response_date": "2021-01-21T18:30:00+00:00",
"organization_id": 1,
"person": {
"name": "Dummy User",
"id": 2
Expand Down
63 changes: 57 additions & 6 deletions cypress/integration/org_event_page.spec.ts
@@ -1,28 +1,79 @@
describe('/o/[orgId]/events/[eventId]', () => {
beforeEach(() => {
cy.request('delete', 'http://localhost:8001/_mocks');
});

after(() => {
cy.request('delete', 'http://localhost:8001/_mocks');
});

it('contains non-interactive event content', () => {
cy.visit('/o/1/events/22');
cy.visit('/o/1/events/25');
cy.get('[data-test="event-title"]').should('be.visible');
cy.get('[data-test="duration"]').should('be.visible');
cy.get('[data-test="location"]').should('be.visible');
});

it('contains clickable org name that leads to org page', () => {
cy.visit('/o/1/events/22');
cy.visit('/o/1/events/25');
cy.waitUntilReactRendered();
cy.findByText('My Organization').click();
cy.url().should('match', /\/o\/1$/);
});

it('contains clickable campaign name that leads to campaign page', () => {
cy.visit('/o/1/events/22');
cy.visit('/o/1/events/25');
cy.waitUntilReactRendered();
cy.findByText('Second campaign').click();
cy.url().should('match', /\/o\/1\/campaigns\/2$/);
});

it('contains a sign-up button', () => {
cy.visit('/o/1/events/22');
cy.findByText('Sign-up').should('be.visible');
it('shows a sign-up button if user is not signed up to the event', () => {
cy.request('put', 'http://localhost:8001/v1/users/me/action_responses/_mocks/get', {
response: {
data: {
data: [],
},
},
});

cy.visit('/login');
cy.get('input[aria-label="E-mail address"]').type('testadmin@example.com');
cy.get('input[aria-label="Password"]').type('password');
cy.get('input[aria-label="Log in"]')
.click();

cy.visit('/o/1/events/25');
cy.waitUntilReactRendered();
cy.findByText('Sign-up').click();
//TODO: Verify that API request is done corrently.
});

it('shows an undo sign-up button if user is signed up to the event', () => {
cy.fixture('dummyEventResponses').then(json => {
cy.request('put', 'http://localhost:8001/v1/users/me/action_responses/_mocks/get', {
response: {
data: json,
},
});

cy.request('put', 'http://localhost:8001/v1/orgs/1/actions/25/responses/2/_mocks/delete', {
response: {
status: 204,
},
});

cy.visit('/login');
cy.get('input[aria-label="E-mail address"]').type('testadmin@example.com');
cy.get('input[aria-label="Password"]').type('password');
cy.get('input[aria-label="Log in"]')
.click();

cy.visit('/o/1/events/25');
cy.waitUntilReactRendered();
cy.findByText('Undo sign-up').click();
//TODO: Verify that API request is done corrently.
});
});
});

Expand Down
57 changes: 49 additions & 8 deletions cypress/integration/org_events_page.spec.ts
Expand Up @@ -3,12 +3,17 @@ describe('/o/[orgId]/events', () => {
cy.request('delete', 'http://localhost:8001/_mocks');
});

after(() => {
cy.request('delete', 'http://localhost:8001/_mocks');
});

it('contains name of organization', () => {
cy.visit('/o/1/events');
cy.waitUntilReactRendered();
cy.contains('My Organization');
});

it.only('contains events which are linked to event pages', () => {
it('contains events which are linked to event pages', () => {
cy.request('put', 'http://localhost:8001/v1/orgs/1/campaigns/_mocks/get', {
response: {
data: {
Expand Down Expand Up @@ -38,7 +43,7 @@ describe('/o/[orgId]/events', () => {
});

it('contains a placeholder if there are no events', () => {
cy.request('put', 'http://localhost:8001/v1/orgs/1/campaigns/1/actions/_mocks/get', {
cy.request('put', 'http://localhost:8001/v1/orgs/1/campaigns/_mocks/get', {
response: {
data: {
data: [],
Expand All @@ -50,18 +55,54 @@ describe('/o/[orgId]/events', () => {
cy.get('[data-test="no-events-placeholder"]').should('be.visible');
});

it('contains conditional functionality for sign-up button', () => {
it('shows a sign-up button if user is not signed up to an event', () => {
cy.request('put', 'http://localhost:8001/v1/users/me/action_responses/_mocks/get', {
response: {
data: {
data: [],
},
},
});

cy.visit('/login');
cy.get('input[aria-label="E-mail address"]').type('testadmin@example.com');
cy.get('input[aria-label="Password"]').type('password');
cy.get('input[aria-label="Log in"]')
.click();

cy.visit('/o/1/events');
cy.get('[data-test="sign-up-button"]')
.eq(5)
.contains('Sign-up')
.click()
.contains('Undo sign-up');
cy.waitUntilReactRendered();
cy.get('[data-test="event-response-button"]')
.eq(4)
.click();
//TODO: Verify that API request is done corrently.
});

it('shows an undo sign-up button if user is signed up to an event', () => {
cy.fixture('dummyEventResponses').then(json => {
cy.request('put', 'http://localhost:8001/v1/users/me/action_responses/_mocks/get', {
response: {
data: json,
},
});

cy.request('put', 'http://localhost:8001/v1/orgs/1/actions/25/responses/2/_mocks/delete', {
response: {
status: 204,
},
});

cy.visit('/login');
cy.get('input[aria-label="E-mail address"]').type('testadmin@example.com');
cy.get('input[aria-label="Password"]').type('password');
cy.get('input[aria-label="Log in"]')
.click();

cy.visit('/o/1/events');
cy.waitUntilReactRendered();
cy.findByText('Undo sign-up').click();
//TODO: Verify that API request is done corrently.
});
});
});

Expand Down
8 changes: 1 addition & 7 deletions src/components/EventList.spec.tsx
Expand Up @@ -74,10 +74,6 @@ describe('EventList', () => {
/>,
);

//Checks for buttons on all events
cy.findByText('misc.eventList.signup');

//Tests button on a single event
cy.findByText('misc.eventList.signup')
.eq(0)
.click()
Expand All @@ -87,13 +83,11 @@ describe('EventList', () => {
});

it('contains a button for more info on each event', () => {
const spyOnSubmit = cy.spy();

mountWithProviders(
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
onEventResponse={ () => null }
org={ dummyOrg }
/>,
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/EventList.tsx
Expand Up @@ -67,15 +67,15 @@ const EventList = ({ eventResponses, events, onEventResponse, org } : EventListP
<View data-test="location-title">{ e.location.title }</View>
{ response ? (
<Button
data-test="undo-sign-up-button"
data-test="event-response-button"
marginTop="size-50"
onPress={ () => onEventResponse(e.id, org.id, true) }
variant="cta">
<Msg id="misc.eventList.undoSignup"/>
</Button>
) : (
<Button
data-test="sign-up-button"
data-test="event-response-button"
marginTop="size-50"
onPress={ () => onEventResponse(e.id, org.id, false) }
variant="cta">
Expand Down
36 changes: 24 additions & 12 deletions src/fetching/deleteEventResponse.ts
@@ -1,17 +1,29 @@
import apiUrl from '../utils/apiUrl';

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

export default async function deleteEventResponse({ eventId, orgId } : ZetkinEventSignup) : Promise<void> {
const mUrl = apiUrl('/users/me/memberships');
const mRes = await fetch(mUrl);
const mData = await mRes.json();
const orgMembership = mData.data.find((m : ZetkinMembership) => m.organization.id === orgId);
function defaultFetch(path : string, init? : RequestInit) {
const url = apiUrl(path);
return fetch(url, init);
}
Copy link
Member

Choose a reason for hiding this comment

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

This reoccurs in many locations, and should probably moved to a shared module and reused. Perhaps in src/fetching/index.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree!


if (orgMembership) {
const url = apiUrl(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`);
await fetch(url, {
method: 'DELETE',
});
}
interface MutationVariables {
eventId: number;
orgId: number;
}

export default function deleteEventResponse(fetch = defaultFetch) {
return async ({ eventId, orgId } : MutationVariables) : Promise<void> => {
const mRes = await fetch('/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}`, {
method: 'DELETE',
});
}
};

}
11 changes: 8 additions & 3 deletions src/fetching/getEvent.ts
@@ -1,13 +1,18 @@
import apiUrl from '../utils/apiUrl';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';

export default function getEvent(orgId : string, eventId : string) {
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(apiUrl(`/orgs/${orgId}/campaigns`));
const cRes = await fetch(`/orgs/${orgId}/campaigns`);
const cData = await cRes.json();

for (const obj of cData.data) {
const eventsRes = await fetch(apiUrl(`/orgs/${orgId}/campaigns/${obj.id}/actions`));
const eventsRes = await fetch(`/orgs/${orgId}/campaigns/${obj.id}/actions`);
const campaignEvents = await eventsRes.json();
const eventData = campaignEvents.data.find((event : ZetkinEvent) => event.id.toString() === eventId);
if (eventData) {
Expand Down
16 changes: 11 additions & 5 deletions src/fetching/getEventResponses.ts
Expand Up @@ -2,9 +2,15 @@ import apiUrl from '../utils/apiUrl';

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

export default async function getEventResponses() : Promise<ZetkinEventResponse[]> {
const rUrl = apiUrl('/users/me/action_responses');
const rRes = await fetch(rUrl);
const rData = await rRes.json();
return rData.data;
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');
const rData = await rRes.json();
return rData.data;
};
}
11 changes: 8 additions & 3 deletions src/fetching/getEvents.ts
@@ -1,15 +1,20 @@
import apiUrl from '../utils/apiUrl';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';

export default function getEvents(orgId : string) {
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(apiUrl(`/orgs/${orgId}/campaigns`));
const cRes = await fetch(`/orgs/${orgId}/campaigns`);
const cData = await cRes.json();

let allEventsData : ZetkinEvent[] = [];

for (const obj of cData.data) {
const eventsRes = await fetch(apiUrl(`/orgs/${orgId}/campaigns/${obj.id}/actions`));
const eventsRes = await fetch(`/orgs/${orgId}/campaigns/${obj.id}/actions`);
const campaignEvents = await eventsRes.json();
allEventsData = allEventsData.concat(campaignEvents.data);
}
Expand Down
10 changes: 7 additions & 3 deletions src/fetching/getOrg.ts
Expand Up @@ -2,10 +2,14 @@ import apiUrl from '../utils/apiUrl';

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

export default function getOrg(orgId : string) {
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 url = apiUrl(`/orgs/${orgId}`);
const oRes = await fetch(url);
const oRes = await fetch(`/orgs/${orgId}`);
const oData = await oRes.json();
return oData.data;
};
Expand Down