Skip to content

Commit

Permalink
[DDW-1186] Shows 'To be defined' if date of the next fund is in the p…
Browse files Browse the repository at this point in the history
…ast (input-output-hk#3105)

* [DDW-1216] Use new Catalyst API

* [DDW-1216] Add changelog entry

* [DDW-1186] Now showing 'To be defined' if date of the next fund coming from API is in the past

* [DDW-1186] Changed JP translation

* [DDW-1186] Apply the latest requirements on showing "To be defined"

* [DDW-1186] Implement Catalyst testing utilities

* [DDW-1186] Fix handling of URL override for Catalyst

* [DDW-1186] Implement time machine for Catalyst voting testing

* [DDW-1186] Fix time machine

---------

Co-authored-by: Marcin Mazurek <marcin.mazurek@iohk.io>
  • Loading branch information
danielmain and Marcin Mazurek authored Jun 29, 2023
1 parent 2990883 commit 43b2b5e
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Show "To be defined" if date of the next Catalyst fund is in the past ([PR 3105](https://github.com/input-output-hk/daedalus/pull/3105))
- Switched to the new Catalyst API ([PR 3129](https://github.com/input-output-hk/daedalus/pull/3129))

### Fixes
Expand Down
2 changes: 2 additions & 0 deletions source/common/types/environment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type Environment = {
isBlankScreenFixActive: boolean;
keepLocalClusterRunning: boolean;
analyticsFeatureEnabled: boolean;
catalystApiUrlOverride?: string;
votingVisibleOverride: boolean;
};
// constants
export const PRODUCTION = 'production';
Expand Down
2 changes: 2 additions & 0 deletions source/main/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const environment: Environment = Object.assign(
keepLocalClusterRunning,
hasMetHardwareRequirements,
analyticsFeatureEnabled,
catalystApiUrlOverride: process.env.CATALYST_API_URL_OVERRIDE,
votingVisibleOverride: process.env.VOTING_VISIBLE_OVERRIDE === 'true',
},
process.env
);
14 changes: 10 additions & 4 deletions source/renderer/app/api/voting/requests/getCatalystFund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { externalRequest } from '../../utils/externalRequest';
import { CATALYST_API_URL } from '../../../config/urlsConfig';
import { GetCatalystFundResponse } from '../types';

export const getCatalystFund = (): Promise<GetCatalystFundResponse> =>
externalRequest({
hostname: CATALYST_API_URL,
export const getCatalystFund = (): Promise<GetCatalystFundResponse> => {
const urlOverride: URL | undefined = environment.catalystApiUrlOverride
? new URL(environment.catalystApiUrlOverride)
: undefined;

return externalRequest({
hostname: urlOverride ? urlOverride.hostname : CATALYST_API_URL,
path: '/api/v0/fund',
method: 'GET',
protocol: 'https',
port: urlOverride ? Number(urlOverride.port) : undefined,
protocol: urlOverride ? urlOverride.protocol.replace(':', '') : 'https',
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ export const messages = defineMessages({
defaultMessage: '!!!Register to vote',
description: 'Button Label for voting registration steps',
},
toBeDefined: {
id: 'voting.resultsPhase.toBeDefined',
defaultMessage: '!!!To be defined',
description:
'Text to show when Catalyst API is returning a past date value',
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { injectIntl } from 'react-intl';
import moment from 'moment';
import { Button } from 'react-polymorph/lib/components/Button';
import { Checkbox } from 'react-polymorph/lib/components/Checkbox';
import {
Expand All @@ -13,6 +14,7 @@ import { messages as votingMessages } from './VotingInfo.messages';
import styles from './RegisterToVote.scss';
import votingStyles from './VotingInfo.scss';
import type { CatalystFund } from '../../../api/voting/types';
import { logger } from '../../../utils/logging';

type Props = {
currentLocale: Locale;
Expand All @@ -23,6 +25,17 @@ type Props = {
onRegisterToVoteClick: (...args: Array<any>) => any;
};

const isFutureDate = (date: Date): boolean => {
try {
return moment().diff(date) < 0;
} catch (error) {
logger.error('Voting::NextFund::Invalid date', {
error,
});
}
return false;
};

function RegisterToVote({
currentLocale,
currentDateFormat,
Expand All @@ -34,17 +47,19 @@ function RegisterToVote({
const [step1, setStep1] = useState(false);
const [step2, setStep2] = useState(false);
const canRegister = step1 && step2;
const snapshotDate = formattedDateTime(
fundInfo.next.registrationSnapshotTime,
{
currentLocale,
...mapToLongDateTimeFormat({
const nextSnapshotDateTime = fundInfo.next.registrationSnapshotTime;

const snapshotDate = isFutureDate(nextSnapshotDateTime)
? formattedDateTime(nextSnapshotDateTime, {
currentLocale,
currentDateFormat,
currentTimeFormat,
}),
}
);
...mapToLongDateTimeFormat({
currentLocale,
currentDateFormat,
currentTimeFormat,
}),
})
: intl.formatMessage(messages.toBeDefined);

return (
<div className={styles.root}>
<span className={styles.title}>
Expand Down
4 changes: 3 additions & 1 deletion source/renderer/app/config/urlsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export const ALLOWED_EXTERNAL_HOSTNAMES = [
TESTNET_NEWS_HASH_URL,
STAGING_NEWS_HASH_URL,
coingeckoConfig.hostname,
CATALYST_API_URL,
environment.catalystApiUrlOverride
? new URL(environment.catalystApiUrlOverride).hostname
: CATALYST_API_URL,
];
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { inject, observer } from 'mobx-react';
import Layout from '../MainLayout';
import { VOTING_REGISTRATION_MIN_WALLET_FUNDS } from '../../config/votingConfig';
import VerticalFlexContainer from '../../components/layout/VerticalFlexContainer';
Expand Down
5 changes: 5 additions & 0 deletions source/renderer/app/i18n/locales/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4475,6 +4475,11 @@
"defaultMessage": "!!!Register to vote",
"description": "Button Label for voting registration steps",
"id": "voting.registerToVote.registerToVoteButtonLabel"
},
{
"defaultMessage": "!!!To be defined",
"description": "Text to show when Catalyst API is returning a past date value",
"id": "voting.resultsPhase.toBeDefined"
}
],
"path": "source/renderer/app/components/voting/voting-info/RegisterToVote.messages.ts"
Expand Down
1 change: 1 addition & 0 deletions source/renderer/app/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
"voting.registerToVote.step2CheckBoxLabel": "Ensure that you register and hold the necessary 500 ADA at the time of the snapshot.",
"voting.registerToVote.stepsTitle": "Follow these steps to vote:",
"voting.resultsPhase.endDateLabel": "End of voting:",
"voting.resultsPhase.toBeDefined": "To be defined",
"voting.resultsPhase.viewResultsLinkLabel": "View results",
"voting.resultsPhase.viewResultsLinkURL": "https://cardano.ideascale.com/a/pages/results",
"voting.snapshotPhase.snapshotDateLabel": "Snapshot date:",
Expand Down
1 change: 1 addition & 0 deletions source/renderer/app/i18n/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
"voting.registerToVote.step2CheckBoxLabel": "スナップショット実施の時点で、登録を済ませ、500ADAを保有していてください",
"voting.registerToVote.stepsTitle": "投票方法:",
"voting.resultsPhase.endDateLabel": "投票締め切り:",
"voting.resultsPhase.toBeDefined": "未定",
"voting.resultsPhase.viewResultsLinkLabel": "結果を見る",
"voting.resultsPhase.viewResultsLinkURL": "https://cardano.ideascale.com/a/pages/results",
"voting.snapshotPhase.snapshotDateLabel": "スナップショット日:",
Expand Down
3 changes: 2 additions & 1 deletion source/renderer/app/stores/SidebarStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export default class SidebarStore extends Store {
[categories.STAKING_DELEGATION_COUNTDOWN.name]: false,
[categories.STAKING.name]: true,
[categories.SETTINGS.name]: true,
[categories.VOTING.name]: isMainnet || isDev,
[categories.VOTING.name]:
isMainnet || isDev || environment.votingVisibleOverride,
[categories.NETWORK_INFO.name]: isFlight,
};
const categoriesFilteredList: Array<SidebarCategoryInfo> = list.filter(
Expand Down
42 changes: 42 additions & 0 deletions source/renderer/app/utils/PersistentTimeMachine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import timeMachine from 'timemachine';

// https://github.com/schickling/timemachine/issues/8
timeMachine.reset();

const TIME_MACHINE_SESSION_STORAGE_KEY = 'time_machine_date';

export class PersistentTimeMachine {
private _isInitialized = false;

init() {
const dateString = sessionStorage.getItem(TIME_MACHINE_SESSION_STORAGE_KEY);
if (dateString !== null) {
timeMachine.config({
dateString,
});
}
this._isInitialized = true;
}

enable(dateString: string) {
this._ensureIsInitialized();
timeMachine.config({
dateString,
});
sessionStorage.setItem(TIME_MACHINE_SESSION_STORAGE_KEY, dateString);
window.location.reload();
}

disable() {
this._ensureIsInitialized();
timeMachine.reset();
sessionStorage.removeItem(TIME_MACHINE_SESSION_STORAGE_KEY);
window.location.reload();
}

private _ensureIsInitialized() {
if (!this._isInitialized) {
throw new Error('Time machine must be initialized before use');
}
}
}
5 changes: 5 additions & 0 deletions source/renderer/app/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { generateMnemonic } from './crypto';
import { PersistentTimeMachine } from './PersistentTimeMachine';

const timeMachine = new PersistentTimeMachine();
timeMachine.init();

export default {
crypto: {
generateMnemonic,
},
timeMachine,
};
5 changes: 5 additions & 0 deletions translations/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4475,6 +4475,11 @@
"defaultMessage": "!!!Register to vote",
"description": "Button Label for voting registration steps",
"id": "voting.registerToVote.registerToVoteButtonLabel"
},
{
"defaultMessage": "!!!To be defined",
"description": "Text to show when Catalyst API is returning a past date value",
"id": "voting.resultsPhase.toBeDefined"
}
],
"path": "source/renderer/app/components/voting/voting-info/RegisterToVote.messages.ts"
Expand Down

0 comments on commit 43b2b5e

Please sign in to comment.