From 63e2c37a8fe540a86e80791e15a115af476504e3 Mon Sep 17 00:00:00 2001 From: mrkvon Date: Tue, 17 Dec 2019 00:29:46 +0100 Subject: [PATCH] Migrate Tribe to React (#1134) A tribe panel on /tribes page. --- config/client/i18n.js | 4 ++ ...{JoinButton.component.js => JoinButton.js} | 0 .../client/components/TribeItem.component.js | 50 +++++++++++++++++++ .../helpers/getTribeBackgroundStyle.js | 44 ++++++++++++++++ modules/tribes/client/less/tribes-grid.less | 2 +- .../client/views/tribes-list.client.view.html | 38 +++----------- public/locales/en/tribes.json | 5 ++ 7 files changed, 110 insertions(+), 33 deletions(-) rename modules/tribes/client/components/{JoinButton.component.js => JoinButton.js} (100%) create mode 100644 modules/tribes/client/components/TribeItem.component.js create mode 100644 modules/tribes/client/components/helpers/getTribeBackgroundStyle.js create mode 100644 public/locales/en/tribes.json diff --git a/config/client/i18n.js b/config/client/i18n.js index 72c454e3e4..a56c3106be 100644 --- a/config/client/i18n.js +++ b/config/client/i18n.js @@ -44,6 +44,10 @@ function format(value, format, languageCode) { return moment(value).format(format); } + if (typeof value === 'number' && format === 'number') { + return value.toLocaleString(languageCode); + } + return value; } diff --git a/modules/tribes/client/components/JoinButton.component.js b/modules/tribes/client/components/JoinButton.js similarity index 100% rename from modules/tribes/client/components/JoinButton.component.js rename to modules/tribes/client/components/JoinButton.js diff --git a/modules/tribes/client/components/TribeItem.component.js b/modules/tribes/client/components/TribeItem.component.js new file mode 100644 index 0000000000..9880ad9d96 --- /dev/null +++ b/modules/tribes/client/components/TribeItem.component.js @@ -0,0 +1,50 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import classnames from 'classnames'; +import { useTranslation } from 'react-i18next'; + +import JoinButton from './JoinButton'; + +import getTribeBackgroundStyle from './helpers/getTribeBackgroundStyle'; + +/** + * @TODO maybe rename to Tribe + */ +export default function TribeItem({ tribe, user, onMembershipUpdated }) { + const { t } = useTranslation('tribes'); + + const countInfo = (tribe.count === 0) + ? t('No members yet') + : t('{{count, number}} members', { count: tribe.count }); + + return
+ + {tribe.new && + + {t('New tribe!')} + + } +
+

{tribe.label}

+ {countInfo} +
+
+
+ {tribe && } +
+
; +} + +TribeItem.propTypes = { + tribe: PropTypes.object.isRequired, + user: PropTypes.object, + onMembershipUpdated: PropTypes.func.isRequired, +}; diff --git a/modules/tribes/client/components/helpers/getTribeBackgroundStyle.js b/modules/tribes/client/components/helpers/getTribeBackgroundStyle.js new file mode 100644 index 0000000000..e0e13ea0b0 --- /dev/null +++ b/modules/tribes/client/components/helpers/getTribeBackgroundStyle.js @@ -0,0 +1,44 @@ +/** + * Generate background color + image styles for tribe panel + * + * usage in React:
...
+ * + * @param {object} tribe - the tribe data + * + * @param {string} [dimensions=1024x768] - Set background image dimensions (width x height) + * See https://uploadcare.com/documentation/cdn/#operation-scale-crop + * + * @param {string} [quality=lighter] - Set background image quality + * Options: normal, better, best, lighter (default), lightest + * See https://uploadcare.com/documentation/cdn/#operation-quality + * + * @param {boolean} [isProgressive=false] - Set progressive image loading + * See https://uploadcare.com/documentation/cdn/#operation-progressive + * + * @returns {object} + */ +export default function getTribeBackgroundStyle(tribe, { quality='lighter', dimensions='1024x768', isProgressive=false }={}) { + const style = {}; + + // Set background image + // Uses Uploadcare.com to resize and deliver images + if (tribe.image_UUID) { + const progressive = isProgressive ? 'yes' : 'no'; + + const imgParams = [ + `progressive/${progressive}`, + `scale_crop/${dimensions}/center`, + `quality/${quality}`, + 'format/jpeg', + ]; + + const imageUrl = `https://ucarecdn.com/${tribe.image_UUID}/-/${imgParams.join('/-/')}/`; + style.backgroundImage = `url(${imageUrl})`; + } + + if (tribe.color) { + style.backgroundColor = tribe.color; + } + + return style; +} diff --git a/modules/tribes/client/less/tribes-grid.less b/modules/tribes/client/less/tribes-grid.less index 49869c8f90..e5c0bd6793 100644 --- a/modules/tribes/client/less/tribes-grid.less +++ b/modules/tribes/client/less/tribes-grid.less @@ -9,7 +9,7 @@ background-color: @brand-primary; background-size: cover; height: 250px; - width: 288px; + width: 100%; margin: 0; padding: 0; color: #fff; diff --git a/modules/tribes/client/views/tribes-list.client.view.html b/modules/tribes/client/views/tribes-list.client.view.html index 6c262cd7de..dd213b3987 100644 --- a/modules/tribes/client/views/tribes-list.client.view.html +++ b/modules/tribes/client/views/tribes-list.client.view.html @@ -14,38 +14,12 @@ ag-grid-width="288" ag-gutter-size="10" ag-refresh-on-img-load="false"> -
  • - - -
    -

    - -
    -
    -
    - -
    +
  • +
  • diff --git a/public/locales/en/tribes.json b/public/locales/en/tribes.json new file mode 100644 index 0000000000..da217e20b8 --- /dev/null +++ b/public/locales/en/tribes.json @@ -0,0 +1,5 @@ +{ + "No members yet": "No members yet", + "{{count, number}} members": "One member", + "{{count, number}} members_plural": "{{count, number}} members" +}