UpLoadImage.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 => {
  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: [1, 1],
  22. quality: 1,
  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. return (
  45. <>
  46. <Button
  47. style={[
  48. props.style,
  49. props.size ? { width: props.size, height: props.size } : {},
  50. ]}
  51. appearance="imageButton"
  52. onPress={() => {
  53. _pickImage(loading).then(img => {
  54. httpPost("/upload/base64", {
  55. base64: img,
  56. }).then(res => {
  57. props.changeIcon(res);
  58. });
  59. });
  60. }}
  61. accessoryLeft={imgprops => {
  62. if (props.value && props.value !== " ") {
  63. return (
  64. <Image
  65. style={{
  66. width: "100%",
  67. height: "100%",
  68. }}
  69. source={{ uri: props.value }}
  70. />
  71. );
  72. }
  73. return renderPulseIcon(imgprops);
  74. }}
  75. />
  76. {/* {props.hasCancel === true && props.value && (
  77. <Button
  78. size="tiny"
  79. status="danger"
  80. accessoryLeft={ForwardIcon}
  81. appearance="imgButton"
  82. style={{
  83. position: "absolute",
  84. right: -5,
  85. top: -5,
  86. borderRadius: 30,
  87. }}
  88. onPress={() => {
  89. showDialog({
  90. bodyText: removeTips,
  91. status: "danger",
  92. cancelable: true,
  93. confirmCallback: () => {
  94. props.delEvent();
  95. },
  96. });
  97. }}
  98. />
  99. )} */}
  100. </>
  101. );
  102. }