Sat Dec 17 2016
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
<template>
  <main>
    <h2 class="ui  header">Create &amp; schedule a flip</h2>
    <form class="ui large form" @submit.prevent='handleSubmit'>
      
      <!--  BASIC INFO -->
      <div class="ui attached message">
        <h3 class="ui  header">Basic info</h3>
      </div>

      <div class="ui attached segment">        
        <div class="three fields">

          <!-- name -->
          <div class="required field">
            <label>Flip Name</label>
            <i>Just a name so you can edit this later if needed</i>
            <input type="text" name="name" placeholder="Cybermonday 2017">
          </div>
          
          <!-- promosoet -->
          <div class=" required field">
            <label>Sitewide promoset</label>
            <i>Will apply to catalog pages</i>
            <select class="ui search selection dropdown" name='promoset' data-dropdown-promos>
              <option value="">select a promo</option>          
            </select>
          </div>

          <!--  date -->
          <div class="required field">
            <label>Flip Date</label>         
            <i>Date &amp; time for this flip</i>
            <div class="ui calendar">
              <div class="ui left icon input">
                <i class="calendar icon"></i>
                <input type="text" name="date" placeholder="select date &amp; time">
              </div>
            </div>
          </div>

        </div>
      </div>    


      <!-- CONSTANT LANDING PAGES -->
      <div class="ui attached message">
        <h3 class="ui header">Landing Pages</h3>
      </div>
      
      <div class="ui attached  segment" :class="{'loading': !constants}">
        <p>
          Below are our <b>"constant"</b> landing pages.
          Specify which page they will "mirror" next   flip.
        </p>

        <div class="four fields" v-for='array in contsantsMultiArray'>          
          <div class="field" v-for='(page, index) in array'>
            <label>{{ page }}</label>          
            <select class="ui search selection dropdown" data-dropdown-pages :name=page>
              <option value="">select a page</option>          
            </select>
          </div>  
        </div>
      </div>


      
      <!-- ASSETS -->
      <div class="ui attached message">
        <h3 class="ui  header">Sitewide Images &amp; Criteo</h3>
      </div>

      <div class="ui attached  segment ">
        <p>Select assets to upload. You can select more than one.</p>
        <div class="field">
          <input type="file" multiple>
        </div>
      </div>


      
      <!-- <div class="ui  bottom attached tiny info  message" style="display:none">
        <i class="warning icon"></i><b>Typically used assets are</b>:
        <div>
            <div class="ui horizontal list">
              <div class="item">mdot-rto.jpg</div>
              <div class="item">us-buynow.jpg</div>
              <div class="item">us-freedemo.jpg</div>
              <div class="item">us-freedemo-mobile.jpg</div>
            </div>
            <div class="ui horizontal list">
              <div class="item">us-hslpbanner.jpg</div>
              <div class="item">es-buynow.jpg</div>
              <div class="item">es-homepage-bg.png</div>
              <div class="item">es-catalog.jpg</div>
            </div>        
          </div>
      </div> -->

      <!-- RTOs -->
      <div class="ui attached message">
        <h3>Desktop and Mobile RTO (catalog)</h3>
      </div>

      <div class="ui attached segment">
        
        <!-- mobile rto -->
        <div class="two fields" style="align-items: center">
          <!--  toggle on|off -->
          <div class="four wide field">
            <div class="ui toggle checkbox">
              <input type="checkbox" name="mobile-rto" class="hidden" @change='handleRtoToggle'>              
              <label>Show m. RTO?</label>
            </div>
          </div>
          
          <!-- url -->
          <div class="fourteen wide field" style="opacity: 0">  
            <input type="text" name="mobile-rto-url" placeholder="Mobile RTO url">
          </div>
        </div>

        <!-- desktop rto -->
        <div class="two fields" style="align-items: center">
          <!-- toggle on|off -->
          <div class="four wide field">
            <div class="ui toggle checkbox">
              <input type="checkbox" name="desktop-rto" class="hidden" @change='handleRtoToggle'>              
              <label>Show desktop RTO?</label>
            </div>
          </div>

          <!-- url -->
          <div class="fourteen wide field" style="opacity: 0">  
            <input type="text" name="desktop-rto-url" placeholder="Desktop RTO url">
          </div>
        </div> 

      </div>

      <div class="fields">
        <div class="sixteen wide field">
          <input type="submit" class="ui fluid primary big button" value="create &amp; schedule" /> 
        </div>
      </div>      
    </form>

    <!-- overwrite modal -->
    <div class="ui modal">
      <i class="close icon"></i>
      <div class="header">
        A flip on this date &amp; time already exists.
      </div>
      <div class="image content">
        <div class="image">
          An image can appear on left or an icon
        </div>
        <div class="description">
          A description can appear on the right
        </div>
      </div>
      <div class="actions">
        <div class="ui button" @click='closeModal'>Cancel</div>
        <div class="ui button">OK</div>
      </div>
    </div>

  </main>

</template>


