diff --git a/app/components/spells.tsx b/app/components/spells.tsx index 74959f0..ca9199c 100644 --- a/app/components/spells.tsx +++ b/app/components/spells.tsx @@ -2,16 +2,26 @@ import React from 'react'; import { StyleSheet, View } from 'react-native'; import withObservables from '@nozbe/with-observables' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' -import { FAB, Text, TextInput, Button, List } from 'react-native-paper'; +import { Text, TextInput, Checkbox, Button, FAB, List } from 'react-native-paper'; import { useForm, Controller } from "react-hook-form"; import { withEventsContext } from "../context/events"; - const _SpellItem = ({ navigation, spell }) => { + let components = ""; + if (spell.is_verbal) { + components += "V,"; + } + if (spell.is_somatic) { + components += "S,"; + } + if (spell.is_material) { + components += "M,"; + } + components = "(" + components.substring(0, components.length - 1) + ")"; return ( { navigation.navigate('View', { model: 'Spells', @@ -44,6 +54,7 @@ const _SpellView = ({ navigation, spell }) => { ); }; + const SpellView = withDatabase(withObservables(['id'], ({ id, database }) => ({ spell: database.get('spells').findAndObserve(id) }))(_SpellView)); @@ -57,6 +68,7 @@ const _SpellEdit = withEventsContext(({ spell }) => { ); }); + const SpellEdit = withDatabase(withObservables(['id'], ({ id, database }) => ({ spell: database.get('spells').findAndObserve(id) }))(_SpellEdit)); @@ -68,11 +80,13 @@ const SpellForm = ({ database, events, spell }) => { const { control, handleSubmit, formState: { errors } } = useForm(); const onSubmit = async (data) => { data.id = spell.id; + console.log("formdata", data) //TODO make events perform database updates and leave the forms unaware of the database await database.action(async () => { await spell.update((spell) => { spell.name = data.name; spell.school = data.school; + spell.is_verbal = data.is_verbal == "checked"; }); }); const spellModel: CSpell = { @@ -85,7 +99,6 @@ const SpellForm = ({ database, events, spell }) => { }; - return ( { name="school" defaultValue={spell.school} /> + ( + <> + + Verbal + + )} + name="is_verbal" + defaultValue={spell.is_verbal ? "checked" : "unchecked"} + /> - ); + ) }; const _SpellList = ({ navigation, database, spells }) => { @@ -137,9 +165,10 @@ const _SpellList = ({ navigation, database, spells }) => { await database.get('spells').create((spell) => { spell.name = 'New Spell' spell.school = 'Lorem ipsum...' + spell.is_verbal = true }) }) - } + }; return ( <> {spells.map((spell) => @@ -153,8 +182,8 @@ const _SpellList = ({ navigation, database, spells }) => { /> ); - }; + const SpellList = withDatabase(withObservables([], ({ database }) => ({ spells: database.collections.get('spells').query().observe(), }))(_SpellList)); @@ -172,4 +201,4 @@ const styles = StyleSheet.create({ right: 0, bottom: 0, }, -}) +}); diff --git a/app/models/spell.ts b/app/models/spell.ts index 22c521b..04efea6 100644 --- a/app/models/spell.ts +++ b/app/models/spell.ts @@ -1,22 +1,48 @@ import { Model, tableSchema } from '@nozbe/watermelondb'; import { field, readonly, date } from '@nozbe/watermelondb/decorators'; - import { EventType } from './event'; export type SpellType = { name: string, school: string, + level: number, + is_ritual: boolean, + casting_time: string, + range: string, + is_verbal: boolean, + is_somatic: boolean, + is_material: boolean, + duration: string, + description: string, } export default class Spell extends Model { static table = 'spells'; fromJSON(data: SpellType) { this.name = data.name; this.school = data.school; + this.level = data.level; + this.is_ritual = data.is_ritual; + this.casting_time = data.casting_time; + this.range = data.range; + this.is_verbal = data.is_verbal; + this.is_somatic = data.is_somatic; + this.is_material = data.is_material; + this.duration = data.duration; + this.description = data.description; } toJSON(): SpellType { return { name: this.name, school: this.school, + level: this.level, + is_ritual: this.is_ritual, + casting_time: this.casting_time, + range: this.range, + is_verbal: this.is_verbal, + is_somatic: this.is_somatic, + is_material: this.is_material, + duration: this.duration, + description: this.description, }; } reduce(event: EventType) { @@ -26,10 +52,51 @@ export default class Spell extends Model { if ('school' in event.data) { this.school = event.data.school; } + if ('level' in event.data) { + this.level = event.data.level; + } + if ('is_ritual' in event.data) { + this.is_ritual = event.data.is_ritual; + } + if ('is_ritual' in event.data) { + this.is_ritual = event.data.is_ritual; + } + if ('casting_time' in event.data) { + this.casting_time = event.data.casting_time; + } + if ('range' in event.data) { + this.range = event.data.range; + } + if ('is_verbal' in event.data) { + this.is_verbal = event.data.is_verbal; + } + if ('is_somatic' in event.data) { + this.is_somatic = event.data.is_somatic; + } + if ('is_material' in event.data) { + this.is_material = event.data.is_material; + } + if ('duration' in event.data) { + this.duration = event.data.duration; + } + if ('description' in event.data) { + this.description = event.data.description; + } } @field('name') name; @field('school') school; + @field('name') name; + @field('school') school; + @field('level') level; + @field('is_ritual') is_ritual; + @field('casting_time') casting_time; + @field('range') range; + @field('is_verbal') is_verbal; + @field('is_somatic') is_somatic; + @field('is_material') is_material; + @field('duration') duration; + @field('description') description; @readonly @date('created_at') createdAt; @readonly @date('updated_at') updatedAt; @@ -40,7 +107,15 @@ export const SpellSchema = tableSchema({ columns: [ { name: 'name', type: 'string' }, { name: 'school', type: 'string' }, - + { name: 'level', type: 'number' }, + { name: 'is_ritual', type: 'boolean' }, + { name: 'casting_time', type: 'string' }, + { name: 'range', type: 'string' }, + { name: 'is_verbal', type: 'boolean' }, + { name: 'is_somatic', type: 'boolean' }, + { name: 'is_material', type: 'boolean' }, + { name: 'duration', type: 'string' }, + { name: 'description', type: 'string' }, { name: 'created_at', type: 'number' }, { name: 'updated_at', type: 'number' }, ],