Skip to content

Commit

Permalink
Merge pull request #84 from academic-innovation/member-upload-backgro…
Browse files Browse the repository at this point in the history
…und-task

Update cohort membership import flow to accommodate background task
  • Loading branch information
aphilipharrell authored Oct 8, 2024
2 parents ffdb845 + a6df7ce commit 05325fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/features/cohorts/CohortDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default function CohortDetails() {
return <Spinner animation="border" className="mie-3" screenReaderText="loading" />;
}

const importCallback = (results) => {
setImportResults(results);
const importCallback = (taskStarted) => {
setImportResults(taskStarted);
closeImportMembers();
};

Expand All @@ -41,7 +41,7 @@ export default function CohortDetails() {
return (
<>
{importResults && (
<Alert variant="success" show dismissible onClose={clearResults}>You successfully imported {importResults} learners.</Alert>
<Alert variant="success" show dismissible onClose={clearResults}>A background task has been initiated to import members. Please check back later.</Alert>
)}
<PartnerHeading partnerName={partner?.name}>
<Button variant="inverse-outline-primary" href={`/${partnerSlug}/details`}>View</Button>
Expand Down
20 changes: 6 additions & 14 deletions src/features/members/ImportMembersModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export default function ImportMembersModal({ isOpen, onClose, cohort }) {
const [filename, setFilename] = useState('');
const [errorMessage, setError] = useState('');

const handleOnClose = (numMembersImported) => {
const handleOnClose = (taskStarted) => {
setError('');
setFilename('');
setEmailList([]);
onClose(numMembersImported);
onClose(taskStarted);
};

const onDrop = useCallback((acceptedFiles) => {
Expand All @@ -44,23 +44,15 @@ export default function ImportMembersModal({ isOpen, onClose, cohort }) {

const onImportMembersClicked = async () => {
setError('');
let numMembersImported = 0;
let taskStarted = false;
try {
const response = await dispatch(importMembers({ cohort, emailList }));
const newMembers = response.payload?.members;

numMembersImported = Object.keys(newMembers).map(
memberId => newMembers[memberId],
).length;
const expectedNumMembers = emailList.length;
if (numMembersImported !== expectedNumMembers) {
return setError(`The uploaded list contained duplicates or invalid emails. Added ${numMembersImported} out of ${expectedNumMembers} expected members.`);
}
taskStarted = [200, 201].includes(response.payload.status);
} catch (err) {
console.error(err);

Check warning on line 52 in src/features/members/ImportMembersModal.jsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
return setError('There was an error reading the uploaded file. Please verify and try again.');
return setError('There was an error importing the list of members. Please verify and try again.');
}
return handleOnClose(numMembersImported);
return handleOnClose(taskStarted);
};

const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
Expand Down
9 changes: 3 additions & 6 deletions src/features/members/membersSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
import {
camelCaseObject, snakeCaseObject,
} from '@edx/frontend-platform';
import { noralizeSliceData } from '../../utils/normalize';
import { setupRequest } from '../../utils/requests';

const membersAdapter = createEntityAdapter({
Expand Down Expand Up @@ -59,12 +58,11 @@ export const importMembers = createAsyncThunk(
async (args) => {
const { cohort, emailList } = args;
const { client, baseUrl } = setupRequest();
const { data } = await client.post(
const response = await client.post(
`${baseUrl}/api/partnerships/v0/memberships/${cohort}/`,
snakeCaseObject(emailList.map(email => ({ email }))),
);
const normalized = noralizeSliceData(camelCaseObject(data), 'members');
return normalized.entities;
return response;
},
);

Expand Down Expand Up @@ -107,9 +105,8 @@ const membersSlice = createSlice({
state.status = 'loading';
state.currentRequestId = action.meta.requestId;
})
.addCase(importMembers.fulfilled, (state, action) => {
.addCase(importMembers.fulfilled, (state) => {
state.status = 'success';
membersAdapter.upsertMany(state, action.payload.members);
})
.addCase(importMembers.rejected, (state, action) => {
state.status = 'failed';
Expand Down
9 changes: 0 additions & 9 deletions src/utils/normalize.js

This file was deleted.

0 comments on commit 05325fa

Please sign in to comment.