Skip to content

Commit

Permalink
unify remote.ts, add transactionId to storeUserEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharqiewicz committed Aug 30, 2024
1 parent f997257 commit 6c61324
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
3 changes: 2 additions & 1 deletion signer-service/src/api/controllers/email.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { storeDataInGoogleSpreadsheet } = require('./googleSpreadsheet.controller
// These are the headers for the Google Spreadsheet
const EMAIL_SHEET_HEADER_VALUES = [
'timestamp',
'email'
'email',
'transactionId'
];

exports.EMAIL_SHEET_HEADER_VALUES = EMAIL_SHEET_HEADER_VALUES;
Expand Down
15 changes: 12 additions & 3 deletions src/components/EmailForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useMutation } from '@tanstack/react-query';
import { useForm } from 'react-hook-form';
import { storeUserEmailInBackend } from '../../services/storage/storeUserEmailInBackend';
import { storeUserEmailInBackend } from '../../services/storage/remote';
import { TextInput } from '../../components/TextInput';

export const EmailForm = () => {
interface EmailFormProps {
transactionId?: string;
}

export const EmailForm = ({ transactionId }: EmailFormProps) => {
const { register, handleSubmit } = useForm();

const {
Expand All @@ -16,7 +20,12 @@ export const EmailForm = () => {
});

const onSubmit = handleSubmit((data) => {
saveUserEmailMutation({ email: data.email });
if (!transactionId) {
console.error('Transaction ID is missing');
return;
}

saveUserEmailMutation({ email: data.email, transactionId });
});

const FormButtonSection = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/failure/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const FailurePage = ({ finishOfframping, transactionId }: FailurePageProp
<div className="h-0.5 m-auto w-1/5 bg-pink-500 mt-8 mb-5" />
<p className="text-center text-gray-400">If you continue to experience issues, contact support on:</p>
<TelegramButton />
<EmailForm />
<EmailForm transactionId={transactionId} />
<button className="w-full mt-5 text-white bg-blue-700 btn rounded-xl" onClick={finishOfframping}>
Try again
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/success/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const SuccessPage = ({ finishOfframping, transactionId }: SuccessPageProp
If your transaction is not completed after 60 minutes please contact support on:
</p>
<TelegramButton />
<EmailForm />
<EmailForm transactionId={transactionId} />
<button className="w-full mt-5 text-white bg-blue-700 btn rounded-xl" onClick={finishOfframping}>
Return Home
</button>
Expand Down
28 changes: 23 additions & 5 deletions src/services/storage/remote.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SIGNING_SERVICE_URL } from '../../constants/constants';

// These are the headers for the Google Spreadsheet
type Data = {
interface DumpData {
timestamp: string;
polygonAddress: string;
stellarEphemeralPublicKey: string;
Expand All @@ -11,10 +11,15 @@ type Data = {
spacewalkRedeemTx: string;
stellarOfframpTx: string;
stellarCleanupTx: string;
};
}

interface EmailData {
email: string;
transactionId: string;
}

export async function storeDataInBackend(data: Data) {
const response = await fetch(`${SIGNING_SERVICE_URL}/v1/storage/create`, {
async function sendRequestToBackend(endpoint: string, data: EmailData | DumpData) {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -23,6 +28,19 @@ export async function storeDataInBackend(data: Data) {
});

if (!response.ok) {
throw new Error(`Error while sending data to storage endpoint`);
throw new Error(`Error while sending data to ${endpoint}`);
}

return await response.json();
}

export async function storeDataInBackend(data: DumpData) {
const endpoint = `${SIGNING_SERVICE_URL}/v1/storage/create`;
return await sendRequestToBackend(endpoint, data);
}

export async function storeUserEmailInBackend(data: EmailData) {
const endpoint = `${SIGNING_SERVICE_URL}/v1/email/create`;
const payload = { ...data, timestamp: new Date().toISOString() };
return await sendRequestToBackend(endpoint, payload);
}
22 changes: 0 additions & 22 deletions src/services/storage/storeUserEmailInBackend.ts

This file was deleted.

0 comments on commit 6c61324

Please sign in to comment.