<script>

  export default {
    name: 'CreateFlip',

    components: {  },

    data() {
      return {
        // Constant landing pages: sitewide, sale, etc
        constants: [],

        // Non constants - campaign based
        temppages: [],

        // Array of subarrays with 4 elements each. For UI purposes
        contsantsMultiArray: [],

        // Promocodes sets
        promosets: [],

        // Is the form being submitted?
        isSubmitting: false,

        // Should we show error modal?
        showModal: false
      }
    },



    updated() {
      $(this.$el).find('.dropdown').dropdown({        
        apiSettings: {
          url: `${window.server}/get-pages`,
          saveRemoteData: true,
          onResponse: function(pages) {
            var response = {
              success: true,
              results: []  
            }
              
            for (let page in pages) {
              if (!pages[page].metadata.constant) {
                response.results.push({name: page, value: page})
              }
            }
            return response
          }
        }
      })

      $(this.$el).find('.ui.calendar').calendar()
      $(this.$el).find('.ui.toggle.checkbox').checkbox()
      
      console.log(this.showModal)
      if (this.showModal) {
        console.log('ok show')
        $(this.$el).find('.ui.modal').modal('show')
          //.modal({ blurring: true})
          //.modal('setting', 'transition', 'scale')
      } else{
        $(this.$el).find('.ui.modal').modal('hide')
      }

    },


    /*
     * After we mount, fetch our pages and our promos.
     * We store them in the store data so we can generate
     * ui (ie, dynamic number of input field of constant pages)
     */
    
    mounted() {  
      this.$http.get(`${window.server}/get-pages`).then(res => {
        const pages = JSON.parse(res.body)

        // Constant pages
        this.constants = Object.keys(pages).filter(e => pages[e].metadata.constant)

        // Temp pages ( campaign based )
        this.constants = Object.keys(pages).filter(e => !pages[e].metadata.constant)

        // Create the multidimensional array for Vue's ui (rows of 4 dropdowns)
        var constantsCopy = this.constants.slice()        
        while (constantsCopy.length) {
          this.contsantsMultiArray.push(constantsCopy.splice(0,4))
        }             

      }, err => {
        // TODO an alert
      })  

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


    methods: {

      closeModal() {
        console.log('closing')
        this.showModal = false
      },

      /*
       * When we toggle an RTO checkbox, we show/hide
       * the url input to its right
       */
      
      handleRtoToggle(e) {
        const isChecked = $(e.target).parent().hasClass('checked')
        $(e.target).parents('.wide.field').siblings('.wide.field').animate({
          opacity: isChecked
        })       
      },


      /*
       * 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.
       * 
       *  @param {bool} overwrite 
       *    If true, request url will have ?overwrite=true, causing
       *    the server to overwrite a campaign if it exists
       *
       */
      
      handleSubmit(overwrite = false) {
        const
          constants         = {}
          , name            = ($('input[name=name]').val() || '').trim()
          , promoset        = ($('select[name=promoset]').val() || '').trim()
          , date            = ($('input[name=date]').val() || '').trim()
          , assets          = [...document.querySelector('input[type=file]').files]
          , mRto            = $('input[name=mobile-rto]').parent().hasClass('checked')
          , mRtoUrl         = $('input[name=mobile-rto-url').val()
          , desktopRto      = $('input[name=desktop-rto]').parent().hasClass('checked')
          , desktopRtoUrl   = $('input[name=desktop-rto-url').val()
          ;

        // Create constant pages map. ie, {sale: 'blackridayaii'}
        $.each($('[data-dropdown-pages]'), (i, select) => {         
          if (select.value) {
            constants[select.getAttribute('name')] = select.value
          }
        })

        // Validate required fields
        if (!name || !promoset || !date) return

        // Pack above data to send to server
        const formdata = new FormData()
        formdata.append('name', name)
        formdata.append('promoset', promoset)
        formdata.append('date', date)
        formdata.append('constants', JSON.stringify(constants))
        formdata.append('mobile-rto', mRto)
        formdata.append('mobile-rto-url', mRtoUrl)
        formdata.append('desktop-rto', desktopRto)
        formdata.append('desktop-rto-url', desktopRtoUrl)
        assets.forEach(ass => formdata.append('assets[]', ass, ass.name))

        var requestUrl = window.server + '/create-flip/'
        requestUrl += overwrite ? '?overwrite=true' : ''

        const req = $.ajax({
          method: 'post',
          url: requestUrl,
          contentType: false,
          processData: false,
          data: formdata
        })

        req.done(r => {
          this.showModal = true
          var msg
          if (r.result == 0) {
            if (r.payload == 'exists') {
              msg = "A flip on this date and time exists. Overwrite?"
            } else {
              msg = "An error occurred. Try again or call a dev"
            }
          }
          
        })

      }
    }
   
  }



/*
 * Things i need to figure out:
 * Even though i fetch the pages when this component is mounted,
 * i still have to fetch them again (and format accordingly) for
 * semantic ui's dropdown API. There must be a way for semantic
 * ui's dropdown to use the data that we already fetched - but
 * it seems like it's either fetch via API or have it in the htm.
 */




</script>