Giter Club home page Giter Club logo

tutorial-photo-gallery-react's Introduction

Build Your First Ionic App: Photo Gallery (Ionic React and Capacitor)

Get started with Ionic by building a photo gallery app that runs on iOS, Android, and the web - with just one codebase. This is the complete project referenced in the "Your First App: React" guide. Follow along to create a complete CRUD (create-read-update-delete) experience.

Powered by Ionic React (web app) and Capacitor (native app runtime).

How It Works

After the user navigates to Tab 2 (Photos), they can tap/click on the camera button to open up the device's camera. After taking or selecting a photo, it's stored permanently into the device's filesystem. When the user reopens the app at a later time, the photo images are loaded from the filesystem and displayed again in the gallery. The user can tap on a photo to be presented with the option to remove the photo.

Feature Overview

Project Structure

  • Tab2 (Photos) (src/pages/Tab2.tsx): Photo Gallery UI and basic logic.
  • usePhotoGallery Hook (src/hooks/usePhotoGallery.ts): Logic encapsulating Capacitor APIs, including Camera, Filesystem, and Preferences.

How to Run

Note: It's highly recommended to follow along with the tutorial guide, which goes into more depth, but this is the fastest way to run the app.

  1. Install Ionic if needed: npm install -g @ionic/cli.
  2. Clone this repository.
  3. In a terminal, change directory into the repo: cd photo-gallery-capacitor-react.
  4. Install all packages: npm install.
  5. Run on the web: ionic serve.
  6. Run on iOS or Android: See here.

tutorial-photo-gallery-react's People

Contributors

dotnetkow avatar elylucas avatar favetisov avatar jcesarmobile avatar mhartington avatar mlynch avatar ykws avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tutorial-photo-gallery-react's Issues

wrong savePicture() function return type - Docs

While following the tutorial in t3rd step, saving photos, the code gives an error.

const takePhoto = async () => {
  const cameraPhoto = await Camera.getPhoto({
    resultType: CameraResultType.Uri,
    source: CameraSource.Camera,
    quality: 100
  });

  const fileName = new Date().getTime() + '.jpeg';
  const savedFileImage = await savePicture(cameraPhoto, fileName);
  const newPhotos = [savedFileImage, ...photos];
  setPhotos(newPhotos); 
};

setPhotos(newPhotos); gives an error because they aren't the same type.

Argument of type '(UserPhoto | Photo)[]' is not assignable to parameter of type 'SetStateAction<UserPhoto[]>'.

The code in this repo has the correct code. The solution:

Change the return type this line in the docs:
const savePicture = async (photo: Photo, fileName: string): Promise

To this one:

const savePicture = async (photo: Photo, fileName: string): Promise<UserPhoto>

Error: Loading module ... disallowed MIME type ("").

In case anyone experiences the issue on page two of trying to capture a photo after first setting up the usePhotoGallery() hook and clicking the photo button for the first time. You may see the following in your console related to loading the pwa-camera-modal:

image

The way to resolve this issue is to go into your vite.config.ts and remove the section for the test and replace it with this:

//Remove:
test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: './src/setupTests.ts',
  }

//Replace with:
optimizeDeps: {
    exclude: ['@ionic/pwa-elements/loader']
  }

Your config file should now look like this:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    exclude: ['@ionic/pwa-elements/loader']
  }
})

This is a known issue in vite: vitejs/vite#12434
You may need to delete your node_modules folder and install again.

Typescript type error for IonReactRouter

Get the below type error after upgrade to React 18,

Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & IntrinsicClassAttributes<IonReactRouter> & Readonly<IonReactRouterProps>'

is there any work around

found 4954 vulnerabilities (4948 low, 6 high) - stuck at emitting

Windows 10
Following how to run instructions
Message Files successfully emitted, waiting for typecheck results...
The console hangs and PWA app does not start.

C:\ae\adaept.com\ae-gallery-react>cd C:\ae\tutorial-photo-gallery-react

C:\ae\tutorial-photo-gallery-react>npm i

> [email protected] postinstall C:\ae\tutorial-photo-gallery-react\node_modules\babel-runtime\node_modules\core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> [email protected] postinstall C:\ae\tutorial-photo-gallery-react\node_modules\core-js
> node -e "try{require('./postinstall')}catch(e){}"


> [email protected] postinstall C:\ae\tutorial-photo-gallery-react\node_modules\core-js-pure
> node -e "try{require('./postinstall')}catch(e){}"

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\watchpack\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\jest-haste-map\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 1789 packages from 901 contributors and audited 1995 packages in 98.387s

58 packages are looking for funding
  run `npm fund` for details

found 4954 vulnerabilities (4948 low, 6 high)
  run `npm audit fix` to fix them, or `npm audit` for details

C:\ae\tutorial-photo-gallery-react>ionic serve
> react-scripts.cmd start
[react-scripts] i 「wds」: Project is running at http://192.168.86.83/
[react-scripts] i 「wds」: webpack output is served from
[react-scripts] i 「wds」: Content not from webpack is served from C:\ae\tutorial-photo-gallery-react\public
[react-scripts] i 「wds」: 404s will fallback to /
[react-scripts] Starting the development server...
[react-scripts]
[react-scripts] Browserslist: caniuse-lite is outdated. Please run:
[react-scripts] npx browserslist@latest --update-db
[react-scripts] Files successfully emitted, waiting for typecheck results...

image

Ionic Info

C:\ae\tutorial-photo-gallery-react>ionic info

