index.ts 658 B

1234567891011121314151617181920212223
  1. import { triumphsApi } from '@api/triumphs';
  2. import { storage } from 'src/storage';
  3. const NAMESPACE = 'triumphs';
  4. function saveData<T>(key: string, data: T) {
  5. const namespacedKey = `${NAMESPACE}:${key}`;
  6. const jsonData = JSON.stringify(data);
  7. storage.set(namespacedKey, jsonData);
  8. }
  9. export async function saveTriumphsData() {
  10. const response = await triumphsApi.getDates();
  11. const last30Dates = response.data.dates.slice(-30);
  12. saveData('dates', last30Dates);
  13. await Promise.all(
  14. last30Dates.map(async (date) => {
  15. const response = await triumphsApi.getData(date);
  16. saveData(`data_${date}`, response.data.triumphs);
  17. })
  18. );
  19. }