UpLoadImage.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* eslint-disable consistent-return */
  2. /* eslint-disable no-else-return */
  3. /* eslint-disable react/destructuring-assignment */
  4. /* eslint-disable no-async-promise-executor */
  5. /* eslint-disable prefer-promise-reject-errors */
  6. /* eslint-disable no-underscore-dangle */
  7. import React from "react";
  8. import { Button, Icon } from "@ui-kitten/components";
  9. import { useModel } from "flooks";
  10. import * as ImagePicker from "expo-image-picker";
  11. import { Image, Platform } from "react-native";
  12. const _pickImage = (loading, aspect) => {
  13. return ImagePicker.requestCameraRollPermissionsAsync()
  14. .then(res => {
  15. if (!res.granted) {
  16. return Promise.reject("notAllod");
  17. } else {
  18. return ImagePicker.launchImageLibraryAsync({
  19. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  20. allowsEditing: true,
  21. aspect: aspect || [(1, 1)],
  22. quality: 0.6,
  23. base64: true,
  24. });
  25. }
  26. })
  27. .then(res => {
  28. if (!res.cancelled) {
  29. loading();
  30. if (Platform.OS === "web") {
  31. return Promise.resolve(res.uri);
  32. }
  33. return Promise.resolve(res.base64);
  34. } else {
  35. return Promise.reject("cancel");
  36. }
  37. });
  38. };
  39. export default function UpLoadImage(props) {
  40. const { httpPost } = useModel("httpModel", true);
  41. const { loading } = useModel("loadingModel", true);
  42. // eslint-disable-next-line no-shadow
  43. const renderPulseIcon = props => <Icon {...props} name="plus-outline" />;
  44. const { aspect, size } = props;
  45. const width = React.useMemo(() => {
  46. if (size) {
  47. return size;
  48. } else {
  49. return 67;
  50. }
  51. }, [size, aspect]);
  52. const height = React.useMemo(() => {
  53. if (size && aspect) {
  54. return (size / aspect[0]) * aspect[1];
  55. } else if (size) {
  56. return size;
  57. } else {
  58. return 67;
  59. }
  60. }, [size, aspect]);
  61. return (
  62. <>
  63. <Button
  64. style={[props.style, { width, height }]}
  65. appearance="imageButton"
  66. onPress={() => {
  67. _pickImage(loading, aspect).then(img => {
  68. httpPost("/upload/base64", {
  69. base64: img,
  70. }).then(res => {
  71. props.changeIcon(res);
  72. });
  73. });
  74. }}
  75. accessoryLeft={imgprops => {
  76. if (props.value && props.value !== " ") {
  77. return (
  78. <Image
  79. style={{
  80. width: "100%",
  81. height: "100%",
  82. }}
  83. source={{ uri: props.value }}
  84. />
  85. );
  86. }
  87. return renderPulseIcon(imgprops);
  88. }}
  89. />
  90. {/* {props.hasCancel === true && props.value && (
  91. <Button
  92. size="tiny"
  93. status="danger"
  94. accessoryLeft={ForwardIcon}
  95. appearance="imgButton"
  96. style={{
  97. position: "absolute",
  98. right: -5,
  99. top: -5,
  100. borderRadius: 30,
  101. }}
  102. onPress={() => {
  103. showDialog({
  104. bodyText: removeTips,
  105. status: "danger",
  106. cancelable: true,
  107. confirmCallback: () => {
  108. props.delEvent();
  109. },
  110. });
  111. }}
  112. />
  113. )} */}
  114. </>
  115. );
  116. }