Store added pharmacy in local storage

This commit is contained in:
abhay-raizada
2024-08-12 18:57:17 +05:30
parent 605b89c578
commit ae9f81ddb7
6 changed files with 121 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
import {useState} from 'react';
import {Alert, Button, Modal, Text, TextInput, View} from 'react-native';
import {Section} from '../common/Section';
import {getData, storeData} from '../../utils/localStorage';
export const AddPharmacy = ({
isVisible,
@@ -24,6 +25,31 @@ export const AddPharmacy = ({
const handleName = (value: string) => {
setName(value);
};
async function handleAddClick() {
if (!npub || !relay || !name) {
Alert.alert(
'Missing Inputs',
'Please enter name, npub and relay of the Pharmacy',
);
return;
}
if (npub.length !== 63 || !npub.startsWith('npub1')) {
Alert.alert('Invalid Npub');
return;
}
let pharmacyListString = (await getData('pharmacyList')) || '[]';
let pharmacyList = JSON.parse(pharmacyListString) as Array<{
label: string;
npub: string;
relay: string;
}>;
pharmacyList = [...pharmacyList, {label: name, relay: relay, npub: npub}];
await storeData('pharmacyList', JSON.stringify(pharmacyList));
onAdd(npub, relay, name);
}
return (
<Modal
visible={isVisible}
@@ -93,16 +119,7 @@ export const AddPharmacy = ({
<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>
<Button title="Add" onPress={handleAddClick}></Button>
</View>
</View>
</Section>

View File

@@ -1,15 +1,16 @@
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 React, {useEffect, useState} from 'react';
import {AddPharmacy} from './AddPharmacy';
import {getData} from '../../utils/localStorage';
export const pharmacyData = [
{
label: 'Pharmacy A',
value: 'A',
label: 'Default pharmacy',
value: 'default',
npub: 'npub1tea09rtjeuzgk4gjajzry37wuyv7h02d4zw38cpadcrkg5yt0qhqncr7km',
relays: ['wss://relay.damus.io'],
relay: 'wss://relay.damus.io',
},
{
label: ' + Add Pharmacy',
@@ -27,6 +28,20 @@ export const PharmacyPicker: React.FC<PharmacyPickerProps> = ({
handleLocationChange,
}) => {
const [showAddPharmacyModal, setShowAddPharmacyModal] = useState(false);
const [pharmacyList, setPharmacyList] = useState(pharmacyData);
const [initialized, setInitialized] = useState(false);
const initialize = async () => {
let pharmacyListString = (await getData('pharmacyList')) || '[]';
let newPharmacyList = JSON.parse(pharmacyListString);
let storePharmacyList = [...newPharmacyList, ...pharmacyList];
setPharmacyList(storePharmacyList);
setInitialized(true);
handleLocationChange(storePharmacyList[0]);
};
useEffect(() => {
if (!initialized) initialize();
}, []);
const renderItem = (item: any) => {
if (item.value === 'custom') {
@@ -56,7 +71,7 @@ export const PharmacyPicker: React.FC<PharmacyPickerProps> = ({
<Text style={{color: 'grey', paddingBottom: 5}}>
Npub: {item.npub}
</Text>
<Text style={{color: 'grey'}}>Relays: {item.relays.join(', ')}</Text>
<Text style={{color: 'grey'}}>Relay: {item.relay}</Text>
</View>
</View>
);
@@ -67,11 +82,11 @@ export const PharmacyPicker: React.FC<PharmacyPickerProps> = ({
<Section title="Choose a Pharmacy">
<View style={{width: width - 40}}>
<Dropdown
data={pharmacyData}
data={pharmacyList}
labelField={'label'}
valueField={'label'}
onChange={handleLocationChange}
value={pharmacyData[0]}
value={pharmacyList[0]}
renderItem={renderItem}
style={{width: '100%'}}
placeholderStyle={{color: 'white'}}
@@ -85,6 +100,7 @@ export const PharmacyPicker: React.FC<PharmacyPickerProps> = ({
setShowAddPharmacyModal(false);
}}
onAdd={() => {
initialize();
setShowAddPharmacyModal(false);
}}
/>

View File

@@ -47,11 +47,17 @@ export const PrescriptionCreator = () => {
const [selectedPharmacyId, setSelectedPharmacyId] = useState(
pharmacyData[0].npub,
);
const [selectedPharmacyRelays, setSelectedPharmacyRelays] = useState([]);
const [selectedPharmacyRelays, setSelectedPharmacyRelays] = useState<
Array<string>
>([pharmacyData[0].relay!]);
const handleLocationChange = (item: any) => {
const handleLocationChange = (item: {
name: string;
npub: string;
relay: string;
}) => {
setSelectedPharmacyId(item.npub);
setSelectedPharmacyRelays(item.relays);
setSelectedPharmacyRelays([item.relay]);
};
useEffect(() => {
@@ -119,10 +125,18 @@ export const PrescriptionCreator = () => {
pubkey: pk,
};
const finalEvent = finalizeEvent(baseKind4Event, sk);
console.log(
'FINAL EVENT IS ',
finalEvent,
'relays are',
selectedPharmacyRelays,
);
const pool = new SimplePool();
console.log('publishing event');
await Promise.allSettled(pool.publish(selectedPharmacyRelays, finalEvent));
console.log('Event Published');
let messages = await Promise.allSettled(
pool.publish(selectedPharmacyRelays, finalEvent),
);
console.log('Messages from relays', messages);
Alert.alert('Prescription Sent to the pharmacy!');
};

View File

@@ -10,16 +10,16 @@
"test": "jest"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.24.0",
"@react-native-community/datetimepicker": "^8.0.1",
"@react-native-community/segmented-control": "^2.2.2",
"@react-native-community/slider": "^4.5.0",
"@react-native-picker/picker": "^2.6.1",
"@rneui/base": "^4.0.0-rc.8",
"@rneui/themed": "^4.0.0-rc.8",
"buffer": "^6.0.3",
"events": "^3.3.0",
"nostr-tools": "^2.3.1",
"react-native-safe-area-context": "^4.5.0",
"@rneui/base": "^4.0.0-rc.8",
"@rneui/themed": "^4.0.0-rc.8",
"react": "18.2.0",
"react-native": "0.73.4",
"react-native-crypto": "^2.2.0",
@@ -29,6 +29,7 @@
"react-native-get-random-values": "^1.11.0",
"react-native-picker-select": "^9.0.1",
"react-native-randombytes": "^3.6.1",
"react-native-safe-area-context": "^4.5.0",
"react-native-url-polyfill": "^2.0.0",
"react-native-vector-icons": "^10.1.0",
"react-native-webview": "^13.8.4",

29
utils/localStorage.ts Normal file
View File

@@ -0,0 +1,29 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
export const storeData = async (key: string, value: string): Promise<void> => {
try {
await AsyncStorage.setItem(key, value);
console.log('Data stored successfully');
} catch (error) {
console.error('Failed to store data:', error);
}
};
export const getData = async (key: string): Promise<string | null> => {
try {
const value = await AsyncStorage.getItem(key);
return value;
} catch (error) {
console.error('Failed to retrieve data:', error);
return null;
}
};
export const removeData = async (key: string): Promise<void> => {
try {
await AsyncStorage.removeItem(key);
console.log('Data removed successfully');
} catch (error) {
console.error('Failed to remove data:', error);
}
};

View File

@@ -1555,6 +1555,13 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"
"@react-native-async-storage/async-storage@^1.24.0":
version "1.24.0"
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.24.0.tgz#888efbc62a26f7d9464b32f4d3027b7f2771999b"
integrity sha512-W4/vbwUOYOjco0x3toB8QCr7EjIP6nE9G7o8PMguvvjYT5Awg09lyV4enACRx4s++PPulBiBSjL0KTFx2u0Z/g==
dependencies:
merge-options "^3.0.4"
"@react-native-community/cli-clean@12.3.2":
version "12.3.2"
resolved "https://registry.yarnpkg.com/@react-native-community/cli-clean/-/cli-clean-12.3.2.tgz#d4f1730c3d22d816b4d513d330d5f3896a3f5921"
@@ -4380,6 +4387,11 @@ is-path-inside@^3.0.3:
resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-3.0.3.tgz#d231362e53a07ff2b0e0ea7fed049161ffd16283"
integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==
is-plain-obj@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
is-plain-object@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
@@ -5207,6 +5219,13 @@ memoize-one@^5.0.0:
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.2.1.tgz#8337aa3c4335581839ec01c3d594090cebe8f00e"
integrity sha512-zYiwtZUcYyXKo/np96AGZAckk+FWWsUdJ3cHGGmld7+AhvcWmQyGCYUh1hc4Q/pkOhb65dQR/pqCyK0cOaHz4Q==
merge-options@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
dependencies:
is-plain-obj "^2.1.0"
merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"