Removed monthlyExpensesComplete from initial state, moved into API call

• Removed:
- monthlyExpensesComplete from initial state, moved into API call
- set up expenses to read from redux for Progress panel/injection
This commit is contained in:
mgabdev 2021-01-15 15:00:20 -05:00
parent 63622b5416
commit cca9a2d24e
6 changed files with 115 additions and 13 deletions

View File

@ -0,0 +1,35 @@
import api from '../api'
import { me } from '../initial_state'
export const EXPENSES_FETCH_REQUEST = 'EXPENSES_FETCH_REQUEST'
export const EXPENSES_FETCH_SUCCESS = 'EXPENSES_FETCH_SUCCESS'
export const EXPENSES_FETCH_FAIL = 'EXPENSES_FETCH_FAIL'
/**
* Fetch monthly expenses completion
*/
export const fetchExpenses = () => (dispatch, getState) => {
if (!me) return
dispatch(fetchExpensesRequest())
api(getState).get('/api/v1/expenses').then((response) => {
dispatch(fetchExpensesSuccess(response.data.expenses))
}).catch((error) => {
dispatch(fetchExpensesFail(error))
})
}
const fetchExpensesRequest = () => ({
type: EXPENSES_FETCH_REQUEST,
})
const fetchExpensesSuccess = (value) => ({
type: EXPENSES_FETCH_SUCCESS,
value,
})
const fetchExpensesFail = (error, listType) => ({
type: EXPENSES_FETCH_FAIL,
error,
})

View File

