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

Synchronisation des tournois #175

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions babel.config.cts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function(api) {
"@components": "./src/components",
"@navigation": "./src/navigation",
"@screens": "./src/screens",
"@services": "./src/services",
"@store": "./src/store",
"@utils": "./src/utils",
}
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@components/*": ["./src/components/*"],
"@navigation/*": ["./src/navigation/*"],
"@screens/*": ["./src/screens/*"],
"@ervices/*": ["./src/services/*"],
"@store/*": ["./src/store/*"],
"@utils/*": ["./src/utils/*"]
}
Expand Down
15 changes: 12 additions & 3 deletions src/screens/Connexion/Compte.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import TopBarBack from '@/components/TopBarBack';
import { withSession } from '@/components/supabase/withSession';
import { LoaderIcon, TrashIcon } from '@/components/ui/icon';
import { Divider } from '@/components/ui/divider';
import { synchroniserTournois } from '@/services/tournoisService';
import { Tournoi } from '@/types/interfaces/tournoi';
import { connector, PropsFromRedux } from '@/store/connector';

export interface Props {
export interface Props extends PropsFromRedux {
navigation: StackNavigationProp<any, any>;
t: TFunction;
session: Session | null;
Expand All @@ -33,6 +36,12 @@ class Compte extends React.Component<Props, State> {
this.props.navigation.navigate('AccueilGeneral');
}

synchronisation(): void {
let tournois: Tournoi[] = this.props.listeTournois;
console.log(tournois.slice(0, 2));
synchroniserTournois(tournois.slice(0, 2));
}

async supprimerCompte() {}

render() {
Expand Down Expand Up @@ -66,7 +75,7 @@ class Compte extends React.Component<Props, State> {
<Button onPress={() => this.deconnexion()}>
<ButtonText>{t('se_deconnecter')}</ButtonText>
</Button>
<Button isDisabled={true} onPress={() => undefined}>
<Button onPress={() => this.synchronisation()}>
<ButtonIcon as={LoaderIcon} />
<ButtonText className="ml-2">
{t('forcer_synchronisation')}
Expand All @@ -90,4 +99,4 @@ class Compte extends React.Component<Props, State> {
}
}

export default withSession(withTranslation()(Compte));
export default connector(withSession(withTranslation()(Compte)));
39 changes: 39 additions & 0 deletions src/services/joueursServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Joueur } from '@/types/interfaces/joueur';
import { supabase } from '@/utils/supabase';

export const synchroniserJoueurs = async (
joueurs: Joueur[],
tournoi_id: number,
): Promise<void> => {
joueurs.forEach(async (joueur) => {
const updatedJoueur = {
tournoi_id: tournoi_id,
joueur_id: joueur.id,
nom: joueur.name,
type: joueur.type,
equipe: joueur.equipe,
coche: joueur.isChecked,
//created_at: tournoi.creationDate,
//updated_at: tournoi.updateDate,
};
upsertJoueurs(updatedJoueur);
});
};

export const upsertJoueurs = async (updatedJoueur): Promise<void> => {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
throw new Error('No user on the session!');
}

updatedJoueur.user_id = user.id;
const { data, error } = await supabase
.from('joueurs')
.upsert(updatedJoueur, { onConflict: 'tournoi_id, joueur_id' })
.select();
console.log('upsertJoueurs');
console.log(data);
console.log(error);
};
36 changes: 36 additions & 0 deletions src/services/matchsServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Match } from '@/types/interfaces/match';
import { supabase } from '@/utils/supabase';

export const synchroniserMatchs = async (
matchs: Match[],
tournoi_id: number,
): Promise<void> => {
matchs.forEach(async (match) => {
const updatedMatch = {
tournoi_id: tournoi_id,
match_id: match.id,
score_1: match.score1,
score_2: match.score2,
manche: match.manche,
manche_nom: match.mancheName,
//created_at: tournoi.creationDate,
//updated_at: tournoi.updateDate,
};
upsertMatchs(updatedMatch);
});
};

export const upsertMatchs = async (updatedMatch): Promise<void> => {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
throw new Error('No user on the session!');
}

updatedMatch.user_id = user.id;
const { data, error } = await supabase

Check warning

Code scanning / ESLint

Disallow unused variables Warning

'data' is assigned a value but never used.

Copilot Autofix AI 3 months ago

The best way to fix the problem is to remove the unused variable data. This will clean up the code and adhere to the ESLint rule. Since data is not used anywhere in the function, we can safely remove its declaration without affecting the existing functionality.

To implement this fix, we need to:

  1. Remove the data variable from the destructuring assignment on line 32.
  2. Ensure that the rest of the code remains unchanged and functional.
Suggested changeset 1
src/services/matchsServices.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/services/matchsServices.ts b/src/services/matchsServices.ts
--- a/src/services/matchsServices.ts
+++ b/src/services/matchsServices.ts
@@ -31,3 +31,3 @@
   updatedMatch.user_id = user.id;
-  const { data, error } = await supabase
+  const { error } = await supabase
     .from('matchs')
EOF
@@ -31,3 +31,3 @@
updatedMatch.user_id = user.id;
const { data, error } = await supabase
const { error } = await supabase
.from('matchs')
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

Check warning

Code scanning / ESLint

Disallow unused variables Warning

'error' is assigned a value but never used.

Copilot Autofix AI 3 months ago

To fix the problem, we need to handle the error variable appropriately. The best way to fix this is to add error handling logic to ensure that any errors returned by the upsert operation are properly managed. This can be done by checking if error is not null and then taking appropriate action, such as logging the error or throwing an exception.

We will modify the upsertMatchs function to include error handling for the error variable. Specifically, we will add a check after the upsert operation to see if error is not null, and if so, we will log the error or throw an exception.

Suggested changeset 1
src/services/matchsServices.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/services/matchsServices.ts b/src/services/matchsServices.ts
--- a/src/services/matchsServices.ts
+++ b/src/services/matchsServices.ts
@@ -35,2 +35,7 @@
     .select();
+  
+  if (error) {
+    console.error('Error upserting match:', error);
+    throw new Error('Failed to upsert match');
+  }
 };
EOF
@@ -35,2 +35,7 @@
.select();

if (error) {
console.error('Error upserting match:', error);
throw new Error('Failed to upsert match');
}
};
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
.from('matchs')
.upsert(updatedMatch, { onConflict: 'tournoi_id, match_id' })
.select();
};
48 changes: 48 additions & 0 deletions src/services/tournoisService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Tournoi } from '@/types/interfaces/tournoi';
import { supabase } from '@/utils/supabase';
import { Match } from '@/types/interfaces/match';
import { OptionsTournoi } from '@/types/interfaces/optionsTournoi';
import { synchroniserJoueurs } from './joueursServices';
import { synchroniserMatchs } from './matchsServices';

export const synchroniserTournois = async (
tournois: Tournoi[],
): Promise<void> => {
tournois.forEach(async (tournoi) => {
const updatedTournoi = {
tournoi_id: tournoi.tournoiId,
nom: tournoi.name,
created_at: tournoi.creationDate,
updated_at: tournoi.updateDate,
};
upsertTournois(updatedTournoi);
let matchs = tournoi.tournoi.slice(0, -1) as Match[];
let optionsTournoi = tournoi.tournoi.at(-1) as OptionsTournoi;
const tournoiServ = await getTournoi(tournoi.tournoiId);
synchroniserMatchs(matchs, tournoiServ.id);
synchroniserJoueurs(optionsTournoi.listeJoueurs, tournoiServ.id);
});
};

export const upsertTournois = async (updatedTournoi): Promise<void> => {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
throw new Error('No user on the session!');
}

updatedTournoi.user_id = user.id;
const { data, error } = await supabase

Check warning

Code scanning / ESLint

Disallow unused variables Warning

'data' is assigned a value but never used.

Copilot Autofix AI 3 months ago

To fix the problem, we should remove the unused variable data from the code. This will eliminate the ESLint error and make the code cleaner and more efficient. Specifically, we need to modify the upsertTournois function to remove the assignment of data and the corresponding destructuring from the supabase query result.

Suggested changeset 1
src/services/tournoisService.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/services/tournoisService.ts b/src/services/tournoisService.ts
--- a/src/services/tournoisService.ts
+++ b/src/services/tournoisService.ts
@@ -35,3 +35,3 @@
   updatedTournoi.user_id = user.id;
-  const { data, error } = await supabase
+  const { error } = await supabase
     .from('tournois')
EOF
@@ -35,3 +35,3 @@
updatedTournoi.user_id = user.id;
const { data, error } = await supabase
const { error } = await supabase
.from('tournois')
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

Check warning

Code scanning / ESLint

Disallow unused variables Warning

'error' is assigned a value but never used.

Copilot Autofix AI 3 months ago

To fix the problem, we need to handle the error variable properly. The best way to fix this is to add error handling logic to the upsertTournois function. This will ensure that any errors encountered during the upsert operation are logged or handled appropriately, improving the robustness of the code.

We will modify the upsertTournois function to check if error is not null and log it or throw an error. This will involve adding a conditional statement to handle the error.

Suggested changeset 1
src/services/tournoisService.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/services/tournoisService.ts b/src/services/tournoisService.ts
--- a/src/services/tournoisService.ts
+++ b/src/services/tournoisService.ts
@@ -39,2 +39,6 @@
     .select();
+  if (error) {
+    console.error('Error upserting tournoi:', error);
+    throw new Error('Failed to upsert tournoi');
+  }
 };
EOF
@@ -39,2 +39,6 @@
.select();
if (error) {
console.error('Error upserting tournoi:', error);
throw new Error('Failed to upsert tournoi');
}
};
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
.from('tournois')
.upsert(updatedTournoi, { onConflict: 'user_id, tournoi_id' })
.select();
};

export const getTournoi = async (tournoiId: number): Promise<any> => {
const { data, error } = await supabase

Check warning

Code scanning / ESLint

Disallow unused variables Warning

'error' is assigned a value but never used.

Copilot Autofix AI 3 months ago

To fix the problem, we need to remove the unused error variable from the getTournoi function. This will involve modifying the destructuring assignment to exclude error and only extract the data variable. This change will not affect the existing functionality of the code.

Suggested changeset 1
src/services/tournoisService.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/services/tournoisService.ts b/src/services/tournoisService.ts
--- a/src/services/tournoisService.ts
+++ b/src/services/tournoisService.ts
@@ -42,3 +42,3 @@
 export const getTournoi = async (tournoiId: number): Promise<any> => {
-  const { data, error } = await supabase
+  const { data } = await supabase
     .from('tournois')
EOF
@@ -42,3 +42,3 @@
export const getTournoi = async (tournoiId: number): Promise<any> => {
const { data, error } = await supabase
const { data } = await supabase
.from('tournois')
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
.from('tournois')
.select()
.eq('tournoi_id', tournoiId);
return data[0];
};
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"@screens/*": [
"./src/screens/*"
],
"@services/*": [
"./src/services/*"
],
"@store/*": [
"./src/store/*"
],
Expand Down
Loading