Ionic:

   Ionic CLI       : 6.11.12 (C:\Users\peter\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework : @ionic/react 5.0.7

Capacitor:

   Capacitor CLI   : 2.0.1
   @capacitor/core : 2.0.1

Utility:

   cordova-res : 0.15.1
   native-run  : 1.2.1

System:

   NodeJS : v12.18.3 (C:\Program Files\nodejs\node.exe)
   npm    : 6.13.4
   OS     : Windows 10


C:\ae\tutorial-photo-gallery-react>

The project is runnig at http://192.168.86.83/
Open the URL and get:

image

Cloned repo with no changes:

https://github.com/peterennis/tutorial-photo-gallery-react

Possibly related - same emitting problem:

ionic-team/ionic-framework#22294

running PWA on ios/android web does not work

I've got the demo working on the desktop browser - that is when i click the camera button i get the popup confirming access to the webcam - i can then take photos and everything works as normal.

Trying the same on android (Chrome) and iOS (also Chrome) the button appears to do nothing.

i tried removing the call to defineCustomElements to see what effect that has and i then do get an error message as expected so it seems it is doing something but not enough to get it working

Missed spelled Filesytem

When following the tutorial

I noticed we are importing Filesystem but the code given shows FileSystem.

export function usePhotoGallery() 

const savePicture = async (photo: CameraPhoto, fileName: string): Promise<Photo> => {
  const base64Data = await base64FromPath(photo.webPath!);
  const savedFile = await FileSystem.writeFile({
    path: fileName,
    data: base64Data,
    directory: Directory.Data
  });

  // Use webPath to display the new image instead of base64 since it's
  // already loaded into memory
  return {
    filepath: fileName,
    webviewPath: photo.webPath
  };
};
}
...
}

Bug on iOS replaces existing photos in gallery

When running this on iOS, the gallery does not work reliably. If you save a couple of photos, close the app, restart it and add another photo, then some of the existing ones are replaced by the new ones. How come?

Error when try to run with Node.js v18 (LTS)

I got this error right after create the project with ionic start and using the template my-first-app. I saw that the builder clone this repo, so that's why I created this issue here.

valdinei@ideapad:~/dev/ionic-test$ ionic serve
> react-scripts start
[react-scripts] ℹ 「wds」: Project is running at http://172.25.17.254/
[react-scripts] ℹ 「wds」: webpack output is served from
[react-scripts] ℹ 「wds」: Content not from webpack is served from /home/valdinei/dev/ionic-test/public
[react-scripts] ℹ 「wds」: 404s will fallback to /
[react-scripts] Starting the development server...
[react-scripts]
[react-scripts] Error: error:0308010C:digital envelope routines::unsupported
[react-scripts]     at new Hash (node:internal/crypto/hash:71:19)
[react-scripts]     at Object.createHash (node:crypto:133:10)
[react-scripts]     at module.exports (/home/valdinei/dev/ionic-test/node_modules/webpack/lib/util/createHash.js:135:53)
[react-scripts]     at NormalModule._initBuildHash (/home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:417:16)
[react-scripts]     at handleParseError (/home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:471:10)
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:503:5
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:358:12
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:373:3
[react-scripts]     at iterateNormalLoaders (/home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
[react-scripts]     at iterateNormalLoaders (/home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
[react-scripts] /home/valdinei/dev/ionic-test/node_modules/react-scripts/scripts/start.js:19
[react-scripts]   throw err;
[react-scripts]   ^
[react-scripts] Error: error:0308010C:digital envelope routines::unsupported
[react-scripts]     at new Hash (node:internal/crypto/hash:71:19)
[react-scripts]     at Object.createHash (node:crypto:133:10)
[react-scripts]     at module.exports (/home/valdinei/dev/ionic-test/node_modules/webpack/lib/util/createHash.js:135:53)
[react-scripts]     at NormalModule._initBuildHash (/home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:417:16)
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:452:10
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/webpack/lib/NormalModule.js:323:13
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:367:11
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:233:18
[react-scripts]     at context.callback (/home/valdinei/dev/ionic-test/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
[react-scripts]     at /home/valdinei/dev/ionic-test/node_modules/babel-loader/lib/index.js:59:103 {
[react-scripts]   opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
[react-scripts]   library: 'digital envelope routines',
[react-scripts]   reason: 'unsupported',
[react-scripts]   code: 'ERR_OSSL_EVP_UNSUPPORTED'
[react-scripts] }
[react-scripts] Node.js v18.12.1

I searched and I saw that's probably the old webpack version, that use the old SSL algorithm, but the new Node versions already use a new algorithm.

Plugins not implemented on Android

Steps I took:

git clone https://github.com/ionic-team/tutorial-photo-gallery-react
npm install
ionic serve

and it works in the browser. Then I do

ionic capacitor copy android
# Complains about cordova plugin not being found if I don't do this first
npm install -g serve
serve -s build

and I've got my gradle build configuration that I then hand off to android studio, which configures and builds the project without errors. When launching on the app on my device, clicking on the camera button does nothing unfortunately.

Logcat shows

2022-03-02 14:59:12.466 11654-11654/io.ionic.starter E/Capacitor/Console: File: http://localhost/static/js/49.e396144e.chunk.js - Line 2 - Msg: Uncaught (in promise) Error: "Camera" plugin is not implemented on android

I'm using ionic 6.18.1.

I tried this solution with npx cap sync android after deleting the assets and plugin in the android folder as they suggest. Clean project in Android Studio. Having trouble figuring out how to get this app working on android, so I figured I'd ask for help.

Many thanks. 🙏

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.