Skip to content

Commit

Permalink
Allow rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipChalupa committed Jan 6, 2025
1 parent a618ad1 commit 45afc90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Components/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { FunctionComponent, useMemo, useState } from 'react'
import { BlocklyWorkspace } from 'react-blockly'
import type { GroupKey, LevelKey } from '../data/levels'
import type { Database } from '../database/Database'
import { FinishedLevelId } from '../database/tables/FinishedLevel'
import { useFinishedLevelId } from '../utilities/useFinishedLevelId'
import styles from './Playground.module.css'

const initialXml =
Expand Down Expand Up @@ -102,8 +104,9 @@ export const Playground: FunctionComponent<{
}),
[allowedBlocks],
)
const { create } = useEvolu<Database>()
const { createOrUpdate } = useEvolu<Database>()
const [code, setCode] = useState('')
const finishedLevelId = useFinishedLevelId(groupKey, levelKey)

// @TODO: update theme with media query changes (workspace.setTheme(theme))

Expand Down Expand Up @@ -166,7 +169,16 @@ export const Playground: FunctionComponent<{
variant="contained"
color="success"
onClick={() => {
create('finishedLevel', {
createOrUpdate('finishedLevel', {
id:
finishedLevelId ??
FinishedLevelId.make(
Math.random()
.toString(16)
.substring(
2,
) /* @TODO: This is very dumb. Assign to each level static global id. Or save each success to FinishedLevelTabel individually (multiple per level). */,
),
levelKey,
groupKey,
rating: PositiveInt.make(Math.floor(Math.random() * 3) + 1),
Expand Down
22 changes: 22 additions & 0 deletions src/utilities/useFinishedLevelId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cast, useQuery } from '@evolu/react'
import { useMemo } from 'react'
import { GroupKey, LevelKey } from '../data/levels'
import { evolu } from '../database/Database'

export const useFinishedLevelId = (groupKey: GroupKey, levelKey: LevelKey) => {
const query = useMemo(
() =>
evolu.createQuery((database) =>
database
.selectFrom('finishedLevel')
.select(['id'])
.where('isDeleted', 'is not', cast(true))
.where('groupKey', 'is', groupKey)
.where('levelKey', 'is', levelKey),
),
[groupKey, levelKey],
)
const { row } = useQuery(query)

return row?.id ?? null
}

0 comments on commit 45afc90

Please sign in to comment.