onboardingTasks.tsx 1.5 KB

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