EditPage.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  2. import Inputs from "@/components/input/Inputs";
  3. import Footer from "@/components/layout/Footer";
  4. import { ColorType, MainColorType } from "@/context/themes/color";
  5. import { ChooseScenarioBtn } from "@/features/common/SpecBtns";
  6. import { update } from "@/services/user";
  7. import { updateSuccess } from "@/store/user";
  8. import { rpxToPx } from "@/utils/tools";
  9. import { Input, View, Text } from "@tarojs/components";
  10. import Taro, { useRouter } from "@tarojs/taro";
  11. import { useEffect, useState } from "react";
  12. import { useTranslation } from "react-i18next";
  13. import { TextInput } from "react-native";
  14. import { useDispatch, useSelector } from "react-redux";
  15. let useNavigation;
  16. let useRoute;
  17. if (process.env.TARO_ENV == 'rn') {
  18. useRoute = require("@react-navigation/native").useRoute
  19. useNavigation = require("@react-navigation/native").useNavigation
  20. }
  21. export default function Page() {
  22. let router
  23. if (process.env.TARO_ENV == 'rn') {
  24. router = useRoute()
  25. }
  26. else {
  27. router = useRouter()
  28. }
  29. const user = useSelector((state: any) => state.user);
  30. const [value, setValue] = useState('');
  31. const [btnDisable, setBtnDisable] = useState(false);
  32. const { t } = useTranslation()
  33. const dispatch = useDispatch();
  34. let navigation;
  35. if (useNavigation) {
  36. navigation = useNavigation()
  37. }
  38. useEffect(() => {
  39. Taro.setNavigationBarTitle({
  40. title:t('health.edit_my_name')
  41. })
  42. if (process.env.TARO_ENV == 'rn') {
  43. setValue(router.params.name as any)
  44. navigation.setOptions({ title: t('page.edit_nickname.title') ?? '' });
  45. return
  46. }
  47. setTimeout(() => {
  48. setValue(router.params.name as any)
  49. // setValue(user.nickname)
  50. }, 300)
  51. }, [])
  52. const handleChange = (e) => {
  53. setValue(e);
  54. };
  55. function save() {
  56. if (value.length == 0) {
  57. Taro.showToast({
  58. title: t('feature.common.toast.input_nickname'),
  59. icon: 'none'
  60. })
  61. return;
  62. }
  63. setBtnDisable(true)
  64. update({ nickname: value }).then(res => {
  65. dispatch(updateSuccess({ nickname: value }));
  66. setBtnDisable(false)
  67. Taro.showToast({
  68. title: t('feature.common.toast.update_success'),
  69. icon: 'none'
  70. })
  71. setTimeout(() => {
  72. if (process.env.TARO_ENV == 'weapp') {
  73. Taro.navigateBack()
  74. console.log('edit page navi back')
  75. }
  76. else {
  77. navigation.goBack()
  78. }
  79. }, 300)
  80. }).catch(e => {
  81. setBtnDisable(false)
  82. })
  83. // dispatch(update({nickname:value}).then() as any)
  84. }
  85. return <View style={{ flex: 1 }}>
  86. <View style={{ height: 20 }}></View>
  87. {/* <Input type="nickname" placeholder="请输入昵称" style={{ color: '#fff' }} value={value} onInput={handleChange} /> */}
  88. {
  89. process.env.TARO_ENV == 'weapp' ? <Inputs autoFocus placeholder={t('feature.common.toast.input_nickname')} value={value} onChange={handleChange} onConfirm={save}
  90. openType="nickname"
  91. /> :
  92. <Inputs autoFocus placeholder={t('feature.common.toast.input_nickname')} value={value} onChange={handleChange} onConfirm={save}
  93. />
  94. }
  95. <View className="cell_footer">{t('page.user_profile.nickname_footer')}</View>
  96. <View style={{ flex: 1 }} />
  97. {/* <Footer>
  98. <ChooseScenarioBtn disable={btnDisable} title={t('page.user_profile.save')} onClick={save} background={ColorType.fast} />
  99. </Footer>
  100. <View style={{ height: 40 }} /> */}
  101. <View className="main_footer">
  102. <NewButton
  103. color={MainColorType.blue}
  104. type={NewButtonType.fill}
  105. title={t('feature.common.picker_confirm_btn')}
  106. width={rpxToPx(646)}
  107. height={rpxToPx(96)}
  108. disable={btnDisable}
  109. bold={true}
  110. onClick={save} />
  111. </View>
  112. </View>
  113. }