Skip to content

Commit

Permalink
edit reducer to fix bug resulting in duplicates ending up in the resp…
Browse files Browse the repository at this point in the history
…onses object for checkbox questions

Relates #148
  • Loading branch information
edificex committed Apr 2, 2020
1 parent ed7f6a2 commit 128ea47
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
status = 301
force = true

[[redirect]]
from = "https://musafespace.netlify.com/*"
to = "https://www.musafespace.org.uk/:splat"
status = 301
force = true

[[redirect]]
from = "https://musafespace.netlify.com/*"
to = "https://www.musafespace.org.uk/:splat"
Expand Down
2 changes: 1 addition & 1 deletion src/App/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Home = () => {
Together we can change this
</Type5>
<TypeB1 use='body1' tag='p'>
This site to a safe space for everyone working in the music industry
This site is a safe space for everyone working in the music industry
to log instances of sexual harassment and abuse on the job.
</TypeB1>
<TypeExtra use='body1' tag='p'>
Expand Down
2 changes: 2 additions & 0 deletions src/App/Report/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const Form = ({ questions, responses, updateResponses }) => {
const page = parseInt(useParams().index, 10);
const history = useHistory();

console.log(responses);

// get questions to be displayed on this page
const pageQuestions = filterQuestions(questions, page, responses);

Expand Down
29 changes: 20 additions & 9 deletions src/App/Report/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import hardDividers from '../../model/dividers';
const Report = () => {
// grab React Router state to determine which components to render at Report level, and which questions/dividers to fetch
const location = useLocation();
// default to first person version if choice not available (i.e. user navigated directly to report)
// default to first person version if choice not available (i.e. if user navigates directly to report)
const choice = useMemo(
() =>
location.state && location.state.choice ? location.state.choice : 'first',
[]
);
console.log('choice: ', choice);

// set up states
const [questions, setQuestions] = useState(null);
Expand Down Expand Up @@ -70,30 +69,42 @@ const Report = () => {
if (type === 'checkbox') {
// checkboxes need special handling since they can take multiple answers
if (checked && state[field]) {
// if the value is an 'Other' submission but we've already collected a response not belonging to pre-set answers, replace it
// for this we will first need to derive the question from which the event emanates, by searching the questions object with field
console.log(
`existing data checkbox branch of reducer invoked with value '${value} and trusted ${trusted}'`
);
// we first derive the question from which the event emanates, by searching the questions object with field
let index;
questions.forEach((question, i) => {
if (question.question === field) index = i;
});
const otherSubmissions = state[field].filter(
answer => !questions[index].content.includes(answer)
);
if (!trusted && otherSubmissions.length > 0) {
// if there's response data, checkbox is checked, but response already includes this value, no change
if (state[field].includes(value)) {
console.log(
`#1 normal existing value branch triggered for value '${value}'`
);
return state;
// else if the value is an 'Other' submission but we've already collected an 'Other' response (i.e. one not belonging to pre-set answers), replace it
// NB. the re-selection of checkboxes on returning to a page are not trusted events, but those relating to non-'Other' options are caught by previous branch
} else if (!trusted && otherSubmissions.length > 0) {
console.log(
`#2 changed other submission branch triggered for value '${value}'`
);
const newResponses = deleteValue(state[field], otherSubmissions[0]);
return {
...state,
[field]: [...newResponses, value],
};
// else if there's response data, checkbox is checked, but response already includes this value, no change
} else if (state[field].includes(value)) {
return state;
// and else simply incorporate the new value
} else
} else {
console.log(`#3 new value branch triggered for value '${value}'`);
return {
...state,
[field]: [...state[field], value],
};
}
} else if (checked) {
// else if there is no response data and checkbox is being checked, it is for the first time, so incorporate given value
return { ...state, [field]: [value] };
Expand Down
2 changes: 0 additions & 2 deletions src/App/Report/Submit/Submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const Submit = ({ responses, updateResponses, choice, userRef }) => {
return responses;
};

// NEED TO devise a way to submit multiple responses to Airtable, rather than just concatenate into one string
// OR have them arrive in separate columns in the responses tables
const handleSubmit = event => {
event.preventDefault();
const finalResponses = {
Expand Down

0 comments on commit 128ea47

Please sign in to comment.