Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions app/components/spells.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<List.Item
title={spell.name}
description={spell.school}
description={components}
onPress={() => {
navigation.navigate('View', {
model: 'Spells',
Expand Down Expand Up @@ -44,6 +54,7 @@ const _SpellView = ({ navigation, spell }) => {
</View>
);
};

const SpellView = withDatabase(withObservables(['id'], ({ id, database }) => ({
spell: database.get('spells').findAndObserve(id)
}))(_SpellView));
Expand All @@ -57,6 +68,7 @@ const _SpellEdit = withEventsContext(({ spell }) => {
</View>
);
});

const SpellEdit = withDatabase(withObservables(['id'], ({ id, database }) => ({
spell: database.get('spells').findAndObserve(id)
}))(_SpellEdit));
Expand All @@ -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 = {
Expand All @@ -85,7 +99,6 @@ const SpellForm = ({ database, events, spell }) => {
};



return (
<View>
<Controller
Expand Down Expand Up @@ -126,9 +139,24 @@ const SpellForm = ({ database, events, spell }) => {
name="school"
defaultValue={spell.school}
/>
<Controller
control={control}
render={({ field: { onChange, value } }) => (
<>
<Checkbox
status={value}
disabled={false}
onPress={onChange}
/>
<Text>Verbal</Text>
</>
)}
name="is_verbal"
defaultValue={spell.is_verbal ? "checked" : "unchecked"}
/>
<Button title="Submit" onPress={handleSubmit(onSubmit)}>Save</Button>
</View>
);
)
};

const _SpellList = ({ navigation, database, spells }) => {
Expand All @@ -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) =>
Expand All @@ -153,8 +182,8 @@ const _SpellList = ({ navigation, database, spells }) => {
/>
</>
);

};

const SpellList = withDatabase(withObservables([], ({ database }) => ({
spells: database.collections.get('spells').query().observe(),
}))(_SpellList));
Expand All @@ -172,4 +201,4 @@ const styles = StyleSheet.create({
right: 0,
bottom: 0,
},
})
});
79 changes: 77 additions & 2 deletions app/models/spell.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand All @@ -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' },
],
Expand Down