forked from italia/design-comuni-plone-theme
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added validity dates to Alert block (#795)
* fix: condition with has no content * feat: new date schedule for alert block * feat: condition to set visible the alert view * fix: file cleaned * chore: i18n * refact: changes to upgrade the code * refact: class component to function component for View * fix: condition re-thinked * Update src/components/ItaliaTheme/Blocks/Alert/Sidebar.jsx Co-authored-by: Giulia Ghisini <51911425+giuliaghisini@users.noreply.github.com> * Update src/components/ItaliaTheme/Blocks/Alert/Sidebar.jsx Co-authored-by: Giulia Ghisini <51911425+giuliaghisini@users.noreply.github.com> * Update src/components/ItaliaTheme/Blocks/Alert/Sidebar.jsx Co-authored-by: Giulia Ghisini <51911425+giuliaghisini@users.noreply.github.com> * chore: i18n * fix: removed utils * fix: message conditional * fix: removed unnecessary code * chore: i18n * fix: message as default --------- Co-authored-by: Giulia Ghisini <51911425+giuliaghisini@users.noreply.github.com>
- Loading branch information
1 parent
824b757
commit 069c248
Showing
11 changed files
with
486 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { Row, Container } from 'design-react-kit/dist/design-react-kit'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
|
||
const messages = defineMessages({ | ||
expiredDate: { | ||
id: 'alert_expiredDate', | ||
defaultMessage: "Non visibile. E' scaduto il {date}.", | ||
}, | ||
activeDate: { | ||
id: 'alert_activeDate', | ||
defaultMessage: 'Pubblicazione attiva.', | ||
}, | ||
futureDate: { | ||
id: 'alert_futureDate', | ||
defaultMessage: 'Non visibile. Verà pubblicato il {date}', | ||
}, | ||
errorDate: { | ||
id: 'alert_errorDate', | ||
defaultMessage: | ||
"Non visibile. C'è un errore sulle date: la data di scadenza è anteriore alla data d'inizio", | ||
}, | ||
willExpire: { | ||
id: 'alert_willExpire', | ||
defaultMessage: 'Scadrà il {date}', | ||
}, | ||
}); | ||
|
||
const AlertWrapper = ({ data, children }) => { | ||
const intl = useIntl(); | ||
const userLogged = useSelector((state) => state.userSession.token); | ||
|
||
const isActive = () => { | ||
const today = new Date().getTime(); | ||
const start = data.startDate ? new Date(data.startDate).getTime() : 0; | ||
const end = data.endDate | ||
? new Date(data.endDate).getTime() | ||
: Number.MAX_SAFE_INTEGER; | ||
|
||
const returnValue = { | ||
active: false, | ||
message: intl.formatMessage(messages.expiredDate, { | ||
date: new Date(data.endDate).toLocaleString(), | ||
}), | ||
}; | ||
|
||
if (end < start) { | ||
returnValue.message = intl.formatMessage(messages.errorDate); | ||
} else if (today < start) { | ||
returnValue.message = intl.formatMessage(messages.futureDate, { | ||
date: new Date(data.startDate).toLocaleString(), | ||
}); | ||
} else if (today < end) { | ||
returnValue.message = intl.formatMessage(messages.activeDate); | ||
if (data.endDate) { | ||
returnValue.message += | ||
' ' + | ||
intl.formatMessage(messages.willExpire, { | ||
date: new Date(data.endDate).toLocaleString(), | ||
}); | ||
} | ||
returnValue.active = true; | ||
} | ||
|
||
return returnValue; | ||
}; | ||
|
||
const activeStatus = isActive(); | ||
|
||
return ( | ||
<> | ||
{(userLogged || activeStatus.active) && ( | ||
<> | ||
{userLogged && ( | ||
<Container className="alert-info-dates"> | ||
<Row> | ||
<p className="alert-info-text">{activeStatus.message}</p> | ||
</Row> | ||
</Container> | ||
)} | ||
{children} | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default AlertWrapper; |
Oops, something went wrong.