All files / views/bank Update.vue

45.49% Statements 116/255
100% Branches 0/0
0% Functions 0/1
45.49% Lines 116/255

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                       1x
<template>
  <div class="q-ma-lg">
    <!-- Title -->
    <HeaderTitleWithBackButton title="Withdrawal Bank Details"/>
    <div 
      class="q-pa-lg q-mt-xl brx-shadow-sm brx-rounded-8" 
      style="background-color: white"
    >
      <q-form
          @submit="submit"
          v-show="!isEmpty"
      >
        <q-input
            data-test="bank-code"
            v-model="bank.code"
            label="Bank Code *"
            readonly
            outlined
            color="blue-brand"
            
        />
        <q-input
            data-test="bank-name"
            class="q-mt-md"
            v-model="bank.name"
            label="Bank Name *"
            outlined
            color="blue-brand"
            :rules="[alphabetOnly, isRequired, trimSpaceRule, lenghthRuleBank]"
        />
        <div class="row full-width q-my-md">
          <q-file 
            data-test="bank-logo"
            outlined
            color="blue-brand" 
            class="col-6 q-pr-md" 
            v-model="logo" 
            label="Bank Logo"
            accept=".jpg,.png,.webp,.jpeg"
            :rules="[isRequired]"
          >
            <template v-slot:prepend>
              <q-icon name="attach_file"/>
            </template>
 
          </q-file>
 
          <q-select 
            ref="bank-status"
            label="Status *" 
            class="col-6 q-pl-md"
            outlined 
            v-model="status" 
            :options="optionStatus" 
            color="blue-brand"
            :rules="[selectRequiredRule]"
          />
        </div>
 
        <q-input
            data-test="bank-description"
            v-model="bank.description"
            label="Bank Description *"
            type="textarea"
            outlined
            color="blue-brand"
            :rules="[isRequired, lenghthRuleDesc, preventEmoticonRule]"
        />
 
        <div class="q-mt-lg display-content text-left">
          <q-btn class="btn q-mr-md text-white brx-rounded-8 bg-blue-brand" :disable="isSubmit" v-if="!read && haveRoles('banks:update')" flat type="submit" label="Update" size="initial" no-caps/>
          <q-btn class="btn red-color brx-rounded-8" type="button" flat text-color="white" v-if="!read && haveRoles('banks:delete')" @click="openDialogDelete" label="Delete" size="initial" no-caps color="red"/>
          <q-btn class="btn q-ml-md cancel-btn brx-rounded-8" outline color="blue-brand" @click="onCancel()" label="Cancel" size="initial"/>
        </div>
      </q-form>
      <!-- popup -->
      <DeletePopupV2
        :open="data.openDelete" 
        :status="data.status"
        @on-hide="(val:any)=>close(val)"
        @on-change="(v: boolean) => remove(v)"
      />
      <global-popup-v-2
        :isOpen="data.openGlobal"
        :status="data.status"
        @on-close="(val: any)=>close(val)"
      />
      <!-- empty -->
      <div v-show="isEmpty">
        <Empty/>
      </div>
    </div>
  </div>
</template>
 
<script lang="ts">
 
