Thu Jan 05 2017
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
<!--
This is the tempalate for creating and editing a page.
When editing, theres should be /create/page/id/<pagee>
where <page> is the page name.
 -->

<template>
  <main>    
    <h2 class="ui header" v-if='!editMode'>Create a Landing Page</h2>
    <h2 class="ui header" v-else>Update {{ name }}</h2>

    <div class="ui massive red message" v-if='editMode && !pageFound && !isFetching'>
      <i class="warning circle icon"></i>
      This page no longer exists.
    </div>

    <form class="ui form" 
    :class="{'loading': isSubmitting}"
    @submit.prevent=""
    v-if='(editMode && pageFound) || !editMode'
    >
      <div class="ui large header"></div>      

      <div class="three fields">
        <div class="required field" :class="{'error' : pageExists}">
          <label>Name <span v-if="pageExists">- Already exists!</span></label>
          <i>
            rosettastone.com/lp/sbsr/
            <span style="color: cadetblue;letter-spacing:1px"><b>{{name}}</b></span>
          </i>
          <input type="text" name="name" v-model="name" value="" @keyup='handleName' :disabled="editMode">
        </div>

        <div class="required field">
          <label>Promoset</label>
          <i>Promos this page will use</i>
          <select class="ui search selection dropdown"
            v-model='promoset'
            name='promoset'
            data-dropdown-promosets>
            <option value="">select a promo</option>
            <option :value=promo v-for='promo in promosets'>{{ promo }}</option>
          </select>
        </div>     

        <div class="required field">
          <label>Phone</label>
          <i>Phone no. on top</i>
          <input type="text" name="phone" v-model="phone">
        </div>
      </div>


      <div class="three fields">
        <div class="required field">
          <label>Expiration</label>
          <i>Expiration date</i>
          <input type="text" placeholder='09/10/2017 11:59pm' name='expiration' v-model='expiration'>
        </div>
         

        <!-- <div class="required field">
          <label>RTO</label>
          <i>RTO to apply:</i>
          <input type="text" value="1-800-ROSETTA">
        </div> -->

        <div class="required field">
          <label>Market</label>
          <i>Market page belongs to</i>
          <select class="ui selection dropdown" v-model='market' name='market'>
            <option value="us">us</option>          
            <option value="es">es</option>          
          </select>
        </div>


        <div class="field">
          <label>Crescendo</label>
          <i>Crescendo to apply, if any</i>
          <select class="ui search selection dropdown" name='crescendos' v-model='crescendo'>
            <option value="">select a crescendo</option>   
            <option :value=c v-for='c in crescendos'>{{ c }}</option>
          </select>
        </div>    

      </div>

      <!-- <div class="three fields">
               
      </div> -->
      
      <div class="ui basic segment">
        <div class="field">
          <div class="ui slider checkbox">
            <input type="checkbox" name="constant" class="hidden" v-model='constant'>
            <label>
              This is a constant page&nbsp;
              <i class="help circle icon color grey" data-popup data-content="Constnat pages are pages that are always receiving 
              traffic, and flip with each campaign. (ie, sitewide, rmsitewide, sale, social, etc)" data-variation="inverted"></i>
            </label>
          </div>
        </div>
      </div>        

      <div class="ui segments">
        <div class="ui secondary segment">
          <div class="two fields">
            <div class="field">
              <div class="ui left icon fluid input">
                <input type="text" @keyup="handleMastheadKeyup" placeholder="Search mastheads...">
                <i class="search icon"></i>
              </div>
            </div>
            <div class="required field">                   
              <select class="ui search selection dropdown" name='mastheadtype' v-model='mastheadType'>
                <option value="">select visual type</option>
                <option :value=type v-for='type in mastheadTypes'>{{ type }}</option>
              </select>
            </div> 
          </div>            
        </div>

        <div class="ui segment" data-mastheads>
          <div class="mastheads-container">
            <div class="masthead-wrap"
              v-for='img in mastheads'
              @click='handleImageClick'
              v-if="img.visible"
              :class='{"selected" : img.selected}'
              :data-fullname="img.fullname">
                <div>{{ img.name }}</div>
                <img :src=img.uri />
            </div>
          </div>
        </div>
        <div class="ui bottom attached red message" style="display:none">
          <i class="warning circle big icon"></i>
          You must select a masthead!
        </div>
      </div>

     <!--  <h4 class="ui section horizontal divider header">
        <i class="setting icon color grey"></i>
        Additional Optional Fields
      </h4> -->


      <div class="ui styled fluid accordion">

        <div class="title"><i class="dropdown icon"></i>Masthead texts</div>
        <div class="content">
          <div class="two fields">
            <div class="field">
              <label>Top HTML</label>
              <i>HTML shown above price</i>
              <textarea name="tophtml" v-model='tophtml' rows="2"></textarea>          
            </div>
            <div class="field">
              <label>Bottom HTML</label>
              <i>HTML displayed below price</i>
              <textarea name='bottomhtml' v-model='bottomhtml' rows="2"></textarea>          
            </div>
          </div>
        </div>

        <div class="title"><i class="dropdown icon"></i>Javascript</div>
        <div class="content">        
          <div class="field">            
            <textarea name="js" v-model='js'></textarea>          
          </div>        
        </div>

        <div class="title"><i class="dropdown icon"></i>CSS</div>
        <div class="content">
          <div class="field">
            <textarea name="css" v-model='css'></textarea>
          </div>        
        </div>

      </div> <!-- /accordion -->
    


      <div class="ui small header" v-if='formResult.result !== null'>
        <div
        class="ui message"
        :class="{'negative' : formResult.result == 0, 'positive' : formResult.result == 1}"
        v-if="formResult.result !== null"
        >
          <div class="header">
            <i class="warning circle icon" v-if='formResult.result == 0'></i>
            <i class="checkmark large icon" v-if='formResult.result == 1'></i>
            <span v-html="formResult.message"></span>
          </div>
        </div>
      </div>

      <div class="ui small header">        
        <div v-if='!editMode'>
          <button class="ui right right floated big  teal button" data-env='prod' @click='handleSubmit'>Create in prod</button>
          <button class="ui right right floated big button" data-env='stg' @click='handleSubmit'>Create in stg</button>
        </div>
        <div v-else>
          <button class="ui right right floated big  teal button" data-env='prod' @click='handleSubmit'>Update in prod</button>
          <button class="ui right right floated big button" data-env='stg' @click='handleSubmit'>Update in stg</button>
        </div>
        <button
          class="ui right right floated big red button"
          disabled="" @click='handleDelete'
          v-if='editMode'>
          Delete
        </button>
      </div>
      
    </form>
    
  </main>
  

