Skip to content

Commit

Permalink
feat: prefix filename with league name
Browse files Browse the repository at this point in the history
  • Loading branch information
shonya3 committed May 19, 2023
1 parent 681c348 commit b10ebcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src-tauri/lib/src/prices.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::paths;
use divi::{error::Error, League, Prices};
use divi::{League, Prices};
use std::{fs, path::Path};

pub const DAY_AS_SECS: u64 = 86_400;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub async fn prices(league: &League) -> Prices {
}

//TODO: add error types
pub async fn update(league: &League) -> Result<Prices, Error> {
pub async fn update(league: &League) -> Result<Prices, reqwest::Error> {
let prices = Prices::fetch(league).await?;
let json = serde_json::to_string(&prices).unwrap();
std::fs::write(paths::prices(league), &json).unwrap();
Expand Down
18 changes: 16 additions & 2 deletions src/composables/useFileCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import { computed, reactive, ref, watch } from 'vue';
import { useSample } from './useSample';
import { useCsvFile } from './useCsvFile';
import { FileCardProps } from '../components/FileCard/FileCard.vue';
import { League } from '../types';
import { CsvExt, League, isCsvExt, leagues } from '../types';
import { command } from '../command';
import { csvFile } from '../lib';

const prefixFilename = (name: string, league: League): CsvExt => {
const UNDERSCORE_GLUE = '_';
const res: CsvExt = isCsvExt(name) ? name : `${name}.csv`;

for (const old of leagues) {
if (res.startsWith(`${old}${UNDERSCORE_GLUE}`)) {
return res.replace(old, league) as CsvExt;
}
}

return `${league}${UNDERSCORE_GLUE}${res}`;
};

export const useFileCard = (file: File, league: League): FileCardProps => {
const { text: csv, name: filename, href } = useCsvFile(file);
const { data, error, isError } = useSample(csv, league);
Expand All @@ -18,7 +31,7 @@ export const useFileCard = (file: File, league: League): FileCardProps => {
valid,
selected,
sample: data,
filename,
filename: prefixFilename(filename.value, league),
href,
error,
isError,
Expand Down Expand Up @@ -47,6 +60,7 @@ export const useFileCard = (file: File, league: League): FileCardProps => {
() => props.league,
async val => {
props.sample = await command('league', { league: val, sample: props.sample });
props.filename = prefixFilename(props.filename, val);
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/stores/fileCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const useFileCardsStore = defineStore('filecardsStore', {
},

downloadAll() {
this.validFiles.forEach(({ filename, href, league }) => downloadFile(`${league}_${filename}`, href));
this.validFiles.forEach(({ filename, href }) => downloadFile(filename, href));
},

deleteMergedFile() {
Expand Down

0 comments on commit b10ebcf

Please sign in to comment.