Last active
May 22, 2021 13:00
-
-
Save ssugimoto/83acb96bce057043bc80c7c43fe7c2a0 to your computer and use it in GitHub Desktop.
Amplifyチュートリアルのモジュール4のGraphQL API とデータベースを追加するをモジュール5まで終えた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import './App.css'; | |
import { API,Storage } from 'aws-amplify'; | |
import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react'; | |
import { listNotes } from './graphql/queries'; | |
import { createNote as createNoteMutation, deleteNote as deleteNoteMutation } from './graphql/mutations'; | |
const initialFormState = { name: '', description: '' } | |
function App() { | |
const [notes, setNotes] = useState([]); | |
const [formData, setFormData] = useState(initialFormState); | |
useEffect(() => { | |
fetchNotes(); | |
}, []); | |
async function fetchNotes() { | |
const apiData = await API.graphql({ query: listNotes }); | |
const notesFromAPI = apiData.data.listNotes.items; | |
await Promise.all(notesFromAPI.map(async note => { | |
if (note.image) { | |
const image = await Storage.get(note.image); | |
note.image = image; | |
} | |
return note; | |
})) | |
setNotes(apiData.data.listNotes.items); | |
} | |
async function createNote() { | |
if (!formData.name || !formData.description) return; | |
await API.graphql({ query: createNoteMutation, variables: { input: formData } }); | |
if (formData.image) { | |
const image = await Storage.get(formData.image); | |
formData.image = image; | |
} | |
setNotes([ ...notes, formData ]); | |
setFormData(initialFormState); | |
} | |
async function deleteNote({ id }) { | |
const newNotesArray = notes.filter(note => note.id !== id); | |
setNotes(newNotesArray); | |
await API.graphql({ query: deleteNoteMutation, variables: { input: { id } }}); | |
} | |
async function onChange(e) { | |
if (!e.target.files[0]) return | |
const file = e.target.files[0]; | |
setFormData({ ...formData, image: file.name }); | |
await Storage.put(file.name, file); | |
fetchNotes(); | |
} | |
return ( | |
<div className="App"> | |
<h1>My Notes App</h1> | |
<input | |
onChange={e => setFormData({ ...formData, 'name': e.target.value})} | |
placeholder="Note name" | |
value={formData.name} | |
/> | |
<input | |
onChange={e => setFormData({ ...formData, 'description': e.target.value})} | |
placeholder="Note description" | |
value={formData.description} | |
/> | |
<input type="file" onChange={onChange} /> | |
<button onClick={createNote}>Create Note</button> | |
<div style={{marginBottom: 30}}> | |
{ | |
notes.map(note => ( | |
<div key={note.id || note.name}> | |
<hr></hr> | |
<h2>{note.name}</h2> | |
<p>{note.description}</p> | |
<button onClick={() => deleteNote(note)}>Delete note</button> | |
{ note.image && <img alt="a" src={note.image} style={{ width: 400 }} />} | |
</div> | |
)) | |
} | |
</div> | |
<AmplifySignOut /> | |
</div> | |
); | |
} | |
export default withAuthenticator(App); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
// this is an auto generated file. This will be overwritten | |
export const createNote = /* GraphQL */ ` | |
mutation CreateNote( | |
$input: CreateNoteInput! | |
$condition: ModelNoteConditionInput | |
) { | |
createNote(input: $input, condition: $condition) { | |
id | |
name | |
description | |
image | |
createdAt | |
updatedAt | |
} | |
} | |
`; | |
export const updateNote = /* GraphQL */ ` | |
mutation UpdateNote( | |
$input: UpdateNoteInput! | |
$condition: ModelNoteConditionInput | |
) { | |
updateNote(input: $input, condition: $condition) { | |
id | |
name | |
description | |
image | |
createdAt | |
updatedAt | |
} | |
} | |
`; | |
export const deleteNote = /* GraphQL */ ` | |
mutation DeleteNote( | |
$input: DeleteNoteInput! | |
$condition: ModelNoteConditionInput | |
) { | |
deleteNote(input: $input, condition: $condition) { | |
id | |
name | |
description | |
image | |
createdAt | |
updatedAt | |
} | |
} | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
// this is an auto generated file. This will be overwritten | |
export const getNote = /* GraphQL */ ` | |
query GetNote($id: ID!) { | |
getNote(id: $id) { | |
id | |
name | |
description | |
image | |
createdAt | |
updatedAt | |
} | |
} | |
`; | |
export const listNotes = /* GraphQL */ ` | |
query ListNotes( | |
$filter: ModelNoteFilterInput | |
$limit: Int | |
$nextToken: String | |
) { | |
listNotes(filter: $filter, limit: $limit, nextToken: $nextToken) { | |
items { | |
id | |
name | |
description | |
image | |
createdAt | |
updatedAt | |
} | |
nextToken | |
} | |
} | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Note @model { | |
id: ID! | |
name: String! | |
description: String | |
image: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
schema.graphql:graphql のスキーマファイルの修正漏れ
Todo → Note へ
修正したら
で jsのいくつか自動生成される。App.jsを修正する