</template>


<script>
  export default {
    name: 'CreatePage',
    data() {
      return {
        // --page info (shown on site)-- //
        name: '',
        promoset: '',
        phone: '1-800-ROSETTA',
        expiration: '',
        market: 'us',
        crescendo: '',
        mastheadType: '',
        constant: '',        
        js: '',
        css: '',
        tophtml: '',
        bottomhtml: '',
        // --other component things-- //
        isSubmitting: false,
        selectedMasthead: null,
        promosets: [],
        pages: [],
        mastheads: [],
        mastheadTypes: [],
        crescendos: [],
        pageExists: false,
        editMode: false,
        pageFound: null,
        isFetching: false,
        formResult: {
          result: null,
          message: null
        }
      }
    },

    created() {

      /*
       * If we are editing (create/flip/id/some-date in the url)
       * fetch all the info to polulate the state with
       */
      
      if (this.$route.params.page) {
        this.editMode = true
        this.isFetching = true
      }

      /* Get values for our dropdowns */

      $.getJSON(`${window.server}/promosets`, r => this.promosets = Object.keys(r))

      $.getJSON(`${window.server}/crescendos`, r =>  this.crescendos = r)

      $.getJSON(`${window.server}/masthead-types`, r =>  this.mastheadTypes = r)

      // This was here for in real time 'page exists' as user typed name
      //$.getJSON(`${window.server}/pages`, r => this.pages = r)

      $.getJSON(`${window.server}/mastheads/`, r=> {
        this.mastheads = r
          .filter(img => img.match(/sbsr-desktop/i))          
          .map(i => ({
            uri: `${window.server}/mastheads/${i}`,
            name: i.split('.')[0].split('-')[2],
            fullname: i,
            selected: self.selectedMasthead == i ? true : false,
            visible: true
          }))  
      })

    },


    mounted() {
      window.sessionStorage.clear()
      var self = this

      /* Initialize  dropdowns */
      const $ddPromosets = $(this.$el).find('[data-dropdown-promosets]')
      $ddPromosets.dropdown()

      const $ddCrescendos = $(this.$el).find('select[name=crescendos]')
      $ddCrescendos.dropdown()      

      const $ddMarket = $(this.$el).find('select[name=market]')
      $ddMarket.dropdown()

      const $ddMastheadTypes = $(this.$el).find('select[name=mastheadtype]')
      $ddMastheadTypes.dropdown()

      if (this.editMode) {
        $.getJSON(`${window.server}/pages/`, r => {
          var page = r.filter(p => p.name == this.$route.params.page)
          if (page.length) {
            page = page[0]
            this.pageFound = true
            this.name = page.name
            this.phone = page.phone
            this.expiration = page.expiration
            this.promoset = page.promoset
            this.crescendo = page.crescendo
            this.mastheadType = page.mastheadtype
            this.selectedMasthead = page.mastheadimage
            this.constant = page.metadata.constant
            this.market = page.metadata.market
            this.tophtml = page.tophtml
            this.bottomhtml = page.bottomhtml
            this.css = page.css
            this.js = page.js
            
            /* Some UI stuff needs manual handling */
            
            // If we're editing, we want to make selected masthead visible.
            // Find masthead with same name as this.selectedMasthead and
            // set its selected value to true. Then sort it to position 0
            // So user sees as first image on the masthead list          
            this.mastheads = this.mastheads.map(m => {
              m.selected = m.fullname === this.selectedMasthead ? true : false
              return m
            })
            .sort(a => !a.selected)

            
            // Presetting dropdown values
            // $ddPromosets.dropdown('set value', this.promoset)
            // $ddCrescendos.dropdown('set value', this.crescendo)            
            // $ddMarket.dropdown('set value', this.market)
            // $ddMastheadTypes.dropdown('set value', this.mastheadType)
          }

          this.isFetching = false
        })
      }
       


      // Initialize some semantic-ui  DOM elements      
      $(this.$el).find('.ui.accordion').accordion()
      $(this.$el).find('.ui.checkbox').checkbox()
      $(this.$el).find('[data-popup]').popup()

      // Define required form fields - visual purposes only
      $('form').form({
        fields: {
         'name': 'empty',
         'promoset': 'empty',
         'phone': 'empty',
         'expiration': 'empty',
         'market': 'empty',
         'mastheadtype': 'empty'
        }
      })

    },

    updated() {

    },


    methods: {
      handleName(e) {
        this.name = e.target.value        
        this.pageExists = this.pages.includes(this.name) ? true : false        
      },

      handleImageClick(e) {
        var $parent = $(e.target).parents('.masthead-wrap')        
        $(this.$el).find('.mastheads-container .selected').removeClass('selected')
        $parent.addClass('selected')        
        this.selectedMasthead = $parent.data('fullname')        
        $('.ui.bottom').hide()
      },


      /*
       * Filter masthead images as user types.
       * Inefficient because done in a hurry
       */
      
      handleMastheadKeyup(e) {
        var value = e.target.value
        this.mastheads = this.mastheads.map(img => {          
          img.visible = img.name.match(value) ? true : false
          return img          
        })     
      },


      /*
       * Submit our form. No need to bind every single input to a state data.
       * So we use jquery to grab the values of the form.
       *
       */
      
      handleSubmit(e) {        
        const
          self            = this
          , name          = this.name.trim()
          , promoset      = this.promoset
          , phone         = this.phone
          , expiration    = this.expiration
          , market        = this.market
          , mastheadimg   = this.selectedMasthead
          , mastheadtype  = this.mastheadType
          , constant      = $('input[name=constant]').parent().hasClass('checked')
          , crescendo     = this.crescendo
          , tophtml       = this.tophtml
          , bottomhtml    = this.bottomhtml
          , css           = this.css
          , js            = this.js
          , env           = $(e.target).data('env')
          ;

        if (!mastheadimg)
          $('.ui.bottom').show()        
        
        if (
          !this.name
          || !this.promoset
          || !this.expiration
          || !this.mastheadType
          || !this.selectedMasthead
          || !this.market
        )  return

        self.isSubmitting = true

        const formdata = new FormData()
        formdata.append('name', name.toLowerCase())
        formdata.append('promoset', promoset)
        formdata.append('phone', phone)
        formdata.append('expiration', expiration)
        formdata.append('market', market)
        formdata.append('constant', constant)
        formdata.append('crescendo', crescendo)
        formdata.append('mastheadimage', mastheadimg)
        formdata.append('mastheadtype', mastheadtype)
        formdata.append('tophtml', tophtml)
        formdata.append('bottomhtml', bottomhtml)
        formdata.append('css', css)
        formdata.append('js', js)
        formdata.append('inprod', env == 'prod' ? true : false)
        
        const method = this.editMode ? 'PUT' : 'POST'
        const url = `${window.server}/page/${this.editMode ? name : ''}`
        const request = new XMLHttpRequest()
        
        request.open(method, url, true)
        request.send(formdata)

        request.onload = function() {
          if (request.status >= 200 && request.status < 400) {
            const response = JSON.parse(request.responseText)            
            self.formResult = {
              result: response.result,
              message: response.payload
            }            
          } else {
            self.formResult = { result: 0, message: 'Error on the server.' }  
          }
          self.isSubmitting = false
        }

        request.onerror = function() {
          self.formResult = { result: 0, message: 'Could not reach server.' }
          self.isSubmitting = false
        }        
      },


      handleDelete(e) {
        return
      }

    }    
  }
</script>