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

lib-user: Setup react-i18next for MainContent components #6561

Open
wants to merge 1 commit into
base: translations-shared-two
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions packages/lib-user/src/components/shared/MainContent/MainContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Anchor, Box, Calendar, ResponsiveContext, Text } from 'grommet'
import { arrayOf, bool, func, number, shape, string } from 'prop-types'
import { useCallback, useContext, useEffect, useState } from 'react'
import Link from 'next/link'
import { useTranslation, Trans } from '../../../translations/i18n.js'

import {
convertStatsSecondsToHours,
Expand Down Expand Up @@ -51,6 +52,7 @@ function MainContent({
source = DEFAULT_SOURCE,
totalProjects = 0
}) {
const { t } = useTranslation()
const [activeTab, setActiveTab] = useState(0)
const [showCalendar, setShowCalendar] = useState(false)
const [customDateRange, setCustomDateRange] = useState([selectedDateRange.startDate, selectedDateRange.endDate])
Expand All @@ -74,10 +76,11 @@ function MainContent({
const { dateRangeOptions, selectedDateRangeOption } = getDateRangeSelectOptions({
sourceCreatedAtDate,
paramsValidationMessage,
selectedDateRange
selectedDateRange,
t
})

const { projectOptions, selectedProjectOption } = getProjectSelectOptions({ projects, selectedProject })
const { projectOptions, selectedProjectOption } = getProjectSelectOptions({ projects, selectedProject, t })

const todayUTC = getStatsDateString(new Date())

Expand Down Expand Up @@ -123,7 +126,7 @@ function MainContent({
active={showCalendar}
closeFn={handleCalendarClose}
position='top'
title='Custom Date Range'
title={t('MainContent.calendarTitle')}
>
<Calendar
bounds={[
Expand All @@ -140,7 +143,7 @@ function MainContent({
margin={{ top: 'small' }}
>
<StyledCalendarButton
label='DONE'
label={t('MainContent.calendarBtn')}
onClick={handleCalendarSave}
/>
</Box>
Expand Down Expand Up @@ -179,7 +182,7 @@ function MainContent({
aria-expanded={activeTab === 0}
aria-selected={activeTab === 0}
active={activeTab === 0}
label='CLASSIFICATIONS'
label={t('common.classifications')}
onClick={() => handleActiveTab(0)}
plain
fill={size === 'small' ? 'horizontal' : false}
Expand All @@ -189,14 +192,14 @@ function MainContent({
aria-expanded={activeTab === 1}
aria-selected={activeTab === 1}
active={activeTab === 1}
label='HOURS'
label={t('common.hours')}
onClick={() => handleActiveTab(1)}
plain
fill={size === 'small' ? 'horizontal' : false}
/>
</Box>
<Tip
contentText='Hours are calculated based on the start and end times of your classification efforts. Hours do not reflect your time spent on Talk.'
contentText={t('MainContent.hoursTip')}
/>
</Box>
<Box
Expand Down Expand Up @@ -224,7 +227,7 @@ function MainContent({
</Box>
<Box
role='tabpanel'
aria-label={activeTab === 0 ? 'CLASSIFICATIONS Tab Contents' : 'HOURS Tab Contents'}
aria-label={activeTab === 0 ? `${t('common.classifications')} ${t('MainContent.tabContents')}` : `${t('common.hours')} ${t('MainContent.tabContents')}`}
height='15rem'
width='100%'
>
Expand Down Expand Up @@ -254,7 +257,7 @@ function MainContent({
pad='medium'
>
<SpacedText uppercase={false}>
There was an error.
{t('MainContent.error')}
</SpacedText>
<SpacedText uppercase={false}>
{error?.message}
Expand All @@ -267,13 +270,17 @@ function MainContent({
justify='center'
pad='medium'
>
<SpacedText uppercase={false}>No data found.</SpacedText>
<Text>{t('MainContent.noData')}</Text>
<Text>
Start by{' '}
<Anchor href='https://www.zooniverse.org/projects'>
classifying a project
</Anchor>
{' ' }now, or change the date range.
<Trans
i18nKey='MainContent.start'
components={[
<Anchor
key='projects-page'
href='https://www.zooniverse.org/projects'
/>
]}
/>
</Text>
</Box>
) : (
Expand All @@ -294,7 +301,7 @@ function MainContent({
forwardedAs={Link}
color='neutral-1'
href={`/users/${source.login}/stats/certificate${window.location.search}`}
label='Generate Volunteer Certificate'
label={t('MainContent.certificate')}
/>
</Box>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('components > shared > MainContent', function () {

it('should show "CLASSIFICATIONS" as the active tab', function () {
render(<DefaultStory />)
const activeTab = screen.getByRole('tab', { name: 'CLASSIFICATIONS', selected: true })
const activeTab = screen.getByRole('tab', { name: 'Classifications', selected: true })

expect(activeTab).to.be.ok()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const StyledCertificateButton = styled(Button)`
border-radius: 4px;
color: ${props => props.theme.dark ? props.theme.global.colors['light-3'] : props.theme.global.colors['dark-5']};
min-width: 100px;
text-transform: uppercase;
`

export default StyledCertificateButton
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const StyledTab = styled(Button)`
color: ${props => props.theme.dark ? props.theme.global.colors['light-3'] : props.theme.global.colors['dark-5']};
font-size: 1em;
text-align: center;
text-transform: uppercase;

${props => props.active && css`
border-bottom: 4px solid ${props.theme.global.colors['neutral-1']};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,52 @@ function getNextMonth(month) {
return month === 11 ? 0 : month + 1
}

function getPresetSelectOptions({ sourceCreatedAtDate, today }) {
function getPresetSelectOptions({ sourceCreatedAtDate, today, t }) {
return [
{
label: 'LAST 7 DAYS',
label: t('MainContent.dateRange.lastSevenDays').toUpperCase(),
value: getStatsDateString(new Date(new Date().setUTCDate(today.getUTCDate() - 6)))
},
{
label: 'LAST 30 DAYS',
label: t('MainContent.dateRange.lastThirtyDays').toUpperCase(),
value: getStatsDateString(new Date(new Date().setUTCDate(today.getUTCDate() - 29)))
},
{
label: 'THIS MONTH',
label: t('MainContent.dateRange.thisMonth').toUpperCase(),
value: getStatsDateString(new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), 1)))
},
{
label: 'LAST 3 MONTHS',
label: t('MainContent.dateRange.lastThreeMonths').toUpperCase(),
value: getStatsDateString(new Date(new Date().setUTCDate(today.getUTCDate() - 90)))
},
{
label: 'THIS YEAR',
label: t('MainContent.dateRange.thisYear').toUpperCase(),
value: getStatsDateString(new Date(Date.UTC(today.getUTCFullYear(), 0, 1)))
},
{
label: 'LAST 12 MONTHS',
label: t('MainContent.dateRange.lastTwelveMonths').toUpperCase(),
value: getStatsDateString(new Date(Date.UTC((today.getUTCFullYear() - 1), getNextMonth(today.getUTCMonth()), 1)))
},
{
label: 'ALL TIME',
label: t('MainContent.dateRange.allTime').toUpperCase(),
value: sourceCreatedAtDate
}
]
}

const DEFAULT_DATE_RANGE = getDefaultDateRange()
const DEFAULT_HANDLER = key => key

export function getDateRangeSelectOptions({
sourceCreatedAtDate = '',
paramsValidationMessage = '',
selectedDateRange = DEFAULT_DATE_RANGE
selectedDateRange = DEFAULT_DATE_RANGE,
t = DEFAULT_HANDLER
}) {
const today = new Date()
const todayUTC = getStatsDateString(today)

const dateRangeOptions = getPresetSelectOptions({ sourceCreatedAtDate, today })
const dateRangeOptions = getPresetSelectOptions({ sourceCreatedAtDate, today, t })

let selectedDateRangeOption = dateRangeOptions.find(option =>
(selectedDateRange.endDate === todayUTC) &&
Expand All @@ -68,7 +70,7 @@ export function getDateRangeSelectOptions({
selectedDateRangeOption = customDateRangeOption
} else {
dateRangeOptions.push({
label: 'CUSTOM',
label: t('MainContent.dateRange.custom').toUpperCase(),
value: 'custom'
})
}
Expand Down
Loading
Loading