Skip to content

Commit

Permalink
Merge pull request #1656 from dedis/work-fe1-mgreub-fix-roll-call-iss…
Browse files Browse the repository at this point in the history
…ues2

Fix roll call issues
  • Loading branch information
pierluca authored Jun 8, 2023
2 parents 2ad2237 + 5b017ff commit 1490247
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
28 changes: 15 additions & 13 deletions fe1-web/src/features/rollCall/components/RollCallOpen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ const RollCallOpen = ({

const [popToken, setPopToken] = useState('');
const [hasWalletBeenInitialized, setHasWalletBeenInitialized] = useState(hasSeed());
const allAttendees = useMemo(() => {
if (isOrganizer && popToken !== '') {
if (scannedPopTokens && scannedPopTokens.length > 0) {
return scannedPopTokens;
}
return [PublicKey.fromState(popToken)];
}
return scannedPopTokens || [];
}, [isOrganizer, popToken, scannedPopTokens]);

const onAddAttendees = useCallback(() => {
// Once the roll call is opened the first time, idAlias is defined
Expand All @@ -71,20 +80,13 @@ const RollCallOpen = ({
screen: STRINGS.events_open_roll_call,
params: {
rollCallId: rollCall.id.toString(),
attendeePopTokens: (scannedPopTokens || []).map((e) => e.valueOf()),
attendeePopTokens: allAttendees.map((e) => e.valueOf()),
},
},
});
}, [toast, navigation, rollCall, scannedPopTokens]);
}, [toast, navigation, rollCall, allAttendees]);

