-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d19dfe8
commit a307d40
Showing
8 changed files
with
140 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/(app)/users/[username]/lists/[listId]/editableName.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use client'; | ||
import { UserList } from '@prisma/client'; | ||
import React, { createRef, useEffect, useState } from 'react'; | ||
import { toast } from 'sonner'; | ||
|
||
const EditableName = ({ userList }: { userList: UserList }) => { | ||
const [name, setName] = useState(userList.name); | ||
|
||
const updateName = async () => { | ||
if (name === userList.name) { | ||
return; | ||
} | ||
|
||
const response = await ( | ||
await fetch(`/api/user/lists/${userList.id}`, { | ||
method: 'PATCH', | ||
body: JSON.stringify({ | ||
name, | ||
}), | ||
}) | ||
).json(); | ||
|
||
if (response.error) { | ||
toast.error(`Error updating list: ${response.error}`); | ||
} else { | ||
toast.success(response.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<input | ||
value={name} | ||
onChange={e => setName(e.target.value)} | ||
onBlur={() => { | ||
updateName(); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default EditableName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import prisma from '@/server/db'; | ||
import { UserList } from '@prisma/client'; | ||
import 'server-only'; | ||
|
||
/** | ||
* | ||
* @returns {number} Returns the largest order number in the list + 1 | ||
*/ | ||
export const normalizeOrderInList = async ( | ||
userList: UserList | ||
): Promise<number> => { | ||
const listEntries = ( | ||
await prisma.userListEntry.findMany({ | ||
where: { | ||
listId: userList.id, | ||
}, | ||
}) | ||
).sort((a, b) => a.order - b.order); | ||
|
||
let idx = 0; | ||
for (const listEntry of listEntries) { | ||
await prisma.userListEntry.update({ | ||
where: { | ||
id: listEntry.id, | ||
}, | ||
data: { | ||
order: idx, | ||
}, | ||
}); | ||
|
||
idx++; | ||
} | ||
|
||
return idx + 1; | ||
}; |