import {defineComponent, ref, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {Bank, option} from '@/common/models'
import {deleteBank, putBanks, getBank, postBankImage} from '@/common/api/api'
import {useQuasar} from "quasar";
import {readOnly} from '@/common/session/user'
import { propertyPublishOptions} from '@/common/enums'
import Empty from '@/components/Empty.vue';
import { getEnumValue, haveRoles } from '@/common/function';
import { alphabetOnlyRules, lenghthRules, numberOnlyRules, preventEmoticon, requiredRules, selectRequiredRules, trimSpace } from '@/utils/validate';
import HeaderTitleWithBackButton from '@/components/HeaderTitleWithBackButton.vue';
import GlobalPopupV2 from '@/components/popup/GlobalPopupV2.vue';
import DeletePopupV2 from '@/components/popup/DeletePopupV2.vue';
 
export default defineComponent({
  name: 'update',
  components: { Empty, HeaderTitleWithBackButton, GlobalPopupV2, DeletePopupV2 },
  setup() {
    const $q = useQuasar()
    const bank = ref({} as Bank)
    const route = useRoute()
    const logo = ref(new File([""], "NOT FOUND", {type: "text/plain"}))
    const success = ref(false)
    const toList = ref('')
    const read = ref(readOnly())
    const isSubmit=ref(false)
    
    const router = useRouter()
    const isEmpty = ref(false)
    const successDeleted = ref<boolean>(false);
    const status = ref({} as option | undefined);
    const optionStatus = ref(propertyPublishOptions)

    // open popup
    const data = ref({
      openDelete:false,
      openGlobal:false,
      status:''
    })

    const isRequired = (v: any) => requiredRules(v);
    const numberOnly = (v: any) => numberOnlyRules(v);
    const alphabetOnly = (v: any) => alphabetOnlyRules(v);
    const lenghthRuleCode = (v: any) => lenghthRules(v, 3, 3);
    const lenghthRuleDesc = (v: any) => lenghthRules(v, 0, 500);
    const lenghthRuleBank = (v: any) => lenghthRules(v, 2, 50);
    const trimSpaceRule = (v: any) => trimSpace(v);
    const selectRequiredRule = (v: any) => selectRequiredRules(v)
    const preventEmoticonRule = (v: any) => preventEmoticon(v)

    getBank(route.params.code.toString()).then((response) => {
      bank.value = response.data
      status.value = propertyPublishOptions.find(o => o.value == bank.value.status.toString())

      let image = bank.value.logo
      if (image){
        logo.value = new File([""], image.filename as string, {type: "text/plain"})
      }
    }).catch(err => {
      console.log(err)
      isEmpty.value = true
    })

    return {
      ...toRefs(data),
      preventEmoticonRule,
      alphabetOnly,
      lenghthRuleCode,
      numberOnly,
      lenghthRuleDesc,
      lenghthRuleBank,
      trimSpaceRule,
      selectRequiredRule,
      haveRoles,
      isEmpty,
      confirm,
      isRequired,
      data,
      isSubmit,
      logo,
      bank,
      read,
      success,
      toList,
      successDeleted,
      status,
      optionStatus,
      async submit() {
        isSubmit.value = true
        $q.loading.show()

        // Set status bank
        bank.value.status = Number(getEnumValue(status.value));

        try{
          const response = await putBanks([bank.value])
          const code = response.data.at(0).code
          $q.loading.show({
            message:'Waiting for upload photo'
          })
          if(logo.value.size>0){
            await postBankImage(code, logo.value)
          }

          // show success popup
          data.value.status = 'update'
          data.value.openGlobal = true;
        }catch(err: any){
          // show fail popup
          if(err.response.status ==400){
            data.value.status = 'fail'
          }else{
            data.value.status = 'error'
          }
          data.value.openGlobal = true;
          console.log(err);
        }
          $q.loading.hide()
          isSubmit.value = false
      },
      close(e:any){
        data.value.openDelete = e
      },
      onCancel(){
        router.push({
              name: 'bank',
            })
      },
      remove(v:boolean) {
        if(v){
          $q.loading.show()
          deleteBank([bank.value]).then((res) => {
            // close delete confirm popup
            data.value.openDelete = false
            // show success popup
            data.value.status = 'update'
            data.value.openGlobal=true
          }).catch((err) => {
            // show fail popup
            if(err.response.status ==400){
              data.value.status = 'fail'
            }else{
              data.value.status = 'error'
            }
            data.value.openGlobal = true;
            console.log(err)
          })
          $q.loading.hide()
        }
      },
      openDialogDelete(){
        data.value.status = 'delete'
        data.value.openDelete = true
      },
    }
  }
})
</script>