Skip to content

Commit

Permalink
refactor(occurrence): drop day and time columns to store only timesta…
Browse files Browse the repository at this point in the history
…mp (#138)

* refactor(occurrence): drop day and time columns to store only timestamp

* docs(readme): fix
  • Loading branch information
domhhv authored Dec 28, 2024
1 parent 4604bac commit 6c98f83
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<a href="https://testing-library.com/" title="React Testing Library"><img src="https://i.ibb.co/YLnX0VY/octopus-64x64.png" alt="React Testing Library" width="32" height="32"></a>
</div>

<br />

Habitrack is a simple and intuitive web app designed for logging habits and visualizing them on a calendar view.

This app showcases the use of the following tools and technologies:
Expand Down Expand Up @@ -148,9 +150,49 @@ To set up a local Supabase instance, run the following commands (Docker required
5. **Stop the local Supabase instance:**

```bash
yarn db:stop
yarn db:stop
```

6. **Restart the local Supabase instance:**

```bash
yarn db:restart
```

##### Migrations

There are a few ways to create and run migrations in the project.

- Diffing the database schema:

_Do the necessary changes in the local Supabase studio and then run the following to automatically generate a new migration file:_

```bash
yarn db:diff
```

- Creating a new migration file manually:

_To create a new migration file manually, run the following command:_

```bash
yarn db:migration:new <your-migration-name>
```

Either way, the new migration file will be created in the `supabase/migrations` directory. You can then apply the migration by running:

```bash
yarn db:migration:up
```

After applying the migration, you also need to regenerate Supabase types by running:

```bash
yarn db:gen-types
```

Once the migration ends up in the `main` branch, it will be automatically applied to the production database.

### Testing

The project uses [Jest](https://jestjs.io/) for unit testing. To run the tests, use the following command:
Expand Down Expand Up @@ -185,6 +227,20 @@ yarn prettier:check # Check for formatting errors
yarn prettier:fix # Fix formatting errors
```

### Building

To create a local production-like build of the app, run the following command:

```bash
yarn build
```

You can run the production build locally using the following command:

```bash
yarn preview
```

## Contributing

Contributions are welcome! Feel free to open issues or pull requests for any improvements, bug fixes, or new features.
2 changes: 0 additions & 2 deletions src/components/calendar/AddOccurrenceDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ const AddOccurrenceDialog = ({
}

const newOccurrence = await addOccurrence({
day: date!.toISOString().split('T')[0],
timestamp: +date!,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
habitId: +selectedHabitId.currentKey,
userId: user?.id as string,
time: null, // TODO: Add time picker
});

if (note) {
Expand Down
3 changes: 2 additions & 1 deletion src/stores/occurrences.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ const useOccurrencesStore = create<OccurrencesState>((set, get) => {
updateOccurrencesMap: (occurrences: Occurrence[]) => {
const nextOccurrencesByDate = occurrences.reduce(
(acc, occurrence) => {
const { day } = occurrence;
const { timestamp } = occurrence;
const day = new Date(timestamp).toISOString().split('T')[0];
if (!acc[day]) {
acc[day] = [occurrence];
} else {
Expand Down
6 changes: 0 additions & 6 deletions supabase/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,30 +833,24 @@ export type Database = {
occurrences: {
Row: {
created_at: string
day: string
habit_id: number
id: number
time: string | null
timestamp: number
updated_at: string | null
user_id: string
}
Insert: {
created_at?: string
day: string
habit_id: number
id?: number
time?: string | null
timestamp: number
updated_at?: string | null
user_id: string
}
Update: {
created_at?: string
day?: string
habit_id?: number
id?: number
time?: string | null
timestamp?: number
updated_at?: string | null
user_id?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE occurrences DROP COLUMN day;
ALTER TABLE occurrences DROP COLUMN time;
2 changes: 0 additions & 2 deletions tests/makeTestOccurrence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const makeTestOccurrence = () => {
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
timestamp: new Date().getTime(),
day: '2021-01-01',
time: '12:00',
habitId: 1,
userId: '1',
habit: {
Expand Down

0 comments on commit 6c98f83

Please sign in to comment.