const onCloseRollCall = useCallback(async () => {
// get the public key as strings from the existing rollcall
const previousAttendees = rollCall.attendees || [];
// add the create a set of all attendees (takes care of deduplication)
const allAttendees = new Set([...previousAttendees, ...(scannedPopTokens || [])]);
// convert it back to a list
const attendeesList = [...allAttendees];

if (!rollCall.idAlias) {
toast.show(STRINGS.roll_call_error_close_roll_call_no_alias, {
type: 'danger',
Expand All @@ -96,7 +98,7 @@ const RollCallOpen = ({
}

try {
await requestCloseRollCall(laoId, rollCall.idAlias, attendeesList);
await requestCloseRollCall(laoId, rollCall.idAlias, allAttendees);
navigation.navigate(STRINGS.navigation_lao_events_home);
} catch (err) {
console.log(err);
Expand All @@ -106,7 +108,7 @@ const RollCallOpen = ({
duration: FOUR_SECONDS,
});
}
}, [toast, navigation, rollCall, laoId, scannedPopTokens]);
}, [toast, navigation, rollCall, laoId, allAttendees]);

const toolbarItems: ToolbarItem[] = useMemo(() => {
if (!isOrganizer) {
Expand All @@ -115,6 +117,7 @@ const RollCallOpen = ({

return [
{
id: 'roll_call_close_button',
title: STRINGS.roll_call_close,
onPress: onCloseRollCall,
buttonStyle: 'secondary',
Expand All @@ -133,7 +136,6 @@ const RollCallOpen = ({
const unsubscribe = navigation.addListener('focus', () => {
setHasWalletBeenInitialized(hasSeed());
});

// Return the function to unsubscribe from the event so it gets removed on unmount
return unsubscribe;
}, [navigation, hasSeed]);
Expand Down Expand Up @@ -199,7 +201,7 @@ const RollCallOpen = ({
</>
)}

{scannedPopTokens && <AttendeeList popTokens={scannedPopTokens} personalToken={popToken} />}
{scannedPopTokens && <AttendeeList popTokens={allAttendees} personalToken={popToken} />}
</ScreenWrapper>
);
};
Expand Down
21 changes: 0 additions & 21 deletions fe1-web/src/features/rollCall/screens/RollCallScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const RollCallOpened = () => {
const toast = useToast();

const laoId = RollCallHooks.useCurrentLaoId();
const generateToken = RollCallHooks.useGenerateToken();

const rollCallSelector = useMemo(() => makeRollCallSelector(new Hash(rollCallId)), [rollCallId]);
const rollCall = useSelector(rollCallSelector);
Expand Down Expand Up @@ -110,18 +109,6 @@ const RollCallOpened = () => {
throw new Error('Impossible to open a Roll Call that does not exist');
}

const handleError = useCallback(
(err: string | Error) => {
console.error(err.toString());
toast.show(err.toString(), {
type: 'danger',
placement: 'bottom',
duration: FOUR_SECONDS,
});
},
[toast],
);

const addAttendeePopToken = useCallback(
(popToken: string) => {
// if the token is already part of attendeePopTokens, do not trigger a state update
Expand Down Expand Up @@ -174,14 +161,6 @@ const RollCallOpened = () => {
}
};

// This will run only when the state changes
useEffect(() => {
// Add the token of the organizer as soon as we open the roll call
generateToken(laoId, rollCall.id)
.then((popToken) => addAttendeePopToken(popToken.publicKey.valueOf()))
.catch(handleError);
}, [laoId, generateToken, rollCall, addAttendeePopToken, handleError]);

return (
<>
<QrCodeScanner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import RollCallScanner, { RollCallOpenedHeaderLeft } from '../RollCallScanner';

const mockPublicKey2 = new PublicKey('mockPublicKey2_fFcHDaVHcCcY8IBfHE7auXJ7h4ms=');
const mockPublicKey3 = new PublicKey('mockPublicKey3_fFcHDaVHcCcY8IBfHE7auXJ7h4ms=');
const mockOrganizerPublicKey = new PublicKey('mockOrganizerPublicKey_fFcHDaVHcCcY8IBfHE7a=');

jest.mock('@react-navigation/core', () => {
const actualNavigation = jest.requireActual('@react-navigation/core');
Expand Down Expand Up @@ -80,14 +81,12 @@ const mockStore = configureStore({
mockStore.dispatch(setCurrentLao(mockLao));
mockStore.dispatch(addRollCall(mockRollCall.toState()));

const mockGenerateToken = jest.fn(() => Promise.resolve(mockPopToken));

const contextValue = {
[ROLLCALL_FEATURE_IDENTIFIER]: {
useCurrentLaoId: () => mockLaoId,
useConnectedToLao: () => true,
makeEventByTypeSelector: makeEventByTypeSelector,
generateToken: mockGenerateToken,
generateToken: jest.fn(),
hasSeed: () => getWalletState(mockStore.getState()).seed !== undefined,
} as RollCallReactContext,
};
Expand All @@ -98,13 +97,18 @@ const didFocus = () =>
.filter(([eventName]) => eventName === 'focus')
.forEach((args) => args[1]());

const renderRollCallOpened = (mockAttendeePopTokens?: string[]) => {
const renderRollCallOpened = (
mockAttendeePopTokens: string[] = [mockOrganizerPublicKey.toString()],
) => {
const renderedRollCallOpened = render(
<Provider store={mockStore}>
<FeatureContext.Provider value={contextValue}>
<MockNavigator
component={RollCallScanner}
params={{ rollCallId, attendeePopTokens: mockAttendeePopTokens || [] }}
params={{
rollCallId,
attendeePopTokens: mockAttendeePopTokens,
}}
screenOptions={{ headerLeft: RollCallOpenedHeaderLeft }}
/>
</FeatureContext.Provider>
Expand All @@ -125,7 +129,6 @@ describe('RollCallOpened', () => {
const { toJSON } = renderRollCallOpened();

await waitFor(() => {
expect(mockGenerateToken).toHaveBeenCalled();
expect(toJSON()).toMatchSnapshot();
});
});
Expand All @@ -140,7 +143,6 @@ describe('RollCallOpened', () => {
// scan invalid pop tokens
fakeQrReaderScan('123');
fakeQrReaderScan('456');
expect(mockGenerateToken).toHaveBeenCalled();
});

expect(toJSON()).toMatchSnapshot();
Expand All @@ -152,10 +154,6 @@ describe('RollCallOpened', () => {
fakeQrReaderScan(mockPublicKey2.valueOf());
fakeQrReaderScan(mockPublicKey3.valueOf());

await waitFor(async () => {
expect(mockGenerateToken).toHaveBeenCalled();
});

expect(mockToastShow).toHaveBeenCalledTimes(2);
});

Expand Down Expand Up @@ -199,10 +197,6 @@ describe('RollCallOpened', () => {
'roll_call_open_stop_scanning',
);

await waitFor(() => {
expect(mockGenerateToken).toHaveBeenCalled();
});

fireEvent.press(button);

expect(navigate).toHaveBeenCalledWith(expect.anything(), {
Expand All @@ -218,7 +212,6 @@ describe('RollCallOpened', () => {
renderRollCallOpened(mockAttendeePopTokens);

await waitFor(() => {
expect(mockGenerateToken).toHaveBeenCalled();
fakeQrReaderScan(ScannablePopToken.encodePopToken({ pop_token: mockPublicKey2.valueOf() }));
fakeQrReaderScan(ScannablePopToken.encodePopToken({ pop_token: mockPublicKey3.valueOf() }));
});
Expand All @@ -243,7 +236,6 @@ describe('RollCallOpened', () => {
await waitFor(() => {
fakeQrReaderScan(ScannablePopToken.encodePopToken({ pop_token: mockPublicKey2.valueOf() }));
fakeQrReaderScan(ScannablePopToken.encodePopToken({ pop_token: mockPublicKey3.valueOf() }));
expect(mockGenerateToken).toHaveBeenCalled();
});

// counter should be at 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { combineReducers } from 'redux';
import MockNavigator from '__tests__/components/MockNavigator';
import { mockLao, mockLaoId } from '__tests__/utils';
import FeatureContext from 'core/contexts/FeatureContext';
import { Hash, Timestamp } from 'core/objects';
import { Hash, PublicKey, Timestamp } from 'core/objects';
import { addEvent, eventReducer, makeEventByTypeSelector } from 'features/events/reducer';
import { laoReducer, setCurrentLao } from 'features/lao/reducer';
import { mockRollCall } from 'features/rollCall/__tests__/utils';
Expand All @@ -16,6 +16,7 @@ import { addRollCall, rollCallReducer, updateRollCall } from 'features/rollCall/
import { generateToken } from 'features/wallet/objects';
import { getWalletState, walletReducer } from 'features/wallet/reducer';

import { requestCloseRollCall } from '../../network';
import { RollCall, RollCallStatus } from '../../objects';
import ViewSingleRollCall, { ViewSingleRollCallScreen } from '../ViewSingleRollCall';

Expand Down Expand Up @@ -43,6 +44,13 @@ const createStateWithStatus: any = (mockStatus: RollCallStatus) => {
idAlias: mockStatus === RollCallStatus.CREATED ? undefined : ID.valueOf(),
};
};
jest.mock('features/rollCall/network', () => {
const actual = jest.requireActual('features/rollCall/network');
return {
...actual,
requestCloseRollCall: jest.fn(() => Promise.resolve()),
};
});

const mockRollCallCreated = RollCall.fromState(createStateWithStatus(RollCallStatus.CREATED));
const mockRollCallOpened = RollCall.fromState(createStateWithStatus(RollCallStatus.OPENED));
Expand Down Expand Up @@ -135,6 +143,30 @@ describe('EventRollCall', () => {
're-opened roll calls',
testRender(mockRollCallReopened, false, ['attendee1', 'attendee2']),
);
it('while closing roll call', () => {
mockStore.dispatch(updateRollCall(mockRollCallReopened.toState()));
const { getByTestId } = render(
<Provider store={mockStore}>
<FeatureContext.Provider value={contextValue}>
<MockNavigator
component={ViewSingleRollCall}
params={{
eventId: mockRollCallReopened.id.valueOf(),
isOrganizer: true,
attendeePopTokens: ATTENDEES,
}}
/>
</FeatureContext.Provider>
</Provider>,
);
fireEvent.press(getByTestId('roll_call_close_button'));
expect(requestCloseRollCall).toHaveBeenCalledTimes(1);
expect(requestCloseRollCall).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
ATTENDEES.map((attendee) => PublicKey.fromState(attendee)),
);
});
});

describe('return button', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ exports[`RollCallOpened renders correctly when no scan 1`] = `
>
Scanned attendees
:
-1
0
</Text>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7833,6 +7833,7 @@ exports[`EventRollCall render correctly organizers opened roll calls 1`] = `
"opacity": 1,
}
}
testID="roll_call_close_button"
>
<View
style={
Expand Down Expand Up @@ -8843,6 +8844,8 @@ exports[`EventRollCall render correctly organizers re-opened roll calls 1`] = `
"opacity": 1,
}
}
testID="roll_call_close_button"
>
<View
style={
Expand Down

0 comments on commit 1490247

Please sign in to comment.