|
1 | | -import { Action } from '.'; |
| 1 | +import { Action, AsyncAction } from '.'; |
2 | 2 | import * as internalActions from './internalActions'; |
3 | 3 | import { withLoadApp } from './factories'; |
| 4 | +import { NotificationType } from '@codesandbox/common/lib/utils/notifications'; |
4 | 5 |
|
5 | 6 | export const internal = internalActions; |
6 | 7 |
|
7 | | -export const appUnmounted: Action = async ({ effects, actions }) => { |
8 | | - effects.connection.removeListener(actions.internal.onConnectionChange); |
| 8 | +export const appUnmounted: AsyncAction = async ({ effects, actions }) => { |
| 9 | + effects.connection.removeListener(actions.connectionChanged); |
9 | 10 | }; |
10 | 11 |
|
11 | | -export const sandboxPageMounted: Action = withLoadApp(() => {}); |
| 12 | +export const sandboxPageMounted: AsyncAction = withLoadApp(); |
| 13 | + |
| 14 | +export const searchMounted: AsyncAction = withLoadApp(); |
| 15 | + |
| 16 | +export const cliMounted: AsyncAction = withLoadApp( |
| 17 | + async ({ state, actions }) => { |
| 18 | + if (state.user) { |
| 19 | + await actions.internal.authorize(); |
| 20 | + } |
| 21 | + } |
| 22 | +); |
| 23 | + |
| 24 | +export const forceRender: Action = ({ state }) => { |
| 25 | + state.editor.forceRender++; |
| 26 | +}; |
| 27 | + |
| 28 | +export const cliInstructionsMounted: AsyncAction = withLoadApp(); |
| 29 | + |
| 30 | +export const githubPageMounted: AsyncAction = withLoadApp(); |
| 31 | + |
| 32 | +export const connectionChanged: Action<boolean> = ({ state }, connected) => { |
| 33 | + state.connected = connected; |
| 34 | +}; |
| 35 | + |
| 36 | +export const modalOpened: Action<{ modal: string; message: string }> = ( |
| 37 | + { state, effects }, |
| 38 | + { modal, message } |
| 39 | +) => { |
| 40 | + effects.analytics.track('Open Modal', { modal }); |
| 41 | + state.currentModalMessage = message; |
| 42 | + state.currentModal = modal; |
| 43 | +}; |
| 44 | + |
| 45 | +export const modalClosed: Action = ({ state }) => { |
| 46 | + state.currentModal = null; |
| 47 | +}; |
| 48 | + |
| 49 | +export const signInClicked: AsyncAction<{ useExtraScopes: boolean }> = ( |
| 50 | + { actions }, |
| 51 | + options |
| 52 | +) => actions.internal.signIn(options); |
| 53 | + |
| 54 | +export const signInCliClicked: AsyncAction = async ({ state, actions }) => { |
| 55 | + await actions.internal.signIn({ |
| 56 | + useExtraScopes: false, |
| 57 | + }); |
| 58 | + |
| 59 | + if (state.user) { |
| 60 | + await actions.internal.authorize(); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const userMenuOpened: Action = ({ state }) => { |
| 65 | + state.userMenuOpen = true; |
| 66 | +}; |
| 67 | + |
| 68 | +export const userMenuClosed: Action = ({ state }) => { |
| 69 | + state.userMenuOpen = false; |
| 70 | +}; |
| 71 | + |
| 72 | +export const addNotification: Action<{ |
| 73 | + message: string; |
| 74 | + type: NotificationType; |
| 75 | + timeAlive: number; |
| 76 | +}> = ({ effects }, { message, type, timeAlive }) => { |
| 77 | + effects.notificationToast.add({ |
| 78 | + message, |
| 79 | + status: effects.notificationToast.convertTypeToStatus(type), |
| 80 | + timeAlive: timeAlive * 1000, |
| 81 | + }); |
| 82 | +}; |
| 83 | + |
| 84 | +export const removeNotification: Action<number> = ({ state }, id) => { |
| 85 | + const notificationToRemoveIndex = state.notifications.findIndex( |
| 86 | + notification => notification.id === id |
| 87 | + ); |
| 88 | + |
| 89 | + state.notifications.splice(notificationToRemoveIndex, 1); |
| 90 | +}; |
| 91 | + |
| 92 | +export const signInZeitClicked: AsyncAction = async ({ |
| 93 | + state, |
| 94 | + effects: { browser, api, notificationToast }, |
| 95 | + actions, |
| 96 | +}) => { |
| 97 | + state.isLoadingZeit = true; |
| 98 | + |
| 99 | + const popup = browser.openPopup('/auth/zeit', 'sign in'); |
| 100 | + const data: { code: string } = await browser.waitForMessage('signin'); |
| 101 | + |
| 102 | + popup.close(); |
| 103 | + |
| 104 | + if (data && data.code) { |
| 105 | + try { |
| 106 | + state.user = await api.post(`/users/current_user/integrations/zeit`, { |
| 107 | + code: data.code, |
| 108 | + }); |
| 109 | + await actions.internal.getZeitUserDetails(); |
| 110 | + } catch (error) { |
| 111 | + notificationToast.add({ |
| 112 | + message: 'Could not authorize with ZEIT', |
| 113 | + status: notificationToast.convertTypeToStatus('error'), |
| 114 | + }); |
| 115 | + } |
| 116 | + } else { |
| 117 | + notificationToast.add({ |
| 118 | + message: 'Could not authorize with ZEIT', |
| 119 | + status: notificationToast.convertTypeToStatus('error'), |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + state.isLoadingZeit = false; |
| 124 | +}; |
| 125 | + |
| 126 | +export const signOutZeitClicked: AsyncAction = async ({ state, effects }) => { |
| 127 | + await effects.http.delete(`/users/current_user/integrations/zeit`); |
| 128 | + state.user.integrations.zeit = null; |
| 129 | +}; |
| 130 | + |
| 131 | +export const authTokenRequested: AsyncAction = async ({ actions }) => { |
| 132 | + await actions.internal.authorize(); |
| 133 | +}; |
| 134 | + |
| 135 | +export const requestAuthorisation: AsyncAction = async ({ actions }) => { |
| 136 | + await actions.internal.authorize(); |
| 137 | +}; |
| 138 | + |
| 139 | +export const signInGithubClicked: AsyncAction = async ({ state, actions }) => { |
| 140 | + state.isLoadingGithub = true; |
| 141 | + await actions.internal.signIn({ useExtraScopes: false }); |
| 142 | + state.isLoadingGithub = false; |
| 143 | +}; |
| 144 | + |
| 145 | +export const signOutClicked: AsyncAction = async ({ |
| 146 | + state, |
| 147 | + effects, |
| 148 | + actions, |
| 149 | +}) => { |
| 150 | + effects.analytics.track('Sign Out', {}); |
| 151 | + state.workspace.openedWorkspaceItem = 'files'; |
| 152 | + if (state.live.isLive) { |
| 153 | + actions.live.internal.disconnect(); |
| 154 | + } |
| 155 | + await effects.api.delete(`/users/signout`); |
| 156 | + actions.internal.removeJwtFromStorage(); |
| 157 | + state.user = null; |
| 158 | + await actions.refetchSandboxInfo(); |
| 159 | +}; |
| 160 | + |
| 161 | +export const signOutGithubIntegration: AsyncAction = async ({ |
| 162 | + state, |
| 163 | + effects, |
| 164 | +}) => { |
| 165 | + await effects.api.delete(`/users/current_user/integrations/github`); |
| 166 | + state.user.integrations.github = null; |
| 167 | +}; |
| 168 | + |
| 169 | +export const setUpdateStatus: Action<string> = ({ state }, status) => { |
| 170 | + state.updateStatus = status; |
| 171 | +}; |
| 172 | + |
| 173 | +export const refetchSandboxInfo: AsyncAction = async ({ state, actions }) => { |
| 174 | + if (state.editor.currentId) { |
| 175 | + const id = state.editor.currentId; |
| 176 | + const sandbox = await actions.internal.getSandbox(id); |
| 177 | + |
| 178 | + state.editor.sandboxes[id].collection = sandbox.collection; |
| 179 | + state.editor.sandboxes[id].owned = sandbox.owned; |
| 180 | + state.editor.sandboxes[id].userLiked = sandbox.userLiked; |
| 181 | + state.editor.sandboxes[id].title = sandbox.title; |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +export const track: Action<{ name: string; data: any }> = ( |
| 186 | + { effects }, |
| 187 | + { name, data } |
| 188 | +) => { |
| 189 | + effects.analytics.track(name, data); |
| 190 | +}; |
0 commit comments