message: app is now notified when a payment arrive. and receive payment bitcoin show an additional pop up if the QR code correspond to the invoice paid
diff: @@ -63,19 +63,11 @@ export const ReceiveBitcoinScreen = observer(({ navigation }) => {
  const [memo, setMemo] = useState("")
  const [amount, setAmount] = useState(0)
  - const [loading, setLoading] = useState(false)
  + const [loading, setLoading] = useState(true)
  const [networkIndex, setNetworkIndex] = useState(0)
  const [data, setData] = useState("")
  -
  - // a list of the hash that has been added to the receive bitcoin tab.
  - // because we are creating a
  - // TODO: send notification from the server would be a better architecture
  - const [hashes, setHashes] = useState([])
  - const [counter, setCounter] = useState(0)
  -
  -
  useEffect(() => {
  update()
  }, [networkIndex])
  @@ -198,73 +190,59 @@ export const ReceiveBitcoinScreen = observer(({ navigation }) => {
  }
  }
  - const createInvoice = async () => {
  - setLoading(true)
  -
  - console.tron.log("createInvoice")
  + useEffect(() => {
  + const unsubscribe = messaging().onMessage(async remoteMessage => {
  + if (networkIndex === 0) {
  - let invoice
  + let invoiceDecoded
  try {
  - const query = `mutation addInvoice($value: Int, $memo: String) {
  - invoice {
  - addInvoice(value: $value, memo: $memo)
  + invoiceDecoded = lightningPayReq.decode(data)
  + } catch (err) {
  + console.tron.log(`error decoding the invoice: ${err}`)
  }
  - }`
  -
  - const result = await store.mutate(query, {value: amount, memo})
  - invoice = result.invoice.addInvoice
  - } catch (err) {
  - console.tron.log(`error with AddInvoice: ${err}`)
  - throw err
  + if (!invoiceDecoded) {
  + return
  }
  - try {
  - const invoiceDecoded = lightningPayReq.decode(invoice)
  const hash = getHash(invoiceDecoded)
  - setHashes(array => [...array, hash])
  -
  - console.tron.log({hash})
  -
  - setData(invoice)
  - } catch (err) {
  - Alert.alert(err.toString())
  - } finally {
  - setLoading(false)
  + if (remoteMessage.data.type === "paid-invoice" && remoteMessage.data.hash === hash) {
  + success()
  }
  +
  + } else {
  + console.tron.log("// TODO bitcoin as well")
  }
  - useEffect(() => {
  - const fetchInvoice = async () => {
  - try {
  + });
  - // lightning
  - const query = `mutation updatePendingInvoice($hash: String!) {
  + return unsubscribe;
  + }, [data]); // FIXME: not sure why data need to be in scope here, but otherwise the async function will have data = null
  +
  + const createInvoice = async () => {
  + setLoading(true)
  + console.tron.log("createInvoice")
  + try {
  + const query = `mutation addInvoice($value: Int, $memo: String) {
  invoice {
  - updatePendingInvoice(hash: $hash)
  + addInvoice(value: $value, memo: $memo)
  }
  }`
  - for (const hash of hashes) {
  - const result = await store.mutate(query, {hash})
  -
  - if (result.invoice.updatePendingInvoice === true) {
  - success()
  - }
  - }
  -
  - // on-chain
  - // TODO
  + const result = await store.mutate(query, {value: amount, memo})
  + const invoice = result.invoice.addInvoice
  + setData(invoice)
  + console.tron.log("data has been set")
  } catch (err) {
  - console.tron.warn(`can't fetch invoice ${err}`)
  + console.tron.log(`error with AddInvoice: ${err}`)
  + throw err
  + } finally {
  + setLoading(false)
  }
  }
  - fetchInvoice()
  - }, [counter])
  -
  const success = () => {
  const options = {
  enableVibrateFallback: true,
  