Created
April 30, 2022 09:15
-
-
Save potaracom/4f8aa69809bee444fbf6e1d8fbb5a79c to your computer and use it in GitHub Desktop.
kintoneのプロフィール画像変更
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
(() => { | |
const token = cybozu.data.REQUEST_TOKEN; | |
const downloadFile = async ({ fileKey }) => { | |
const res = await fetch(`/k/v1/file.json?fileKey=${fileKey}`, { | |
method: "GET", | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
}); | |
return res.blob(); | |
}; | |
const uploadPhoto = async ({ arrayBuffer, fileName }) => { | |
const formData = new FormData(); | |
formData.append( | |
"file", | |
new Blob([arrayBuffer], { type: "application/octet-binary" }), | |
fileName | |
); | |
const res = await fetch("/api/blob/uploadPhoto.json", { | |
method: "POST", | |
headers: { "x-cybozu-requesttoken": token }, | |
body: formData, | |
}); | |
return res.json(); | |
}; | |
const updateProfile = async ({ fileKey }) => { | |
return fetch("/api/settings/profile.json", { | |
method: "POST", | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify({ | |
photoKey: fileKey, | |
__REQUEST_TOKEN__: token, | |
}), | |
}); | |
}; | |
const updateProfilePhoto = async ({ field }) => { | |
const { fileKey, name: fileName } = field.value[0]; | |
const arrayBuffer = await downloadFile({ fileKey }); | |
const res = await uploadPhoto({ arrayBuffer, fileName }); | |
await updateProfile({ fileKey: res.result.fileKey }); | |
}; | |
kintone.events.on(["app.record.detail.show"], async (event) => { | |
const buttonWork = document.createElement("button"); | |
buttonWork.textContent = "勤務用に変更する"; | |
buttonWork.style["margin-right"] = "1rem"; | |
buttonWork.onclick = async () => { | |
await updateProfilePhoto({ field: event.record.勤務用プロフィール画像 }); | |
window.location.reload(); | |
}; | |
const buttonBreak = document.createElement("button"); | |
buttonBreak.textContent = "休暇用に変更する"; | |
buttonBreak.style["margin-right"] = "1rem"; | |
buttonBreak.onclick = async () => { | |
await updateProfilePhoto({ field: event.record.休暇用プロフィール画像 }); | |
window.location.reload(); | |
}; | |
const space = kintone.app.record.getHeaderMenuSpaceElement(); | |
space.append(buttonWork, buttonBreak); | |
return event; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment