Skip to content

Commit

Permalink
Merge pull request #5 from AloyBranCheung/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
AloyBranCheung authored Nov 5, 2023
2 parents 55fbb07 + 1538eae commit 4b721cd
Show file tree
Hide file tree
Showing 62 changed files with 1,558 additions and 257 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

.env
.env

.vscode/
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,43 @@
<img alt="prisma" width="60" src="https://logowik.com/content/uploads/images/prisma2244.jpg">
</div>

<br />

## Workout App

- [ ] Create your own workout plan
- [ ] Add your own exercises
- [ ] Track your workouts
- [ ] Track your progress (BMI, weight etc.)

<br />

## Getting Started

1. Configure `.env` file
- Supabase API Settings
- Copy `Project URL` to `DATABASE_URL` and `NEXT_PUBLIC_SUPABASE_URL`
- Copy `Project API Keys: anon/public key` to `NEXT_PUBLIC_SUPABASE_ANON_KEY`
2. Run `yarn install && npx prisma generate`
3. Run `./dev.sh` (make sure have docker)
3. Run `./reset_db.sh`
4. Run `./dev.sh` (make sure have docker)
5. Disable email confirmation under Authentication > Providers > Email in Supabase UI
6. run `create_user.sh` in `scripts` folder

Debugging:

- make sure you are not ip banned on the supabase admin dashboard (if you failed to login)

## Prisma

https://www.prisma.io/docs/concepts/components/prisma-migrate/mental-model

`prisma migrate dev` for development environment

`prisma migrate deploy` for deployment (PRD) environment

- Edit the DBML documentation before editing the schema

## Resources

- [Figma](https://www.figma.com/file/R0i3v0IsjhkOhDSYITeWHU/Workout-App?type=design&node-id=0%3A1&mode=design&t=4R5sghDXxfNWkufE-1)

<br />

## Notes to self

- index.tsx is the 'container'
Expand Down
3 changes: 0 additions & 3 deletions dev.sh

This file was deleted.

2 changes: 0 additions & 2 deletions logs.sh

This file was deleted.

6 changes: 6 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const nextConfig = {
port: "",
pathname: "/**",
},
{
protocol: "https",
hostname: "cdn-icons-png.flaticon.com",
port: "",
pathname: "/**",
},
],
},
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"@dnd-kit/sortable": "^7.0.2",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^3.1.0",
"@prisma/client": "4.14.1",
"@prisma/client": "^5.4.2",
"@supabase/auth-helpers-nextjs": "^0.7.0",
"@supabase/auth-helpers-react": "^0.4.0",
"@supabase/supabase-js": "^2.23.0",
"@tanstack/react-query": "^4.29.17",
"@tanstack/react-query-devtools": "^5.1.0",
"@trpc/client": "^10.32.0",
"@trpc/next": "^10.32.0",
"@trpc/react-query": "^10.32.0",
Expand Down Expand Up @@ -62,7 +63,7 @@
"postcss-import": "^15.1.0",
"prettier": "^2.8.8",
"prettier-plugin-prisma": "^4.13.0",
"prisma": "4.14.1",
"prisma": "^5.4.2",
"typescript-plugin-css-modules": "^5.0.1"
}
}
10 changes: 10 additions & 0 deletions prisma/db-diagram-io.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Table WorkoutPlan {
exerciseOrder "string[]"
name string
userId string [unique]
gymLocationId string [unique]
}

