33
loading...
This website collects cookies to deliver better user experience
yarn create redwood-app ./redwood-dashboard
api
directory and the web
directory. The api
directory holds all of the code for the back-end and database while the web
directory holds everything for the front-end.yarn rw dev
api > db
, open the schema.prisma
file.datasource db provider
to postgresql
which you can see in the code snippet below.datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
TODO
and update the example user model.model User {
id Int @id @default(autoincrement())
email String @unique
name String
role String
}
User
model has a few columns for information we want to store about a user. The email
field has a constraint that it always has to be a unique value. The same email address can't be in the database multiple times. Next, we'll add a new model for our Item
and Location
tables. These need to be created at the same time because they have a relationship with each other.model Item {
id Int @id @default(autoincrement())
sku String @unique
name String
location Location @relation(fields: [locationId], references: [id])
locationId Int
}
model Location {
id Int @id @default(autoincrement())
name String
district Int
Item Item[]
}
seed.js
file so that there are a few values present before we start our app. This is common on product dashboards for populating dropdown options or initializing tags that users can add to different settings. We'll be adding a user, an item, and one location to seed our database. Just a heads up, I've deleted a lot of comments from this file./* eslint-disable no-console */
const { PrismaClient } = require('@prisma/client')
const dotenv = require('dotenv')
dotenv.config()
const db = new PrismaClient()
async function main() {
console.warn('Please define your seed data.')
await db.user.create({
data: { name: 'Kaiya', email: '[email protected]', role: 'inventory' },
})
await db.location.create({
data: { name: 'Compound North', district: 1 },
})
await db.item.create({
data: {
name: 'Morning Glory',
sku: 'hf8029ht8942ht8429pht8p942p',
locationId: 1,
},
})
}
main()
.catch((e) => console.error(e))
.finally(async () => {
await db.$disconnect()
})
.env
file at the root of the project, update the DATABASE_URL
to match your Postgres instance. It'll look similar to this.DATABASE_URL=postgres://postgres:admin@localhost:5432/dashboard
yarn rw prisma migrate dev
yarn rw g scaffold user
web > pages
. You should see a new directory called User
. This has all of the pages we need to handle everything about users.web > components
. There will be another User
folder. This one contains the components we use to interact with the GraphQL server. Most of the data is fetched in these components and requests are sent from these components.web > layouts
folder will also have a new directory containing a layout file for your User
pages. This is how you can create styles for a specific set of pages.Routes.js
file, you'll see there is a new set of routes for users wrapped in the UserLayout
. All of the pages generate also have their routes automatically added to this file inside of their parent layout.api
directory. If you look inside api > src > graphql
, you'll find the GraphQL schema for all the CRUD operations you need and the types have been defined by the model in the schema.prisma
file.api > src > services
, you'll find all of the GraphQL resolvers for the queries and mutations defined in the schema.yarn rw dev
so you can see all of this working. If you go to the users
page in the browser, you should see something similar to this./users
page will still work.scaffold
command two more times to generate the CRUD for the items and locations.yarn rw g scaffold location
yarn rw g scaffold item
styled-components
package because we're going to use it to style a few things. First, go to the web
directory in your terminal. That's where this package will need to be installed.yarn add styled-components
yarn rw g page home /
web > src > pages
for the HomePage
component and it'll add a new route to Routes.js
. You can re-run your app here to see the new home page. It'll look like this in the browser.yarn rw g layout home
web > src > layouts
for our home page. We're going to use a few styled components in the HomeLayout
component to create a side navigation menu and give the dashboard a little definition. We'll also be using Redwood routing to add links to the other pages in the side navigation.import { Link, routes } from '@redwoodjs/router'
import styled from 'styled-components'
const HomeLayout = ({ children }) => {
return (
<FlexBox>
<SideNav>
<LinkContainer>
<Link to={routes.users()}>Users</Link>
</LinkContainer>
<LinkContainer>
<Link to={routes.locations()}>Locations</Link>
</LinkContainer>
<LinkContainer>
<Link to={routes.items()}>Items</Link>
</LinkContainer>
</SideNav>
<FullPage>{children}</FullPage>
</FlexBox>
)
}
const FlexBox = styled.div`
display: flex;
`
const FullPage = styled.div`
height: 100vh;
width: 100%;
`
const LinkContainer = styled.div`
padding: 12px;
> * {
color: #000;
font-family: sans-serif;
font-size: 18px;
text-decoration: none;
}
> *:hover {
color: #5b5b5b;
}
`
const SideNav = styled.nav`
border-right: 1px solid;
width: 250px;
`
export default HomeLayout
Routes.js
file. This means adding a <Set>
component around all of the existing routes like below....
<Router>
<Set wrap={HomeLayout}>
<Route path="/" page={HomePage} name="home" />
<Set wrap={ItemsLayout}>
<Route path="/items/new" page={ItemNewItemPage} name="newItem" />
<Route path="/items/{id:Int}/edit" page={ItemEditItemPage} name="editItem" />
<Route path="/items/{id:Int}" page={ItemItemPage} name="item" />
<Route path="/items" page={ItemItemsPage} name="items" />
</Set>
<Set wrap={LocationsLayout}>
<Route path="/locations/new" page={LocationNewLocationPage} name="newLocation" />
<Route path="/locations/{id:Int}/edit" page={LocationEditLocationPage} name="editLocation" />
<Route path="/locations/{id:Int}" page={LocationLocationPage} name="location" />
<Route path="/locations" page={LocationLocationsPage} name="locations" />
</Set>
<Set wrap={UsersLayout}>
<Route path="/users/new" page={UserNewUserPage} name="newUser" />
<Route path="/users/{id:Int}/edit" page={UserEditUserPage} name="editUser" />
<Route path="/users/{id:Int}" page={UserUserPage} name="user" />
<Route path="/users" page={UserUsersPage} name="users" />
</Set>
<Route notfound page={NotFoundPage} />
</Set>
</Router>
...
yarn rw dev
, you'll see something like this.redwood-user-dashboard
in this Git repo!