Skip to content

Commit

Permalink
updated expiry date fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
suyeshs committed Sep 21, 2023
1 parent 9e69443 commit bb56da8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 50 deletions.
30 changes: 19 additions & 11 deletions src/pages/nse-options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const NseFlatDataOptions = observer(({ initialData, initialStock }: { initialDat

useEffect(() => {
const expiryDateStore = initializeExpiryDateStore();
expiryDateStore.fetchExpiryDates().then(() => {

// Since fetchExpiryDates() is removed, we use fetchExpiryDatesForSymbol
// Default to 'NIFTY' or whatever symbol you want to start with
expiryDateStore.fetchExpiryDatesForSymbol('NIFTY').then(() => {
setExpiryDateStore(expiryDateStore);
const firstExpiryDate = expiryDateStore.expiryDates[0] || '';

Expand All @@ -53,7 +56,7 @@ const NseFlatDataOptions = observer(({ initialData, initialStock }: { initialDat

// Initialize DefaultStore
const myDefaultStore = new DefaultStore();
myDefaultStore.setExpiryDate(firstExpiryDate);
myDefaultStore.setExpiryDate(firstExpiryDate);

// Now that we have the expiry date, we can fetch the data
const nseFetchStore = initializeNseFetchStore(myDefaultStore, expiryDateStore, initialData);
Expand All @@ -63,6 +66,7 @@ const NseFlatDataOptions = observer(({ initialData, initialStock }: { initialDat
setIsLoading(false);
});
});

}, [initialData, initialStock]); // Removed store from the dependency array

useEffect(() => {
Expand Down Expand Up @@ -245,15 +249,19 @@ const NseFlatDataOptions = observer(({ initialData, initialStock }: { initialDat
</div>
<div>
<DropDownListComponent
placeholder="Select Instrument"
dataSource={symbolStore?.symbolStore.symbols || []}
value="NIFTY"
change={(e) => {
const selectedSymbol = e.value as string;
store?.nseFetchStore.setSymbol(selectedSymbol);
expiryDateStore?.fetchExpiryDatesForSymbol(selectedSymbol);
}}
/>
placeholder="Select Instrument"
dataSource={symbolStore?.symbolStore.symbols || []}
value="NIFTY"
change={(e) => {
const selectedSymbol = e.value as string;
store?.nseFetchStore.setSymbol(selectedSymbol);
expiryDateStore?.fetchExpiryDatesForSymbol(selectedSymbol).then(() => {
const firstExpiryDate = expiryDateStore.expiryDates[0] || '';
setExpiryDate(firstExpiryDate);
store?.nseFetchStore.setExpiryDate(firstExpiryDate);
});
}}
/>
</div>
</div>

Expand Down
69 changes: 36 additions & 33 deletions src/stores/ExpiryDateStore.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
import { makeObservable, observable, action } from 'mobx';
import axios from 'axios';

// Caching utility, mimicking SWR's caching mechanism
const cache = new Map();

const fetcher = async (url: string) => {
const { data } = await axios.get(url);
return data;
};

export class ExpiryDateStore {
expiryDates: string[] = []; // Initialized as an array
expiryDates: string[] = [];
isLoading: boolean = false;

constructor() {
makeObservable(this, {
expiryDates: observable,
isLoading: observable,
setExpiryDates: action,
fetchExpiryDates: action,
fetchExpiryDatesForSymbol: action, // Add this line
fetchExpiryDatesForSymbol: action,
});
}

setExpiryDates(dates: string[]): void {
this.expiryDates = dates;
}

fetchExpiryDates = async () => {
fetchExpiryDatesForSymbol = async (symbol: string = "NIFTY") => {
this.isLoading = true;

// Fetch expiry dates
const expiryResponse = await axios.get(`https://tradepodapisrv.azurewebsites.net/api/get-expiry/?symbol=NIFTY`);
const expiryData = expiryResponse.data;
if (expiryData && expiryData.expiry_dates) {
this.setExpiryDates(expiryData.expiry_dates);
console.log(this.expiryDates); // Log the expiryDates
} else {
throw new Error('Expiry dates not found');

const url = `https://tradepodapisrv.azurewebsites.net/api/get-expiry/?symbol=${encodeURIComponent(symbol)}`;
console.log(`Generated URL for Symbol ${symbol}:`, url);

if (cache.has(url)) {
this.setExpiryDates(cache.get(url));
this.isLoading = false;
return;
}

this.isLoading = false;
};

fetchExpiryDatesForSymbol = async (symbol: string) => {
this.isLoading = true;
// Define the API URL based on the environment
const API_URL = process.env.NODE_ENV === 'production'
? process.env.REACT_APP_API_URL_PRODUCTION
: process.env.REACT_APP_API_URL_LOCAL;
// Fetch expiry dates for a specific symbol
const expiryResponse = await axios.get(`${API_URL}/api/get-expiry/?symbol=${encodeURIComponent(symbol)}`);
const expiryData = expiryResponse.data;
if (expiryData && expiryData.expiry_dates) {
this.setExpiryDates(expiryData.expiry_dates);
console.log(this.expiryDates); // Log the expiryDates
} else {
throw new Error('Expiry dates not found');
try {
const data = await fetcher(url);

if (data?.expiry_dates?.length) {
this.setExpiryDates(data.expiry_dates);
cache.set(url, data.expiry_dates); // Cache the data
console.log(`Expiry Dates Stored for Symbol ${symbol}:`, this.expiryDates);
} else {
console.error(`Expiry dates not found for symbol: ${symbol}`);
this.setExpiryDates([]); // Clear the expiry dates
}
} catch (error) {
console.error(`Error fetching expiry dates for symbol: ${symbol}`, error);
this.setExpiryDates([]); // Clear the expiry dates in case of an error
} finally {
this.isLoading = false;
}

this.isLoading = false;
};
}

export const initializeExpiryDateStore = (): ExpiryDateStore => {
return new ExpiryDateStore();
};
};
14 changes: 8 additions & 6 deletions src/stores/NseFetchStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,16 @@ export class NseFetchStore {
setExpiryDates(dates: string[]): void {
this.expiryDates = dates;
}

fetchData = async (userSelectedStock: string = this.symbol || 'NIFTY', firstExpiryDate: string = this.expiryDate || '') => {
this.isLoading = true;

// Define the API URL based on the environment
const API_URL = process.env.NODE_ENV === 'production'
? process.env.REACT_APP_API_URL_PRODUCTION
: process.env.REACT_APP_API_URL_LOCAL;

// If we don't have an expiry date, log an error and stop further execution
if (!firstExpiryDate) {
console.error('Expiry date is not available.');
this.isLoading = false;
return [];
}

try {
const response = await axios.get(`https://tradepodapisrv.azurewebsites.net/api/option-chain-copy/?symbol=${encodeURIComponent(this.symbol)}&expiry_date=${encodeURIComponent(firstExpiryDate)}`);
const data = response.data as NseApiResponse;
Expand All @@ -154,6 +155,7 @@ export class NseFetchStore {
this.isLoading = false;
}
};


dispose() {
if (this.intervalId) {
Expand Down

2 comments on commit bb56da8

@vercel
Copy link

@vercel vercel bot commented on bb56da8 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tradepodapp – ./

tradepodapp-suyeshs.vercel.app
tradepodapp.vercel.app
tradepodapp-git-master-suyeshs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bb56da8 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tradepod – ./

tradepod-git-master-suyeshs.vercel.app
tradepod.vercel.app
tradepod-suyeshs.vercel.app

Please sign in to comment.