Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

392 - Rename context states for DatasetManagerContext #434

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/components/Dataset/MoveToDatasetButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import { MoveToDatasetModal } from './MoveToDatasetModal'

export const MoveToDatasetButton = ({ dataset }) => {
const { push } = useRouter()
const {
dataset: myDataset,
addSamples,
getTotalSamples
} = useDatasetManager()
const { myDataset, addSamples, getTotalSamples } = useDatasetManager()
const { openModal, closeModal } = useModal()
const id = `move-to-dataset-${dataset.id}`
const radioOptions = [
Expand Down
11 changes: 3 additions & 8 deletions src/components/Dataset/MoveToDatasetModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ export const MoveToDatasetModal = ({
value,
setValue
}) => {
const {
dataset: datasetState,
loading,
addSamples,
getTotalSamples,
replaceSamples
} = useDatasetManager()
const { myDataset, loading, addSamples, getTotalSamples, replaceSamples } =
useDatasetManager()
const { setResponsive } = useResponsive()
const { push } = useRouter()
const totalSamples = formatNumbers(getTotalSamples(datasetState.data))
const totalSamples = formatNumbers(getTotalSamples(myDataset.data))
const newDatasetTotalSamples = formatNumbers(getTotalSamples(dataset.data))

const handleMoveSamples = async (action = 'append') => {
Expand Down
10 changes: 5 additions & 5 deletions src/components/Header/GlobalNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ export const GlobalNav = ({ light = false, toggle = false, setToggle }) => {
const router = useRouter()
const { asPath, pathname } = router
const { viewport, setResponsive } = useResponsive()
const { dataset, datasetId, getDataset, getTotalSamples } =
const { myDataset, myDatasetId, getDataset, getTotalSamples } =
useDatasetManager()
const [totalSamples, setTotalSamples] = useState()

useEffect(() => {
if (dataset) setTotalSamples(getTotalSamples(dataset.data))
}, [dataset])
if (myDataset) setTotalSamples(getTotalSamples(myDataset.data))
}, [myDataset])

useEffect(() => {
if (datasetId) getDataset()
}, [datasetId])
if (myDatasetId) getDataset()
}, [myDatasetId])

const handleClick = () => {
if (viewport !== 'small') return
Expand Down
12 changes: 6 additions & 6 deletions src/components/shared/DatasetActionButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ export const DatasetActionButton = ({
disableAddRemaining = false,
...props
}) => {
const { dataset } = useDatasetManager()
const { myDataset } = useDatasetManager()

const {
getHasAllProcessed,
anyProcessedSamples,
getAddedSamples,
getTotalSamplesInDataset
} = useDatasetAction(dataset?.data, data)
} = useDatasetAction(myDataset?.data, data)

// shows the disabled add button if no processed samples
if (!anyProcessedSamples()) {
// eslint-disable-next-line react/jsx-props-no-spreading
return <Button disabled {...props} />
}

// shows the remvove from button if all processed samples are in my dataset
// shows the remvove from button if all processed samples are in myDataset
if (getHasAllProcessed()) {
return <RemoveDatasetButton dataToRemove={getAddedSamples()} />
}

// shows the add remaming button if some of the processed samples are in my dataset
// shows the add remaming button if some of the processed samples are in myDataset
if (
!disableAddRemaining &&
dataset?.data &&
dataset.data[accessionCode]?.length < downloadableSamples &&
myDataset?.data &&
myDataset.data[accessionCode]?.length < downloadableSamples &&
getTotalSamplesInDataset() > 0
) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { getFormattedExperiment } from 'helpers/formatDatasetAction'
import { CheckBox } from 'components/shared/CheckBox'

export const ShowOnlyAddedSamplesFilter = ({ experiment }) => {
const { dataset } = useDatasetManager()
const { myDataset } = useDatasetManager()
const { updateDatasetId, samplesQuery } = useSamplesContext()
const { getAnyProcessedInDataset } = useDatasetAction(
dataset.data,
myDataset.data,
getFormattedExperiment(
samplesQuery.experiment_accession_code,
experiment.num_downloadable_samples
Expand All @@ -21,7 +21,7 @@ export const ShowOnlyAddedSamplesFilter = ({ experiment }) => {

const handleToggle = () => {
const newShowOnly = !showOnly
updateDatasetId(newShowOnly ? dataset.id : null)
updateDatasetId(newShowOnly ? myDataset.id : null)
setShowOnly(newShowOnly)
}

Expand Down
24 changes: 12 additions & 12 deletions src/contexts/DatasetManagerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export const DatasetManagerContext = createContext({})

export const DatasetManagerContextProvider = ({ children }) => {
const {
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
Expand All @@ -19,24 +19,24 @@ export const DatasetManagerContextProvider = ({ children }) => {

const value = useMemo(
() => ({
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
setProcessingDatasets
}),
[
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
Expand Down
22 changes: 11 additions & 11 deletions src/contexts/RefinebioContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { api } from 'api'
export const RefinebioContext = createContext({})

export const RefinebioContextProvider = ({ children }) => {
const [dataset, setDataset] = useLocalStorage('dataset', {})
const [datasetId, setDatasetId] = useLocalStorage('datasetId', null)
const [myDataset, setMyDataset] = useLocalStorage('dataset', {})
const [myDatasetId, setMyDatasetId] = useLocalStorage('datasetId', null)
const [email, setEmail] = useLocalStorage('email-address', null)
const [datasetAccessions, setDatasetAccessions] = useLocalStorage(
'dataset-accessions',
Expand Down Expand Up @@ -44,7 +44,7 @@ export const RefinebioContextProvider = ({ children }) => {
const oldKeyValue = getOldLocalStorageKey(oldKey)

if (oldKeyValue) {
setDatasetId(getOldLocalStorageKey(oldKeyValue))
setMyDatasetId(getOldLocalStorageKey(oldKeyValue))
removeOldLocalStorageKey(oldKey)
}
}, [])
Expand Down Expand Up @@ -82,12 +82,12 @@ export const RefinebioContextProvider = ({ children }) => {

const value = useMemo(
() => ({
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
Expand All @@ -100,12 +100,12 @@ export const RefinebioContextProvider = ({ children }) => {
tokenPromise
}),
[
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
Expand Down
60 changes: 30 additions & 30 deletions src/hooks/useDatasetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { api } from 'api'

export const useDatasetManager = () => {
const {
dataset,
setDataset,
myDataset,
setMyDataset,
datasetAccessions,
setDatasetAccessions,
datasetId,
setDatasetId,
myDatasetId,
setMyDatasetId,
email,
setEmail,
processingDatasets,
Expand Down Expand Up @@ -67,9 +67,9 @@ export const useDatasetManager = () => {
const clearDataset = async (id = '') => {
setLoading(true)
const body = { data: {} }
const response = await updateDataset(id || datasetId, body)
const response = await updateDataset(id || myDatasetId, body)

setDataset(response)
setMyDataset(response)
setLoading(false)
}

Expand All @@ -79,7 +79,7 @@ export const useDatasetManager = () => {

// stores the newly created dataset ID to localStorge
if (setCurrentDatasetId) {
setDatasetId(response.id)
setMyDatasetId(response.id)
}

return response.id
Expand All @@ -95,7 +95,7 @@ export const useDatasetManager = () => {
}

const getDataset = async (id) => {
if (!id && !datasetId) return null // TODO: Throw an error
if (!id && !myDatasetId) return null // TODO: Throw an error

setLoading(true)

Expand All @@ -105,11 +105,11 @@ export const useDatasetManager = () => {
headers['API-KEY'] = await tokenPromise
}

const response = await api.dataset.get(id || datasetId, headers)
const response = await api.dataset.get(id || myDatasetId, headers)
const { ok, statusCode } = response

if (ok && isMyDatasetId(id)) {
setDataset(response)
setMyDataset(response)
}

// sets the error if any, otherwise resets it
Expand All @@ -126,8 +126,8 @@ export const useDatasetManager = () => {
return response
}

// checks if the given dataset ID is My dataset ID
const isMyDatasetId = (id) => id === datasetId
// checks if the given dataset ID is myDatasetId
const isMyDatasetId = (id) => id === myDatasetId

const startProcessingDataset = async (
options,
Expand Down Expand Up @@ -159,10 +159,10 @@ export const useDatasetManager = () => {
addToProcessingDatasets(processingDatasetId, accessionCode)
// saves the user's newly entered email or replace the existing one
setEmail(emailAddress)
// deletes the locally saved dataset data once it has started processing (no longer mutable)
// deletes the locally saved myDataset data once it has started processing (no longer mutable)
if (id && isMyDatasetId(id)) {
setDataset({})
setDatasetId(null)
setMyDataset({})
setMyDatasetId(null)
}

return response
Expand All @@ -178,7 +178,7 @@ export const useDatasetManager = () => {
const response = await api.dataset.update(id, body, headers)

if (isMyDatasetId(id)) {
setDataset(response)
setMyDataset(response)
}

return response
Expand Down Expand Up @@ -212,20 +212,20 @@ export const useDatasetManager = () => {
setLoading(true)
const body = { data: {} }

for (const experiment in dataset.data) {
for (const experiment in myDataset.data) {
if (experimentAccessionCode.includes(experiment)) continue
body.data[experiment] = dataset.data[experiment]
body.data[experiment] = myDataset.data[experiment]
}

const response = await updateDataset(datasetId, body)
setDataset(response)
const response = await updateDataset(myDatasetId, body)
setMyDataset(response)
setLoading(false)
}

/* Sample */
const addSamples = async (data) => {
setLoading(true)
const body = { data: dataset ? { ...dataset.data } : {} }
const body = { data: myDataset ? { ...myDataset.data } : {} }

for (const accessionCode of Object.keys(data)) {
if (data[accessionCode].all) {
Expand All @@ -241,10 +241,10 @@ export const useDatasetManager = () => {
}

const response = await updateDataset(
datasetId || (await createDataset(true)),
myDatasetId || (await createDataset(true)),
body
)
setDataset(response)
setMyDataset(response)
setLoading(false)

return response
Expand All @@ -257,7 +257,7 @@ export const useDatasetManager = () => {
isEmptyObject(data) ? 0 : unionizeArrays(...Object.values(data)).length

const removeSamples = async (data) => {
const body = { data: { ...dataset.data } }
const body = { data: { ...myDataset.data } }

for (const accessionCode of Object.keys(data)) {
if (!body.data[accessionCode]) continue
Expand All @@ -275,15 +275,15 @@ export const useDatasetManager = () => {
}

setLoading(true)
const response = await updateDataset(datasetId, body)
setDataset(response)
const response = await updateDataset(myDatasetId, body)
setMyDataset(response)
setLoading(false)
}

const replaceSamples = async (data) => {
setLoading(true)
const response = await updateDataset(datasetId, { data })
setDataset(response)
const response = await updateDataset(myDatasetId, { data })
setMyDataset(response)
setLoading(false)
}

Expand All @@ -293,8 +293,8 @@ export const useDatasetManager = () => {
setError,
datasetAccessions,
setDatasetAccessions,
dataset,
datasetId,
myDataset,
myDatasetId,
loading,
processingDatasets,
setProcessingDatasets,
Expand Down
Loading