Skip to content

Commit

Permalink
Fix warrior not found when calculating votes
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWeathers committed Jun 17, 2023
1 parent 0bb3ec0 commit e5727ac
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 76 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/battle/BattlePlans.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
notifications="{notifications}"
eventTag="{eventTag}"
testid="plans-Csvimport"
/>
/>
{/if}
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/battle/CreateBattle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@
</div>
<div class="control mb-4">
<CsvImport
handlePlanAdd="{handlePlanImport}"
notifications="{notifications}"
eventTag="{eventTag}"
handlePlanAdd="{handlePlanImport}"
notifications="{notifications}"
eventTag="{eventTag}"
/>
</div>

Expand Down
142 changes: 73 additions & 69 deletions frontend/src/components/battle/CsvImport.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,97 @@
let plans = []
function uploadFile() {
let file = this.files[0]
if (!file) {
return
}
if (file.type !== 'text/csv') {
notifications.danger($_('importCsvFileBadFileTypeError'))
eventTag('Csv_import_failed', 'battle', `file.type not text/csv or application/vnd.ms-excel`)
return
}
let file = this.files[0]
if (!file) {
return
}
if (file.type !== 'text/csv') {
notifications.danger($_('importCsvFileBadFileTypeError'))
eventTag(
'Csv_import_failed',
'battle',
`file.type not text/csv or application/vnd.ms-excel`,
)
return
}
let reader = new FileReader()
let reader = new FileReader()
reader.readAsText(file)
reader.readAsText(file)
reader.onload = () => {
try {
const content = reader.result;
const items = parseCsvFile(content);
if (items) {
const totalItems = items.length
for (let i = 0; i < totalItems; i++) {
const item = items[i]
const plan = extractPlanData(item);
plans.push(plan)
handlePlanAdd(plan);
reader.onload = () => {
try {
const content = reader.result
const items = parseCsvFile(content)
if (items) {
const totalItems = items.length
for (let i = 0; i < totalItems; i++) {
const item = items[i]
const plan = extractPlanData(item)
plans.push(plan)
handlePlanAdd(plan)
}
eventTag(
'Csv_import_success',
'battle',
`total stories imported: ${totalItems}`,
)
}
} catch (e) {
notifications.danger($_('importCsvFileReadFileError'))
eventTag('Csv_import_failed', 'battle', `error reading file`)
}
eventTag(
'Csv_import_success',
'battle',
`total stories imported: ${totalItems}`,
)
}
} catch (e) {
notifications.danger($_('importCsvFileReadFileError'))
eventTag('Csv_import_failed', 'battle', `error reading file`)
}
}
reader.onerror = () => {
notifications.danger($_('importCsvFileReadFileError'))
eventTag('Csv_import_failed', 'battle', `error reading file`)
}
reader.onerror = () => {
notifications.danger($_('importCsvFileReadFileError'))
eventTag('Csv_import_failed', 'battle', `error reading file`)
}
}
function parseCsvFile(content) {
const lines = content.split('\n');
const items = [];
const lines = content.split('\n')
const items = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
items.push(line);
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()
if (line) {
items.push(line)
}
}
}
return items;
return items
}
function extractPlanData(item) {
const fields = item.split(',');
const plan = {
type: fields[0].trim(),
planName: fields[1].trim(),
referenceId: fields[2].trim(),
link: fields[3].trim(),
description: fields[4].trim(),
acceptanceCriteria: fields[5] ? fields[5].trim() : ''
};
const fields = item.split(',')
const plan = {
type: fields[0].trim(),
planName: fields[1].trim(),
referenceId: fields[2].trim(),
link: fields[3].trim(),
description: fields[4].trim(),
acceptanceCriteria: fields[5] ? fields[5].trim() : '',
}
return plan;
return plan
}
</script>
</script>

{#if allowCsvImport}
{#if allowCsvImport}
<HollowButton
type="label"
additionalClasses="rtl:ml-2 ltr:mr-2"
color="purple"
labelFor="csvimport"
type="label"
additionalClasses="rtl:ml-2 ltr:mr-2"
color="purple"
labelFor="csvimport"
>
{$_('importCsv')}
<input
type="file"
on:change="{uploadFile}"
class="hidden"
id="csvimport"
accept=".csv"
/>
{$_('importCsv')}
<input
type="file"
on:change="{uploadFile}"
class="hidden"
id="csvimport"
accept=".csv"
/>
</HollowButton>
{/if}
{/if}
7 changes: 4 additions & 3 deletions frontend/src/pages/Battle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,10 @@
// build a count of each vote
activePlan.votes.forEach(v => {
const { spectator = false } = battle.users.find(
w => w.id === v.warriorId,
)
const voteWarrior =
battle.users.find(w => w.id === v.warriorId) || {}
const { spectator = false } = voteWarrior
if (typeof voteCounts[v.vote] !== 'undefined' && !spectator) {
++voteCounts[v.vote]
}
Expand Down

0 comments on commit e5727ac

Please sign in to comment.