Skip to content

Commit

Permalink
add to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
cchamangwana committed Aug 10, 2022
1 parent 1907a43 commit ac7a374
Show file tree
Hide file tree
Showing 24 changed files with 1,021 additions and 83 deletions.
4 changes: 2 additions & 2 deletions api/src/graphql/products.sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const schema = gql`
}
type Query {
products: [Product!]! @requireAuth
product(id: String!): Product @requireAuth
products: [Product!]! @skipAuth
product(id: String!): Product @skipAuth
}
input CreateProductInput {
Expand Down
15 changes: 14 additions & 1 deletion web/config/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,18 @@ module.exports = {
theme: {
extend: {},
},
plugins: [],
// plugins: [],
plugins: [require("daisyui")],

// daisyUI config (optional)
daisyui: {
styled: true,
themes: true,
base: true,
utils: true,
logs: true,
rtl: false,
prefix: "",
darkTheme: "light",
},
}
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@redwoodjs/forms": "2.1.0",
"@redwoodjs/router": "2.1.0",
"@redwoodjs/web": "2.1.0",
"daisyui": "^2.19.1",
"prop-types": "15.8.1",
"react": "17.0.2",
"react-dom": "17.0.2",
Expand Down
11 changes: 2 additions & 9 deletions web/src/Routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// In this file, all Page components from 'src/pages` are auto-imported. Nested
// directories are supported, and should be uppercase. Each subdirectory will be
// prepended onto the component name.
//
// Examples:
//
// 'src/pages/HomePage/HomePage.js' -> HomePage
// 'src/pages/Admin/BooksPage/BooksPage.js' -> AdminBooksPage

import { Router, Route, Set } from '@redwoodjs/router'

Expand All @@ -16,6 +8,7 @@ import ProductsLayout from 'src/layouts/ProductsLayout'

import CategoriesLayout from 'src/layouts/CategoriesLayout'
import UserLayout from 'src/layouts/UserLayout'
import SupplierLayout from './layouts/SupplierLayout/SupplierLayout'

