Skip to content
Merged
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: 0 additions & 1 deletion components/SelectNames.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { useMyVariable } from '../context/MyVariableContext';
import styles from '../styles/typea.module.css';
import CreatableSelect from 'react-select/creatable';
import { saveNewNames } from '../utils/saveNewNames'

Expand Down
15 changes: 6 additions & 9 deletions utils/saveNewNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,29 @@ export async function saveNewNames(inputNames) {
async function updateNames(inputNames) {
let status = 'started';

// Fetch all existing SubGroups from the database
// Fetch all existing names from the database
const { data: existingNames, error: fetchError } = await supabase
.from("names")
.select("name");

if (fetchError) throw fetchError;

// Convert the existing Namess to a Set for faster lookup
// Convert the existing names to a Set for faster lookup
const existingNamesSet = new Set(existingNames.map(item => item.name));

// Filter out the Namess that already exist
// Filter out the names that already exist
const newNames = inputNames.filter(name => !existingNamesSet.has(name));

// Insert new labels
// Insert new names
for (const name of newNames) {
const updates = {
name,
};
const updates = { name };

const { data, error } = await supabase
.from("names")
.upsert(updates)
.upsert(updates, { onConflict: ['name'] }) // Specify conflict target
.select('*');

if (error) throw error;

if (!data) {
throw new Error("Failed to update the name");
}
Expand Down