2021-03-11 05:43:45 +01:00
import $ from "jquery" ;
2021-02-28 01:28:31 +01:00
import * as loading from "../loading" ;
2021-03-25 22:35:45 +01:00
import { page _params } from "../page_params" ;
2020-08-01 03:43:15 +02:00
2020-12-10 18:15:09 +01:00
export function create _ajax _request (
url ,
form _name ,
stripe _token = null ,
2020-12-30 19:00:49 +01:00
ignored _inputs = [ ] ,
2020-12-10 18:15:09 +01:00
redirect _to = "/billing" ,
type = "POST" ,
) {
2021-02-03 23:23:32 +01:00
const form = $ ( ` # ${ CSS . escape ( form _name ) } -form ` ) ;
const form _loading _indicator = ` # ${ CSS . escape ( form _name ) } _loading_indicator ` ;
const form _input _section = ` # ${ CSS . escape ( form _name ) } -input-section ` ;
const form _success = ` # ${ CSS . escape ( form _name ) } -success ` ;
const form _error = ` # ${ CSS . escape ( form _name ) } -error ` ;
const form _loading = ` # ${ CSS . escape ( form _name ) } -loading ` ;
2019-01-02 09:12:19 +01:00
2020-07-16 23:29:01 +02:00
const zulip _limited _section = "#zulip-limited-section" ;
const free _trial _alert _message = "#free-trial-alert-message" ;
2020-05-22 15:42:46 +02:00
2020-07-15 00:34:28 +02:00
loading . make _indicator ( $ ( form _loading _indicator ) , {
text : "Processing ..." ,
abs _positioned : true ,
} ) ;
2019-01-02 09:12:19 +01:00
$ ( form _input _section ) . hide ( ) ;
$ ( form _error ) . hide ( ) ;
$ ( form _loading ) . show ( ) ;
2020-05-22 15:42:46 +02:00
$ ( zulip _limited _section ) . hide ( ) ;
$ ( free _trial _alert _message ) . hide ( ) ;
2019-01-02 09:12:19 +01:00
2019-11-02 00:06:25 +01:00
const data = { } ;
2019-01-02 09:12:19 +01:00
if ( stripe _token ) {
2021-04-09 12:43:44 +02:00
data . stripe _token = stripe _token . id ;
2019-01-02 09:12:19 +01:00
}
2021-01-22 22:29:08 +01:00
for ( const item of form . serializeArray ( ) ) {
2020-12-30 19:00:49 +01:00
if ( ignored _inputs . includes ( item . name ) ) {
continue ;
}
2021-04-09 12:43:44 +02:00
data [ item . name ] = item . value ;
2021-01-22 22:29:08 +01:00
}
2019-01-02 09:12:19 +01:00
2020-12-10 18:15:09 +01:00
$ . ajax ( {
type ,
2020-07-20 22:18:43 +02:00
url ,
data ,
success ( ) {
2019-01-02 09:12:19 +01:00
$ ( form _loading ) . hide ( ) ;
$ ( form _error ) . hide ( ) ;
$ ( form _success ) . show ( ) ;
2020-02-08 04:04:36 +01:00
if ( [ "autopay" , "invoice" ] . includes ( form _name ) ) {
2019-01-09 09:26:15 +01:00
if ( "pushState" in history ) {
history . pushState ( "" , document . title , location . pathname + location . search ) ;
} else {
location . hash = "" ;
}
}
2020-06-09 12:24:32 +02:00
window . location . replace ( redirect _to ) ;
2019-01-02 09:12:19 +01:00
} ,
2020-07-20 22:18:43 +02:00
error ( xhr ) {
2019-01-02 09:12:19 +01:00
$ ( form _loading ) . hide ( ) ;
$ ( form _error ) . show ( ) . text ( JSON . parse ( xhr . responseText ) . msg ) ;
$ ( form _input _section ) . show ( ) ;
2020-05-22 15:42:46 +02:00
$ ( zulip _limited _section ) . show ( ) ;
$ ( free _trial _alert _message ) . show ( ) ;
2019-01-02 09:12:19 +01:00
} ,
} ) ;
2021-02-28 01:28:31 +01:00
}
2019-01-02 09:12:19 +01:00
2021-02-28 01:28:31 +01:00
export function format _money ( cents ) {
2019-01-02 09:12:19 +01:00
// allow for small floating point errors
cents = Math . ceil ( cents - 0.001 ) ;
2019-11-02 00:06:25 +01:00
let precision ;
2019-01-02 09:12:19 +01:00
if ( cents % 100 === 0 ) {
precision = 0 ;
} else {
precision = 2 ;
}
// TODO: Add commas for thousands, millions, etc.
return ( cents / 100 ) . toFixed ( precision ) ;
2021-02-28 01:28:31 +01:00
}
2019-01-02 09:12:19 +01:00
2021-02-28 01:28:31 +01:00
export function update _charged _amount ( prices , schedule ) {
$ ( "#charged_amount" ) . text ( format _money ( page _params . seat _count * prices [ schedule ] ) ) ;
}
2019-01-02 09:12:19 +01:00
2021-02-28 01:28:31 +01:00
export function update _discount _details ( organization _type ) {
2021-07-19 03:36:52 +02:00
let discount _notice =
"Your organization may be eligible for a discount on Zulip Standard. Generally, use cases where the users are not your employees are eligible for discounts." ;
2020-06-09 12:24:32 +02:00
const discount _details = {
2021-07-19 03:36:52 +02:00
opensource : "Zulip Cloud Standard is free for open-source projects." ,
research : "Zulip Cloud Standard is free for academic research." ,
nonprofit : "Zulip Cloud Standard is discounted 85%+ for registered nonprofits." ,
event : "Zulip Cloud Standard is free for academic conferences and most nonprofit events." ,
education : "Zulip Cloud Standard is discounted 85% for education." ,
education _nonprofit :
2021-07-19 06:02:10 +02:00
"Zulip Cloud Standard is discounted 90% for education nonprofits with online purchase." ,
2020-06-09 12:24:32 +02:00
} ;
2021-07-19 03:36:52 +02:00
if ( discount _details [ organization _type ] ) {
discount _notice = discount _details [ organization _type ] ;
}
$ ( "#sponsorship-discount-details" ) . text ( discount _notice ) ;
2021-02-28 01:28:31 +01:00
}
2020-06-09 12:24:32 +02:00
2021-02-28 01:28:31 +01:00
export function show _license _section ( license ) {
2019-01-02 09:12:19 +01:00
$ ( "#license-automatic-section" ) . hide ( ) ;
$ ( "#license-manual-section" ) . hide ( ) ;
2020-07-15 01:29:15 +02:00
$ ( "#automatic_license_count" ) . prop ( "disabled" , true ) ;
$ ( "#manual_license_count" ) . prop ( "disabled" , true ) ;
2019-01-02 09:12:19 +01:00
2021-02-03 23:23:32 +01:00
const section _id = ` #license- ${ CSS . escape ( license ) } -section ` ;
2019-01-02 09:12:19 +01:00
$ ( section _id ) . show ( ) ;
2021-02-03 23:23:32 +01:00
const input _id = ` # ${ CSS . escape ( license ) } _license_count ` ;
2019-01-02 09:12:19 +01:00
$ ( input _id ) . prop ( "disabled" , false ) ;
2021-02-28 01:28:31 +01:00
}
2019-01-02 09:12:19 +01:00
2020-10-07 11:47:20 +02:00
let current _page ;
function handle _hashchange ( ) {
2021-02-03 23:23:32 +01:00
$ ( ` # ${ CSS . escape ( current _page ) } -tabs.nav a[href=" ${ CSS . escape ( location . hash ) } "] ` ) . tab ( "show" ) ;
2020-10-07 11:47:20 +02:00
$ ( "html" ) . scrollTop ( 0 ) ;
}
2021-02-28 01:28:31 +01:00
export function set _tab ( page ) {
2019-11-02 00:06:25 +01:00
const hash = location . hash ;
2019-01-02 09:44:45 +01:00
if ( hash ) {
2021-02-03 23:23:32 +01:00
$ ( ` # ${ CSS . escape ( page ) } -tabs.nav a[href=" ${ CSS . escape ( hash ) } "] ` ) . tab ( "show" ) ;
2020-07-15 01:29:15 +02:00
$ ( "html" ) . scrollTop ( 0 ) ;
2019-01-02 09:44:45 +01:00
}
2021-02-03 23:23:32 +01:00
$ ( ` # ${ CSS . escape ( page ) } -tabs.nav-tabs a ` ) . on ( "click" , function ( ) {
2019-02-26 19:28:56 +01:00
location . hash = this . hash ;
2019-01-09 11:09:15 +01:00
} ) ;
2020-10-07 11:47:20 +02:00
current _page = page ;
window . addEventListener ( "hashchange" , handle _hashchange ) ;
2021-02-28 01:28:31 +01:00
}
2019-01-02 09:44:45 +01:00
2021-02-28 01:28:31 +01:00
export function is _valid _input ( elem ) {
2019-01-29 18:28:50 +01:00
return elem [ 0 ] . checkValidity ( ) ;
2021-02-28 01:28:31 +01:00
}