const Routes = () => {
return (
Expand All @@ -32,7 +25,7 @@ const Routes = () => {
<Route path="/orders/{id}" page={OrderOrderPage} name="order" />
<Route path="/orders" page={OrderOrdersPage} name="orders" />
</Set>
<Set wrap={ProductsLayout}>
<Set wrap={SupplierLayout} private unauthenticated="home" role="admin" >
<Route path="/products/new" page={ProductNewProductPage} name="newProduct" />
<Route path="/products/{id}/edit" page={ProductEditProductPage} name="editProduct" />
<Route path="/products/{id}" page={ProductProductPage} name="product" />
Expand Down
1 change: 1 addition & 0 deletions web/src/components/Product/ProductCell/ProductCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const QUERY = gql`
userId
createdAt
categoryId
}
}
`
Expand Down
18 changes: 14 additions & 4 deletions web/src/components/Product/Products/Products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import humanize from 'humanize-string'
import { Link, routes } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { useAuth } from '@redwoodjs/auth'

import { QUERY } from 'src/components/Product/ProductsCell'

Expand Down Expand Up @@ -54,6 +55,15 @@ const checkboxInputTag = (checked) => {
}

const ProductsList = ({ products }) => {
const { currentUser } = useAuth()
console.log('currentUser', currentUser)

// filter products by userId
const filteredProducts = products.filter((product) => {
return product.userId === currentUser.id
})
console.log('filteredProducts', filteredProducts)

const [deleteProduct] = useMutation(DELETE_PRODUCT_MUTATION, {
onCompleted: () => {
toast.success('Product deleted')
Expand Down Expand Up @@ -83,9 +93,9 @@ const ProductsList = ({ products }) => {
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Image</th>
{/* <th>Image</th> */}
<th>Is active</th>
<th>User id</th>
<th>Owner</th>
<th>Created at</th>
<th>Category id</th>
<th>&nbsp;</th>
Expand All @@ -98,9 +108,9 @@ const ProductsList = ({ products }) => {
<td>{truncate(product.name)}</td>
<td>{truncate(product.description)}</td>
<td>{truncate(product.price)}</td>
<td>{truncate(product.image)}</td>
<td>{checkboxInputTag(product.isActive)}</td>
<td>{truncate(product.userId)}</td>
{/* <td>{truncate(product.userId)}</td> */}
<td>{truncate(product.user.name)}</td>
<td>{timeTag(product.createdAt)}</td>
<td>{truncate(product.categoryId)}</td>
<td>
Expand Down
7 changes: 6 additions & 1 deletion web/src/components/Product/ProductsCell/ProductsCell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Link, routes } from '@redwoodjs/router'

import Products from 'src/components/Product/Products'

export const QUERY = gql`
Expand All @@ -14,9 +13,15 @@ export const QUERY = gql`
userId
createdAt
categoryId
user {
id
name
}
}
}
`
// get products where userId = userId


export const Loading = () => <div>Loading...</div>

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/ProductItem/ProductItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link, routes } from '@redwoodjs/router'
const ProductItem = ({ product }) => {
return (
<div className="p-4 md:w-1/3">
<div className="h-full border-2 border-gray-800 rounded-lg overflow-hidden">
<div className="h-full border-2 border-gray-300 rounded-lg overflow-hidden">
<Link to={routes.productDetails({id: product.id })}>
<img
className="lg:h-96 md:h-36 w-full object-cover object-center"
Expand Down
68 changes: 68 additions & 0 deletions web/src/components/User/EditUserCell/EditUserCell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { navigate, routes } from '@redwoodjs/router'

import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'

import UserForm from 'src/components/User/UserForm'

export const QUERY = gql`
query EditUserById($id: String!) {
user: user(id: $id) {
id
name
email
hashedPassword
salt
resetToken
resetTokenExpiresAt
roles
}
}
`
const UPDATE_USER_MUTATION = gql`
mutation UpdateUserMutation($id: String!, $input: UpdateUserInput!) {
updateUser(id: $id, input: $input) {
id
name
email
hashedPassword
salt
resetToken
resetTokenExpiresAt
roles
}
}
`

export const Loading = () => <div>Loading...</div>

export const Failure = ({ error }) => (
<div className="rw-cell-error">{error.message}</div>
)

export const Success = ({ user }) => {
const [updateUser, { loading, error }] = useMutation(UPDATE_USER_MUTATION, {
onCompleted: () => {
toast.success('User updated')
navigate(routes.users())
},
onError: (error) => {
toast.error(error.message)
},
})

const onSave = (input, id) => {
updateUser({ variables: { id, input } })
}

return (
<div className="rw-segment">
<header className="rw-segment-header">
<h2 className="rw-heading rw-heading-secondary">Edit User {user.id}</h2>
</header>
<div className="rw-segment-main">
<UserForm user={user} onSave={onSave} error={error} loading={loading} />
</div>
</div>
)
}
42 changes: 42 additions & 0 deletions web/src/components/User/NewUser/NewUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { navigate, routes } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'

import UserForm from 'src/components/User/UserForm'

const CREATE_USER_MUTATION = gql`
mutation CreateUserMutation($input: CreateUserInput!) {
createUser(input: $input) {
id
}
}
`

const NewUser = () => {
const [createUser, { loading, error }] = useMutation(CREATE_USER_MUTATION, {
onCompleted: () => {
toast.success('User created')
navigate(routes.users())
},
onError: (error) => {
toast.error(error.message)
},
})

const onSave = (input) => {
createUser({ variables: { input } })
}

return (
<div className="rw-segment">
<header className="rw-segment-header">
<h2 className="rw-heading rw-heading-secondary">New User</h2>
</header>
<div className="rw-segment-main">
<UserForm onSave={onSave} loading={loading} error={error} />
</div>
</div>
)
}

export default NewUser
129 changes: 129 additions & 0 deletions web/src/components/User/User/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import humanize from 'humanize-string'

import { Link, routes, navigate } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'

const DELETE_USER_MUTATION = gql`
mutation DeleteUserMutation($id: String!) {
deleteUser(id: $id) {
id
}
}
`

const formatEnum = (values) => {
if (values) {
if (Array.isArray(values)) {
const humanizedValues = values.map((value) => humanize(value))
return humanizedValues.join(', ')
} else {
return humanize(values)
}
}
}

const jsonDisplay = (obj) => {
return (
<pre>
<code>{JSON.stringify(obj, null, 2)}</code>
</pre>
)
}

const timeTag = (datetime) => {
return (
datetime && (
<time dateTime={datetime} title={datetime}>
{new Date(datetime).toUTCString()}
</time>
)
)
}

const checkboxInputTag = (checked) => {
return <input type="checkbox" checked={checked} disabled />
}

const User = ({ user }) => {
const [deleteUser] = useMutation(DELETE_USER_MUTATION, {
onCompleted: () => {
toast.success('User deleted')
navigate(routes.users())
},
onError: (error) => {
toast.error(error.message)
},
})

const onDeleteClick = (id) => {
if (confirm('Are you sure you want to delete user ' + id + '?')) {
deleteUser({ variables: { id } })
}
}

return (
<>
<div className="rw-segment">
<header className="rw-segment-header">
<h2 className="rw-heading rw-heading-secondary">
User {user.id} Detail
</h2>
</header>
<table className="rw-table">
<tbody>
<tr>
<th>Id</th>
<td>{user.id}</td>
</tr>
<tr>
<th>Name</th>
<td>{user.name}</td>
</tr>
<tr>
<th>Email</th>
<td>{user.email}</td>
</tr>
<tr>
<th>Hashed password</th>
<td>{user.hashedPassword}</td>
</tr>
<tr>
<th>Salt</th>
<td>{user.salt}</td>
</tr>
<tr>
<th>Reset token</th>
<td>{user.resetToken}</td>
</tr>
<tr>
<th>Reset token expires at</th>
<td>{timeTag(user.resetTokenExpiresAt)}</td>
</tr>
<tr>
<th>Roles</th>
<td>{user.roles}</td>
</tr>
</tbody>
</table>
</div>
<nav className="rw-button-group">
<Link
to={routes.editUser({ id: user.id })}
className="rw-button rw-button-blue"
>
Edit
</Link>
<button
type="button"
className="rw-button rw-button-red"
onClick={() => onDeleteClick(user.id)}
>
Delete
</button>
</nav>
</>
)
}

export default User
Loading

0 comments on commit ac7a374

Please sign in to comment.