Table RunPlan {
Expand Down Expand Up @@ -119,5 +120,14 @@ Table Activity {
userId string
}

Table GymLocation {
gymId string [pk, unique, default: 'uuid()', ref: > User.userId, ref: < WorkoutPlan.gymLocationId]
name string
description string [note: 'optional']
}





Ref: "WorkoutPlan"."planId" < "WorkoutPlan"."exerciseOrder"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[gymLocation]` on the table `WorkoutPlan` will be added. If there are existing duplicate values, this will fail.
- Added the required column `gymLocation` to the `WorkoutPlan` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "WorkoutPlan" ADD COLUMN "gymLocation" TEXT NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "WorkoutPlan_gymLocation_key" ON "WorkoutPlan"("gymLocation");
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Warnings:
- You are about to drop the column `gymLocation` on the `WorkoutPlan` table. All the data in the column will be lost.
*/
-- DropIndex
DROP INDEX "WorkoutPlan_gymLocation_key";

-- AlterTable
ALTER TABLE "WorkoutPlan" DROP COLUMN "gymLocation";

-- CreateTable
CREATE TABLE "GymLocation" (
"gymId" UUID NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL,
"userId" TEXT NOT NULL,

CONSTRAINT "GymLocation_pkey" PRIMARY KEY ("gymId")
);

-- CreateIndex
CREATE UNIQUE INDEX "GymLocation_gymId_key" ON "GymLocation"("gymId");

-- AddForeignKey
ALTER TABLE "GymLocation" ADD CONSTRAINT "GymLocation_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("userId") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "GymLocation" ALTER COLUMN "description" DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- Added the required column `gymId` to the `WorkoutPlan` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "WorkoutPlan" ADD COLUMN "gymId" UUID NOT NULL;

-- AddForeignKey
ALTER TABLE "WorkoutPlan" ADD CONSTRAINT "WorkoutPlan_gymId_fkey" FOREIGN KEY ("gymId") REFERENCES "GymLocation"("gymId") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "WorkoutPlan" DROP CONSTRAINT "WorkoutPlan_gymId_fkey";

-- AddForeignKey
ALTER TABLE "WorkoutPlan" ADD CONSTRAINT "WorkoutPlan_gymId_fkey" FOREIGN KEY ("gymId") REFERENCES "GymLocation"("gymId") ON DELETE CASCADE ON UPDATE CASCADE;
23 changes: 19 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ model User {
profile Profile?
runPlans RunPlan[]
activities Activity[]
gymLocations GymLocation[]
}

// Profile Model Table
Expand Down Expand Up @@ -125,16 +126,18 @@ model Set {
}

model WorkoutPlan {
planId String @id @unique @default(uuid()) @db.Uuid
updatedAt DateTime @default(now()) @updatedAt
createdAt DateTime @default(now())
planId String @id @unique @default(uuid()) @db.Uuid
updatedAt DateTime @default(now()) @updatedAt
createdAt DateTime @default(now())
name String
exerciseOrder String[]
lastWorkout DateTime?
duration Int?
// relations
user User @relation(fields: [userId], references: [userId])
user User @relation(fields: [userId], references: [userId])
userId String
gymLocation GymLocation @relation(fields: [gymId], references: [gymId], onDelete: Cascade)
gymId String @db.Uuid
exercises Exercise[]
sessions Session[]
Expand Down Expand Up @@ -187,3 +190,15 @@ model Run {
runExercise RunExercise @relation(fields: [exerciseId], references: [exerciseId])
exerciseId String @db.Uuid
}

model GymLocation {
gymId String @id @unique @default(uuid()) @db.Uuid
name String
description String?
// relation
user User @relation(fields: [userId], references: [userId])
userId String
workoutPlans WorkoutPlan[]
}
5 changes: 5 additions & 0 deletions scripts/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

# dev docker compose setup
docker compose -f ../docker-compose.dev.yml up --build --detach
./logs.sh
4 changes: 4 additions & 0 deletions scripts/logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# dev logs
docker compose -f ../docker-compose.dev.yml logs --timestamps --follow
2 changes: 2 additions & 0 deletions pre_PR.sh → scripts/pre_PR.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

# if i feel like it
yarn lint
yarn jest-ci
2 changes: 2 additions & 0 deletions reset_db.sh → scripts/reset_db.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/usr/bin/env bash

# wipe db
yarn prisma migrate reset
2 changes: 2 additions & 0 deletions restart.sh → scripts/restart.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash

# restart docker compose setup
./stop.sh
./dev.sh
./logs.sh
2 changes: 2 additions & 0 deletions stop.sh → scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/usr/bin/env bash

# stop all containers and remove all containers
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)
2 changes: 2 additions & 0 deletions update_db.sh → scripts/update_db.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

# development only migrate db
yarn prisma migrate dev
yarn prisma generate
30 changes: 0 additions & 30 deletions src/__tests__/jest/Workouts.test.tsx

This file was deleted.

63 changes: 63 additions & 0 deletions src/components/CreateWorkout/AddGymLocationModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from "react"
// react-hook-forms
import { useForm, FieldValues } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
// z
import { z } from "zod"
// types/validators
import { AddGymLocationSchema } from "src/validators/workout-schema"
import FormInput from "../UI/FormInput"
import PrimaryButton from "../UI/PrimaryButton"
// hooks
import useMutationAddGymLocation from "src/hooks/useMutationAddGymLocation"
import useToastMessage, { ToastMessage } from "src/hooks/useToastMessage"

interface AddGymLocationModalProps {
// trigger setValues in the parent form for the new location from getValues().name in this form
onSuccessAdd: (gymName: string) => void
}

export default function AddGymLocationModal({
onSuccessAdd,
}: AddGymLocationModalProps) {
const toastMessage = useToastMessage()

const { handleSubmit, reset, control } = useForm<
z.infer<typeof AddGymLocationSchema> & FieldValues
>({
defaultValues: {
description: "",
name: "",
},
resolver: zodResolver(AddGymLocationSchema),
})

const { mutate, isLoading } = useMutationAddGymLocation(
(data) => {
toastMessage("Successfully added gym location.", ToastMessage.Success)
onSuccessAdd(data.gymId)
reset()
},
() => toastMessage("Error adding gym location.", ToastMessage.Error)
)

const handleSubmitForm = (data: z.infer<typeof AddGymLocationSchema>) => {
mutate(data)
}

return (
<form
className="flex flex-col gap-2"
onSubmit={handleSubmit(handleSubmitForm)}
>
<FormInput control={control} name="name" />
<FormInput control={control} name="description" required={false} />
<PrimaryButton
label="Submit"
type="submit"
className="py-2 px-3 mt-5"
isLoading={isLoading}
/>
</form>
)
}
Loading

0 comments on commit 4b721cd

Please sign in to comment.