From cee223e604e1ff1baff9459c2482d9e5a15b29c0 Mon Sep 17 00:00:00 2001 From: Wille Marcel Date: Fri, 6 Nov 2020 07:24:24 -0300 Subject: [PATCH] Replace injectIntl by useIntl + replace props, by explicit object --- frontend/package.json | 2 +- frontend/src/components/header/index.js | 4 +- frontend/src/components/licenses/index.js | 13 +++--- frontend/src/components/localeSelect.js | 27 +++++------- .../src/components/projectCard/dueDateBox.js | 8 ++-- .../projectCard/tests/dueDateBox.test.js | 2 +- .../src/components/projectDetail/infoPanel.js | 2 +- .../projectStats/contributorsStats.js | 16 ++++---- frontend/src/components/projectStats/edits.js | 4 +- .../src/components/projectStats/taskStatus.js | 13 +++--- .../src/components/taskSelection/action.js | 2 +- .../components/taskSelection/contributions.js | 41 ++++++++----------- 12 files changed, 59 insertions(+), 75 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 4228ea7abf..f423e50e09 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -73,7 +73,7 @@ "build": "npm run preparation && npm run update-static && react-scripts build", "prettier": "prettier --write src/**/*.js", "lint": "eslint src", - "test": "npm run lint && react-scripts test --transformIgnorePatterns \"node_modules/(?!mapbox-gl-draw-circle)/\" --env=jsdom-fourteen", + "test": "npm run lint && react-scripts test --env=jsdom-fourteen", "coverage": "npm run test -- --coverage --watchAll=false" }, "eslintConfig": { diff --git a/frontend/src/components/header/index.js b/frontend/src/components/header/index.js index 0340bc3cdc..43a4cc363c 100644 --- a/frontend/src/components/header/index.js +++ b/frontend/src/components/header/index.js @@ -48,11 +48,11 @@ function getMenuItensForUser(userDetails, organisations) { return filteredMenuItems; } -const UserDisplay = (props) => { +const UserDisplay = ({ username }) => { return ( - {props.username} + {username} ); }; diff --git a/frontend/src/components/licenses/index.js b/frontend/src/components/licenses/index.js index 887b16ca8e..05163102b0 100644 --- a/frontend/src/components/licenses/index.js +++ b/frontend/src/components/licenses/index.js @@ -49,7 +49,7 @@ export const LicensesManagement = ({ licenses, userDetails }) => { ); }; -export const LicenseInformation = (props) => { +export const LicenseInformation = () => { const labelClasses = 'db pt3 pb2'; const fieldClasses = 'blue-grey w-100 pv3 ph2 input-reset ba b--grey-light bg-transparent'; @@ -73,13 +73,13 @@ export const LicenseInformation = (props) => { ); }; -export const LicenseForm = (props) => { +export const LicenseForm = ({ license, updateLicense, disabledForm }) => { const [editMode, setEditMode] = useState(false); return (
props.updateLicense(values)} - initialValues={props.license} + onSubmit={(values) => updateLicense(values)} + initialValues={license} render={({ handleSubmit, pristine, form, submitting, values }) => { return (
@@ -89,10 +89,7 @@ export const LicenseForm = (props) => { -
+
diff --git a/frontend/src/components/localeSelect.js b/frontend/src/components/localeSelect.js index cbc111a13c..a2d5bb367f 100644 --- a/frontend/src/components/localeSelect.js +++ b/frontend/src/components/localeSelect.js @@ -7,19 +7,17 @@ import { Dropdown } from './dropdown'; import { supportedLocales } from '../utils/internationalization'; import { setLocale } from '../store/actions/userPreferences'; -function LocaleSelect(props) { - const onLocaleSelect = arr => { - props.setLocale(arr[0].value); +function LocaleSelect({ userPreferences, setLocale, className }) { + const onLocaleSelect = (arr) => { + setLocale(arr[0].value); }; const getActiveLanguageNames = () => { - const locales = [ - props.userPreferences.locale, - navigator.language, - navigator.language.substr(0, 2), - ]; + const locales = [userPreferences.locale, navigator.language, navigator.language.substr(0, 2)]; let supportedLocaleNames = []; - locales.forEach(locale => - supportedLocales.filter(i => i.value === locale).forEach(i => supportedLocaleNames.push(i)), + locales.forEach((locale) => + supportedLocales + .filter((i) => i.value === locale) + .forEach((i) => supportedLocaleNames.push(i)), ); return supportedLocaleNames.length ? supportedLocaleNames[0].value : 'en'; }; @@ -31,18 +29,15 @@ function LocaleSelect(props) { value={getActiveLanguageNames()} options={supportedLocales} display={} - className={`blue-dark bg-white mr1 v-mid pv2 ph3 ${props.className}`} + className={`blue-dark bg-white mr1 v-mid pv2 ph3 ${className}`} /> ); } -const mapStateToProps = state => ({ +const mapStateToProps = (state) => ({ userPreferences: state.preferences, }); -const LocaleSelector = connect( - mapStateToProps, - { setLocale }, -)(LocaleSelect); +const LocaleSelector = connect(mapStateToProps, { setLocale })(LocaleSelect); export { LocaleSelector }; diff --git a/frontend/src/components/projectCard/dueDateBox.js b/frontend/src/components/projectCard/dueDateBox.js index dd044589e2..4a4bf8e7a3 100644 --- a/frontend/src/components/projectCard/dueDateBox.js +++ b/frontend/src/components/projectCard/dueDateBox.js @@ -1,11 +1,12 @@ import React, { useState, useEffect } from 'react'; -import { FormattedMessage, injectIntl } from 'react-intl'; +import { FormattedMessage, useIntl } from 'react-intl'; import humanizeDuration from 'humanize-duration'; import { ClockIcon } from '../svgIcons'; import messages from './messages'; -function DueDateBox({ intl, dueDate, intervalMili, align = 'right' }: Object) { +export function DueDateBox({ dueDate, intervalMili, align = 'right' }: Object) { + const intl = useIntl(); const [timer, setTimer] = useState(Date.now()); useEffect(() => { let interval; @@ -72,6 +73,3 @@ function DueDateBox({ intl, dueDate, intervalMili, align = 'right' }: Object) { return null; } } - -//decorator pattern to provide the intl object from IntlProvider into function props. -export default injectIntl(DueDateBox, { forwardRef: true }); diff --git a/frontend/src/components/projectCard/tests/dueDateBox.test.js b/frontend/src/components/projectCard/tests/dueDateBox.test.js index 18f7c271e7..622a0ce91d 100644 --- a/frontend/src/components/projectCard/tests/dueDateBox.test.js +++ b/frontend/src/components/projectCard/tests/dueDateBox.test.js @@ -2,7 +2,7 @@ import React from 'react'; import TestRenderer from 'react-test-renderer'; import { IntlProvider } from 'react-intl'; -import DueDateBox from '../../../components/projectCard/dueDateBox'; +import { DueDateBox } from '../../../components/projectCard/dueDateBox'; const createComponentWithIntl = (children, props = { locale: 'en' }) => { return TestRenderer.create({children}); diff --git a/frontend/src/components/projectDetail/infoPanel.js b/frontend/src/components/projectDetail/infoPanel.js index 7bf33dcd9a..526363adc9 100644 --- a/frontend/src/components/projectDetail/infoPanel.js +++ b/frontend/src/components/projectDetail/infoPanel.js @@ -6,7 +6,7 @@ import messages from './messages'; import { MappingTypes } from '../mappingTypes'; import { Imagery } from '../taskSelection/imagery'; import ProjectProgressBar from '../projectCard/projectProgressBar'; -import DueDateBox from '../projectCard/dueDateBox'; +import { DueDateBox } from '../projectCard/dueDateBox'; import { MappingLevelMessage } from '../mappingLevel'; import { BigProjectTeaser } from './bigProjectTeaser'; import { useComputeCompleteness } from '../../hooks/UseProjectCompletenessCalc'; diff --git a/frontend/src/components/projectStats/contributorsStats.js b/frontend/src/components/projectStats/contributorsStats.js index 3209b03b6a..a72b54de46 100644 --- a/frontend/src/components/projectStats/contributorsStats.js +++ b/frontend/src/components/projectStats/contributorsStats.js @@ -1,6 +1,6 @@ import React from 'react'; import { Doughnut, Bar } from 'react-chartjs-2'; -import { FormattedMessage, injectIntl } from 'react-intl'; +import { FormattedMessage, useIntl } from 'react-intl'; import messages from './messages'; import userMessages from '../user/messages'; @@ -9,11 +9,11 @@ import { formatChartData, formatTooltip } from '../../utils/formatChartJSData'; import { useContributorStats } from '../../hooks/UseContributorStats'; import { StatsCardContent } from '../statsCardContent'; -function ContributorsStats(props) { - const stats = useContributorStats(props.contributors.userContributions); - const getUserLevelLabel = (level) => - props.intl.formatMessage(userMessages[`mapperLevel${level}`]); - const getUserExpLabel = (id) => props.intl.formatMessage(messages[`${id}`]); +export default function ContributorsStats({ contributors }) { + const intl = useIntl(); + const stats = useContributorStats(contributors.userContributions); + const getUserLevelLabel = (level) => intl.formatMessage(userMessages[`mapperLevel${level}`]); + const getUserExpLabel = (id) => intl.formatMessage(messages[`${id}`]); let userLevelsReference = [ { @@ -78,7 +78,7 @@ function ContributorsStats(props) { className="pv3-l pv2 mb3 shadow-4 bg-white" /> } className="pv3-l pv2 mb3 shadow-4 bg-white" /> @@ -115,5 +115,3 @@ function ContributorsStats(props) {
); } - -export default injectIntl(ContributorsStats); diff --git a/frontend/src/components/projectStats/edits.js b/frontend/src/components/projectStats/edits.js index 0d0e63f12f..e11685b44b 100644 --- a/frontend/src/components/projectStats/edits.js +++ b/frontend/src/components/projectStats/edits.js @@ -51,8 +51,8 @@ const Element = ({ field, value }) => { ); }; -export const EditsStats = (props) => { - const { changesets, buildings, roads, edits } = props.data; +export const EditsStats = ({ data }) => { + const { changesets, buildings, roads, edits } = data; return (
diff --git a/frontend/src/components/projectStats/taskStatus.js b/frontend/src/components/projectStats/taskStatus.js index 50a5355654..5b6f59d2ce 100644 --- a/frontend/src/components/projectStats/taskStatus.js +++ b/frontend/src/components/projectStats/taskStatus.js @@ -1,6 +1,6 @@ import React from 'react'; import { Doughnut } from 'react-chartjs-2'; -import { FormattedMessage, injectIntl } from 'react-intl'; +import { FormattedMessage, useIntl } from 'react-intl'; import statusMessages from '../taskSelection/messages'; import messages from './messages'; @@ -8,8 +8,9 @@ import { formatChartData, formatTooltip } from '../../utils/formatChartJSData'; import { TASK_COLOURS } from '../../config'; import { StatsCardContent } from '../statsCardContent'; -const TasksByStatus = (props) => { - const getLabel = (status) => props.intl.formatMessage(statusMessages[`taskStatus_${status}`]); +const TasksByStatus = ({ stats }) => { + const intl = useIntl(); + const getLabel = (status) => intl.formatMessage(statusMessages[`taskStatus_${status}`]); let reference = [ { @@ -54,7 +55,7 @@ const TasksByStatus = (props) => { borderColor: TASK_COLOURS.BADIMAGERY, }, ]; - const data = formatChartData(reference, props.stats); + const data = formatChartData(reference, stats); return (
@@ -75,7 +76,7 @@ const TasksByStatus = (props) => { {reference.map((status, n) => ( @@ -86,4 +87,4 @@ const TasksByStatus = (props) => { ); }; -export default injectIntl(TasksByStatus); +export default TasksByStatus; diff --git a/frontend/src/components/taskSelection/action.js b/frontend/src/components/taskSelection/action.js index 0b801371bd..a79e7c8a24 100644 --- a/frontend/src/components/taskSelection/action.js +++ b/frontend/src/components/taskSelection/action.js @@ -17,7 +17,7 @@ import { TaskHistory } from './taskActivity'; import { ChangesetCommentTags } from './changesetComment'; import { useSetProjectPageTitleTag } from '../../hooks/UseMetaTags'; import { useFetch } from '../../hooks/UseFetch'; -import DueDateBox from '../projectCard/dueDateBox'; +import { DueDateBox } from '../projectCard/dueDateBox'; import { CompletionTabForMapping, CompletionTabForValidation, diff --git a/frontend/src/components/taskSelection/contributions.js b/frontend/src/components/taskSelection/contributions.js index 208da8cf97..98e831b562 100644 --- a/frontend/src/components/taskSelection/contributions.js +++ b/frontend/src/components/taskSelection/contributions.js @@ -2,7 +2,7 @@ import React, { useState } from 'react'; import { Link } from '@reach/router'; import Select from 'react-select'; import ReactPlaceholder from 'react-placeholder'; -import { FormattedMessage, injectIntl, useIntl } from 'react-intl'; +import { FormattedMessage, useIntl } from 'react-intl'; import messages from './messages.js'; import { UserAvatar } from '../user/avatar'; @@ -114,31 +114,30 @@ function Contributor({ user, activeUser, activeStatus, displayTasks }: Object) { ); } -const Contributions = (props) => { +const Contributions = ({ project, tasks, contribsData, activeUser, activeStatus, selectTask }) => { + const intl = useIntl(); const mappingLevels = [ - { value: 'ALL', label: props.intl.formatMessage(messages.mappingLevelALL) }, - { value: 'ADVANCED', label: props.intl.formatMessage(messages.mappingLevelADVANCED) }, - { value: 'INTERMEDIATE', label: props.intl.formatMessage(messages.mappingLevelINTERMEDIATE) }, - { value: 'BEGINNER', label: props.intl.formatMessage(messages.mappingLevelBEGINNER) }, - { value: 'NEWUSER', label: props.intl.formatMessage(messages.mappingLevelNEWUSER) }, + { value: 'ALL', label: intl.formatMessage(messages.mappingLevelALL) }, + { value: 'ADVANCED', label: intl.formatMessage(messages.mappingLevelADVANCED) }, + { value: 'INTERMEDIATE', label: intl.formatMessage(messages.mappingLevelINTERMEDIATE) }, + { value: 'BEGINNER', label: intl.formatMessage(messages.mappingLevelBEGINNER) }, + { value: 'NEWUSER', label: intl.formatMessage(messages.mappingLevelNEWUSER) }, ]; const [level, setLevel] = useState(mappingLevels[0]); - const { percentMapped, percentValidated, percentBadImagery } = useComputeCompleteness( - props.tasks, - ); + const { percentMapped, percentValidated, percentBadImagery } = useComputeCompleteness(tasks); - let contributionsArray = props.contribsData.userContributions || []; + let contributionsArray = contribsData.userContributions || []; if (level.value !== 'ALL' && level.value !== 'NEWUSER') { contributionsArray = contributionsArray.filter((u) => u.mappingLevel === level.value); } const displayTasks = (taskIds, status, user) => { - if (props.activeStatus === status && user === props.activeUser) { - props.selectTask([]); + if (activeStatus === status && user === activeUser) { + selectTask([]); return; } - props.selectTask(taskIds, status, user); + selectTask(taskIds, status, user); }; if (level.value === 'NEWUSER') { @@ -159,18 +158,14 @@ const Contributions = (props) => { percentBadImagery={percentBadImagery} className="pt1 pb3" /> - + · - +