• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@ohos.router'
17import prompt from '@ohos.promptAction'
18import contact from '@ohos.contact'
19import Logger from '../data/Logger'
20import { UpdateContact } from '../component/UpdateContact'
21
22const TAG: string = 'EditContact'
23
24@Entry
25@Component
26struct EditContact {
27  @State contactId: number = 0
28  @State name: string = ''
29  @State email: string = ''
30  @State nickName: string = ''
31  @State phoneNumber: string = ''
32  @State postalAddress: string = ''
33  @State contact: contact.Contact = <contact.Contact> router.getParams()['data']
34
35  build() {
36    Column() {
37      Row() {
38        Image($r('app.media.no'))
39          .id('cancelEdit')
40          .width(32)
41          .height(32)
42          .margin({ left: 32, right: 22 })
43          .objectFit(ImageFit.Contain)
44          .onClick(() => {
45            router.back()
46          })
47
48        Text($r('app.string.editor_contact'))
49          .fontSize(26)
50          .fontWeight(400)
51          .textAlign(TextAlign.End)
52          .fontColor(Color.Black)
53
54        Blank()
55
56        Image($r('app.media.yes'))
57          .id('confirmEdit')
58          .width(32)
59          .height(32)
60          .margin({ right: 32 })
61          .objectFit(ImageFit.Contain)
62          .onClick(async () => {
63            let test = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}/
64            Logger.info(TAG, `this number is ${this.phoneNumber} this test result ${test.test(this.phoneNumber)}`)
65            if (test.test(this.phoneNumber) === true) {
66              let contacts = {
67                id: this.contactId,
68                name: { fullName: this.name },
69                nickName: { nickName: this.nickName },
70                phoneNumbers: [{ phoneNumber: this.phoneNumber }],
71                emails: [{ email: this.email }],
72                postalAddresses: [{ postalAddress: this.postalAddress }]
73              }
74              await contact.updateContact(contacts)
75              router.replaceUrl({
76                url: 'pages/Index'
77              })
78              router.clear()
79            } else {
80              prompt.showToast({
81                message: 'Please input phone number.',
82                duration: 2000,
83              })
84            }
85          })
86      }
87      .height('6%')
88      .width('100%')
89      .margin({ top: 32 })
90      .constraintSize({ minHeight: 50 })
91
92      UpdateContact({
93        name: $name,
94        phoneNumber: $phoneNumber,
95        nickName: $nickName,
96        email: $email,
97        postalAddress: $postalAddress
98      })
99    }
100    .width('100%')
101    .height('100%')
102    .backgroundColor($r('app.color.add_contact_bg'))
103  }
104
105  aboutToAppear() {
106    this.contact.name.fullName === '' ? this.name = '' : this.name = this.contact.name.fullName
107    try {
108      this.contactId = this.contact.id
109    } catch (err) {
110      this.contactId = 0
111    }
112    try {
113      this.nickName = this.contact.nickName.nickName
114    } catch (err) {
115      this.nickName = ''
116    }
117    if (this.contact.phoneNumbers !== undefined) {
118      this.phoneNumber = this.contact.phoneNumbers[0].phoneNumber === '' ? '' : this.contact.phoneNumbers[0].phoneNumber
119    }
120    if (this.contact.emails !== undefined) {
121      this.email = this.contact.emails[0].email === '' ? '' : this.contact.emails[0].email
122    }
123    if (this.contact.postalAddresses !== undefined) {
124      this.postalAddress = this.contact.postalAddresses[0].postalAddress === '' ? '' : this.contact.postalAddresses[0].postalAddress
125    }
126  }
127}