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 6 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
13 changes: 13 additions & 0 deletions cypress/fixtures/dummyEventResponses.json
@@ -0,0 +1,13 @@
{
"data": [
{
"action_id": 22,
"response_date": "1111 11 11, 11:11",
Copy link
Member

Choose a reason for hiding this comment

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

I dislike the use of this very weird date. Why the year 1111, and not something more alike to what would occur in live data? It's also not using the correct ISO format. The API delivers dates like 2021-01-21T18:30:00+00:00, and the dummy data should mimic that.

"person": {
"name": "Dummy User",
"id": 2
},
"id": 2
}
]
}
10 changes: 10 additions & 0 deletions cypress/integration/org_events_page.spec.ts
Expand Up @@ -22,6 +22,16 @@ describe('/o/[orgId]/events', () => {
cy.visit('/o/1/events');
cy.get('[data-test="no-events-placeholder"]').should('be.visible');
});

//TODO: Figure out how to make this work. Requires login?
xit('contains conditional sign-up/undo sign-up button functionality for event sign-up', () => {
cy.visit('/o/1/events');
cy.get('[data-test="sign-up-button"]')
.eq(5)
.contains('Sign-up')
.click()
.contains('Undo sign-up');
});
});

// Hack to flag for typescript as module
Expand Down
1 change: 1 addition & 0 deletions cypress/integration/reocurring_functionality.spec.ts
@@ -1,4 +1,5 @@
describe('Reocurring functionality', () => {

it('contains a clickable org logo which leads to org page', () => {
cy.visit('/o/1/events');
cy.get('[data-test="org-avatar"]')
Expand Down
71 changes: 64 additions & 7 deletions src/components/EventList.spec.tsx
@@ -1,11 +1,13 @@
import EventList from './EventList';
import { mountWithProviders } from '../utils/testing';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';
import { ZetkinEventResponse } from '../types/zetkin';
import { ZetkinOrganization } from '../interfaces/ZetkinOrganization';

describe('EventList', () => {
let dummyOrg : ZetkinOrganization;
let dummyEvents : ZetkinEvent[];
let dummyEventResponses : ZetkinEventResponse[];

beforeEach(()=> {
cy.fixture('dummyOrg.json')
Expand All @@ -16,11 +18,22 @@ describe('EventList', () => {
.then((data : {data: ZetkinEvent[]}) => {
dummyEvents = data.data;
});
cy.fixture('dummyEventResponses.json')
.then((data : {data: ZetkinEventResponse[]}) => {
dummyEventResponses = data.data;
});
});

it('contains data for each event', () => {
const spyOnSubmit = cy.spy();
Copy link
Member

Choose a reason for hiding this comment

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

The naming is off, implies that there is an onSubmit-callback. Is it a rest product from another test?

Copy link
Member

Choose a reason for hiding this comment

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

Also, there should be no need for a spy in this test (or the subsequent ones) where the spy is not actually used for spying.


mountWithProviders(
<EventList events={ dummyEvents } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

cy.get('[data-test="event"]').each((item) => {
Expand All @@ -36,8 +49,15 @@ describe('EventList', () => {

it('contains an activity title instead of missing event title', () => {
dummyEvents[0].title = undefined;
const spyOnSubmit = cy.spy();

mountWithProviders(
<EventList events={ dummyEvents } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

cy.get('[data-test="event"]')
Expand All @@ -46,33 +66,70 @@ describe('EventList', () => {
});

it('contains a sign-up button for each event', () => {
const spyOnSubmit = cy.spy();

mountWithProviders(
<EventList events={ dummyEvents } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

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

Choose a reason for hiding this comment

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

What is the purpose of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see now it's unnecessary since line 81 is already doing the same thing. Will remove this!


//Tests button on a single event
cy.findByText('misc.eventList.signup')
.eq(0)
.click()
.then(() => {
expect(spyOnSubmit).to.be.calledOnce;
richardolsson marked this conversation as resolved.
Show resolved Hide resolved
});
});

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

Choose a reason for hiding this comment

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

Why the spy here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like I missed this when removing spies, will remove.


mountWithProviders(
<EventList events={ dummyEvents } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

cy.contains('misc.eventList.moreInfo');
});

it('shows a placeholder when the list is empty', () => {
const spyOnSubmit = cy.spy();

dummyEvents = [];
mountWithProviders(
<EventList events={ dummyEvents } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ dummyEvents }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

cy.contains('misc.eventList.placeholder');
});

it('shows a placeholder when the list is undefined', () => {
const spyOnSubmit = cy.spy();

mountWithProviders(
<EventList events={ undefined } org={ dummyOrg }/>,
<EventList
eventResponses={ dummyEventResponses }
events={ undefined }
onEventResponse={ spyOnSubmit }
org={ dummyOrg }
/>,
);

cy.contains('misc.eventList.placeholder');
Expand Down
104 changes: 63 additions & 41 deletions src/components/EventList.tsx
Expand Up @@ -11,15 +11,20 @@ import {
FormattedMessage as Msg,
} from 'react-intl';

import { OnEventResponse } from '../types/misc';
import { ZetkinEvent } from '../interfaces/ZetkinEvent';
import { ZetkinEventResponse } from '../types/zetkin';
import { ZetkinOrganization } from '../interfaces/ZetkinOrganization';

interface EventListProps {
events: ZetkinEvent[] | undefined;
org: ZetkinOrganization | undefined;
org: ZetkinOrganization;
eventResponses: ZetkinEventResponse[] | undefined;
onEventResponse: OnEventResponse;
}

const EventList = ({ events, org } : EventListProps) : JSX.Element => {
const EventList = ({ eventResponses, events, onEventResponse, org } : EventListProps) : JSX.Element => {

if (!events || events.length === 0) {
return (
<Text data-test="no-events-placeholder">
Expand All @@ -31,46 +36,63 @@ const EventList = ({ events, org } : EventListProps) : JSX.Element => {
return (
<>
<Flex data-test="event-list" direction="row" gap="100" wrap>
{ events?.map((e) => (
<Flex key={ e.id } data-test="event" direction="column" margin="size-200">
<View data-test="event-title">
{ e.title ? e.title : e.activity.title }
</View>
<View data-test="org-title">{ org?.title }</View>
<View data-test="campaign-title">{ e.campaign.title }</View>
<View data-test="start-time">
<FormattedDate
day="2-digit"
month="long"
value={ Date.parse(e.start_time) }
/>
, <FormattedTime
value={ Date.parse(e.start_time) }
/>
</View>
<View data-test="end-time">
<FormattedDate
day="2-digit"
month="long"
value={ Date.parse(e.end_time) }
/>
, <FormattedTime
value={ Date.parse(e.end_time) }
/>
</View>
<View data-test="location-title">{ e.location.title }</View>
<Button data-test="sign-up-button" marginTop="size-50" variant="cta">
<Msg id="misc.eventList.signup"/>
</Button>
<NextLink href={ `/o/${org?.id}/events/${e.id}` }>
<a>
<Button marginTop="size-50" variant="cta">
<Msg id="misc.eventList.moreInfo"/>
{ events?.map((e) => {
const response = eventResponses?.find(response => response.action_id === e.id);
return (
<Flex key={ e.id } data-test="event" direction="column" margin="size-200">
<View data-test="event-title">
{ e.title ? e.title : e.activity.title }
</View>
<View data-test="org-title">{ org.title }</View>
<View data-test="campaign-title">{ e.campaign.title }</View>
<View data-test="start-time">
<FormattedDate
day="2-digit"
month="long"
value={ Date.parse(e.start_time) }
/>
, <FormattedTime
value={ Date.parse(e.start_time) }
/>
</View>
<View data-test="end-time">
<FormattedDate
day="2-digit"
month="long"
value={ Date.parse(e.end_time) }
/>
, <FormattedTime
value={ Date.parse(e.end_time) }
/>
</View>
<View data-test="location-title">{ e.location.title }</View>
{ response ? (
<Button
data-test="undo-sign-up-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"
marginTop="size-50"
onPress={ () => onEventResponse(e.id, org.id, false) }
variant="cta">
<Msg id="misc.eventList.signup"/>
</Button>
</a>
</NextLink>
</Flex>
)) }
) }
<NextLink href={ `/o/${org.id}/events/${e.id}` }>
<a>
<Button marginTop="size-50" variant="cta">
<Msg id="misc.eventList.moreInfo"/>
</Button>
</a>
</NextLink>
</Flex>
);
}) }
</Flex>
</>
);
Expand Down
17 changes: 17 additions & 0 deletions src/fetching/deleteEventResponse.ts
@@ -0,0 +1,17 @@
import apiUrl from '../utils/apiUrl';

import { ZetkinEventSignup, 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);
Copy link
Member

Choose a reason for hiding this comment

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

This solution is a hack. Maybe leave it like this for now, but add a TODO comment or an issue so we remember to get back to it. It should not be necessary to retrieve the memberships every time. They should be cached.


if (orgMembership) {
const url = apiUrl(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`);
await fetch(url, {
method: 'DELETE',
});
}
}
10 changes: 10 additions & 0 deletions src/fetching/getEventResponses.ts
@@ -0,0 +1,10 @@
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;
}
21 changes: 21 additions & 0 deletions src/fetching/putEventResponse.ts
@@ -0,0 +1,21 @@
import apiUrl from '../utils/apiUrl';

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

export default async function putEventResponse({ eventId, orgId } : ZetkinEventSignup) : Promise<ZetkinEventResponse> {
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);

if (orgMembership) {
const eventUrl = apiUrl(`/orgs/${orgId}/actions/${eventId}/responses/${orgMembership.profile.id}`);
const eventRes = await fetch(eventUrl, {
method: 'PUT',
});
const eventData = await eventRes.json();
return eventData.data;
}

throw 'no membership';
}
3 changes: 2 additions & 1 deletion src/locale/misc/eventList/en.yml
@@ -1,3 +1,4 @@
placeholder: Sorry, there are no planned events at the moment.
moreInfo: More info
signup: Sign-up
signup: Sign-up
undoSignup: Undo sign-up
3 changes: 2 additions & 1 deletion src/locale/misc/eventList/sv.yml
@@ -1,3 +1,4 @@
placeholder: Tyvärr finns det inga planerade händelser för tillfället.
moreInfo: Mer info
signup: Anmälan
signup: Anmälan
undoSignup: Ångra anmälan
2 changes: 1 addition & 1 deletion src/locale/pages/orgEvent/en.yml
@@ -1,2 +1,2 @@
actions:
signUp: Sign-up
signUp: Sign-up
2 changes: 1 addition & 1 deletion src/locale/pages/orgEvent/sv.yml
@@ -1,2 +1,2 @@
actions:
signUp: Anmälan
signUp: Anmälan