Skip to content

Commit

Permalink
Replace new Date by parseDate call (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Sep 15, 2023
1 parent 251e884 commit 2ec207d
Show file tree
Hide file tree
Showing 30 changed files with 113 additions and 120 deletions.
15 changes: 8 additions & 7 deletions src/api/DTD.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { newPaginatedResult } from 'api/pagination'
import { formatISO } from 'date-fns'
import { downloadFile } from './upload'
import { parseDate } from 'shared/helpers'

export const getDTDGlobalKPIQuery = async () => {
const data = await apiClient.get('api/v3/pap_campaigns/kpi')
Expand All @@ -51,8 +52,8 @@ export const getDTDCampaignsQuery = async ({ pageParam: page = 1 }) => {
return new DTDCampaignItem(
c.uuid,
c.creator,
new Date(c.begin_at),
new Date(c.finish_at),
parseDate(c.begin_at),
parseDate(c.finish_at),
c.title,
score,
c.enabled
Expand All @@ -69,7 +70,7 @@ export const getDTDCampaignQuery = async campaignId => {

export const getDTDCampaignDetailQuery = async campaignId => {
const data = await apiClient.get(`api/v3/pap_campaigns/${campaignId}`)
const remaining = new DTDCampaignDetailKPIRemaining(new Date(data.begin_at), new Date(data.finish_at))
const remaining = new DTDCampaignDetailKPIRemaining(parseDate(data.begin_at), parseDate(data.finish_at))
const surveys = new DTDCampaignDetailKPISurveys(data.nb_surveys)
const doors = new DTDCampaignDetailKPIDoors(data.nb_visited_doors, data.nb_open_doors)
const contacts = new DTDCampaignDetailKPIContacts(data.nb_collected_contacts, data.nb_to_join)
Expand Down Expand Up @@ -118,7 +119,7 @@ export const getDTDCampaignDetailHistory = async ({ campaignId, pageParam: page
h.questioner.age
)
: null
return new DTDCampaignDetailHistory(h.uuid, h.status, address, questioner, new Date(h.created_at), h.duration)
return new DTDCampaignDetailHistory(h.uuid, h.status, address, questioner, parseDate(h.created_at), h.duration)
})

return newPaginatedResult(history, data.metadata)
Expand All @@ -144,7 +145,7 @@ export const getDTDCampaignSurveysReplies = async ({ campaignId, pageSize, pageN
sr.answers.map(a => new DTDCampaignDetailSurveysReplyAnswer(a.type, a.answer, a.question)),
questioner,
sr.pap_campaign_history.duration,
new Date(sr.pap_campaign_history.created_at),
parseDate(sr.pap_campaign_history.created_at),
sr.pap_campaign_history.first_name,
sr.pap_campaign_history.last_name,
sr.pap_campaign_history.gender,
Expand Down Expand Up @@ -224,8 +225,8 @@ export const updateDTDLocalCampaign = async campaign => {
title: campaign.title,
brief: campaign.brief,
goal: +campaign.goal,
begin_at: formatISO(new Date(campaign.startDate)),
finish_at: formatISO(new Date(campaign.endDate)),
begin_at: formatISO(parseDate(campaign.startDate)),
finish_at: formatISO(parseDate(campaign.endDate)),
survey: campaign.survey,
vote_places: campaign.votePlaces,
})
Expand Down
3 changes: 2 additions & 1 deletion src/api/messagerie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { apiClient } from 'services/networking/client'
import Message, { Statistics } from 'domain/message'
import { newPaginatedResult } from 'api/pagination'
import ReportRatio, { GeoRatio } from 'domain/reportRatio'
import { parseDate } from 'shared/helpers'

export const getMessages = async ({ page, isMailsStatutory }) => {
const query = isMailsStatutory ? '&statutory=1' : ''
Expand Down Expand Up @@ -65,7 +66,7 @@ export const reportsRatio = async () => {
data.national.clicked_rate,
data.national.unsubscribed_rate
),
new Date(data.since)
parseDate(data.since)
)
}
export const getTemplates = async isMailsStatutory => {
Expand Down
3 changes: 2 additions & 1 deletion src/api/news.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { apiClient } from 'services/networking/client'
import News from 'domain/news'
import { newPaginatedResult } from 'api/pagination'
import { parseDate } from 'shared/helpers'

export const getNewsQuery = async ({ pageParam: page = 1 }) => {
const data = await apiClient.get(`api/v3/jecoute/news?order[created_at]=desc&page=${page}&page_size=20`)
Expand All @@ -14,7 +15,7 @@ export const getNewsQuery = async ({ pageParam: page = 1 }) => {
n.external_link,
n.link_label,
n.creator,
new Date(n.created_at),
parseDate(n.created_at),
n.notification,
n.published,
n.pinned,
Expand Down
15 changes: 8 additions & 7 deletions src/api/phoning.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { newPaginatedResult } from 'api/pagination'
import { Zone } from 'domain/zone'
import { ZONE_AUTOCOMPLETE_URI } from 'components/Filters/Element/ZoneAutocomplete'
import { downloadFile } from './upload'
import { parseDate } from 'shared/helpers'

export const getPhoningGlobalKPIQuery = async () => {
const data = await apiClient.get('api/v3/phoning_campaigns/kpi')
Expand All @@ -51,7 +52,7 @@ export const getPhoningCampaignsQuery = async ({ pageParam: page = 1 }, visibili
)
return new PhoningCampaignItem(
c.uuid,
new Date(c.finish_at),
parseDate(c.finish_at),
c.title,
c.creator,
team,
Expand All @@ -68,23 +69,23 @@ export const getPhoningCampaignsQuery = async ({ pageParam: page = 1 }, visibili
export const getPhoningCampaignQuery = async campaignId => {
const data = await apiClient.get(`api/v3/phoning_campaigns/${campaignId}`)
const isNational = data.visibility === 'national'
const remaining = new PhoningCampaignDetailKPIRemaining(new Date(data.created_at), new Date(data.finish_at))
const remaining = new PhoningCampaignDetailKPIRemaining(parseDate(data.created_at), parseDate(data.finish_at))
const surveys = new PhoningCampaignDetailKPISurveys(data.nb_surveys, data.goal * data.team.members_count)
const calls = new PhoningCampaignDetailKPICalls(data.nb_calls, data.to_remind)
const KPI = new PhoningCampaignDetailKPI(remaining, surveys, calls, data.average_calling_time)
const global = isNational
? new PhoningCampaignCreateEditGlobal(
data.title,
data.goal,
new Date(data.finish_at),
parseDate(data.finish_at),
data.brief,
data.visibility,
null
)
: new PhoningCampaignCreateEditGlobal(
data.title,
data.goal,
new Date(data.finish_at),
parseDate(data.finish_at),
data.brief,
data.visibility,
new Zone(data.zone.uuid, data.zone.name, data.zone.code)
Expand Down Expand Up @@ -138,7 +139,7 @@ export const getPhoningCampaignHistory = async ({ campaignId, pageParam: page =
h.caller.gender,
h.caller.age
)
return new PhoningCampaignDetailHistory(h.uuid, h.status, adherent, caller, new Date(h.begin_at))
return new PhoningCampaignDetailHistory(h.uuid, h.status, adherent, caller, parseDate(h.begin_at))
})

return newPaginatedResult(history, data.metadata)
Expand All @@ -160,8 +161,8 @@ export const getPhoningCampaignSurveysReplies = async campaignId => {
return new PhoningCampaignDetailSurveysReply(
sr.answers.map(a => new PhoningCampaignDetailSurveysReplyAnswer(a.type, a.answer, a.question)),
callee,
new Date(sr.phoning_campaign_history.begin_at),
new Date(sr.phoning_campaign_history.finish_at)
parseDate(sr.phoning_campaign_history.begin_at),
parseDate(sr.phoning_campaign_history.finish_at)
)
}),
}
Expand Down
6 changes: 3 additions & 3 deletions src/api/surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from 'domain/surveys'
import { newPaginatedResult } from 'api/pagination'
import { downloadFile } from './upload'
import { formatDate } from 'shared/helpers'
import { formatDate, parseDate } from 'shared/helpers'

export const formatZone = zone => new SurveyItemZone(zone.uuid, zone.code, zone.name)

Expand Down Expand Up @@ -64,8 +64,8 @@ export const getSurveyRepliesQuery = async surveyId => {
return new SurveyDetailReply(
sr.answers.map(a => new SurveyDetailReplyAnswer(a.type, a.answer, a.question, a.question_id)),
author,
new Date(sr.begin_at),
new Date(sr.finish_at)
parseDate(sr.begin_at),
parseDate(sr.finish_at)
)
})
}
Expand Down
82 changes: 33 additions & 49 deletions src/components/Activists/Activists.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const Activists = () => {
const [isShadowLoading, setIsShadowLoading] = useState(false)
const { handleError } = useErrorHandler()

const isElectFeatureEnabled = currentScope.hasFeature(features.elected_representative)

const {
data: activists = new PaginatedResult([], 0, 0, 0, 0, 0),
refetch,
Expand All @@ -59,9 +61,12 @@ const Activists = () => {
setLoader(false)
}

const toggleDrawer = (e, member = null) => {
e.preventDefault()
setMember(member)
const handleDrawerClose = () => {
setMember(null)
if (isElectFeatureEnabled) {
setIsShadowLoading(true)
refetch()
}
}

return (
Expand Down Expand Up @@ -99,60 +104,39 @@ const Activists = () => {
</AccordionDetails>
</Accordion>

<Box sx={{ mt: 4, position: 'relative' }} className="space-y-4">
{(loader || (isFetching && !isShadowLoading)) && (
<Box sx={{ mt: 4 }} className="space-y-4">
{((loader || (isFetching && !isShadowLoading)) && (
<Box
sx={{
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
zIndex: 1000,
backgroundColor: 'rgba(255,255,255,0.5)',
}}
>
<Loader isCenter />
</Box>
)}
{activists.total > 0 ? (
<Box display="flex" alignItems="center" justifyContent="space-between">
<Typography variant="body2" color="gray700" sx={{ flexShrink: 0 }}>
Affichage de {activists.pageSize * (activists.currentPage - 1) + 1} à{' '}
{activists.pageSize * (activists.currentPage - 1) + activists.currentPageCount} résultats sur{' '}
{activists.total}
</Typography>
<Pagination
sx={{ justifyContent: 'flex-end' }}
count={activists.lastPage || 0}
page={filters.page}
onChange={(event, page) => setFilters(prevState => ({ ...prevState, page }))}
/>
<Loader isCenter color={'colors.blue.500'} />
</Box>
) : (
!isFetching && <EmptyContent description="Aucun résultat ne correspond à votre recherche" />
)}

<MembersList members={activists.data} onMemberClick={toggleDrawer} />
)) ||
(activists.total > 0 && (
<>
<Box display="flex" alignItems="center" justifyContent="space-between">
<Typography variant="body2" color="gray700" sx={{ flexShrink: 0 }}>
Affichage de {activists.pageSize * (activists.currentPage - 1) + 1} à{' '}
{activists.pageSize * (activists.currentPage - 1) + activists.currentPageCount} résultats sur{' '}
{activists.total}
</Typography>
<Pagination
sx={{ justifyContent: 'flex-end' }}
count={activists.lastPage || 0}
page={filters.page}
onChange={(event, page) => setFilters(prevState => ({ ...prevState, page }))}
/>
</Box>
<MembersList members={activists.data} onMemberClick={m => setMember(m)} />
</>
)) ||
(!isFetching && <EmptyContent description="Aucun résultat ne correspond à votre recherche" />)}
</Box>

<Drawer
anchor="right"
open={member !== null}
onClose={e => {
toggleDrawer(e, null)
setIsShadowLoading(true)
refetch()
}}
>
<Member
member={member}
handleClose={e => {
toggleDrawer(e, null)
setIsShadowLoading(true)
refetch()
}}
/>
<Drawer anchor="right" open={member !== null} onClose={handleDrawerClose}>
<Member member={member} enableElectTab={isElectFeatureEnabled} handleClose={handleDrawerClose} />
</Drawer>
</Container>
)
Expand Down
11 changes: 4 additions & 7 deletions src/components/Activists/Member/Member.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import { useState } from 'react'
import { TabContext, TabList, TabPanel } from '@mui/lab'
import AdherentTab from 'components/Activists/Member/Tabs/Adherent/AdherentTab'
import ElectedTab from 'components/Activists/Member/Tabs/Elected/ElectedTab'
import { useUserScope } from '../../../redux/user/hooks'
import features from 'shared/features'

const Member = ({ member, handleClose }) => {
const [currentScope] = useUserScope()
const isElectFeatureEneblad = currentScope.hasFeature(features.elected_representative)
const Member = ({ enableElectTab, member, handleClose }) => {
const [currentTab, setCurrentTab] = useState('1')

if (!member) {
Expand Down Expand Up @@ -125,7 +121,7 @@ const Member = ({ member, handleClose }) => {
<Box sx={{ mt: 3 }}>
<TabList onChange={(event, newValue) => setCurrentTab(newValue)}>
<Tab sx={{ textTransform: 'none' }} label={'Adhérent'} value={'1'} />
{isElectFeatureEneblad && (
{enableElectTab && (
<Tab
sx={{ textTransform: 'none' }}
label={
Expand All @@ -147,7 +143,7 @@ const Member = ({ member, handleClose }) => {
<TabPanel value={'1'}>
<AdherentTab member={member} />
</TabPanel>
{isElectFeatureEneblad && (
{enableElectTab && (
<TabPanel value={'2'}>
<ElectedTab adherentUuid={member.adherentUuid} />
</TabPanel>
Expand All @@ -163,5 +159,6 @@ export default Member

Member.propTypes = {
member: Activist.propTypes,
enableElectTab: PropTypes.bool,
handleClose: PropTypes.func.isRequired,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AdherentTab = ({ member }) => (
<Grid item xs={12} sm={4}>
<LineText
label="Date de naissance"
value={member.raw.birthdate ? formatDate(new Date(member.raw.birthdate), 'dd/MM/yyyy') : '--'}
value={member.raw.birthdate ? formatDate(member.raw.birthdate, 'dd/MM/yyyy') : '--'}
/>
</Grid>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CotisationHistory = ({ dates }) =>
<EventAvailableOutlinedIcon />
</ListItemIcon>
<ListItemText
primary={formatDate(new Date(date), 'dd MMMM yyyy à HH:mm')}
primary={formatDate(date, 'dd MMMM yyyy à HH:mm')}
primaryTypographyProps={{
variant: 'span',
color: 'colors.gray.700',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const ElectedTab = ({ adherentUuid }) => {
<Grid container justifyContent={'space-between'}>
<Grid item>
<Typography sx={{ color: 'colors.gray.500', fontSize: '14px', fontWeight: '500' }}>
{formatDate(new Date(p.date), 'dd/MM/yyyy')} via {p.method}
{formatDate(p.date, 'dd/MM/yyyy')} via {p.method}
</Typography>
</Grid>
<Grid item>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Activists/Member/Tabs/Elected/Mandate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ const Mandate = ({ mandate, removeAction, editAction }) => (
>{`${mandate.zone.name} (${mandate.zone.code})`}</Typography>
<Typography component={'p'} sx={{ color: 'colors.gray.500', fontSize: '13px', mb: 0.5 }}>
{mandate.finishAt
? `du ${formatDate(new Date(mandate.beginAt), 'dd/MM/yyyy')} au ${formatDate(
new Date(mandate.finishAt),
'dd/MM/yyyy'
)}`
: `depuis le ${formatDate(new Date(mandate.beginAt), 'dd/MM/yyyy')}`}
? `du ${formatDate(mandate.beginAt, 'dd/MM/yyyy')} au ${formatDate(mandate.finishAt, 'dd/MM/yyyy')}`
: `depuis le ${formatDate(mandate.beginAt, 'dd/MM/yyyy')}`}
</Typography>
</Grid>
</Grid>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Activists/MembersList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MembersList = ({ members, onMemberClick }) => (
{members.map((member, index) => (
<Button
key={index}
onClick={e => onMemberClick(e, member)}
onClick={() => onMemberClick(member)}
sx={{
display: 'flex',
width: '100%',
Expand Down Expand Up @@ -42,7 +42,7 @@ const MembersList = ({ members, onMemberClick }) => (
<Grid item xs={6} sx={{ display: 'flex', alignItems: 'center' }}>
<FmdGoodIcon sx={{ color: 'colors.gray.400', fontSize: '22px', mr: 0.5 }} />
<Typography component="p" sx={{ color: 'colors.gray.500', fontSize: '14px' }}>
{member.city} ({member.cityId ?? '!'})
{`${member.city} ${member.cityId ? ` (${member.cityId})` : ''}`}
</Typography>
</Grid>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Committees/Committees.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const Committees = () => {
<Box sx={{ display: 'flex', alignItems: 'center', mt: 4 }}>
<AccessTime sx={{ mr: 0.5, color: 'colors.gray.400', fontSize: '15px' }} />
<Typography variant="subtitle2" sx={{ color: 'colors.gray.500' }}>
Créé Le {formatDate(new Date(committee.created_at), 'dd/MM/yyyy à HH:mm')}
Créé Le {formatDate(committee.created_at, 'dd/MM/yyyy à HH:mm')}
</Typography>
</Box>
</Box>
Expand Down
Loading

0 comments on commit 2ec207d

Please sign in to comment.