houdini/client/js/nonprofits/payments_chart.js
Bradley M. Kuhn fc77ee76d6 Relicense Javascript code in accordance with project's new license
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

The Additional Permission is designed to permit publicly distributed
Javascript code to be relicensed under LGPL-3.0-or-later, but not server-side
Javascript code.  As such, we've relicensed here static Javscript files under
LGPL-3.0-or-later, and those that run as part of build and/or server side
under AGPL-3.0-or-later.

Note that in future, Javascript files may be updated to be stronger copyleft
license with the Additional Permission, particularly if they adapted to run
on server side and/or turned into templates.  Of course, we'd seek public
discussion with the contributor community about such changes.

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

111 lines
3 KiB
JavaScript

// License: LGPL-3.0-or-later
const request = require('../common/client')
const R = require('ramda')
const Chart = require('chart.js')
const Pikaday = require('pikaday')
const moment = require('moment')
const chartOptions = require('../components/chart-options')
var frontendFormat = 'M/D/YYYY'
var backendFormat = 'YYYY-MM-DD'
// set the default query to get the last year of payments
// and group them by month
var defaultParams = {
endDate: moment().format(backendFormat)
, startDate: moment().subtract(12, 'months').format(backendFormat)
, timeSpan: 'month'
}
var pickadayDefaults = {format: frontendFormat, setDefaultDate: true}
appl.def('updateChartParams', function(formObj) {
updateChart({
endDate: moment(formObj.endDate).format(backendFormat)
, startDate: moment(formObj.startDate).format(backendFormat)
, timeSpan: formObj.timeSpan
})
})
// start date Pickaday
new Pikaday(R.merge({
field: document.getElementById('js-paymentsChart-startDate')
, maxDate: moment().subtract(1, 'week').toDate()
, defaultDate: moment().subtract(1, 'years').toDate()
}, pickadayDefaults))
// end date Pickaday
new Pikaday(R.merge({
field: document.getElementById('js-paymentsChart-endDate')
, maxDate: moment().toDate()
, defaultDate: moment().toDate()
}, pickadayDefaults))
var ctx = document.getElementById('js-paymentsChart').getContext('2d')
var chart = new Chart(ctx, {
type: 'bar'
, options: chartOptions.dollars
, data: {labels: [], datasets: []}
})
var url = `/nonprofits/${app.nonprofit_id}/payment_history`
function updateChart(params) {
appl.def('loading_chart', true)
request.get(url)
.query(params)
.end(function(err, resp) {
chart.data.labels = formatLabels(R.pluck('time_span', resp.body), params.timeSpan)
chart.data.datasets = formatDatasets(resp.body)
chart.update()
appl.def('loading_chart', false)
})
}
function formatLabels(dates, type) {
switch (type) {
case "year":
return R.map(st => moment(st).format('YYYY'), dates)
case "month":
return R.map(st => moment(st).format('MMM YYYY'), dates)
case "week":
return R.map(st =>
`${moment(st).format('M/D/YY')} - ${moment(st).add(7, 'days').format('M/D/YY')}`
, dates)
default:
return R.map(st => moment(st).format(frontendFormat), dates)
}
}
const formatDatasets = (data) => [
dataset('One time'
, 'onetime_cents'
, '66, 179, 223'
, data)
, dataset('Recurring'
, 'recurring_cents'
, '240, 205, 108'
, data)
, dataset('Tickets'
, 'tickets_cents'
, '238, 132, 128'
, data)
, dataset('Total'
, 'total_cents'
, '195, 195, 195'
, data)
]
function dataset(label, key, rgb, data) {
return {
label: label
, data: R.pluck(key, data)
, borderWidth: 1
, borderColor: `rgb(${rgb})`
, backgroundColor: `rgba(${rgb},0.3)`
}
}
updateChart(defaultParams)