Create Add Pharmacy Modal

This commit is contained in:
abhay-raizada
2024-08-12 17:41:39 +05:30
parent 26e9a80275
commit 605b89c578
8 changed files with 347 additions and 232 deletions

21
App.tsx
View File

@@ -6,7 +6,13 @@
*/ */
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
import {SafeAreaView, ScrollView, StatusBar, StyleSheet} from 'react-native'; import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen'; import {Colors} from 'react-native/Libraries/NewAppScreen';
import {PrescriptionCreator} from './components/PrescriptionCreator'; import {PrescriptionCreator} from './components/PrescriptionCreator';
@@ -15,22 +21,15 @@ import PolyfillCrypto from 'react-native-webview-crypto';
function App(): React.JSX.Element { function App(): React.JSX.Element {
const backgroundStyle = { const backgroundStyle = {
backgroundColor: Colors.darker, backgroundColor: 'black',
color: 'white',
}; };
const [form, setForm] = useState<{} | null>(null); const [form, setForm] = useState<{} | null>(null);
useEffect(() => { useEffect(() => {
console.log('inside useeffect'); console.log('inside useeffect');
const fetchForm = async () => { }, []);
if (!form) {
// let form = await getFormTemplate(
// 'eb3df1f89653475f0bcbd22da35f8d2f126db8a68a88a7abedc53535c76c39b4',
// );
}
};
fetchForm();
}, [form, setForm]);
return ( return (
<SafeAreaView style={backgroundStyle}> <SafeAreaView style={backgroundStyle}>

View File

@@ -0,0 +1,112 @@
import {useState} from 'react';
import {Alert, Button, Modal, Text, TextInput, View} from 'react-native';
import {Section} from '../common/Section';
export const AddPharmacy = ({
isVisible,
onClose,
onAdd,
}: {
isVisible: boolean;
onClose: () => void;
onAdd: (npub: string, relay: string, name: string) => void;
}) => {
const [npub, setNpub] = useState('');
const [relay, setRelay] = useState('');
const [name, setName] = useState('');
const handleNpub = (value: string) => {
setNpub(value);
};
const handleRelay = (value: string) => {
setRelay(value);
};
const handleName = (value: string) => {
setName(value);
};
return (
<Modal
visible={isVisible}
onRequestClose={() => {
console.log('closing....');
onClose();
return true;
}}
onDismiss={() => {
onClose();
}}
transparent={true}
style={{backgroundColor: 'black', margin: 0, padding: 0, height: '80%'}}
animationType="slide">
<View
style={{
backgroundColor: 'black',
justifyContent: 'center',
minHeight: '80%',
display: 'flex',
margin: 30,
alignItems: 'center',
}}>
<Section title="Add A Pharmacy">
<View style={{margin: 5}}>
<Text style={{color: 'white', margin: 5}}>Add Pharmacy Name</Text>
<TextInput
style={{
borderColor: '#000000',
borderWidth: 1,
borderRadius: 5,
color: 'white',
}}
placeholderTextColor="grey"
placeholder="Pharmacy X"
onChangeText={handleName}
/>
<Text style={{color: 'white', margin: 5}}>Add Pharmacy Npub</Text>
<TextInput
style={{
borderColor: '#000000',
borderWidth: 1,
borderRadius: 5,
color: 'white',
}}
placeholderTextColor="grey"
placeholder="npub1...."
onChangeText={handleNpub}
/>
<Text style={{color: 'white', margin: 5}}>Add Pharmacy Relay</Text>
<TextInput
style={{
borderColor: '#000000',
borderWidth: 1,
borderRadius: 5,
color: 'white',
}}
placeholderTextColor="grey"
placeholder="wss://<relay-url>"
onChangeText={handleRelay}
/>
</View>
<View style={{flexDirection: 'row'}}>
<View style={{margin: 10}}>
<Button title="Cancel" onPress={() => onClose()}></Button>
</View>
<View style={{margin: 10}}>
<Button
title="Add"
onPress={() => {
if (!npub || !relay || !name)
Alert.alert(
'Missing Inputs',
'Please enter name, npub and relay of the Pharmacy',
);
onAdd(npub, relay, name);
}}></Button>
</View>
</View>
</Section>
</View>
</Modal>
);
};

View File

@@ -0,0 +1,93 @@
import {Dropdown} from 'react-native-element-dropdown';
import {Section} from '../common/Section';
import {Button, Dimensions, Text, View} from 'react-native';
import React, {useState} from 'react';
import {AddPharmacy} from './AddPharmacy';
export const pharmacyData = [
{
label: 'Pharmacy A',
value: 'A',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://relay.damus.io'],
},
{
label: ' + Add Pharmacy',
value: 'custom',
},
];
let width = Dimensions.get('window').width;
interface PharmacyPickerProps {
handleLocationChange: (item: any) => void;
}
export const PharmacyPicker: React.FC<PharmacyPickerProps> = ({
handleLocationChange,
}) => {
const [showAddPharmacyModal, setShowAddPharmacyModal] = useState(false);
const renderItem = (item: any) => {
if (item.value === 'custom') {
return (
<View>
<Button
title="Add Pharmacy"
onPress={() => {
setShowAddPharmacyModal(true);
}}
/>
</View>
);
}
return (
<View
style={{
width: width,
display: 'flex',
flexDirection: 'column',
padding: 10,
flexWrap: 'wrap',
}}>
<Text style={{color: 'black', fontSize: 24}}>{item.label}</Text>
<View style={{width: width - 100}}>
<Text style={{color: 'grey', paddingBottom: 5}}>
Npub: {item.npub}
</Text>
<Text style={{color: 'grey'}}>Relays: {item.relays.join(', ')}</Text>
</View>
</View>
);
};
return (
<View>
<Section title="Choose a Pharmacy">
<View style={{width: width - 40}}>
<Dropdown
data={pharmacyData}
labelField={'label'}
valueField={'label'}
onChange={handleLocationChange}
value={pharmacyData[0]}
renderItem={renderItem}
style={{width: '100%'}}
placeholderStyle={{color: 'white'}}
selectedTextStyle={{color: 'white'}}
/>
</View>
</Section>
<AddPharmacy
isVisible={showAddPharmacyModal}
onClose={() => {
setShowAddPharmacyModal(false);
}}
onAdd={() => {
setShowAddPharmacyModal(false);
}}
/>
</View>
);
};

View File

@@ -28,7 +28,6 @@ export const AddressForm: React.FC<AddressFormProps> = ({
}; };
return ( return (
<Section title="Address">
<View> <View>
<View> <View>
<Text style={TextTheme}>Address Line 1</Text> <Text style={TextTheme}>Address Line 1</Text>
@@ -71,9 +70,7 @@ export const AddressForm: React.FC<AddressFormProps> = ({
placeholder="Enter postal code..." placeholder="Enter postal code..."
value={form.postal_code} value={form.postal_code}
placeholderTextColor="white" placeholderTextColor="white"
onChangeText={(text: string) => onChangeText={(text: string) => handleTextChange('postal_code', text)}
handleTextChange('postal_code', text)
}
/> />
</View> </View>
<View> <View>
@@ -89,6 +86,5 @@ export const AddressForm: React.FC<AddressFormProps> = ({
/> />
</View> </View>
</View> </View>
</Section>
); );
}; };

View File

@@ -29,7 +29,6 @@ export const MedicineForm: React.FC<MedicineFormProps> = ({
}; };
return ( return (
<Section title="Medicine">
<View> <View>
<View> <View>
<Text style={TextTheme}>Name of Medicine</Text> <Text style={TextTheme}>Name of Medicine</Text>
@@ -48,9 +47,7 @@ export const MedicineForm: React.FC<MedicineFormProps> = ({
placeholder="what is the dosage form" placeholder="what is the dosage form"
value={form.dosage_form} value={form.dosage_form}
placeholderTextColor="white" placeholderTextColor="white"
onChangeText={(text: string) => onChangeText={(text: string) => handleTextChange('dosage_form', text)}
handleTextChange('dosage_form', text)
}
/> />
</View> </View>
<View> <View>
@@ -90,12 +87,9 @@ export const MedicineForm: React.FC<MedicineFormProps> = ({
placeholder="Enter directions" placeholder="Enter directions"
value={form.refills} value={form.refills}
placeholderTextColor="white" placeholderTextColor="white"
onChangeText={(text: string) => onChangeText={(text: string) => handleTextChange('directions', text)}
handleTextChange('directions', text)
}
/> />
</View> </View>
</View> </View>
</Section>
); );
}; };

View File

@@ -1,15 +1,6 @@
import { import {Alert, Dimensions, Image, View, Button} from 'react-native';
Alert,
Appearance,
Dimensions,
Image,
Text,
View,
Button,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen'; import {Colors} from 'react-native/Libraries/NewAppScreen';
import {useEffect, useState} from 'react'; import {useEffect, useState} from 'react';
import {Dropdown} from 'react-native-element-dropdown';
import { import {
SimplePool, SimplePool,
UnsignedEvent, UnsignedEvent,
@@ -19,11 +10,12 @@ import {
nip19, nip19,
} from 'nostr-tools'; } from 'nostr-tools';
import EncryptedStorage from 'react-native-encrypted-storage'; import EncryptedStorage from 'react-native-encrypted-storage';
import {ImportNsec} from './ImportNsec'; import {ImportNsec} from '../common/ImportNsec';
import {Section} from '../common/Section'; import {Section} from '../common/Section';
import {PatientForm} from './PatientForm'; import {PatientForm} from './PatientForm';
import {AddressForm} from './AddressForm'; import {AddressForm} from './AddressForm';
import {MedicineForm} from './MedicineForm'; import {MedicineForm} from './MedicineForm';
import {PharmacyPicker, pharmacyData} from '../PharmacyPicker';
function OBJtoXML(obj: any) { function OBJtoXML(obj: any) {
var xml = ''; var xml = '';
@@ -48,52 +40,19 @@ function OBJtoXML(obj: any) {
return xml; return xml;
} }
const colorScheme = Appearance.getColorScheme();
const backgroundStyle = {
backgroundColor: Colors.darker,
};
const width = Dimensions.get('window').width; //full width
const height = Dimensions.get('window').height;
const locationData = [
{
label: 'Pharmacy A',
value: 'A',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://relay.damus.io'],
},
{
label: 'Pharmacy B',
value: 'B',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://relay.primal.net'],
},
{
label: 'Pharmacy C',
value: 'C',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://relay.hllo.live'],
},
{
label: 'Pharmacy D',
value: 'D',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://nos.lol', 'wss://relay.damus.io'],
},
];
const locationDummyData = [{label: 'Pharmacy A', value: 'A'}];
export const PrescriptionCreator = () => { export const PrescriptionCreator = () => {
const [showImportNsec, setShowImportNsec] = useState(false); const [showImportNsec, setShowImportNsec] = useState(false);
const [loggedInNpub, setLoggedInNpub] = useState(''); const [loggedInNpub, setLoggedInNpub] = useState('');
const [finalJSON, setFinalJson] = useState({});
const [selectedPharmacyId, setSelectedPharmacyId] = useState( const [selectedPharmacyId, setSelectedPharmacyId] = useState(
locationData[0].npub, pharmacyData[0].npub,
); );
const [selectedPharmacyRelays, setSelectedPharmacyRelays] = useState([]); const [selectedPharmacyRelays, setSelectedPharmacyRelays] = useState([]);
const [finalJSON, setFinalJson] = useState({});
const handleLocationChange = (item: any) => {
setSelectedPharmacyId(item.npub);
setSelectedPharmacyRelays(item.relays);
};
useEffect(() => { useEffect(() => {
async function initialize() { async function initialize() {
@@ -115,26 +74,6 @@ export const PrescriptionCreator = () => {
} }
initialize(); initialize();
}, []); }, []);
const renderItem = (item: any) => {
return (
<View
style={{
width: width,
display: 'flex',
flexDirection: 'column',
padding: 10,
flexWrap: 'wrap',
}}>
<Text style={{color: 'black', fontSize: 24}}>{item.label}</Text>
<View style={{width: width - 100}}>
<Text style={{color: 'grey', paddingBottom: 5}}>
Npub: {item.npub}
</Text>
<Text style={{color: 'grey'}}>Relays: {item.relays.join(', ')}</Text>
</View>
</View>
);
};
const nestedFormCallback = ( const nestedFormCallback = (
xmlTag: string, xmlTag: string,
@@ -144,11 +83,6 @@ export const PrescriptionCreator = () => {
setFinalJson({...finalJSON, [xmlTag]: value}); setFinalJson({...finalJSON, [xmlTag]: value});
}; };
const handleLocationChange = (item: any) => {
setSelectedPharmacyId(item.npub);
setSelectedPharmacyRelays(item.relays);
};
const handleImportNsec = (nsec: string) => { const handleImportNsec = (nsec: string) => {
EncryptedStorage.setItem('user_credentials', nsec); EncryptedStorage.setItem('user_credentials', nsec);
if (nsec.startsWith('nsec1') && nsec.length !== 63) { if (nsec.startsWith('nsec1') && nsec.length !== 63) {
@@ -174,7 +108,7 @@ export const PrescriptionCreator = () => {
(await EncryptedStorage.getItem('user_credentials')) as `nsec1${string}`, (await EncryptedStorage.getItem('user_credentials')) as `nsec1${string}`,
).data as Uint8Array; ).data as Uint8Array;
const pk = getPublicKey(sk); const pk = getPublicKey(sk);
const pharmacyId = nip19.decode(selectedPharmacyId).data as string; const pharmacyId = nip19.decode(selectedPharmacyId!).data as string;
console.log('Got ids'); console.log('Got ids');
console.log('content is ', xml); console.log('content is ', xml);
const baseKind4Event: UnsignedEvent = { const baseKind4Event: UnsignedEvent = {
@@ -217,23 +151,9 @@ export const PrescriptionCreator = () => {
/> />
</Section> </Section>
<Section title="Choose a Pharmacy"> <PharmacyPicker handleLocationChange={handleLocationChange} />
<View style={{width: width - 40}}>
<Dropdown
data={locationData}
labelField={'label'}
valueField={'label'}
onChange={handleLocationChange}
value={locationData[0]}
renderItem={renderItem}
style={{width: '100%'}}
placeholderStyle={{color: 'white'}}
selectedTextStyle={{color: 'white'}}
/>
</View>
</Section>
<Section title="Prescription"> <Section title="Create Prescription">
<View style={{display: 'flex', flexDirection: 'column', width: '100%'}}> <View style={{display: 'flex', flexDirection: 'column', width: '100%'}}>
<Section title="Patient" collapsible={true}> <Section title="Patient" collapsible={true}>
{' '} {' '}
@@ -245,8 +165,10 @@ export const PrescriptionCreator = () => {
<Section title="Medicine" collapsible={true}> <Section title="Medicine" collapsible={true}>
<MedicineForm nestedFormCallback={nestedFormCallback} /> <MedicineForm nestedFormCallback={nestedFormCallback} />
</Section> </Section>
<View style={{margin: 15}}>
<Button onPress={handleButtonPress} title="Create Rx" /> <Button onPress={handleButtonPress} title="Create Rx" />
</View> </View>
</View>
</Section> </Section>
<ImportNsec <ImportNsec
isVisible={showImportNsec} isVisible={showImportNsec}

View File

@@ -7,7 +7,6 @@ import {
TouchableOpacity, TouchableOpacity,
View, View,
} from 'react-native'; } from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {styles} from './styles'; import {styles} from './styles';
type SectionProps = PropsWithChildren<{ type SectionProps = PropsWithChildren<{