This commit is contained in:
David Price
2021-07-12 20:42:52 -07:00
parent c86e479ffe
commit 3dc6ccde15
27 changed files with 2883 additions and 1416 deletions

View File

@@ -1,9 +1,10 @@
import { useMutation, useFlash } from '@redwoodjs/web'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { navigate, routes } from '@redwoodjs/router'
import PostForm from 'src/components/PostForm'
import PostForm from 'src/components/Post/PostForm'
export const QUERY = gql`
query FIND_POST_BY_ID($id: Int!) {
query FindPostById($id: Int!) {
post: post(id: $id) {
id
title
@@ -26,11 +27,10 @@ const UPDATE_POST_MUTATION = gql`
export const Loading = () => <div>Loading...</div>
export const Success = ({ post }) => {
const { addMessage } = useFlash()
const [updatePost, { loading, error }] = useMutation(UPDATE_POST_MUTATION, {
onCompleted: () => {
toast.success('Post updated')
navigate(routes.posts())
addMessage('Post updated.', { classes: 'rw-flash-success' })
},
})

View File

@@ -1,6 +1,7 @@
import { useMutation, useFlash } from '@redwoodjs/web'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { navigate, routes } from '@redwoodjs/router'
import PostForm from 'src/components/PostForm'
import PostForm from 'src/components/Post/PostForm'
const CREATE_POST_MUTATION = gql`
mutation CreatePostMutation($input: CreatePostInput!) {
@@ -11,11 +12,10 @@ const CREATE_POST_MUTATION = gql`
`
const NewPost = () => {
const { addMessage } = useFlash()
const [createPost, { loading, error }] = useMutation(CREATE_POST_MUTATION, {
onCompleted: () => {
toast.success('Post created')
navigate(routes.posts())
addMessage('Post created.', { classes: 'rw-flash-success' })
},
})

View File

@@ -1,4 +1,5 @@
import { useMutation, useFlash } from '@redwoodjs/web'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { Link, routes, navigate } from '@redwoodjs/router'
const DELETE_POST_MUTATION = gql`
@@ -30,11 +31,10 @@ const checkboxInputTag = (checked) => {
}
const Post = ({ post }) => {
const { addMessage } = useFlash()
const [deletePost] = useMutation(DELETE_POST_MUTATION, {
onCompleted: () => {
toast.success('Post deleted')
navigate(routes.posts())
addMessage('Post deleted.', { classes: 'rw-flash-success' })
},
})

View File

@@ -1,7 +1,7 @@
import Post from 'src/components/Post'
import Post from 'src/components/Post/Post'
export const QUERY = gql`
query FIND_POST_BY_ID($id: Int!) {
query FindPostById($id: Int!) {
post: post(id: $id) {
id
title

View File

@@ -7,6 +7,12 @@ import {
Submit,
} from '@redwoodjs/forms'
const formatDatetime = (value) => {
if (value) {
return value.replace(/:\d{2}\.\d{3}\w/, '')
}
}
const PostForm = (props) => {
const onSubmit = (data) => {
props.onSave(data, props?.post?.id)

View File

@@ -1,6 +1,9 @@
import { useMutation, useFlash } from '@redwoodjs/web'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { Link, routes } from '@redwoodjs/router'
import { QUERY } from 'src/components/Post/PostsCell'
const DELETE_POST_MUTATION = gql`
mutation DeletePostMutation($id: Int!) {
deletePost(id: $id) {
@@ -36,16 +39,20 @@ const checkboxInputTag = (checked) => {
}
const PostsList = ({ posts }) => {
const { addMessage } = useFlash()
const [deletePost] = useMutation(DELETE_POST_MUTATION, {
onCompleted: () => {
addMessage('Post deleted.', { classes: 'rw-flash-success' })
toast.success('Post deleted')
},
// This refetches the query on the list page. Read more about other ways to
// update the cache over here:
// https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates
refetchQueries: [{ query: QUERY }],
awaitRefetchQueries: true,
})
const onDeleteClick = (id) => {
if (confirm('Are you sure you want to delete post ' + id + '?')) {
deletePost({ variables: { id }, refetchQueries: ['POSTS'] })
deletePost({ variables: { id } })
}
}

View File

@@ -1,6 +1,6 @@
import { Link, routes } from '@redwoodjs/router'
import Posts from 'src/components/Posts'
import Posts from 'src/components/Post/Posts'
export const QUERY = gql`
query POSTS {