forked from mozilla/experimenter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move api calls into actions fixes mozilla#2957 * remove commented out code * types * typing Co-authored-by: Jared Lockhart <[email protected]>
- Loading branch information
1 parent
185bb57
commit 29d1431
Showing
6 changed files
with
79 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/experimenter/static/rapid/contexts/experiment/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
ExperimentReducerActionType, | ||
ExperimentData, | ||
} from "experimenter-types/experiment"; | ||
import { ExperimentReducerAction } from "experimenter-types/experiment"; | ||
|
||
export const fetchExperiment = (experimentSlug: string) => async ( | ||
experimentData: ExperimentData, | ||
dispatch: React.Dispatch<ExperimentReducerAction>, | ||
): Promise<void> => { | ||
const response = await fetch(`/api/v3/experiments/${experimentSlug}/`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
const data = await response.json(); | ||
dispatch({ | ||
type: ExperimentReducerActionType.UPDATE_STATE, | ||
state: data, | ||
}); | ||
}; | ||
|
||
export const saveExperiment = async ( | ||
experimentSlug: string, | ||
formData: ExperimentData, | ||
): Promise<Response> => { | ||
const url = experimentSlug | ||
? `/api/v3/experiments/${experimentSlug}/` | ||
: "/api/v3/experiments/"; | ||
return await fetch(url, { | ||
method: experimentSlug ? "PUT" : "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(formData), | ||
}); | ||
}; | ||
|
||
export const updateExperiment = (name: string, value: string | string[]) => ( | ||
experimentData: ExperimentData, | ||
dispatch: React.Dispatch<ExperimentReducerAction>, | ||
): void => { | ||
dispatch({ | ||
type: ExperimentReducerActionType.UPDATE_STATE, | ||
state: { | ||
...experimentData, | ||
[name]: value, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import React from "react"; | ||
|
||
import context from "experimenter-rapid/contexts/experiment/context"; | ||
import { | ||
ExperimentData, | ||
ExperimentReducerAction, | ||
} from "experimenter-types/experiment"; | ||
import { Dispatch, ExperimentData } from "experimenter-types/experiment"; | ||
|
||
export function useExperimentState(): ExperimentData { | ||
const { state } = React.useContext(context); | ||
return state; | ||
} | ||
|
||
export function useExperimentDispatch(): React.Dispatch< | ||
ExperimentReducerAction | ||
> { | ||
export function useExperimentDispatch(): Dispatch { | ||
const { dispatch } = React.useContext(context); | ||
return dispatch; | ||
} |
26 changes: 5 additions & 21 deletions
26
app/experimenter/static/rapid/contexts/experiment/provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters