onboardingTasks.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {Client} from 'sentry/api';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import OrganizationStore from 'sentry/stores/organizationStore';
  4. import {OnboardingTask, OnboardingTaskStatus, Organization} from 'sentry/types';
  5. interface UpdatedTask extends Partial<Pick<OnboardingTask, 'status' | 'data'>> {
  6. task: OnboardingTask['task'];
  7. /**
  8. * Marks completion seen. This differs from the OnboardingTask
  9. * completionSeen type as that returns the date completion was seen.
  10. */
  11. completionSeen?: boolean;
  12. }
  13. /**
  14. * Update an onboarding task.
  15. *
  16. * If no API client is provided the task will not be updated on the server side
  17. * and will only update in the organization store.
  18. */
  19. export function updateOnboardingTask(
  20. api: Client | null,
  21. organization: Organization,
  22. updatedTask: UpdatedTask
  23. ) {
  24. if (api !== null) {
  25. api.requestPromise(`/organizations/${organization.slug}/onboarding-tasks/`, {
  26. method: 'POST',
  27. data: updatedTask,
  28. });
  29. }
  30. const hasExistingTask = organization.onboardingTasks.find(
  31. task => task.task === updatedTask.task
  32. );
  33. const user = ConfigStore.get('user');
  34. const onboardingTasks = hasExistingTask
  35. ? organization.onboardingTasks.map(task =>
  36. task.task === updatedTask.task ? {...task, ...updatedTask} : task
  37. )
  38. : [...organization.onboardingTasks, {...updatedTask, user} as OnboardingTaskStatus];
  39. OrganizationStore.onUpdate({onboardingTasks});
  40. }