@ -1,11 +1,12 @@
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { injectIntl, defineMessages } from 'react-intl' import { injectIntl, defineMessages } from 'react-intl'
import { monthlyExpensesComplete } from '../../initial_state'
import { import {
URL_DISSENTER_SHOP, URL_DISSENTER_SHOP,
URL_DISSENTER_SHOP_DONATIONS, URL_DISSENTER_SHOP_DONATIONS,
} from '../../constants' } from '../../constants'
import { fetchExpenses } from '../../actions/expenses'
import PanelLayout from './panel_layout'; import PanelLayout from './panel_layout';
import ProgressBar from '../progress_bar' import ProgressBar from '../progress_bar'
import Button from '../button' import Button from '../button'
@ -14,12 +15,17 @@ import Icon from '../icon'
class ProgressPanel extends React.PureComponent { class ProgressPanel extends React.PureComponent {
componentDidMount() {
if (!this.props.isFetched) {
this.props.onFetchExpenses()
}
}
render() { render() {
const { intl } = this.props const { intl, value, isFetched } = this.props
if (!monthlyExpensesComplete) return null if (value === 0 && !isFetched) return null
const value = Math.min(parseFloat(monthlyExpensesComplete), 100)
const subtitle = ( const subtitle = (
<div className={[_s.d, _s.flexRow, _s.aiCenter, _s.jcCenter].join(' ')}> <div className={[_s.d, _s.flexRow, _s.aiCenter, _s.jcCenter].join(' ')}>
<Text color='secondary' size='small' weight='bold' className={_s.mrAuto}> <Text color='secondary' size='small' weight='bold' className={_s.mrAuto}>
@ -46,7 +52,7 @@ class ProgressPanel extends React.PureComponent {
> >
<div className={[_s.d, _s.px15, _s.pb15, _s.pt5].join(' ')}> <div className={[_s.d, _s.px15, _s.pb15, _s.pt5].join(' ')}>
<ProgressBar <ProgressBar
progress={monthlyExpensesComplete} progress={value}
title={intl.formatMessage(messages.progressTitle, { value })} title={intl.formatMessage(messages.progressTitle, { value })}
href={URL_DISSENTER_SHOP} href={URL_DISSENTER_SHOP}
/> />
@ -64,8 +70,22 @@ const messages = defineMessages({
donationTitle: { id: 'make_donation', defaultMessage: 'Make a Donation' }, donationTitle: { id: 'make_donation', defaultMessage: 'Make a Donation' },
}) })
const mapStateToProps = (state) => ({
isFetched: state.getIn(['expenses', 'fetched'], false),
value: state.getIn(['expenses', 'value'], 0),
})
const mapDispatchToProps = (dispatch) => ({
onFetchExpenses() {
dispatch(fetchExpenses())
},
})
ProgressPanel.propTypes = { ProgressPanel.propTypes = {
intl: PropTypes.object.isRequired, intl: PropTypes.object.isRequired,
isFetched: PropTypes.bool.isRequired,
value: PropTypes.number.isRequired,
onFetchExpenses: PropTypes.func.isRequired,
} }
export default injectIntl(ProgressPanel) export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ProgressPanel))

View File

@ -1,11 +1,12 @@
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { injectIntl, defineMessages } from 'react-intl' import { injectIntl, defineMessages } from 'react-intl'
import { monthlyExpensesComplete } from '../../initial_state'
import { import {
URL_DISSENTER_SHOP, URL_DISSENTER_SHOP,
URL_DISSENTER_SHOP_DONATIONS, URL_DISSENTER_SHOP_DONATIONS,
} from '../../constants' } from '../../constants'
import { fetchExpenses } from '../../actions/expenses'
import TimelineInjectionLayout from './timeline_injection_layout' import TimelineInjectionLayout from './timeline_injection_layout'
import ProgressBar from '../progress_bar' import ProgressBar from '../progress_bar'
import Button from '../button' import Button from '../button'
@ -13,16 +14,23 @@ import Text from '../text'
class ProgressInjection extends React.PureComponent { class ProgressInjection extends React.PureComponent {
componentDidMount() {
const { isFetched, isXS } = this.props
if (!isFetched && isXS) {
this.props.onFetchExpenses()
}
}
render() { render() {
const { const {
intl, intl,
isXS, isXS,
value,
isFetched,
injectionId, injectionId,
} = this.props } = this.props
if (!monthlyExpensesComplete || !isXS) return <div /> if ((value === 0 && isFetched) || !isXS) return <div />
const value = Math.min(parseFloat(monthlyExpensesComplete), 100)
return ( return (
<TimelineInjectionLayout <TimelineInjectionLayout
@ -35,7 +43,7 @@ class ProgressInjection extends React.PureComponent {
> >
<div className={[_s.d, _s.pt5, _s.pb15, _s.w100PC].join(' ')}> <div className={[_s.d, _s.pt5, _s.pb15, _s.w100PC].join(' ')}>
<ProgressBar <ProgressBar
progress={monthlyExpensesComplete} progress={value}
title={intl.formatMessage(messages.progressTitle, { value })} title={intl.formatMessage(messages.progressTitle, { value })}
href={URL_DISSENTER_SHOP} href={URL_DISSENTER_SHOP}
/> />
@ -53,8 +61,23 @@ const messages = defineMessages({
donationTitle: { id: 'make_donation', defaultMessage: 'Make a Donation' }, donationTitle: { id: 'make_donation', defaultMessage: 'Make a Donation' },
}) })
const mapStateToProps = (state) => ({
isFetched: state.getIn(['expenses', 'fetched'], false),
value: state.getIn(['expenses', 'value'], 0),
})
const mapDispatchToProps = (dispatch) => ({
onFetchExpenses() {
dispatch(fetchExpenses())
},
})
ProgressInjection.propTypes = { ProgressInjection.propTypes = {
injectionId: PropTypes.string.isRequired, injectionId: PropTypes.string.isRequired,
intl: PropTypes.object.isRequired,
isFetched: PropTypes.bool.isRequired,
value: PropTypes.number.isRequired,
onFetchExpenses: PropTypes.func.isRequired,
} }
export default injectIntl(ProgressInjection) export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ProgressInjection))

View File

@ -19,7 +19,6 @@ export const version = getMeta('version');
export const isStaff = getMeta('is_staff'); export const isStaff = getMeta('is_staff');
export const unreadCount = getMeta('unread_count'); export const unreadCount = getMeta('unread_count');
export const lastReadNotificationId = getMeta('last_read_notification_id'); export const lastReadNotificationId = getMeta('last_read_notification_id');
export const monthlyExpensesComplete = getMeta('monthly_expenses_complete');
export const trendingHashtags = getMeta('trending_hashtags'); export const trendingHashtags = getMeta('trending_hashtags');
export const isFirstSession = getMeta('is_first_session'); export const isFirstSession = getMeta('is_first_session');
export const emailConfirmed = getMeta('email_confirmed'); export const emailConfirmed = getMeta('email_confirmed');

View File

@ -0,0 +1,23 @@
import { Map as ImmutableMap } from 'immutable'
import {
EXPENSES_FETCH_REQUEST,
EXPENSES_FETCH_SUCCESS,
EXPENSES_FETCH_FAIL,
} from '../actions/expenses'
const initialState = ImmutableMap({
fetched: false,
value: 0,
})
export default function expenses(state = initialState, action) {
switch (action.type) {
case EXPENSES_FETCH_REQUEST:
case EXPENSES_FETCH_FAIL:
return state.set('fetched', true).set('value', 0)
case EXPENSES_FETCH_SUCCESS:
return state.set('fetched', true).set('value', action.value)
default:
return state
}
}

View File

@ -15,6 +15,7 @@ import compose from './compose'
import contexts from './contexts' import contexts from './contexts'
import custom_emojis from './custom_emojis' import custom_emojis from './custom_emojis'
import deck from './deck' import deck from './deck'
import expenses from './expenses'
import filters from './filters' import filters from './filters'
import groups from './groups' import groups from './groups'
import group_categories from './group_categories' import group_categories from './group_categories'
@ -70,6 +71,7 @@ const reducers = {
contexts, contexts,
custom_emojis, custom_emojis,
deck, deck,
expenses,
filters, filters,
groups, groups,
group_categories, group_categories,