-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'renovate/sst-2.x' of github.com:cuculus-dev/cuculus int…
…o renovate/sst-2.x # Conflicts: # package-lock.json
- Loading branch information
Showing
19 changed files
with
417 additions
and
440 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
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
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
# @/swr | ||
SWRを使用したデータフェッチ関数置き場 | ||
# swrディレクトリ | ||
|
||
## @/swr/client | ||
クライアントサイド(`'use client';`)で呼び出す用 | ||
SWRを使用したhook関数置き場です。 | ||
`key`にhook関数名を指定することで一意のキャッシュを作成しています。 | ||
共通のキャッシュを参照する際には元となるキーを指定すること。 | ||
|
||
## @/swr/server | ||
サーバーサイドで呼び出す用 | ||
## [client](./client) | ||
|
||
hook関数置き場です。 | ||
clientとserverで分けるつもりで作りましたが、serverで使えないのでclientしかありません。 | ||
|
||
ファイルはAPIのタグ単位で分けていますが、特にルールはありません。 |
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,81 @@ | ||
import { accountsApi, usersApi } from '@/libs/cuculus-client'; | ||
import { getAuthorizationHeader } from '@/libs/auth'; | ||
import useSWR from 'swr'; | ||
import { User } from '@cuculus/cuculus-api'; | ||
import { useAuth } from '@/swr/client/auth'; | ||
import { UserWithFollows } from '@cuculus/cuculus-api/dist/models'; | ||
import useSWRMutation from 'swr/mutation'; | ||
|
||
type SWRKey = { | ||
key: string; | ||
authId: number; | ||
}; | ||
|
||
type UpdateRequest = { | ||
name?: string; | ||
bio?: string; | ||
profileImage?: Blob; | ||
}; | ||
|
||
/** | ||
* 自身の情報を取得する | ||
*/ | ||
export const useProfile = () => { | ||
const { data: authId } = useAuth(); | ||
// 非ログイン時はキー値にnullを渡して実行させないようにする | ||
const swrKey = authId ? { key: 'useProfile', authId } : null; | ||
return useSWR<User | undefined, Error>(swrKey, async () => { | ||
try { | ||
return await usersApi.getMe({ | ||
headers: await getAuthorizationHeader(authId), | ||
}); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* プロフィール更新とプロフィール情報再取得 | ||
*/ | ||
export const useProfileUpdate = () => { | ||
const { data: authId } = useAuth(); | ||
// 非ログイン時はキー値にnullを渡して実行させないようにする | ||
const key = authId ? { key: 'useProfile', authId } : null; | ||
return useSWRMutation<UserWithFollows, Error, SWRKey | null, UpdateRequest>( | ||
key, | ||
async (_, { arg: request }) => { | ||
const headers = await getAuthorizationHeader(authId); | ||
|
||
let user: UserWithFollows | undefined = undefined; | ||
|
||
if (request.bio != undefined || request.name) { | ||
user = await accountsApi.updateProfile( | ||
{ | ||
updateProfile: { name: request.name, bio: request.bio }, | ||
}, | ||
{ | ||
headers: { | ||
...headers, | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
} | ||
if (request.profileImage) { | ||
user = await accountsApi.updateProfileImage( | ||
{ file: request.profileImage }, | ||
{ headers }, | ||
); | ||
} | ||
if (user) { | ||
return user; | ||
} else { | ||
throw new Error('更新に失敗しました。'); | ||
} | ||
}, | ||
{ | ||
revalidate: true, | ||
}, | ||
); | ||
}; |
Oops, something went wrong.